• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 
17 package com.android.settings.sound;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.media.AudioManager;
23 import android.text.TextUtils;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settingslib.Utils;
29 import com.android.settingslib.bluetooth.A2dpProfile;
30 import com.android.settingslib.bluetooth.HearingAidProfile;
31 import com.android.settingslib.media.MediaOutputSliceConstants;
32 
33 import java.util.List;
34 
35 /**
36  * This class allows launching MediaOutputSlice to switch output device.
37  * Preference would hide only when
38  * - Bluetooth = OFF
39  * - Bluetooth = ON and Connected Devices = 0 and Previously Connected = 0
40  * - Media stream captured by remote device
41  * - During a call.
42  */
43 public class MediaOutputPreferenceController extends AudioSwitchPreferenceController {
44 
MediaOutputPreferenceController(Context context, String key)45     public MediaOutputPreferenceController(Context context, String key) {
46         super(context, key);
47     }
48 
49     @Override
updateState(Preference preference)50     public void updateState(Preference preference) {
51         if (preference == null) {
52             // In case UI is not ready.
53             return;
54         }
55 
56         if (Utils.isAudioModeOngoingCall(mContext)) {
57             // Ongoing call status, switch entry for media will be disabled.
58             mPreference.setVisible(false);
59             preference.setSummary(
60                     mContext.getText(R.string.media_out_summary_ongoing_call_state));
61             return;
62         }
63 
64         boolean deviceConnected = false;
65         BluetoothDevice activeDevice = null;
66         // Show preference if there is connected or previously connected device
67         // Find active device and set its name as the preference's summary
68         List<BluetoothDevice> connectedA2dpDevices = getConnectedA2dpDevices();
69         List<BluetoothDevice> connectedHADevices = getConnectedHearingAidDevices();
70         if (mAudioManager.getMode() == AudioManager.MODE_NORMAL
71                 && ((connectedA2dpDevices != null && !connectedA2dpDevices.isEmpty())
72                 || (connectedHADevices != null && !connectedHADevices.isEmpty()))) {
73             deviceConnected = true;
74             activeDevice = findActiveDevice();
75         }
76         mPreference.setVisible(deviceConnected);
77         mPreference.setSummary((activeDevice == null) ?
78                 mContext.getText(R.string.media_output_default_summary) :
79                 activeDevice.getAliasName());
80     }
81 
82     @Override
findActiveDevice()83     public BluetoothDevice findActiveDevice() {
84         BluetoothDevice activeDevice = findActiveHearingAidDevice();
85         final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
86 
87         if (activeDevice == null && a2dpProfile != null) {
88             activeDevice = a2dpProfile.getActiveDevice();
89         }
90         return activeDevice;
91     }
92 
93     /**
94      * Find active hearing aid device
95      */
96     @Override
findActiveHearingAidDevice()97     protected BluetoothDevice findActiveHearingAidDevice() {
98         final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
99 
100         if (hearingAidProfile != null) {
101             List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
102             for (BluetoothDevice btDevice : activeDevices) {
103                 if (btDevice != null) {
104                     return btDevice;
105                 }
106             }
107         }
108         return null;
109     }
110 
111     @Override
handlePreferenceTreeClick(Preference preference)112     public boolean handlePreferenceTreeClick(Preference preference) {
113         if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
114             final Intent intent = new Intent()
115                     .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
116                     .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
117             mContext.startActivity(intent);
118             return true;
119         }
120         return false;
121     }
122 }
123