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.content.Context;
20 import android.content.pm.PackageManager;
21 import android.nfc.NfcAdapter;
22 import android.os.UserManager;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnPause;
30 import com.android.settingslib.core.lifecycle.events.OnResume;
31 
32 /**
33  * This Controller works with PaymentSettingsEnabler to control payment features availability
34  * based on NFC status
35  */
36 public class DefaultPaymentSettingsPreferenceController extends BasePreferenceController
37         implements LifecycleObserver, OnResume, OnPause {
38 
39     private final NfcAdapter mNfcAdapter;
40     private PaymentSettingsEnabler mPaymentSettingsEnabler;
41     private final PackageManager mPackageManager;
42     private final UserManager mUserManager;
43 
DefaultPaymentSettingsPreferenceController(Context context, String preferenceKey)44     public DefaultPaymentSettingsPreferenceController(Context context, String preferenceKey) {
45         super(context, preferenceKey);
46 
47         mPackageManager = context.getPackageManager();
48         mUserManager = context.getSystemService(UserManager.class);
49         mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext);
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         if (!isAvailable()) {
56             mPaymentSettingsEnabler = null;
57             return;
58         }
59 
60         final Preference preference = screen.findPreference(getPreferenceKey());
61         mPaymentSettingsEnabler = new PaymentSettingsEnabler(mContext, preference);
62     }
63 
64     @Override
onResume()65     public void onResume() {
66         if (mPaymentSettingsEnabler != null) {
67             mPaymentSettingsEnabler.resume();
68         }
69     }
70 
71     @Override
onPause()72     public void onPause() {
73         if (mPaymentSettingsEnabler != null) {
74             mPaymentSettingsEnabler.pause();
75         }
76     }
77 
78     @Override
getAvailabilityStatus()79     public int getAvailabilityStatus() {
80         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
81                 || !mPackageManager.hasSystemFeature(
82                         PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
83             return UNSUPPORTED_ON_DEVICE;
84         }
85         if (!mUserManager.isAdminUser()) {
86             return DISABLED_FOR_USER;
87         }
88         if (mNfcAdapter == null) {
89             return CONDITIONALLY_UNAVAILABLE;
90         }
91         if (!mNfcAdapter.isEnabled()) {
92             return DISABLED_DEPENDENT_SETTING;
93         }
94         return AVAILABLE;
95     }
96 }
97