1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.nfc.cardemulation;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.pm.PackageManager;
25 import android.nfc.NfcAdapter;
26 import android.nfc.cardemulation.ApduServiceInfo;
27 import android.nfc.cardemulation.CardEmulation;
28 import android.os.Bundle;
29 import android.view.Window;
30 import android.view.WindowManager;
31 import android.widget.TextView;
32 
33 import com.android.internal.R;
34 import com.android.internal.app.AlertActivity;
35 import com.android.internal.app.AlertController;
36 
37 public class TapAgainDialog extends AlertActivity implements DialogInterface.OnClickListener {
38     public static final String ACTION_CLOSE =
39             "com.android.nfc.cardemulation.action.CLOSE_TAP_DIALOG";
40     public static final String EXTRA_APDU_SERVICE = "apdu_service";
41 
42     public static final String EXTRA_CATEGORY = "category";
43 
44     // Variables below only accessed on the main thread
45     private CardEmulation mCardEmuManager;
46     private boolean mClosedOnRequest = false;
47     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
48         @Override
49         public void onReceive(Context context, Intent intent) {
50             mClosedOnRequest = true;
51             finish();
52         }
53     };
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         setTheme(com.android.nfc.R.style.DialogAlertDayNight);
60 
61         final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
62         mCardEmuManager = CardEmulation.getInstance(adapter);
63         Intent intent = getIntent();
64         String category = intent.getStringExtra(EXTRA_CATEGORY);
65         ApduServiceInfo serviceInfo = intent.getParcelableExtra(EXTRA_APDU_SERVICE);
66         IntentFilter filter = new IntentFilter(ACTION_CLOSE);
67         filter.addAction(Intent.ACTION_SCREEN_OFF);
68         registerReceiver(mReceiver, filter);
69         AlertController.AlertParams ap = mAlertParams;
70 
71         ap.mTitle = "";
72         ap.mView = getLayoutInflater().inflate(com.android.nfc.R.layout.tapagain, null);
73 
74         PackageManager pm = getPackageManager();
75         TextView tv = (TextView) ap.mView.findViewById(com.android.nfc.R.id.textview);
76         String description = serviceInfo.getDescription();
77         if (description == null) {
78             CharSequence label = serviceInfo.loadLabel(pm);
79             if (label == null) {
80                 finish();
81             } else {
82                 description = label.toString();
83             }
84         }
85         if (CardEmulation.CATEGORY_PAYMENT.equals(category)) {
86             String formatString = getString(com.android.nfc.R.string.tap_again_to_pay);
87             tv.setText(String.format(formatString, description));
88         } else {
89             String formatString = getString(com.android.nfc.R.string.tap_again_to_complete);
90             tv.setText(String.format(formatString, description));
91         }
92         ap.mNegativeButtonText = getString(R.string.cancel);
93         setupAlert();
94         Window window = getWindow();
95         window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
96     }
97 
98     @Override
onDestroy()99     protected void onDestroy() {
100         super.onDestroy();
101         unregisterReceiver(mReceiver);
102     }
103 
104     @Override
onStop()105     protected void onStop() {
106         super.onStop();
107         if (!mClosedOnRequest) {
108             mCardEmuManager.setDefaultForNextTap(null);
109         }
110     }
111 
112     @Override
onClick(DialogInterface dialog, int which)113     public void onClick(DialogInterface dialog, int which) {
114         finish();
115     }
116 }