1 /* 2 * Copyright 2019 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.car.developeroptions.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.car.developeroptions.bluetooth.BluetoothDeviceUpdater; 27 import com.android.car.developeroptions.bluetooth.SavedBluetoothDeviceUpdater; 28 import com.android.car.developeroptions.connecteddevice.dock.DockUpdater; 29 import com.android.car.developeroptions.core.BasePreferenceController; 30 import com.android.car.developeroptions.dashboard.DashboardFragment; 31 import com.android.car.developeroptions.overlay.FeatureFactory; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnStart; 34 import com.android.settingslib.core.lifecycle.events.OnStop; 35 36 public class PreviouslyConnectedDevicePreferenceController extends BasePreferenceController 37 implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback { 38 39 private static final int MAX_DEVICE_NUM = 3; 40 41 private PreferenceGroup mPreferenceGroup; 42 private BluetoothDeviceUpdater mBluetoothDeviceUpdater; 43 private DockUpdater mSavedDockUpdater; 44 private int mPreferenceSize; 45 PreviouslyConnectedDevicePreferenceController(Context context, String preferenceKey)46 public PreviouslyConnectedDevicePreferenceController(Context context, String preferenceKey) { 47 super(context, preferenceKey); 48 49 mSavedDockUpdater = FeatureFactory.getFactory( 50 context).getDockUpdaterFeatureProvider().getSavedDockUpdater(context, this); 51 } 52 53 @Override getAvailabilityStatus()54 public int getAvailabilityStatus() { 55 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) 56 ? AVAILABLE 57 : CONDITIONALLY_UNAVAILABLE; 58 } 59 60 @Override displayPreference(PreferenceScreen screen)61 public void displayPreference(PreferenceScreen screen) { 62 super.displayPreference(screen); 63 mPreferenceGroup = screen.findPreference(getPreferenceKey()); 64 mPreferenceGroup.setVisible(false); 65 66 if (isAvailable()) { 67 final Context context = screen.getContext(); 68 mBluetoothDeviceUpdater.setPrefContext(context); 69 mSavedDockUpdater.setPreferenceContext(context); 70 } 71 } 72 73 @Override onStart()74 public void onStart() { 75 mBluetoothDeviceUpdater.registerCallback(); 76 mSavedDockUpdater.registerCallback(); 77 } 78 79 @Override onStop()80 public void onStop() { 81 mBluetoothDeviceUpdater.unregisterCallback(); 82 mSavedDockUpdater.unregisterCallback(); 83 } 84 init(DashboardFragment fragment)85 public void init(DashboardFragment fragment) { 86 mBluetoothDeviceUpdater = new SavedBluetoothDeviceUpdater(fragment.getContext(), 87 fragment, PreviouslyConnectedDevicePreferenceController.this); 88 } 89 90 @Override onDeviceAdded(Preference preference)91 public void onDeviceAdded(Preference preference) { 92 mPreferenceSize++; 93 if (mPreferenceSize <= MAX_DEVICE_NUM) { 94 mPreferenceGroup.addPreference(preference); 95 } 96 updatePreferenceVisiblity(); 97 } 98 99 @Override onDeviceRemoved(Preference preference)100 public void onDeviceRemoved(Preference preference) { 101 mPreferenceSize--; 102 mPreferenceGroup.removePreference(preference); 103 updatePreferenceVisiblity(); 104 } 105 106 @VisibleForTesting setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater)107 void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) { 108 mBluetoothDeviceUpdater = bluetoothDeviceUpdater; 109 } 110 111 @VisibleForTesting setSavedDockUpdater(DockUpdater savedDockUpdater)112 void setSavedDockUpdater(DockUpdater savedDockUpdater) { 113 mSavedDockUpdater = savedDockUpdater; 114 } 115 116 @VisibleForTesting setPreferenceGroup(PreferenceGroup preferenceGroup)117 void setPreferenceGroup(PreferenceGroup preferenceGroup) { 118 mPreferenceGroup = preferenceGroup; 119 } 120 121 @VisibleForTesting updatePreferenceVisiblity()122 void updatePreferenceVisiblity() { 123 mPreferenceGroup.setVisible(mPreferenceSize > 0); 124 } 125 } 126