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