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