175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen/* 275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * Copyright (C) 2013 The Android Open Source Project 375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * 475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * Licensed under the Apache License, Version 2.0 (the "License"); 575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * you may not use this file except in compliance with the License. 675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * You may obtain a copy of the License at 775f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * 875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * http://www.apache.org/licenses/LICENSE-2.0 975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * 1075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * Unless required by applicable law or agreed to in writing, software 1175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * distributed under the License is distributed on an "AS IS" BASIS, 1275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * See the License for the specific language governing permissions and 1475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen * limitations under the License. 1575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen */ 1675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 1775f63db568f953e935e62cb3046d167b881979c8Martijn Coenenpackage com.android.nfc.cardemulation; 1875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 1975f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.BroadcastReceiver; 2075f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.ComponentName; 2175f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.Context; 22dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenenimport android.content.DialogInterface; 2375f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.Intent; 2475f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.IntentFilter; 2575f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.pm.ApplicationInfo; 2675f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.pm.PackageManager; 2775f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.pm.PackageManager.NameNotFoundException; 2875f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.nfc.NfcAdapter; 29a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenenimport android.nfc.cardemulation.ApduServiceInfo; 30f5cd84c3a7ffb66196ab3c0745569da937d7533bMartijn Coenenimport android.nfc.cardemulation.CardEmulation; 3175f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.os.Bundle; 32451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenenimport android.view.Window; 33451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenenimport android.view.WindowManager; 3475f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.widget.TextView; 3575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 3675f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport com.android.internal.R; 3775f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport com.android.internal.app.AlertActivity; 3875f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport com.android.internal.app.AlertController; 3975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 40dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenenpublic class TapAgainDialog extends AlertActivity implements DialogInterface.OnClickListener { 4175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen public static final String ACTION_CLOSE = "com.android.nfc.cardmeulation.close_tap_dialog"; 42a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen public static final String EXTRA_APDU_SERVICE = "apdu_service"; 43a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen 4475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen public static final String EXTRA_CATEGORY = "category"; 4575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 4675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen // Variables below only accessed on the main thread 47f5cd84c3a7ffb66196ab3c0745569da937d7533bMartijn Coenen private CardEmulation mCardEmuManager; 4875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen private boolean mClosedOnRequest = false; 4975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen final BroadcastReceiver mReceiver = new BroadcastReceiver() { 5075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen @Override 5175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen public void onReceive(Context context, Intent intent) { 5275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen mClosedOnRequest = true; 5375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen finish(); 5475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } 5575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen }; 5675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 5775f63db568f953e935e62cb3046d167b881979c8Martijn Coenen @Override 5875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen protected void onCreate(Bundle savedInstanceState) { 5975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen super.onCreate(savedInstanceState); 6075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 61451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen setTheme(R.style.Theme_DeviceDefault_Light_Dialog_Alert); 62451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen 6375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this); 64f5cd84c3a7ffb66196ab3c0745569da937d7533bMartijn Coenen mCardEmuManager = CardEmulation.getInstance(adapter); 6575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen Intent intent = getIntent(); 6675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen String category = intent.getStringExtra(EXTRA_CATEGORY); 67a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen ApduServiceInfo serviceInfo = intent.getParcelableExtra(EXTRA_APDU_SERVICE); 6875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen IntentFilter filter = new IntentFilter(ACTION_CLOSE); 69dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen filter.addAction(Intent.ACTION_SCREEN_OFF); 7075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen registerReceiver(mReceiver, filter); 7175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen AlertController.AlertParams ap = mAlertParams; 7275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 7375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen ap.mTitle = ""; 7475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen ap.mView = getLayoutInflater().inflate(com.android.nfc.R.layout.tapagain, null); 7575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 7675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen PackageManager pm = getPackageManager(); 77a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen TextView tv = (TextView) ap.mView.findViewById(com.android.nfc.R.id.textview); 78a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen String description = serviceInfo.getDescription(); 79a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen if (description == null) { 80a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen CharSequence label = serviceInfo.loadLabel(pm); 81a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen if (label == null) { 82a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen finish(); 8375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } else { 84a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen description = label.toString(); 8575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } 8675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } 87a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen if (CardEmulation.CATEGORY_PAYMENT.equals(category)) { 88a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen String formatString = getString(com.android.nfc.R.string.tap_again_to_pay); 89a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen tv.setText(String.format(formatString, description)); 90a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen } else { 91a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen String formatString = getString(com.android.nfc.R.string.tap_again_to_complete); 92a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen tv.setText(String.format(formatString, description)); 93a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen } 94a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen ap.mNegativeButtonText = getString(R.string.cancel); 95a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen setupAlert(); 96451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen Window window = getWindow(); 97451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 9875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } 9975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 10075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen @Override 10175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen protected void onDestroy() { 10275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen super.onDestroy(); 103dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen unregisterReceiver(mReceiver); 10475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } 10575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen 10675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen @Override 10775f63db568f953e935e62cb3046d167b881979c8Martijn Coenen protected void onStop() { 10875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen super.onStop(); 10975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen if (!mClosedOnRequest) { 11075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen mCardEmuManager.setDefaultForNextTap(null); 11175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } 11275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen } 113dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen 114dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen @Override 115dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen public void onClick(DialogInterface dialog, int which) { 116dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen finish(); 117dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen } 11875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen}