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