1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.nfc;
15 
16 import android.app.settings.SettingsEnums;
17 import android.content.Context;
18 import android.content.pm.PackageManager;
19 import android.permission.flags.Flags;
20 
21 import androidx.preference.ListPreference;
22 import androidx.preference.Preference;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.BasePreferenceController;
26 import com.android.settings.overlay.FeatureFactory;
27 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnStart;
30 import com.android.settingslib.core.lifecycle.events.OnStop;
31 
32 import java.util.List;
33 
34 public class NfcForegroundPreferenceController extends BasePreferenceController implements
35         PaymentBackend.Callback, Preference.OnPreferenceChangeListener,
36         LifecycleObserver, OnStart, OnStop {
37 
38     private ListPreference mPreference;
39     private PaymentBackend mPaymentBackend;
40     private MetricsFeatureProvider mMetricsFeatureProvider;
41 
42     private final String[] mListValues;
43     private final String[] mListEntries;
44 
NfcForegroundPreferenceController(Context context, String key)45     public NfcForegroundPreferenceController(Context context, String key) {
46         super(context, key);
47         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
48         mListValues = context.getResources().getStringArray(R.array.nfc_payment_favor_values);
49         mListEntries = context.getResources().getStringArray(R.array.nfc_payment_favor);
50     }
51 
setPaymentBackend(PaymentBackend backend)52     public void setPaymentBackend(PaymentBackend backend) {
53         mPaymentBackend = backend;
54     }
55 
56     @Override
onStart()57     public void onStart() {
58         if (mPaymentBackend != null) {
59             mPaymentBackend.registerCallback(this);
60         }
61     }
62 
63     @Override
onStop()64     public void onStop() {
65         if (mPaymentBackend != null) {
66             mPaymentBackend.unregisterCallback(this);
67         }
68     }
69 
70     @Override
getAvailabilityStatus()71     public int getAvailabilityStatus() {
72         if (Flags.walletRoleEnabled()) {
73             return UNSUPPORTED_ON_DEVICE;
74         }
75         final PackageManager pm = mContext.getPackageManager();
76         if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
77             return UNSUPPORTED_ON_DEVICE;
78         }
79         if (mPaymentBackend == null) {
80             return UNSUPPORTED_ON_DEVICE;
81         }
82         final List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
83         return (appInfos != null && !appInfos.isEmpty())
84                 ? AVAILABLE
85                 : UNSUPPORTED_ON_DEVICE;
86     }
87 
88     @Override
onPaymentAppsChanged()89     public void onPaymentAppsChanged() {
90         updateState(mPreference);
91     }
92 
93     @Override
updateState(Preference preference)94     public void updateState(Preference preference) {
95         super.updateState(preference);
96         if (!(preference instanceof ListPreference)) {
97             return;
98         }
99         final ListPreference listPreference = (ListPreference) preference;
100         listPreference.setIconSpaceReserved(false);
101         listPreference.setValue(mListValues[mPaymentBackend.isForegroundMode() ? 1 : 0]);
102     }
103 
104     @Override
getSummary()105     public CharSequence getSummary() {
106         return mListEntries[mPaymentBackend.isForegroundMode() ? 1 : 0];
107     }
108 
109     @Override
onPreferenceChange(Preference preference, Object newValue)110     public boolean onPreferenceChange(Preference preference, Object newValue) {
111         if (!(preference instanceof ListPreference)) {
112             return false;
113         }
114 
115         final ListPreference listPreference = (ListPreference) preference;
116         final String newValueString = (String) newValue;
117         listPreference.setSummary(mListEntries[listPreference.findIndexOfValue(newValueString)]);
118         final boolean foregroundMode = Integer.parseInt(newValueString) != 0;
119         mPaymentBackend.setForegroundMode(foregroundMode);
120         mMetricsFeatureProvider.action(mContext,
121                 foregroundMode ? SettingsEnums.ACTION_NFC_PAYMENT_FOREGROUND_SETTING
122                         : SettingsEnums.ACTION_NFC_PAYMENT_ALWAYS_SETTING);
123         return true;
124     }
125 }
126