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.Context;
21import android.content.pm.PackageManager;
22import android.graphics.drawable.Drawable;
23import android.nfc.NfcAdapter;
24import android.nfc.cardemulation.ApduServiceInfo;
25import android.nfc.cardemulation.CardEmulation;
26import android.provider.Settings;
27
28import java.util.ArrayList;
29import java.util.List;
30
31public class PaymentBackend {
32    public static final String TAG = "Settings.PaymentBackend";
33
34    public static class PaymentAppInfo {
35        CharSequence caption;
36        Drawable banner;
37        boolean isDefault;
38        public ComponentName componentName;
39    }
40
41    private final Context mContext;
42    private final NfcAdapter mAdapter;
43    private final CardEmulation mCardEmuManager;
44
45    public PaymentBackend(Context context) {
46        mContext = context;
47
48        mAdapter = NfcAdapter.getDefaultAdapter(context);
49        mCardEmuManager = CardEmulation.getInstance(mAdapter);
50    }
51
52    public List<PaymentAppInfo> getPaymentAppInfos() {
53        PackageManager pm = mContext.getPackageManager();
54        List<ApduServiceInfo> serviceInfos =
55                mCardEmuManager.getServices(CardEmulation.CATEGORY_PAYMENT);
56        List<PaymentAppInfo> appInfos = new ArrayList<PaymentAppInfo>();
57
58        if (serviceInfos == null) return appInfos;
59
60        ComponentName defaultApp = getDefaultPaymentApp();
61
62        for (ApduServiceInfo service : serviceInfos) {
63            PaymentAppInfo appInfo = new PaymentAppInfo();
64            appInfo.banner = service.loadBanner(pm);
65            appInfo.caption = service.getDescription();
66            if (appInfo.caption == null) {
67                appInfo.caption = service.loadLabel(pm);
68            }
69            appInfo.isDefault = service.getComponent().equals(defaultApp);
70            appInfo.componentName = service.getComponent();
71            appInfos.add(appInfo);
72        }
73
74        return appInfos;
75    }
76
77    ComponentName getDefaultPaymentApp() {
78        String componentString = Settings.Secure.getString(mContext.getContentResolver(),
79                Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
80        if (componentString != null) {
81            return ComponentName.unflattenFromString(componentString);
82        } else {
83            return null;
84        }
85    }
86
87    public void setDefaultPaymentApp(ComponentName app) {
88        Settings.Secure.putString(mContext.getContentResolver(),
89                Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
90                app != null ? app.flattenToString() : null);
91    }
92}