• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.drawable.Drawable;
26 import android.nfc.NfcAdapter;
27 import android.nfc.cardemulation.ApduServiceInfo;
28 import android.nfc.cardemulation.CardEmulation;
29 import android.os.Bundle;
30 import android.os.UserHandle;
31 import android.util.Log;
32 import android.view.View;
33 import android.view.Window;
34 import android.view.WindowManager;
35 import android.widget.ImageView;
36 
37 import androidx.appcompat.widget.Toolbar;
38 
39 import com.android.nfc.cardemulation.util.AlertActivity;
40 
41 import java.util.concurrent.atomic.AtomicReference;
42 
43 public class TapAgainDialog extends AlertActivity implements DialogInterface.OnClickListener {
44     private static final String TAG = "TapAgainDialog";
45     public static final String ACTION_CLOSE =
46             "com.android.nfc.cardemulation.action.CLOSE_TAP_DIALOG";
47     public static final String EXTRA_APDU_SERVICE = "apdu_service";
48 
49     public static final String EXTRA_CATEGORY = "category";
50 
51     // Variables below only accessed on the main thread
52     private CardEmulation mCardEmuManager;
53     private boolean mClosedOnRequest = false;
54     private AtomicReference<BroadcastReceiver> mReceiver;
55 
56     @Override
onCreate(Bundle savedInstanceState)57     protected void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59 
60         setTheme(com.android.nfc.R.style.TapAgainDayNight);
61 
62         final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
63         if (adapter == null) {
64             Log.e(TAG, "adapter is null");
65             finish();
66             return;
67         }
68         mCardEmuManager = CardEmulation.getInstance(adapter);
69         Intent intent = getIntent();
70         ApduServiceInfo serviceInfo = intent.getParcelableExtra(EXTRA_APDU_SERVICE);
71         if (serviceInfo == null) {
72             Log.e(TAG, "onCreate: serviceInfo is null");
73             finish();
74             return;
75         }
76 
77         final BroadcastReceiver receiver = new BroadcastReceiver() {
78             @Override
79             public void onReceive(Context context, Intent intent) {
80                 mClosedOnRequest = true;
81                 finish();
82             }
83         };
84         mReceiver = new AtomicReference<BroadcastReceiver>(receiver);
85         IntentFilter filter = new IntentFilter(ACTION_CLOSE);
86         filter.addAction(Intent.ACTION_SCREEN_OFF);
87         registerReceiver(receiver, filter);
88 
89         View view = getLayoutInflater().inflate(com.android.nfc.R.layout.tapagain, null);
90         Toolbar toolbar = (Toolbar) view.findViewById(com.android.nfc.R.id.tap_again_toolbar);
91         toolbar.setNavigationIcon(getDrawable(com.android.nfc.R.drawable.ic_close));
92         toolbar.setNavigationOnClickListener(new View.OnClickListener() {
93             @Override
94             public void onClick(View view) {
95                 finish();
96             }
97         });
98 
99         PackageManager pm = getPackageManager();
100         ImageView iv = (ImageView) view.findViewById(com.android.nfc.R.id.tap_again_appicon);
101         Drawable icon = pm.getUserBadgedIcon(serviceInfo.loadIcon(pm),
102                 UserHandle.getUserHandleForUid(serviceInfo.getUid()));
103 
104         iv.setImageDrawable(icon);
105 
106         mAlertBuilder.setTitle("");
107         mAlertBuilder.setView(view);
108 
109         setupAlert();
110         Window window = getWindow();
111         window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
112     }
113 
114     /**
115      * Safely unregister the broadcast receiver and empty the mReceiver internal elements.
116      */
close()117     public void close() {
118         final BroadcastReceiver receiver = mReceiver.getAndSet(null);
119         if (receiver != null) {
120             unregisterReceiver(receiver);
121         }
122     }
123 
124     @Override
onDestroy()125     protected void onDestroy() {
126         super.onDestroy();
127         if (mReceiver.get() != null) {
128             Log.e(TAG, "onDestroy: Failed to unregister receiver");
129             close();
130         }
131     }
132 
133     @Override
onStop()134     protected void onStop() {
135         super.onStop();
136         if (!mClosedOnRequest && mCardEmuManager != null) {
137             mCardEmuManager.setDefaultForNextTap(null);
138         }
139     }
140 
141     @Override
onClick(DialogInterface dialog, int which)142     public void onClick(DialogInterface dialog, int which) {
143         finish();
144     }
145 }
146