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