1 /* 2 * Copyright 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.connecteddevice; 17 18 import android.content.Context; 19 import android.content.pm.PackageManager; 20 import android.support.annotation.VisibleForTesting; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceScreen; 23 24 import android.util.Log; 25 import com.android.settings.bluetooth.BluetoothDeviceUpdater; 26 import com.android.settings.bluetooth.SavedBluetoothDeviceUpdater; 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settings.dashboard.DashboardFragment; 29 30 import com.android.settingslib.core.lifecycle.LifecycleObserver; 31 import com.android.settingslib.core.lifecycle.events.OnStart; 32 import com.android.settingslib.core.lifecycle.events.OnStop; 33 import com.android.settingslib.utils.ThreadUtils; 34 35 public class PreviouslyConnectedDevicePreferenceController extends BasePreferenceController 36 implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback { 37 38 private Preference mPreference; 39 private BluetoothDeviceUpdater mBluetoothDeviceUpdater; 40 private int mPreferenceSize; 41 42 public PreviouslyConnectedDevicePreferenceController(Context context, String preferenceKey) { 43 super(context, preferenceKey); 44 } 45 46 @Override 47 public int getAvailabilityStatus() { 48 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) 49 ? AVAILABLE 50 : CONDITIONALLY_UNAVAILABLE; 51 } 52 53 @Override 54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 if (isAvailable()) { 57 mPreference = screen.findPreference(getPreferenceKey()); 58 mBluetoothDeviceUpdater.setPrefContext(screen.getContext()); 59 } 60 } 61 62 @Override 63 public void onStart() { 64 mBluetoothDeviceUpdater.registerCallback(); 65 updatePreferenceOnSizeChanged(); 66 } 67 68 @Override 69 public void onStop() { 70 mBluetoothDeviceUpdater.unregisterCallback(); 71 } 72 73 public void init(DashboardFragment fragment) { 74 mBluetoothDeviceUpdater = new SavedBluetoothDeviceUpdater(fragment.getContext(), 75 fragment, PreviouslyConnectedDevicePreferenceController.this); 76 } 77 78 @Override 79 public void onDeviceAdded(Preference preference) { 80 mPreferenceSize++; 81 updatePreferenceOnSizeChanged(); 82 } 83 84 @Override 85 public void onDeviceRemoved(Preference preference) { 86 mPreferenceSize--; 87 updatePreferenceOnSizeChanged(); 88 } 89 90 @VisibleForTesting 91 void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) { 92 mBluetoothDeviceUpdater = bluetoothDeviceUpdater; 93 } 94 95 @VisibleForTesting 96 void setPreferenceSize(int size) { 97 mPreferenceSize = size; 98 } 99 100 @VisibleForTesting 101 void setPreference(Preference preference) { 102 mPreference = preference; 103 } 104 105 private void updatePreferenceOnSizeChanged() { 106 if (isAvailable()) { 107 mPreference.setEnabled(mPreferenceSize != 0); 108 } 109 } 110 }