1 /* 2 * Copyright (C) 2019 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.car.developeroptions.nfc; 15 16 import android.app.settings.SettingsEnums; 17 import android.content.Context; 18 import android.content.pm.PackageManager; 19 import android.text.TextUtils; 20 21 import androidx.preference.DropDownPreference; 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceScreen; 24 25 import com.android.car.developeroptions.R; 26 import com.android.car.developeroptions.core.BasePreferenceController; 27 import com.android.car.developeroptions.overlay.FeatureFactory; 28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 29 import com.android.settingslib.core.lifecycle.LifecycleObserver; 30 import com.android.settingslib.core.lifecycle.events.OnStart; 31 import com.android.settingslib.core.lifecycle.events.OnStop; 32 33 import java.util.List; 34 35 public class NfcForegroundPreferenceController extends BasePreferenceController implements 36 PaymentBackend.Callback, Preference.OnPreferenceChangeListener, 37 LifecycleObserver, OnStart, OnStop { 38 39 private DropDownPreference mPreference; 40 private PaymentBackend mPaymentBackend; 41 private MetricsFeatureProvider mMetricsFeatureProvider; 42 NfcForegroundPreferenceController(Context context, String key)43 public NfcForegroundPreferenceController(Context context, String key) { 44 super(context, key); 45 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 46 } 47 setPaymentBackend(PaymentBackend backend)48 public void setPaymentBackend(PaymentBackend backend) { 49 mPaymentBackend = backend; 50 } 51 52 @Override onStart()53 public void onStart() { 54 if (mPaymentBackend != null) { 55 mPaymentBackend.registerCallback(this); 56 } 57 } 58 59 @Override onStop()60 public void onStop() { 61 if (mPaymentBackend != null) { 62 mPaymentBackend.unregisterCallback(this); 63 } 64 } 65 66 @Override getAvailabilityStatus()67 public int getAvailabilityStatus() { 68 final PackageManager pm = mContext.getPackageManager(); 69 if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) { 70 return UNSUPPORTED_ON_DEVICE; 71 } 72 if (mPaymentBackend == null) { 73 return UNSUPPORTED_ON_DEVICE; 74 } 75 final List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos(); 76 return (appInfos != null && !appInfos.isEmpty()) 77 ? AVAILABLE 78 : UNSUPPORTED_ON_DEVICE; 79 } 80 81 @Override displayPreference(PreferenceScreen screen)82 public void displayPreference(PreferenceScreen screen) { 83 super.displayPreference(screen); 84 mPreference = screen.findPreference(getPreferenceKey()); 85 if (mPreference == null) { 86 return; 87 } 88 89 mPreference.setEntries(new CharSequence[]{ 90 mContext.getText(R.string.nfc_payment_favor_open), 91 mContext.getText(R.string.nfc_payment_favor_default) 92 }); 93 mPreference.setEntryValues(new CharSequence[]{"1", "0"}); 94 } 95 96 @Override onPaymentAppsChanged()97 public void onPaymentAppsChanged() { 98 updateState(mPreference); 99 } 100 101 @Override updateState(Preference preference)102 public void updateState(Preference preference) { 103 if (preference instanceof DropDownPreference) { 104 ((DropDownPreference) preference).setValue( 105 mPaymentBackend.isForegroundMode() ? "1" : "0"); 106 } 107 super.updateState(preference); 108 } 109 110 @Override getSummary()111 public CharSequence getSummary() { 112 return mPreference.getEntry(); 113 } 114 115 @Override onPreferenceChange(Preference preference, Object newValue)116 public boolean onPreferenceChange(Preference preference, Object newValue) { 117 if (!(preference instanceof DropDownPreference)) { 118 return false; 119 } 120 final DropDownPreference pref = (DropDownPreference) preference; 121 final String newValueString = (String) newValue; 122 pref.setSummary(pref.getEntries()[pref.findIndexOfValue(newValueString)]); 123 final boolean foregroundMode = Integer.parseInt(newValueString) != 0; 124 mPaymentBackend.setForegroundMode(foregroundMode); 125 mMetricsFeatureProvider.action(mContext, 126 foregroundMode ? SettingsEnums.ACTION_NFC_PAYMENT_FOREGROUND_SETTING 127 : SettingsEnums.ACTION_NFC_PAYMENT_ALWAYS_SETTING); 128 return true; 129 } 130 131 @Override updateNonIndexableKeys(List<String> keys)132 public void updateNonIndexableKeys(List<String> keys) { 133 final String key = getPreferenceKey(); 134 if (!TextUtils.isEmpty(key)) { 135 keys.add(key); 136 } 137 } 138 }