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 public final class PaymentDefaultDialog extends AlertActivity implements
34         DialogInterface.OnClickListener {
35 
36     public static final String TAG = "PaymentDefaultDialog";
37     private static final int PAYMENT_APP_MAX_CAPTION_LENGTH = 40;
38 
39     private PaymentBackend mBackend;
40     private ComponentName mNewDefault;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         try {
46             mBackend = new PaymentBackend(this);
47         } catch (NullPointerException e) {
48             finish();
49         }
50         Intent intent = getIntent();
51         ComponentName component = intent.getParcelableExtra(
52                 CardEmulation.EXTRA_SERVICE_COMPONENT);
53         String category = intent.getStringExtra(CardEmulation.EXTRA_CATEGORY);
54 
55         setResult(RESULT_CANCELED);
56         if (!buildDialog(component, category)) {
57             finish();
58         }
59 
60     }
61 
62     @Override
onClick(DialogInterface dialog, int which)63     public void onClick(DialogInterface dialog, int which) {
64         switch (which) {
65             case BUTTON_POSITIVE:
66                 mBackend.setDefaultPaymentApp(mNewDefault);
67                 setResult(RESULT_OK);
68                 break;
69             case BUTTON_NEGATIVE:
70                 break;
71         }
72     }
73 
buildDialog(ComponentName component, String category)74     private boolean buildDialog(ComponentName component, String category) {
75         if (component == null || category == null) {
76             Log.e(TAG, "Component or category are null");
77             return false;
78         }
79 
80         if (!CardEmulation.CATEGORY_PAYMENT.equals(category)) {
81             Log.e(TAG, "Don't support defaults for category " + category);
82             return false;
83         }
84 
85         // Check if passed in service exists
86         PaymentAppInfo requestedPaymentApp = null;
87         PaymentAppInfo defaultPaymentApp = null;
88 
89         List<PaymentAppInfo> services = mBackend.getPaymentAppInfos();
90         for (PaymentAppInfo service : services) {
91             if (component.equals(service.componentName)) {
92                 requestedPaymentApp = service;
93             }
94             if (service.isDefault) {
95                 defaultPaymentApp = service;
96             }
97         }
98 
99         if (requestedPaymentApp == null) {
100             Log.e(TAG, "Component " + component + " is not a registered payment service.");
101             return false;
102         }
103 
104         // Get current mode and default component
105         ComponentName defaultComponent = mBackend.getDefaultPaymentApp();
106         if (defaultComponent != null && defaultComponent.equals(component)) {
107             Log.e(TAG, "Component " + component + " is already default.");
108             return false;
109         }
110 
111         mNewDefault = component;
112         // Compose dialog; get
113         final AlertController.AlertParams p = mAlertParams;
114         if (defaultPaymentApp == null) {
115             p.mTitle = getString(R.string.nfc_payment_set_default_label);
116             String formatString = getString(R.string.nfc_payment_set_default);
117             String msg = String.format(formatString,
118                     sanitizePaymentAppCaption(requestedPaymentApp.label.toString()));
119             p.mMessage = msg;
120             p.mPositiveButtonText = getString(R.string.nfc_payment_btn_text_set_deault);
121         } else {
122             p.mTitle = getString(R.string.nfc_payment_update_default_label);
123             String formatString = getString(R.string.nfc_payment_set_default_instead_of);
124             String msg = String.format(formatString,
125                     sanitizePaymentAppCaption(requestedPaymentApp.label.toString()),
126                     sanitizePaymentAppCaption(defaultPaymentApp.label.toString()));
127             p.mMessage = msg;
128             p.mPositiveButtonText = getString(R.string.nfc_payment_btn_text_update);
129         }
130         p.mNegativeButtonText = getString(R.string.cancel);
131         p.mPositiveButtonListener = this;
132         p.mNegativeButtonListener = this;
133         setupAlert();
134 
135         return true;
136     }
137 
sanitizePaymentAppCaption(String input)138     private String sanitizePaymentAppCaption(String input) {
139         String sanitizedString = input.replace('\n', ' ').replace('\r', ' ').trim();
140 
141 
142         if (sanitizedString.length() > PAYMENT_APP_MAX_CAPTION_LENGTH) {
143             return sanitizedString.substring(0, PAYMENT_APP_MAX_CAPTION_LENGTH);
144         }
145 
146         return sanitizedString;
147     }
148 
149 }
150