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.view.View;
32 import android.view.Window;
33 import android.view.WindowManager;
34 import android.widget.ImageView;
35 import android.widget.TextView;
36 import androidx.appcompat.widget.Toolbar;
37 
38 import com.android.nfc.cardemulation.util.AlertActivity;
39 
40 public class TapAgainDialog extends AlertActivity implements DialogInterface.OnClickListener {
41     public static final String ACTION_CLOSE =
42             "com.android.nfc.cardemulation.action.CLOSE_TAP_DIALOG";
43     public static final String EXTRA_APDU_SERVICE = "apdu_service";
44 
45     public static final String EXTRA_CATEGORY = "category";
46 
47     // Variables below only accessed on the main thread
48     private CardEmulation mCardEmuManager;
49     private boolean mClosedOnRequest = false;
50     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
51         @Override
52         public void onReceive(Context context, Intent intent) {
53             mClosedOnRequest = true;
54             finish();
55         }
56     };
57 
58     @Override
onCreate(Bundle savedInstanceState)59     protected void onCreate(Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61 
62         setTheme(com.android.nfc.R.style.TapAgainDayNight);
63 
64         final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
65         mCardEmuManager = CardEmulation.getInstance(adapter);
66         Intent intent = getIntent();
67         String category = intent.getStringExtra(EXTRA_CATEGORY);
68         ApduServiceInfo serviceInfo = intent.getParcelableExtra(EXTRA_APDU_SERVICE);
69         IntentFilter filter = new IntentFilter(ACTION_CLOSE);
70         filter.addAction(Intent.ACTION_SCREEN_OFF);
71         registerReceiver(mReceiver, filter);
72 
73         View view = getLayoutInflater().inflate(com.android.nfc.R.layout.tapagain, null);
74         Toolbar toolbar = (Toolbar) view.findViewById(com.android.nfc.R.id.tap_again_toolbar);
75         toolbar.setNavigationIcon(getDrawable(com.android.nfc.R.drawable.ic_close));
76         toolbar.setNavigationOnClickListener(new View.OnClickListener() {
77             @Override
78             public void onClick(View view) {
79                 finish();
80             }
81         });
82 
83         PackageManager pm = getPackageManager();
84         ImageView iv = (ImageView) view.findViewById(com.android.nfc.R.id.tap_again_appicon);
85         Drawable icon = pm.getUserBadgedIcon(serviceInfo.loadIcon(pm),
86                 UserHandle.getUserHandleForUid(serviceInfo.getUid()));
87 
88         iv.setImageDrawable(icon);
89 
90         mAlertBuilder.setTitle("");
91         mAlertBuilder.setView(view);
92 
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 }
117