• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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.dashboard.DashboardFragment;
31 import com.android.settings.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                 || mSavedDockUpdater != null)
57                 ? AVAILABLE
58                 : CONDITIONALLY_UNAVAILABLE;
59     }
60 
61     @Override
displayPreference(PreferenceScreen screen)62     public void displayPreference(PreferenceScreen screen) {
63         super.displayPreference(screen);
64         mPreferenceGroup = screen.findPreference(getPreferenceKey());
65         mPreferenceGroup.setVisible(false);
66 
67         if (isAvailable()) {
68             final Context context = screen.getContext();
69             mBluetoothDeviceUpdater.setPrefContext(context);
70             mSavedDockUpdater.setPreferenceContext(context);
71         }
72     }
73 
74     @Override
onStart()75     public void onStart() {
76         mBluetoothDeviceUpdater.registerCallback();
77         mSavedDockUpdater.registerCallback();
78     }
79 
80     @Override
onStop()81     public void onStop() {
82         mBluetoothDeviceUpdater.unregisterCallback();
83         mSavedDockUpdater.unregisterCallback();
84     }
85 
init(DashboardFragment fragment)86     public void init(DashboardFragment fragment) {
87         mBluetoothDeviceUpdater = new SavedBluetoothDeviceUpdater(fragment.getContext(),
88                 fragment, PreviouslyConnectedDevicePreferenceController.this);
89     }
90 
91     @Override
onDeviceAdded(Preference preference)92     public void onDeviceAdded(Preference preference) {
93         mPreferenceSize++;
94         if (mPreferenceSize <= MAX_DEVICE_NUM) {
95             mPreferenceGroup.addPreference(preference);
96         }
97         updatePreferenceVisiblity();
98     }
99 
100     @Override
onDeviceRemoved(Preference preference)101     public void onDeviceRemoved(Preference preference) {
102         mPreferenceSize--;
103         mPreferenceGroup.removePreference(preference);
104         updatePreferenceVisiblity();
105     }
106 
107     @VisibleForTesting
setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater)108     void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) {
109         mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
110     }
111 
112     @VisibleForTesting
setSavedDockUpdater(DockUpdater savedDockUpdater)113     void setSavedDockUpdater(DockUpdater savedDockUpdater) {
114         mSavedDockUpdater = savedDockUpdater;
115     }
116 
117     @VisibleForTesting
setPreferenceGroup(PreferenceGroup preferenceGroup)118     void setPreferenceGroup(PreferenceGroup preferenceGroup) {
119         mPreferenceGroup = preferenceGroup;
120     }
121 
122     @VisibleForTesting
updatePreferenceVisiblity()123     void updatePreferenceVisiblity() {
124         mPreferenceGroup.setVisible(mPreferenceSize > 0);
125     }
126 }
127