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 package com.android.settings.connecteddevice; 17 18 import android.content.Context; 19 import android.content.pm.PackageManager; 20 21 import androidx.annotation.VisibleForTesting; 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceGroup; 24 import androidx.preference.PreferenceScreen; 25 26 import com.android.settings.bluetooth.BluetoothDeviceUpdater; 27 import com.android.settings.bluetooth.SavedBluetoothDeviceUpdater; 28 import com.android.settings.connecteddevice.dock.DockUpdater; 29 import com.android.settings.core.BasePreferenceController; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settings.dashboard.DashboardFragment; 32 import com.android.settings.overlay.DockUpdaterFeatureProvider; 33 import com.android.settings.overlay.FeatureFactory; 34 import com.android.settingslib.core.lifecycle.LifecycleObserver; 35 import com.android.settingslib.core.lifecycle.events.OnStart; 36 import com.android.settingslib.core.lifecycle.events.OnStop; 37 38 /** 39 * Controller to maintain the {@link PreferenceGroup} for all 40 * saved devices. It uses {@link DevicePreferenceCallback} to add/remove {@link Preference} 41 */ 42 public class SavedDeviceGroupController extends BasePreferenceController 43 implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop, 44 DevicePreferenceCallback { 45 46 private static final String KEY = "saved_device_list"; 47 48 @VisibleForTesting 49 PreferenceGroup mPreferenceGroup; 50 private BluetoothDeviceUpdater mBluetoothDeviceUpdater; 51 private DockUpdater mSavedDockUpdater; 52 SavedDeviceGroupController(Context context)53 public SavedDeviceGroupController(Context context) { 54 super(context, KEY); 55 56 DockUpdaterFeatureProvider dockUpdaterFeatureProvider = 57 FeatureFactory.getFactory(context).getDockUpdaterFeatureProvider(); 58 mSavedDockUpdater = 59 dockUpdaterFeatureProvider.getSavedDockUpdater(context, this); 60 } 61 62 @Override onStart()63 public void onStart() { 64 mBluetoothDeviceUpdater.registerCallback(); 65 mSavedDockUpdater.registerCallback(); 66 } 67 68 @Override onStop()69 public void onStop() { 70 mBluetoothDeviceUpdater.unregisterCallback(); 71 mSavedDockUpdater.unregisterCallback(); 72 } 73 74 @Override displayPreference(PreferenceScreen screen)75 public void displayPreference(PreferenceScreen screen) { 76 mPreferenceGroup = screen.findPreference(KEY); 77 mPreferenceGroup.setVisible(false); 78 79 if (isAvailable()) { 80 final Context context = screen.getContext(); 81 mBluetoothDeviceUpdater.setPrefContext(context); 82 mBluetoothDeviceUpdater.forceUpdate(); 83 mSavedDockUpdater.setPreferenceContext(context); 84 mSavedDockUpdater.forceUpdate(); 85 } 86 } 87 88 @Override getAvailabilityStatus()89 public int getAvailabilityStatus() { 90 return (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) 91 || mSavedDockUpdater != null) 92 ? AVAILABLE 93 : UNSUPPORTED_ON_DEVICE; 94 } 95 96 @Override getPreferenceKey()97 public String getPreferenceKey() { 98 return KEY; 99 } 100 101 @Override onDeviceAdded(Preference preference)102 public void onDeviceAdded(Preference preference) { 103 if (mPreferenceGroup.getPreferenceCount() == 0) { 104 mPreferenceGroup.setVisible(true); 105 } 106 mPreferenceGroup.addPreference(preference); 107 } 108 109 @Override onDeviceRemoved(Preference preference)110 public void onDeviceRemoved(Preference preference) { 111 mPreferenceGroup.removePreference(preference); 112 if (mPreferenceGroup.getPreferenceCount() == 0) { 113 mPreferenceGroup.setVisible(false); 114 } 115 } 116 init(DashboardFragment fragment)117 public void init(DashboardFragment fragment) { 118 mBluetoothDeviceUpdater = new SavedBluetoothDeviceUpdater(fragment.getContext(), 119 fragment, SavedDeviceGroupController.this); 120 } 121 122 @VisibleForTesting setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater)123 public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) { 124 mBluetoothDeviceUpdater = bluetoothDeviceUpdater; 125 } 126 127 @VisibleForTesting setSavedDockUpdater(DockUpdater savedDockUpdater)128 public void setSavedDockUpdater(DockUpdater savedDockUpdater) { 129 mSavedDockUpdater = savedDockUpdater; 130 } 131 } 132