1 /* 2 * Copyright (C) 2020 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.notification; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.media.MediaRouter2Manager; 22 import android.media.RoutingSessionInfo; 23 import android.text.TextUtils; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceCategory; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.Utils; 32 import com.android.settings.core.BasePreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnDestroy; 35 import com.android.settingslib.media.LocalMediaManager; 36 import com.android.settingslib.media.MediaDevice; 37 import com.android.settingslib.media.MediaOutputConstants; 38 import com.android.settingslib.utils.ThreadUtils; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 /** 44 * A group preference controller to add/remove/update preference 45 * {@link com.android.settings.notification.RemoteVolumeSeekBarPreference} 46 **/ 47 public class RemoteVolumeGroupController extends BasePreferenceController implements 48 Preference.OnPreferenceChangeListener, LifecycleObserver, OnDestroy, 49 LocalMediaManager.DeviceCallback { 50 51 private static final String KEY_REMOTE_VOLUME_GROUP = "remote_media_group"; 52 private static final String TAG = "RemoteVolumePrefCtr"; 53 @VisibleForTesting 54 static final String SWITCHER_PREFIX = "OUTPUT_SWITCHER"; 55 56 private PreferenceCategory mPreferenceCategory; 57 private List<RoutingSessionInfo> mRoutingSessionInfos = new ArrayList<>(); 58 59 @VisibleForTesting 60 LocalMediaManager mLocalMediaManager; 61 @VisibleForTesting 62 MediaRouter2Manager mRouterManager; 63 RemoteVolumeGroupController(Context context, String preferenceKey)64 public RemoteVolumeGroupController(Context context, String preferenceKey) { 65 super(context, preferenceKey); 66 if (mLocalMediaManager == null) { 67 mLocalMediaManager = new LocalMediaManager(mContext, null, null); 68 mLocalMediaManager.registerCallback(this); 69 mLocalMediaManager.startScan(); 70 } 71 mRouterManager = MediaRouter2Manager.getInstance(context); 72 } 73 74 @Override getAvailabilityStatus()75 public int getAvailabilityStatus() { 76 if (mRoutingSessionInfos.isEmpty()) { 77 return CONDITIONALLY_UNAVAILABLE; 78 } 79 return AVAILABLE_UNSEARCHABLE; 80 } 81 82 @Override displayPreference(PreferenceScreen screen)83 public void displayPreference(PreferenceScreen screen) { 84 super.displayPreference(screen); 85 mPreferenceCategory = screen.findPreference(getPreferenceKey()); 86 initRemoteMediaSession(); 87 refreshPreference(); 88 } 89 initRemoteMediaSession()90 private void initRemoteMediaSession() { 91 mRoutingSessionInfos.clear(); 92 for (RoutingSessionInfo info : mLocalMediaManager.getActiveMediaSession()) { 93 if (!info.isSystemSession()) { 94 mRoutingSessionInfos.add(info); 95 } 96 } 97 } 98 99 @Override onDestroy()100 public void onDestroy() { 101 mLocalMediaManager.unregisterCallback(this); 102 mLocalMediaManager.stopScan(); 103 } 104 refreshPreference()105 private synchronized void refreshPreference() { 106 if (!isAvailable()) { 107 mPreferenceCategory.setVisible(false); 108 return; 109 } 110 final CharSequence castVolume = mContext.getText(R.string.remote_media_volume_option_title); 111 mPreferenceCategory.setVisible(true); 112 for (RoutingSessionInfo info : mRoutingSessionInfos) { 113 final CharSequence appName = Utils.getApplicationLabel(mContext, 114 info.getClientPackageName()); 115 RemoteVolumeSeekBarPreference seekBarPreference = mPreferenceCategory.findPreference( 116 info.getId()); 117 if (seekBarPreference != null) { 118 // Update slider 119 if (seekBarPreference.getProgress() != info.getVolume()) { 120 seekBarPreference.setProgress(info.getVolume()); 121 } 122 } else { 123 // Add slider 124 seekBarPreference = new RemoteVolumeSeekBarPreference(mContext); 125 seekBarPreference.setKey(info.getId()); 126 seekBarPreference.setTitle(castVolume); 127 seekBarPreference.setMax(info.getVolumeMax()); 128 seekBarPreference.setProgress(info.getVolume()); 129 seekBarPreference.setMin(0); 130 seekBarPreference.setOnPreferenceChangeListener(this); 131 seekBarPreference.setIcon(R.drawable.ic_volume_remote); 132 seekBarPreference.setEnabled(mLocalMediaManager.shouldEnableVolumeSeekBar(info)); 133 mPreferenceCategory.addPreference(seekBarPreference); 134 } 135 136 Preference switcherPreference = mPreferenceCategory.findPreference( 137 SWITCHER_PREFIX + info.getId()); 138 final boolean isMediaOutputDisabled = mLocalMediaManager.shouldDisableMediaOutput( 139 info.getClientPackageName()); 140 final CharSequence outputTitle = mContext.getString(R.string.media_output_label_title, 141 appName); 142 if (switcherPreference != null) { 143 // Update output indicator 144 switcherPreference.setTitle(isMediaOutputDisabled ? appName : outputTitle); 145 switcherPreference.setSummary(info.getName()); 146 switcherPreference.setEnabled(!isMediaOutputDisabled); 147 } else { 148 // Add output indicator 149 switcherPreference = new Preference(mContext); 150 switcherPreference.setKey(SWITCHER_PREFIX + info.getId()); 151 switcherPreference.setTitle(isMediaOutputDisabled ? appName : outputTitle); 152 switcherPreference.setSummary(info.getName()); 153 switcherPreference.setEnabled(!isMediaOutputDisabled); 154 mPreferenceCategory.addPreference(switcherPreference); 155 } 156 } 157 158 // Check and remove non-active session preference 159 // There is a pair of preferences for each session. First one is a seekBar preference. 160 // The second one shows the session information and provide an entry-point to launch output 161 // switcher. It is unnecessary to go through all preferences. It is fine ignore the second 162 // preference and only to check the seekBar's key value. 163 for (int i = 0; i < mPreferenceCategory.getPreferenceCount(); i = i + 2) { 164 final Preference preference = mPreferenceCategory.getPreference(i); 165 boolean isActive = false; 166 for (RoutingSessionInfo info : mRoutingSessionInfos) { 167 if (TextUtils.equals(preference.getKey(), info.getId())) { 168 isActive = true; 169 break; 170 } 171 } 172 if (isActive) { 173 continue; 174 } 175 final Preference switcherPreference = mPreferenceCategory.getPreference(i + 1); 176 if (switcherPreference != null) { 177 mPreferenceCategory.removePreference(preference); 178 mPreferenceCategory.removePreference(switcherPreference); 179 } 180 } 181 } 182 183 @Override onPreferenceChange(Preference preference, Object newValue)184 public boolean onPreferenceChange(Preference preference, Object newValue) { 185 ThreadUtils.postOnBackgroundThread(() -> { 186 mLocalMediaManager.adjustSessionVolume(preference.getKey(), (int) newValue); 187 }); 188 return true; 189 } 190 191 @Override handlePreferenceTreeClick(Preference preference)192 public boolean handlePreferenceTreeClick(Preference preference) { 193 if (!preference.getKey().startsWith(SWITCHER_PREFIX)) { 194 return false; 195 } 196 for (RoutingSessionInfo info : mRoutingSessionInfos) { 197 if (TextUtils.equals(info.getId(), 198 preference.getKey().substring(SWITCHER_PREFIX.length()))) { 199 final Intent intent = new Intent() 200 .setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG) 201 .setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME) 202 .putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME, 203 info.getClientPackageName()); 204 mContext.sendBroadcast(intent); 205 return true; 206 } 207 } 208 return false; 209 } 210 211 @Override getPreferenceKey()212 public String getPreferenceKey() { 213 return KEY_REMOTE_VOLUME_GROUP; 214 } 215 216 @Override onDeviceListUpdate(List<MediaDevice> devices)217 public void onDeviceListUpdate(List<MediaDevice> devices) { 218 if (mPreferenceCategory == null) { 219 // Preference group is not ready. 220 return; 221 } 222 ThreadUtils.postOnMainThread(() -> { 223 initRemoteMediaSession(); 224 refreshPreference(); 225 }); 226 } 227 228 @Override onSelectedDeviceStateChanged(MediaDevice device, int state)229 public void onSelectedDeviceStateChanged(MediaDevice device, int state) { 230 } 231 } 232