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.nfc.NfcAdapter; 20 import android.support.v7.preference.Preference; 21 import android.support.v7.preference.PreferenceScreen; 22 23 import com.android.settings.core.BasePreferenceController; 24 import com.android.settingslib.RestrictedPreference; 25 import com.android.settingslib.core.lifecycle.LifecycleObserver; 26 import com.android.settingslib.core.lifecycle.events.OnPause; 27 import com.android.settingslib.core.lifecycle.events.OnResume; 28 29 import java.util.List; 30 31 public class AndroidBeamPreferenceController extends BasePreferenceController 32 implements LifecycleObserver, OnResume, OnPause { 33 34 public static final String KEY_ANDROID_BEAM_SETTINGS = "android_beam_settings"; 35 private final NfcAdapter mNfcAdapter; 36 private AndroidBeamEnabler mAndroidBeamEnabler; 37 private NfcAirplaneModeObserver mAirplaneModeObserver; 38 39 public AndroidBeamPreferenceController(Context context, String key) { 40 super(context, key); 41 mNfcAdapter = NfcAdapter.getDefaultAdapter(context); 42 } 43 44 @Override 45 public void displayPreference(PreferenceScreen screen) { 46 super.displayPreference(screen); 47 if (!isAvailable()) { 48 mAndroidBeamEnabler = null; 49 return; 50 } 51 52 final RestrictedPreference restrictedPreference = 53 (RestrictedPreference) screen.findPreference(getPreferenceKey()); 54 mAndroidBeamEnabler = new AndroidBeamEnabler(mContext, restrictedPreference); 55 56 // Manually set dependencies for NFC when not toggleable. 57 if (!NfcPreferenceController.isToggleableInAirplaneMode(mContext)) { 58 mAirplaneModeObserver = new NfcAirplaneModeObserver(mContext, mNfcAdapter, 59 (Preference) restrictedPreference); 60 } 61 } 62 63 @Override 64 @AvailabilityStatus 65 public int getAvailabilityStatus() { 66 return mNfcAdapter != null 67 ? AVAILABLE 68 : UNSUPPORTED_ON_DEVICE; 69 } 70 71 @Override 72 public void onResume() { 73 if (mAirplaneModeObserver != null) { 74 mAirplaneModeObserver.register(); 75 } 76 if (mAndroidBeamEnabler != null) { 77 mAndroidBeamEnabler.resume(); 78 } 79 } 80 81 @Override 82 public void onPause() { 83 if (mAirplaneModeObserver != null) { 84 mAirplaneModeObserver.unregister(); 85 } 86 if (mAndroidBeamEnabler != null) { 87 mAndroidBeamEnabler.pause(); 88 } 89 } 90 } 91