1 /*
2  * Copyright (C) 2018 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 package com.android.settings.nfc;
17 
18 import android.content.Context;
19 import android.content.pm.PackageManager;
20 import android.nfc.NfcAdapter;
21 
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.settings.core.BasePreferenceController;
25 import com.android.settingslib.RestrictedPreference;
26 import com.android.settingslib.core.lifecycle.LifecycleObserver;
27 import com.android.settingslib.core.lifecycle.events.OnPause;
28 import com.android.settingslib.core.lifecycle.events.OnResume;
29 
30 public class AndroidBeamPreferenceController extends BasePreferenceController
31         implements LifecycleObserver, OnResume, OnPause {
32 
33     public static final String KEY_ANDROID_BEAM_SETTINGS = "android_beam_settings";
34     private final NfcAdapter mNfcAdapter;
35     private AndroidBeamEnabler mAndroidBeamEnabler;
36 
AndroidBeamPreferenceController(Context context, String key)37     public AndroidBeamPreferenceController(Context context, String key) {
38         super(context, key);
39         mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
40     }
41 
42     @Override
displayPreference(PreferenceScreen screen)43     public void displayPreference(PreferenceScreen screen) {
44         super.displayPreference(screen);
45         if (!isAvailable()) {
46             mAndroidBeamEnabler = null;
47             return;
48         }
49 
50         final RestrictedPreference restrictedPreference = screen.findPreference(getPreferenceKey());
51         mAndroidBeamEnabler = new AndroidBeamEnabler(mContext, restrictedPreference);
52     }
53 
54     @Override
55     @AvailabilityStatus
getAvailabilityStatus()56     public int getAvailabilityStatus() {
57         PackageManager pm = mContext.getPackageManager();
58         if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)) {
59             return UNSUPPORTED_ON_DEVICE;
60         }
61         return mNfcAdapter != null
62                 ? AVAILABLE
63                 : UNSUPPORTED_ON_DEVICE;
64     }
65 
66     @Override
onResume()67     public void onResume() {
68         if (mAndroidBeamEnabler != null) {
69             mAndroidBeamEnabler.resume();
70         }
71     }
72 
73     @Override
onPause()74     public void onPause() {
75         if (mAndroidBeamEnabler != null) {
76             mAndroidBeamEnabler.pause();
77         }
78     }
79 }
80