PaymentDefaultDialog.java revision e0a38911a3c2d884faaec4da32d19827d3347479
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.content.pm.ApplicationInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.PackageManager.NameNotFoundException;
25import android.nfc.cardemulation.CardEmulationManager;
26import android.os.Bundle;
27import android.util.Log;
28
29import com.android.internal.app.AlertActivity;
30import com.android.internal.app.AlertController;
31import com.android.settings.R;
32import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
33
34import java.util.List;
35
36public final class PaymentDefaultDialog extends AlertActivity implements
37        DialogInterface.OnClickListener {
38
39    public static final String TAG = "PaymentDefaultDialog";
40
41    private PaymentBackend mBackend;
42    private ComponentName mNewDefault;
43
44    @Override
45    protected void onCreate(Bundle savedInstanceState) {
46        super.onCreate(savedInstanceState);
47        mBackend = new PaymentBackend(this);
48        Intent intent = getIntent();
49        ComponentName component = intent.getParcelableExtra(
50                CardEmulationManager.EXTRA_SERVICE_COMPONENT);
51        String category = intent.getStringExtra(CardEmulationManager.EXTRA_CATEGORY);
52
53        setResult(RESULT_CANCELED);
54        if (!buildDialog(component, category)) {
55            finish();
56        }
57
58    }
59
60    @Override
61    public void onClick(DialogInterface dialog, int which) {
62        switch (which) {
63            case BUTTON_POSITIVE:
64                mBackend.setDefaultPaymentApp(mNewDefault);
65                mBackend.setAutoPaymentMode(true);
66                setResult(RESULT_OK);
67                break;
68            case BUTTON_NEGATIVE:
69                break;
70        }
71    }
72
73    private boolean buildDialog(ComponentName component, String category) {
74        if (component == null || category == null) {
75            Log.e(TAG, "Component or category are null");
76            return false;
77        }
78
79        if (!CardEmulationManager.CATEGORY_PAYMENT.equals(category)) {
80            Log.e(TAG, "Don't support defaults for category " + category);
81            return false;
82        }
83
84        // Check if passed in service exists
85        boolean found = false;
86
87        List<PaymentAppInfo> services = mBackend.getPaymentAppInfos();
88        for (PaymentAppInfo service : services) {
89            if (component.equals(service.componentName)) {
90                found = true;
91                break;
92            }
93        }
94
95        if (!found) {
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        boolean isAuto = mBackend.isAutoPaymentMode();
102        ComponentName defaultComponent = mBackend.getDefaultPaymentApp();
103        if (defaultComponent != null && defaultComponent.equals(component)) {
104            Log.e(TAG, "Component " + component + " is already default.");
105            return false;
106        }
107
108        PackageManager pm = getPackageManager();
109        ApplicationInfo newAppInfo;
110        try {
111            newAppInfo = pm.getApplicationInfo(component.getPackageName(), 0);
112        } catch (NameNotFoundException e) {
113            Log.e(TAG, "PM could not load app info for " + component);
114            return false;
115        }
116        ApplicationInfo defaultAppInfo = null;
117        try {
118            if (defaultComponent != null) {
119                defaultAppInfo = pm.getApplicationInfo(defaultComponent.getPackageName(), 0);
120            }
121        } catch (NameNotFoundException e) {
122            Log.e(TAG, "PM could not load app info for " + defaultComponent);
123            // Continue intentionally
124        }
125
126        mNewDefault = component;
127
128        // Compose dialog; get
129        final AlertController.AlertParams p = mAlertParams;
130        p.mTitle = getString(R.string.nfc_payment_set_default);
131        if (defaultAppInfo == null || !isAuto) {
132            p.mMessage = "Always use " + newAppInfo.loadLabel(pm) + " when you tap and pay?";
133        } else {
134            p.mMessage = "Always use " + newAppInfo.loadLabel(pm) + " instead of " +
135                    defaultAppInfo.loadLabel(pm) + " when you tap and pay?";
136        }
137        p.mPositiveButtonText = getString(R.string.yes);
138        p.mNegativeButtonText = getString(R.string.no);
139        p.mPositiveButtonListener = this;
140        p.mNegativeButtonListener = this;
141        setupAlert();
142
143        return true;
144    }
145
146}
147