Home | History | Annotate | Download | only in nfc
      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 
     28 import java.util.ArrayList;
     29 import java.util.List;
     30 
     31 public 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 }