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