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