ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 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 | |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 19 | import android.bluetooth.BluetoothDevice; |
| 20 | import android.content.Context; |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 21 | import android.content.Intent; |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 22 | import android.media.AudioManager; |
Tim Peng | f392eae | 2020-05-06 13:52:24 +0800 | [diff] [blame] | 23 | import android.media.session.MediaController; |
| 24 | import android.media.session.MediaSessionManager; |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 26 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 27 | import androidx.preference.Preference; |
Tim Peng | 9d3092c | 2020-03-27 20:24:06 +0800 | [diff] [blame] | 28 | import androidx.preference.PreferenceScreen; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 29 | |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 30 | import com.android.settings.R; |
Hugh Chen | e16a807 | 2020-10-07 15:55:51 +0800 | [diff] [blame] | 31 | import com.android.settings.media.MediaOutputUtils; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 32 | import com.android.settingslib.Utils; |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 33 | import com.android.settingslib.bluetooth.A2dpProfile; |
ryanywlin | 8276d96 | 2018-04-28 07:26:46 +0800 | [diff] [blame] | 34 | import com.android.settingslib.bluetooth.HearingAidProfile; |
timhypeng | a7a73c4 | 2020-12-22 21:15:49 +0800 | [diff] [blame] | 35 | import com.android.settingslib.media.MediaOutputConstants; |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 36 | |
| 37 | import java.util.List; |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 38 | |
| 39 | /** |
timhypeng | deb9306 | 2020-12-15 14:25:22 +0800 | [diff] [blame] | 40 | * This class allows launching MediaOutputDialog to switch output device. |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 41 | * Preference would hide only when |
| 42 | * - Bluetooth = OFF |
| 43 | * - Bluetooth = ON and Connected Devices = 0 and Previously Connected = 0 |
| 44 | * - Media stream captured by remote device |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 45 | * - During a call. |
| 46 | */ |
timhypeng | eee759e | 2020-10-06 12:12:53 +0800 | [diff] [blame] | 47 | public class MediaOutputPreferenceController extends AudioSwitchPreferenceController { |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 48 | |
Tim Peng | f392eae | 2020-05-06 13:52:24 +0800 | [diff] [blame] | 49 | private MediaController mMediaController; |
| 50 | |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 51 | public MediaOutputPreferenceController(Context context, String key) { |
| 52 | super(context, key); |
Hugh Chen | e16a807 | 2020-10-07 15:55:51 +0800 | [diff] [blame] | 53 | mMediaController = MediaOutputUtils.getActiveLocalMediaController(context.getSystemService( |
| 54 | MediaSessionManager.class)); |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
Tim Peng | 9d3092c | 2020-03-27 20:24:06 +0800 | [diff] [blame] | 58 | public void displayPreference(PreferenceScreen screen) { |
| 59 | super.displayPreference(screen); |
| 60 | |
Tim Peng | f392eae | 2020-05-06 13:52:24 +0800 | [diff] [blame] | 61 | if (!Utils.isAudioModeOngoingCall(mContext) && mMediaController != null) { |
Tim Peng | 9d3092c | 2020-03-27 20:24:06 +0800 | [diff] [blame] | 62 | mPreference.setVisible(true); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @Override |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 67 | public void updateState(Preference preference) { |
| 68 | if (preference == null) { |
| 69 | // In case UI is not ready. |
| 70 | return; |
| 71 | } |
| 72 | |
Tim Peng | f392eae | 2020-05-06 13:52:24 +0800 | [diff] [blame] | 73 | if (mMediaController == null) { |
| 74 | // No active local playback |
| 75 | return; |
| 76 | } |
| 77 | |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 78 | if (Utils.isAudioModeOngoingCall(mContext)) { |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 79 | // Ongoing call status, switch entry for media will be disabled. |
caxtonchan | 458fb23 | 2018-04-23 15:04:21 +0800 | [diff] [blame] | 80 | mPreference.setVisible(false); |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 81 | preference.setSummary( |
| 82 | mContext.getText(R.string.media_out_summary_ongoing_call_state)); |
| 83 | return; |
| 84 | } |
| 85 | |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 86 | BluetoothDevice activeDevice = null; |
| 87 | // Show preference if there is connected or previously connected device |
| 88 | // Find active device and set its name as the preference's summary |
Tim Peng | 7b0f887 | 2019-04-24 10:47:31 +0800 | [diff] [blame] | 89 | List<BluetoothDevice> connectedA2dpDevices = getConnectedA2dpDevices(); |
| 90 | List<BluetoothDevice> connectedHADevices = getConnectedHearingAidDevices(); |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 91 | if (mAudioManager.getMode() == AudioManager.MODE_NORMAL |
Tim Peng | 7b0f887 | 2019-04-24 10:47:31 +0800 | [diff] [blame] | 92 | && ((connectedA2dpDevices != null && !connectedA2dpDevices.isEmpty()) |
| 93 | || (connectedHADevices != null && !connectedHADevices.isEmpty()))) { |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 94 | activeDevice = findActiveDevice(); |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 95 | } |
Tim Peng | f392eae | 2020-05-06 13:52:24 +0800 | [diff] [blame] | 96 | mPreference.setTitle(mContext.getString(R.string.media_output_label_title, |
| 97 | com.android.settings.Utils.getApplicationLabel(mContext, |
| 98 | mMediaController.getPackageName()))); |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 99 | mPreference.setSummary((activeDevice == null) ? |
| 100 | mContext.getText(R.string.media_output_default_summary) : |
Rahul Sabnis | c96469f | 2019-10-22 15:34:40 -0700 | [diff] [blame] | 101 | activeDevice.getAlias()); |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 102 | } |
hughchen | 244c758 | 2018-07-05 14:59:57 +0800 | [diff] [blame] | 103 | |
| 104 | @Override |
| 105 | public BluetoothDevice findActiveDevice() { |
| 106 | BluetoothDevice activeDevice = findActiveHearingAidDevice(); |
| 107 | final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile(); |
| 108 | |
| 109 | if (activeDevice == null && a2dpProfile != null) { |
| 110 | activeDevice = a2dpProfile.getActiveDevice(); |
| 111 | } |
| 112 | return activeDevice; |
| 113 | } |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Find active hearing aid device |
| 117 | */ |
| 118 | @Override |
| 119 | protected BluetoothDevice findActiveHearingAidDevice() { |
| 120 | final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile(); |
| 121 | |
| 122 | if (hearingAidProfile != null) { |
| 123 | List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices(); |
| 124 | for (BluetoothDevice btDevice : activeDevices) { |
| 125 | if (btDevice != null) { |
| 126 | return btDevice; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean handlePreferenceTreeClick(Preference preference) { |
| 135 | if (TextUtils.equals(preference.getKey(), getPreferenceKey())) { |
timhypeng | 3084d06 | 2020-09-26 15:43:11 +0800 | [diff] [blame] | 136 | mContext.sendBroadcast(new Intent() |
timhypeng | a7a73c4 | 2020-12-22 21:15:49 +0800 | [diff] [blame] | 137 | .setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG) |
| 138 | .setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME) |
| 139 | .putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME, |
timhypeng | 3084d06 | 2020-09-26 15:43:11 +0800 | [diff] [blame] | 140 | mMediaController.getPackageName()) |
timhypeng | a7a73c4 | 2020-12-22 21:15:49 +0800 | [diff] [blame] | 141 | .putExtra(MediaOutputConstants.KEY_MEDIA_SESSION_TOKEN, |
timhypeng | 3084d06 | 2020-09-26 15:43:11 +0800 | [diff] [blame] | 142 | mMediaController.getSessionToken())); |
timhypeng | abdf739 | 2019-03-04 16:32:47 +0800 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | return false; |
| 146 | } |
ryanywlin | 63bdfa8 | 2018-03-23 16:57:51 +0800 | [diff] [blame] | 147 | } |