• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         p.mTitle = getString(R.string.nfc_payment_set_default_label);
115         if (defaultPaymentApp == null) {
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         } else {
121             String formatString = getString(R.string.nfc_payment_set_default_instead_of);
122             String msg = String.format(formatString,
123                     sanitizePaymentAppCaption(requestedPaymentApp.label.toString()),
124                     sanitizePaymentAppCaption(defaultPaymentApp.label.toString()));
125             p.mMessage = msg;
126         }
127         p.mPositiveButtonText = getString(R.string.yes);
128         p.mNegativeButtonText = getString(R.string.no);
129         p.mPositiveButtonListener = this;
130         p.mNegativeButtonListener = this;
131         setupAlert();
132 
133         return true;
134     }
135 
sanitizePaymentAppCaption(String input)136     private String sanitizePaymentAppCaption(String input) {
137         String sanitizedString = input.replace('\n', ' ').replace('\r', ' ').trim();
138 
139 
140         if (sanitizedString.length() > PAYMENT_APP_MAX_CAPTION_LENGTH) {
141             return sanitizedString.substring(0, PAYMENT_APP_MAX_CAPTION_LENGTH);
142         }
143 
144         return sanitizedString;
145     }
146 
147 }
148