• 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.support.annotation.VisibleForTesting;
23 import android.support.v7.preference.Preference;
24 import android.support.v7.preference.PreferenceGroup;
25 import android.support.v7.preference.PreferenceScreen;
26 import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
27 import com.android.settings.bluetooth.BluetoothDeviceUpdater;
28 import com.android.settings.bluetooth.Utils;
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.settings.dashboard.DashboardFragment;
31 import com.android.settings.R;
32 import com.android.settingslib.bluetooth.BluetoothCallback;
33 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
34 import com.android.settingslib.bluetooth.LocalBluetoothManager;
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 android.support.v7.preference.PreferenceGroup} for all
41  * available media devices. It uses {@link DevicePreferenceCallback}
42  * to add/remove {@link Preference}
43  */
44 public class AvailableMediaDeviceGroupController extends BasePreferenceController
45         implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback, BluetoothCallback {
46 
47     private static final String KEY = "available_device_list";
48 
49     @VisibleForTesting
50     PreferenceGroup mPreferenceGroup;
51     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
52     private final LocalBluetoothManager mLocalBluetoothManager;
53 
AvailableMediaDeviceGroupController(Context context)54     public AvailableMediaDeviceGroupController(Context context) {
55         super(context, KEY);
56         mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
57     }
58 
59     @Override
onStart()60     public void onStart() {
61         mBluetoothDeviceUpdater.registerCallback();
62         mLocalBluetoothManager.getEventManager().registerCallback(this);
63     }
64 
65     @Override
onStop()66     public void onStop() {
67         mBluetoothDeviceUpdater.unregisterCallback();
68         mLocalBluetoothManager.getEventManager().unregisterCallback(this);
69     }
70 
71     @Override
displayPreference(PreferenceScreen screen)72     public void displayPreference(PreferenceScreen screen) {
73         super.displayPreference(screen);
74         if (isAvailable()) {
75             mPreferenceGroup = (PreferenceGroup) screen.findPreference(KEY);
76             mPreferenceGroup.setVisible(false);
77             updateTitle();
78             mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
79             mBluetoothDeviceUpdater.forceUpdate();
80         }
81     }
82 
83     @Override
getAvailabilityStatus()84     public int getAvailabilityStatus() {
85         return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
86                 ? AVAILABLE
87                 : UNSUPPORTED_ON_DEVICE;
88     }
89 
90     @Override
getPreferenceKey()91     public String getPreferenceKey() {
92         return KEY;
93     }
94 
95     @Override
onDeviceAdded(Preference preference)96     public void onDeviceAdded(Preference preference) {
97         if (mPreferenceGroup.getPreferenceCount() == 0) {
98             mPreferenceGroup.setVisible(true);
99         }
100         mPreferenceGroup.addPreference(preference);
101     }
102 
103     @Override
onDeviceRemoved(Preference preference)104     public void onDeviceRemoved(Preference preference) {
105         mPreferenceGroup.removePreference(preference);
106         if (mPreferenceGroup.getPreferenceCount() == 0) {
107             mPreferenceGroup.setVisible(false);
108         }
109     }
110 
init(DashboardFragment fragment)111     public void init(DashboardFragment fragment) {
112         mBluetoothDeviceUpdater = new AvailableMediaBluetoothDeviceUpdater(fragment.getContext(),
113                 fragment, AvailableMediaDeviceGroupController.this);
114     }
115 
116     @VisibleForTesting
setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater)117     public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) {
118         mBluetoothDeviceUpdater  = bluetoothDeviceUpdater;
119     }
120 
121     @Override
onBluetoothStateChanged(int bluetoothState)122     public void onBluetoothStateChanged(int bluetoothState) {
123         // do nothing
124     }
125 
126     @Override
onScanningStateChanged(boolean started)127     public void onScanningStateChanged(boolean started) {
128         // do nothing
129     }
130 
131     @Override
onDeviceAdded(CachedBluetoothDevice cachedDevice)132     public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
133         // do nothing
134     }
135 
136     @Override
onDeviceDeleted(CachedBluetoothDevice cachedDevice)137     public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
138         // do nothing
139     }
140 
141     @Override
onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState)142     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
143         // do nothing
144     }
145 
146     @Override
onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state)147     public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
148         // do nothing
149     }
150 
151     @Override
onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile)152     public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
153         // do nothing
154     }
155 
156     @Override
onAudioModeChanged()157     public void onAudioModeChanged() {
158         updateTitle();
159     }
160 
updateTitle()161     private void updateTitle() {
162         if (isAudioModeOngoingCall(mContext)) {
163             // in phone call
164             mPreferenceGroup.
165                     setTitle(mContext.getString(R.string.connected_device_available_call_title));
166         } else {
167             // without phone call
168             mPreferenceGroup.
169                     setTitle(mContext.getString(R.string.connected_device_available_media_title));
170         }
171     }
172 }
173