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.settings.nfc; 18 19 import android.content.ComponentName; 20 import android.content.DialogInterface; 21 import android.content.Intent; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.content.pm.PackageManager.NameNotFoundException; 26 import android.nfc.cardemulation.ApduServiceInfo; 27 import android.nfc.cardemulation.CardEmulation; 28 import android.nfc.cardemulation.HostApduService; 29 import android.nfc.cardemulation.OffHostApduService; 30 import android.os.Bundle; 31 import android.util.Log; 32 33 import com.android.internal.app.AlertActivity; 34 import com.android.internal.app.AlertController; 35 import com.android.settings.R; 36 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo; 37 38 import java.io.IOException; 39 import java.util.List; 40 41 import org.xmlpull.v1.XmlPullParserException; 42 43 public final class PaymentDefaultDialog extends AlertActivity implements 44 DialogInterface.OnClickListener { 45 46 public static final String TAG = "PaymentDefaultDialog"; 47 48 private PaymentBackend mBackend; 49 private ComponentName mNewDefault; 50 51 @Override 52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 mBackend = new PaymentBackend(this); 55 Intent intent = getIntent(); 56 ComponentName component = intent.getParcelableExtra( 57 CardEmulation.EXTRA_SERVICE_COMPONENT); 58 String category = intent.getStringExtra(CardEmulation.EXTRA_CATEGORY); 59 60 setResult(RESULT_CANCELED); 61 if (!buildDialog(component, category)) { 62 finish(); 63 } 64 65 } 66 67 @Override 68 public void onClick(DialogInterface dialog, int which) { 69 switch (which) { 70 case BUTTON_POSITIVE: 71 mBackend.setDefaultPaymentApp(mNewDefault); 72 setResult(RESULT_OK); 73 break; 74 case BUTTON_NEGATIVE: 75 break; 76 } 77 } 78 79 private boolean buildDialog(ComponentName component, String category) { 80 if (component == null || category == null) { 81 Log.e(TAG, "Component or category are null"); 82 return false; 83 } 84 85 if (!CardEmulation.CATEGORY_PAYMENT.equals(category)) { 86 Log.e(TAG, "Don't support defaults for category " + category); 87 return false; 88 } 89 90 // Check if passed in service exists 91 PaymentAppInfo requestedPaymentApp = null; 92 PaymentAppInfo defaultPaymentApp = null; 93 94 List<PaymentAppInfo> services = mBackend.getPaymentAppInfos(); 95 for (PaymentAppInfo service : services) { 96 if (component.equals(service.componentName)) { 97 requestedPaymentApp = service; 98 } 99 if (service.isDefault) { 100 defaultPaymentApp = service; 101 } 102 } 103 104 if (requestedPaymentApp == null) { 105 Log.e(TAG, "Component " + component + " is not a registered payment service."); 106 return false; 107 } 108 109 // Get current mode and default component 110 ComponentName defaultComponent = mBackend.getDefaultPaymentApp(); 111 if (defaultComponent != null && defaultComponent.equals(component)) { 112 Log.e(TAG, "Component " + component + " is already default."); 113 return false; 114 } 115 116 mNewDefault = component; 117 // Compose dialog; get 118 final AlertController.AlertParams p = mAlertParams; 119 p.mTitle = getString(R.string.nfc_payment_set_default_label); 120 if (defaultPaymentApp == null) { 121 String formatString = getString(R.string.nfc_payment_set_default); 122 String msg = String.format(formatString, requestedPaymentApp.caption); 123 p.mMessage = msg; 124 } else { 125 String formatString = getString(R.string.nfc_payment_set_default_instead_of); 126 String msg = String.format(formatString, requestedPaymentApp.caption, 127 defaultPaymentApp.caption); 128 p.mMessage = msg; 129 } 130 p.mPositiveButtonText = getString(R.string.yes); 131 p.mNegativeButtonText = getString(R.string.no); 132 p.mPositiveButtonListener = this; 133 p.mNegativeButtonListener = this; 134 setupAlert(); 135 136 return true; 137 } 138 139 } 140