1 /* 2 * Copyright (C) 2017 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.applications.specialaccess; 18 19 import android.app.role.RoleManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.nfc.NfcAdapter; 24 import android.nfc.cardemulation.CardEmulation; 25 import android.os.UserManager; 26 import android.permission.flags.Flags; 27 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.core.BasePreferenceController; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnPause; 34 import com.android.settingslib.core.lifecycle.events.OnResume; 35 36 /** 37 * This Controller works with PaymentSettingsEnabler to control payment features availability 38 * based on NFC status 39 */ 40 public class DefaultPaymentSettingsPreferenceController extends BasePreferenceController 41 implements LifecycleObserver, OnResume, OnPause { 42 43 private final NfcAdapter mNfcAdapter; 44 private PaymentSettingsEnabler mPaymentSettingsEnabler; 45 private final PackageManager mPackageManager; 46 private final UserManager mUserManager; 47 DefaultPaymentSettingsPreferenceController(Context context, String preferenceKey)48 public DefaultPaymentSettingsPreferenceController(Context context, String preferenceKey) { 49 super(context, preferenceKey); 50 51 mPackageManager = context.getPackageManager(); 52 mUserManager = context.getSystemService(UserManager.class); 53 mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext); 54 } 55 56 @Override displayPreference(PreferenceScreen screen)57 public void displayPreference(PreferenceScreen screen) { 58 super.displayPreference(screen); 59 if (!isAvailable()) { 60 mPaymentSettingsEnabler = null; 61 return; 62 } 63 64 final Preference preference = screen.findPreference(getPreferenceKey()); 65 mPaymentSettingsEnabler = new PaymentSettingsEnabler(mContext, preference); 66 } 67 68 @Override handlePreferenceTreeClick(Preference preference)69 public boolean handlePreferenceTreeClick(Preference preference) { 70 if (Flags.walletRoleEnabled() 71 && mPreferenceKey.equals(preference.getKey())) { 72 RoleManager roleManager = mContext.getSystemService(RoleManager.class); 73 if (roleManager.isRoleAvailable(RoleManager.ROLE_WALLET)) { 74 Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT) 75 .setPackage(mPackageManager.getPermissionControllerPackageName()); 76 mContext.startActivity(intent); 77 return true; 78 } 79 } 80 return false; 81 } 82 83 @Override onResume()84 public void onResume() { 85 if (mPaymentSettingsEnabler != null) { 86 mPaymentSettingsEnabler.resume(); 87 } 88 } 89 90 @Override onPause()91 public void onPause() { 92 if (mPaymentSettingsEnabler != null) { 93 mPaymentSettingsEnabler.pause(); 94 } 95 } 96 97 @Override getAvailabilityStatus()98 public int getAvailabilityStatus() { 99 if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC) 100 || !mPackageManager.hasSystemFeature( 101 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) { 102 return UNSUPPORTED_ON_DEVICE; 103 } 104 if (mUserManager.isGuestUser()) { 105 return DISABLED_FOR_USER; 106 } 107 if (mNfcAdapter == null) { 108 return CONDITIONALLY_UNAVAILABLE; 109 } 110 if (!mNfcAdapter.isEnabled()) { 111 return DISABLED_DEPENDENT_SETTING; 112 } 113 return AVAILABLE; 114 } 115 } 116