• 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 static com.android.settingslib.Utils.isAudioModeOngoingCall;
19 
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceGroup;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
31 import com.android.settings.bluetooth.BluetoothDeviceUpdater;
32 import com.android.settings.bluetooth.Utils;
33 import com.android.settings.core.BasePreferenceController;
34 import com.android.settings.dashboard.DashboardFragment;
35 import com.android.settingslib.bluetooth.BluetoothCallback;
36 import com.android.settingslib.bluetooth.LocalBluetoothManager;
37 import com.android.settingslib.core.lifecycle.LifecycleObserver;
38 import com.android.settingslib.core.lifecycle.events.OnStart;
39 import com.android.settingslib.core.lifecycle.events.OnStop;
40 
41 /**
42  * Controller to maintain the {@link androidx.preference.PreferenceGroup} for all
43  * available media devices. It uses {@link DevicePreferenceCallback}
44  * to add/remove {@link Preference}
45  */
46 public class AvailableMediaDeviceGroupController extends BasePreferenceController
47         implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback, BluetoothCallback {
48 
49     private static final String TAG = "AvailableMediaDeviceGroupController";
50     private static final String KEY = "available_device_list";
51 
52     @VisibleForTesting
53     PreferenceGroup mPreferenceGroup;
54     @VisibleForTesting
55     LocalBluetoothManager mLocalBluetoothManager;
56     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
57 
AvailableMediaDeviceGroupController(Context context)58     public AvailableMediaDeviceGroupController(Context context) {
59         super(context, KEY);
60         mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
61     }
62 
63     @Override
onStart()64     public void onStart() {
65         if (mLocalBluetoothManager == null) {
66             Log.e(TAG, "onStart() Bluetooth is not supported on this device");
67             return;
68         }
69         mBluetoothDeviceUpdater.registerCallback();
70         mLocalBluetoothManager.getEventManager().registerCallback(this);
71         mBluetoothDeviceUpdater.refreshPreference();
72     }
73 
74     @Override
onStop()75     public void onStop() {
76         if (mLocalBluetoothManager == null) {
77             Log.e(TAG, "onStop() Bluetooth is not supported on this device");
78             return;
79         }
80         mBluetoothDeviceUpdater.unregisterCallback();
81         mLocalBluetoothManager.getEventManager().unregisterCallback(this);
82     }
83 
84     @Override
displayPreference(PreferenceScreen screen)85     public void displayPreference(PreferenceScreen screen) {
86         super.displayPreference(screen);
87 
88         mPreferenceGroup = screen.findPreference(KEY);
89         mPreferenceGroup.setVisible(false);
90 
91         if (isAvailable()) {
92             updateTitle();
93             mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
94             mBluetoothDeviceUpdater.forceUpdate();
95         }
96     }
97 
98     @Override
getAvailabilityStatus()99     public int getAvailabilityStatus() {
100         return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
101                 ? AVAILABLE_UNSEARCHABLE
102                 : UNSUPPORTED_ON_DEVICE;
103     }
104 
105     @Override
getPreferenceKey()106     public String getPreferenceKey() {
107         return KEY;
108     }
109 
110     @Override
onDeviceAdded(Preference preference)111     public void onDeviceAdded(Preference preference) {
112         if (mPreferenceGroup.getPreferenceCount() == 0) {
113             mPreferenceGroup.setVisible(true);
114         }
115         mPreferenceGroup.addPreference(preference);
116     }
117 
118     @Override
onDeviceRemoved(Preference preference)119     public void onDeviceRemoved(Preference preference) {
120         mPreferenceGroup.removePreference(preference);
121         if (mPreferenceGroup.getPreferenceCount() == 0) {
122             mPreferenceGroup.setVisible(false);
123         }
124     }
125 
init(DashboardFragment fragment)126     public void init(DashboardFragment fragment) {
127         mBluetoothDeviceUpdater = new AvailableMediaBluetoothDeviceUpdater(fragment.getContext(),
128                 fragment, AvailableMediaDeviceGroupController.this);
129     }
130 
131     @VisibleForTesting
setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater)132     public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) {
133         mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
134     }
135 
136     @Override
onAudioModeChanged()137     public void onAudioModeChanged() {
138         updateTitle();
139     }
140 
updateTitle()141     private void updateTitle() {
142         if (isAudioModeOngoingCall(mContext)) {
143             // in phone call
144             mPreferenceGroup.
145                     setTitle(mContext.getString(R.string.connected_device_call_device_title));
146         } else {
147             // without phone call
148             mPreferenceGroup.
149                     setTitle(mContext.getString(R.string.connected_device_media_device_title));
150         }
151     }
152 }
153