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.Context;
21dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenenimport android.content.DialogInterface;
2275f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.Intent;
2375f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.IntentFilter;
2475f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.content.pm.PackageManager;
2575f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.nfc.NfcAdapter;
26a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenenimport android.nfc.cardemulation.ApduServiceInfo;
27f5cd84c3a7ffb66196ab3c0745569da937d7533bMartijn Coenenimport android.nfc.cardemulation.CardEmulation;
2875f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.os.Bundle;
29451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenenimport android.view.Window;
30451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenenimport android.view.WindowManager;
3175f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport android.widget.TextView;
3275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
3375f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport com.android.internal.R;
3475f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport com.android.internal.app.AlertActivity;
3575f63db568f953e935e62cb3046d167b881979c8Martijn Coenenimport com.android.internal.app.AlertController;
3675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
37dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenenpublic class TapAgainDialog extends AlertActivity implements DialogInterface.OnClickListener {
3844e38e2f0983e1a89c4ea31b4e3dc1e174a3f614Martijn Coenen    public static final String ACTION_CLOSE =
3944e38e2f0983e1a89c4ea31b4e3dc1e174a3f614Martijn Coenen            "com.android.nfc.cardemulation.action.CLOSE_TAP_DIALOG";
40a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen    public static final String EXTRA_APDU_SERVICE = "apdu_service";
41a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen
4275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    public static final String EXTRA_CATEGORY = "category";
4375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
4475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    // Variables below only accessed on the main thread
45f5cd84c3a7ffb66196ab3c0745569da937d7533bMartijn Coenen    private CardEmulation mCardEmuManager;
4675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    private boolean mClosedOnRequest = false;
4775f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
4875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        @Override
4975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        public void onReceive(Context context, Intent intent) {
5075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen            mClosedOnRequest = true;
5175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen            finish();
5275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        }
5375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    };
5475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
5575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    @Override
5675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    protected void onCreate(Bundle savedInstanceState) {
5775f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        super.onCreate(savedInstanceState);
5875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
59451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen        setTheme(R.style.Theme_DeviceDefault_Light_Dialog_Alert);
60451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen
6175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
62f5cd84c3a7ffb66196ab3c0745569da937d7533bMartijn Coenen        mCardEmuManager = CardEmulation.getInstance(adapter);
6375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        Intent intent = getIntent();
6475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        String category = intent.getStringExtra(EXTRA_CATEGORY);
65a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        ApduServiceInfo serviceInfo = intent.getParcelableExtra(EXTRA_APDU_SERVICE);
6675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        IntentFilter filter = new IntentFilter(ACTION_CLOSE);
67dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen        filter.addAction(Intent.ACTION_SCREEN_OFF);
6875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        registerReceiver(mReceiver, filter);
6975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        AlertController.AlertParams ap = mAlertParams;
7075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
7175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        ap.mTitle = "";
7275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        ap.mView = getLayoutInflater().inflate(com.android.nfc.R.layout.tapagain, null);
7375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
7475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        PackageManager pm = getPackageManager();
75a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        TextView tv = (TextView) ap.mView.findViewById(com.android.nfc.R.id.textview);
76a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        String description = serviceInfo.getDescription();
77a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        if (description == null) {
78a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen            CharSequence label = serviceInfo.loadLabel(pm);
79a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen            if (label == null) {
80a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen                finish();
8175f63db568f953e935e62cb3046d167b881979c8Martijn Coenen            } else {
82a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen                description = label.toString();
8375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen            }
8475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        }
85a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        if (CardEmulation.CATEGORY_PAYMENT.equals(category)) {
86a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen            String formatString = getString(com.android.nfc.R.string.tap_again_to_pay);
87a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen            tv.setText(String.format(formatString, description));
88a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        } else {
89a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen            String formatString = getString(com.android.nfc.R.string.tap_again_to_complete);
90a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen            tv.setText(String.format(formatString, description));
91a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        }
92a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        ap.mNegativeButtonText = getString(R.string.cancel);
93a3b2c7944266cbd02afc08ce48ce8259d8a65019Martijn Coenen        setupAlert();
94451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen        Window window = getWindow();
95451ba48faa87d78bfbec0597ff06af1747cf6acbMartijn Coenen        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
9675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    }
9775f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
9875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    @Override
9975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    protected void onDestroy() {
10075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        super.onDestroy();
101dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen        unregisterReceiver(mReceiver);
10275f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    }
10375f63db568f953e935e62cb3046d167b881979c8Martijn Coenen
10475f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    @Override
10575f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    protected void onStop() {
10675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        super.onStop();
10775f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        if (!mClosedOnRequest) {
10875f63db568f953e935e62cb3046d167b881979c8Martijn Coenen            mCardEmuManager.setDefaultForNextTap(null);
10975f63db568f953e935e62cb3046d167b881979c8Martijn Coenen        }
11075f63db568f953e935e62cb3046d167b881979c8Martijn Coenen    }
111dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen
112dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen    @Override
113dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen    public void onClick(DialogInterface dialog, int which) {
114dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen        finish();
115dc304dadda013e383fdd1348b2a24bc6b3c9acacMartijn Coenen    }
11675f63db568f953e935e62cb3046d167b881979c8Martijn Coenen}