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
17package com.android.nfc.cardemulation;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.pm.PackageManager;
25import android.nfc.NfcAdapter;
26import android.nfc.cardemulation.ApduServiceInfo;
27import android.nfc.cardemulation.CardEmulation;
28import android.os.Bundle;
29import android.view.Window;
30import android.view.WindowManager;
31import android.widget.TextView;
32
33import com.android.internal.R;
34import com.android.internal.app.AlertActivity;
35import com.android.internal.app.AlertController;
36
37public class TapAgainDialog extends AlertActivity implements DialogInterface.OnClickListener {
38    public static final String ACTION_CLOSE = "com.android.nfc.cardmeulation.close_tap_dialog";
39    public static final String EXTRA_APDU_SERVICE = "apdu_service";
40
41    public static final String EXTRA_CATEGORY = "category";
42
43    // Variables below only accessed on the main thread
44    private CardEmulation mCardEmuManager;
45    private boolean mClosedOnRequest = false;
46    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
47        @Override
48        public void onReceive(Context context, Intent intent) {
49            mClosedOnRequest = true;
50            finish();
51        }
52    };
53
54    @Override
55    protected void onCreate(Bundle savedInstanceState) {
56        super.onCreate(savedInstanceState);
57
58        setTheme(R.style.Theme_DeviceDefault_Light_Dialog_Alert);
59
60        final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
61        mCardEmuManager = CardEmulation.getInstance(adapter);
62        Intent intent = getIntent();
63        String category = intent.getStringExtra(EXTRA_CATEGORY);
64        ApduServiceInfo serviceInfo = intent.getParcelableExtra(EXTRA_APDU_SERVICE);
65        IntentFilter filter = new IntentFilter(ACTION_CLOSE);
66        filter.addAction(Intent.ACTION_SCREEN_OFF);
67        registerReceiver(mReceiver, filter);
68        AlertController.AlertParams ap = mAlertParams;
69
70        ap.mTitle = "";
71        ap.mView = getLayoutInflater().inflate(com.android.nfc.R.layout.tapagain, null);
72
73        PackageManager pm = getPackageManager();
74        TextView tv = (TextView) ap.mView.findViewById(com.android.nfc.R.id.textview);
75        String description = serviceInfo.getDescription();
76        if (description == null) {
77            CharSequence label = serviceInfo.loadLabel(pm);
78            if (label == null) {
79                finish();
80            } else {
81                description = label.toString();
82            }
83        }
84        if (CardEmulation.CATEGORY_PAYMENT.equals(category)) {
85            String formatString = getString(com.android.nfc.R.string.tap_again_to_pay);
86            tv.setText(String.format(formatString, description));
87        } else {
88            String formatString = getString(com.android.nfc.R.string.tap_again_to_complete);
89            tv.setText(String.format(formatString, description));
90        }
91        ap.mNegativeButtonText = getString(R.string.cancel);
92        setupAlert();
93        Window window = getWindow();
94        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
95    }
96
97    @Override
98    protected void onDestroy() {
99        super.onDestroy();
100        unregisterReceiver(mReceiver);
101    }
102
103    @Override
104    protected void onStop() {
105        super.onStop();
106        if (!mClosedOnRequest) {
107            mCardEmuManager.setDefaultForNextTap(null);
108        }
109    }
110
111    @Override
112    public void onClick(DialogInterface dialog, int which) {
113        finish();
114    }
115}