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.bluetooth; 18 19 import android.content.Context; 20 21 import androidx.preference.PreferenceFragmentCompat; 22 import androidx.preference.PreferenceScreen; 23 24 import com.android.settings.core.PreferenceControllerMixin; 25 import com.android.settings.overlay.FeatureFactory; 26 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 27 import com.android.settingslib.core.AbstractPreferenceController; 28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 29 import com.android.settingslib.core.lifecycle.Lifecycle; 30 import com.android.settingslib.core.lifecycle.LifecycleObserver; 31 import com.android.settingslib.core.lifecycle.events.OnPause; 32 import com.android.settingslib.core.lifecycle.events.OnResume; 33 34 /** 35 * This class provides common lifecycle and bluetooth device event registration for Bluetooth device 36 * details controllers. 37 */ 38 public abstract class BluetoothDetailsController extends AbstractPreferenceController 39 implements PreferenceControllerMixin, CachedBluetoothDevice.Callback, LifecycleObserver, 40 OnPause, OnResume { 41 42 protected final Context mContext; 43 protected final PreferenceFragmentCompat mFragment; 44 protected final CachedBluetoothDevice mCachedDevice; 45 protected final MetricsFeatureProvider mMetricsFeatureProvider; 46 BluetoothDetailsController(Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)47 public BluetoothDetailsController(Context context, PreferenceFragmentCompat fragment, 48 CachedBluetoothDevice device, Lifecycle lifecycle) { 49 super(context); 50 mContext = context; 51 mFragment = fragment; 52 mCachedDevice = device; 53 lifecycle.addObserver(this); 54 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 55 } 56 57 @Override onPause()58 public void onPause() { 59 mCachedDevice.unregisterCallback(this); 60 } 61 62 @Override onResume()63 public void onResume() { 64 mCachedDevice.registerCallback(this); 65 refresh(); 66 } 67 68 @Override isAvailable()69 public boolean isAvailable() { 70 return true; 71 } 72 73 @Override onDeviceAttributesChanged()74 public void onDeviceAttributesChanged() { 75 refresh(); 76 } 77 78 @Override displayPreference(PreferenceScreen screen)79 public final void displayPreference(PreferenceScreen screen) { 80 init(screen); 81 super.displayPreference(screen); 82 } 83 84 /** 85 * This is a method to do one-time initialization when the screen is first created, such as 86 * adding preferences. 87 * @param screen the screen where this controller's preferences should be added 88 */ init(PreferenceScreen screen)89 protected abstract void init(PreferenceScreen screen); 90 91 /** 92 * This method is called when something about the bluetooth device has changed, and this object 93 * should update the preferences it manages based on the new state. 94 */ refresh()95 protected abstract void refresh(); 96 }