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.nfc.cardemulation.CardEmulation; 23 import android.os.Bundle; 24 import android.util.Log; 25 26 import com.android.internal.app.AlertActivity; 27 import com.android.internal.app.AlertController; 28 import com.android.settings.R; 29 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo; 30 31 import java.util.List; 32 33 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; 34 35 public final class PaymentDefaultDialog extends AlertActivity implements 36 DialogInterface.OnClickListener { 37 38 public static final String TAG = "PaymentDefaultDialog"; 39 private static final int PAYMENT_APP_MAX_CAPTION_LENGTH = 40; 40 41 private PaymentBackend mBackend; 42 private ComponentName mNewDefault; 43 44 @Override onCreate(Bundle savedInstanceState)45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 getWindow().addPrivateFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 49 50 try { 51 mBackend = new PaymentBackend(this); 52 } catch (NullPointerException e) { 53 finish(); 54 } 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 onClick(DialogInterface dialog, int which)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 buildDialog(ComponentName component, String category)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 if (defaultPaymentApp == null) { 120 p.mTitle = getString(R.string.nfc_payment_set_default_label); 121 String formatString = getString(R.string.nfc_payment_set_default); 122 String msg = String.format(formatString, 123 sanitizePaymentAppCaption(requestedPaymentApp.label.toString())); 124 p.mMessage = msg; 125 p.mPositiveButtonText = getString(R.string.nfc_payment_btn_text_set_deault); 126 } else { 127 p.mTitle = getString(R.string.nfc_payment_update_default_label); 128 String formatString = getString(R.string.nfc_payment_set_default_instead_of); 129 String msg = String.format(formatString, 130 sanitizePaymentAppCaption(requestedPaymentApp.label.toString()), 131 sanitizePaymentAppCaption(defaultPaymentApp.label.toString())); 132 p.mMessage = msg; 133 p.mPositiveButtonText = getString(R.string.nfc_payment_btn_text_update); 134 } 135 p.mNegativeButtonText = getString(R.string.cancel); 136 p.mPositiveButtonListener = this; 137 p.mNegativeButtonListener = this; 138 setupAlert(); 139 140 return true; 141 } 142 sanitizePaymentAppCaption(String input)143 private String sanitizePaymentAppCaption(String input) { 144 String sanitizedString = input.replace('\n', ' ').replace('\r', ' ').trim(); 145 146 147 if (sanitizedString.length() > PAYMENT_APP_MAX_CAPTION_LENGTH) { 148 return sanitizedString.substring(0, PAYMENT_APP_MAX_CAPTION_LENGTH); 149 } 150 151 return sanitizedString; 152 } 153 154 } 155