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