1 /*
2  * Copyright (C) 2022 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.Context;
20 import android.content.pm.PackageManager;
21 import android.graphics.drawable.Drawable;
22 import android.nfc.NfcAdapter;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
28 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
29 import com.android.settingslib.applications.DefaultAppInfo;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnPause;
33 import com.android.settingslib.core.lifecycle.events.OnResume;
34 
35 import java.util.List;
36 
37 /**
38  * NfcDefaultPaymentPreferenceController shows an app icon and text summary for current selected
39  * default payment, and links to the nfc default payment selection page.
40  */
41 public class NfcDefaultPaymentPreferenceController extends DefaultAppPreferenceController implements
42         PaymentBackend.Callback, LifecycleObserver, OnResume, OnPause {
43 
44     private static final String TAG = "NfcDefaultPaymentController";
45     private static final String KEY = "nfc_payment_app";
46 
47     private PaymentBackend mPaymentBackend;
48     private Preference mPreference;
49     private Context mContext;
50 
NfcDefaultPaymentPreferenceController(Context context, Lifecycle lifecycle)51     public NfcDefaultPaymentPreferenceController(Context context, Lifecycle lifecycle) {
52         super(context);
53         mContext = context;
54         mPaymentBackend = new PaymentBackend(context);
55         if (lifecycle != null) {
56             lifecycle.addObserver(this);
57         }
58     }
59 
60     @Override
isAvailable()61     public boolean isAvailable() {
62         final PackageManager pm = mContext.getPackageManager();
63         if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
64             return false;
65         }
66         if (NfcAdapter.getDefaultAdapter(mContext) == null) {
67             return false;
68         }
69         if (mPaymentBackend == null) {
70             mPaymentBackend = new PaymentBackend(mContext);
71         }
72         final List<PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
73         return (appInfos != null && !appInfos.isEmpty())
74                 ? true
75                 : false;
76     }
77 
78     @Override
getPreferenceKey()79     public String getPreferenceKey() {
80         return KEY;
81     }
82 
83     @Override
onResume()84     public void onResume() {
85         if (mPaymentBackend != null) {
86             mPaymentBackend.registerCallback(this);
87             mPaymentBackend.onResume();
88         }
89     }
90 
91     @Override
onPause()92     public void onPause() {
93         if (mPaymentBackend != null) {
94             mPaymentBackend.unregisterCallback(this);
95             mPaymentBackend.onPause();
96         }
97     }
98 
99     @Override
displayPreference(PreferenceScreen screen)100     public void displayPreference(PreferenceScreen screen) {
101         mPreference = screen.findPreference(getPreferenceKey());
102         super.displayPreference(screen);
103     }
104 
105     @Override
updateState(Preference preference)106     public void updateState(Preference preference) {
107         super.updateState(preference);
108         preference.setIconSpaceReserved(true);
109     }
110 
111     @Override
onPaymentAppsChanged()112     public void onPaymentAppsChanged() {
113         updateState(mPreference);
114     }
115 
116     /**
117      * PaymentDefaultAppInfo is used to store the default payment app info.
118      */
119     public static class PaymentDefaultAppInfo extends DefaultAppInfo {
120         public PaymentAppInfo mInfo;
121 
PaymentDefaultAppInfo(Context context, PackageManager pm, int userId, PaymentAppInfo info)122         public PaymentDefaultAppInfo(Context context, PackageManager pm, int userId,
123                 PaymentAppInfo info) {
124             super(context, pm, userId, info.componentName);
125             mInfo = info;
126         }
127 
128         @Override
loadIcon()129         public Drawable loadIcon() {
130             return mInfo.icon;
131         }
132     }
133 
134     @Override
getDefaultAppInfo()135     protected DefaultAppInfo getDefaultAppInfo() {
136         if (mPaymentBackend == null) {
137             return null;
138         }
139         final PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
140         if (defaultApp != null) {
141             return new PaymentDefaultAppInfo(mContext, mPackageManager,
142                     defaultApp.userHandle.getIdentifier(), defaultApp);
143         }
144         return null;
145     }
146 }
147