• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 seekBarPreference.setEnabled(mLocalMediaManager.shouldEnableVolumeSeekBar(info));
123             } else {
124                 // Add slider
125                 seekBarPreference = new RemoteVolumeSeekBarPreference(mContext);
126                 seekBarPreference.setKey(info.getId());
127                 seekBarPreference.setTitle(castVolume);
128                 seekBarPreference.setMax(info.getVolumeMax());
129                 seekBarPreference.setProgress(info.getVolume());
130                 seekBarPreference.setMin(0);
131                 seekBarPreference.setOnPreferenceChangeListener(this);
132                 seekBarPreference.setIcon(R.drawable.ic_volume_remote);
133                 seekBarPreference.setEnabled(mLocalMediaManager.shouldEnableVolumeSeekBar(info));
134                 mPreferenceCategory.addPreference(seekBarPreference);
135             }
136 
137             Preference switcherPreference = mPreferenceCategory.findPreference(
138                     SWITCHER_PREFIX + info.getId());
139             final boolean isMediaOutputDisabled = mLocalMediaManager.shouldDisableMediaOutput(
140                     info.getClientPackageName());
141             final CharSequence outputTitle = mContext.getString(R.string.media_output_label_title,
142                     appName);
143             if (switcherPreference != null) {
144                 // Update output indicator
145                 switcherPreference.setTitle(isMediaOutputDisabled ? appName : outputTitle);
146                 switcherPreference.setSummary(info.getName());
147                 switcherPreference.setEnabled(!isMediaOutputDisabled);
148             } else {
149                 // Add output indicator
150                 switcherPreference = new Preference(mContext);
151                 switcherPreference.setKey(SWITCHER_PREFIX + info.getId());
152                 switcherPreference.setTitle(isMediaOutputDisabled ? appName : outputTitle);
153                 switcherPreference.setSummary(info.getName());
154                 switcherPreference.setEnabled(!isMediaOutputDisabled);
155                 mPreferenceCategory.addPreference(switcherPreference);
156             }
157         }
158 
159         // Check and remove non-active session preference
160         // There is a pair of preferences for each session. First one is a seekBar preference.
161         // The second one shows the session information and provide an entry-point to launch output
162         // switcher. It is unnecessary to go through all preferences. It is fine ignore the second
163         // preference and only to check the seekBar's key value.
164         for (int i = 0; i < mPreferenceCategory.getPreferenceCount(); i = i + 2) {
165             final Preference preference = mPreferenceCategory.getPreference(i);
166             boolean isActive = false;
167             for (RoutingSessionInfo info : mRoutingSessionInfos) {
168                 if (TextUtils.equals(preference.getKey(), info.getId())) {
169                     isActive = true;
170                     break;
171                 }
172             }
173             if (isActive) {
174                 continue;
175             }
176             final Preference switcherPreference = mPreferenceCategory.getPreference(i + 1);
177             if (switcherPreference != null) {
178                 mPreferenceCategory.removePreference(preference);
179                 mPreferenceCategory.removePreference(switcherPreference);
180             }
181         }
182     }
183 
184     @Override
onPreferenceChange(Preference preference, Object newValue)185     public boolean onPreferenceChange(Preference preference, Object newValue) {
186         ThreadUtils.postOnBackgroundThread(() -> {
187             mLocalMediaManager.adjustSessionVolume(preference.getKey(), (int) newValue);
188         });
189         return true;
190     }
191 
192     @Override
handlePreferenceTreeClick(Preference preference)193     public boolean handlePreferenceTreeClick(Preference preference) {
194         if (!preference.getKey().startsWith(SWITCHER_PREFIX)) {
195             return false;
196         }
197         for (RoutingSessionInfo info : mRoutingSessionInfos) {
198             if (TextUtils.equals(info.getId(),
199                     preference.getKey().substring(SWITCHER_PREFIX.length()))) {
200                 final Intent intent = new Intent()
201                         .setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG)
202                         .setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME)
203                         .putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME,
204                                 info.getClientPackageName());
205                 mContext.sendBroadcast(intent);
206                 return true;
207             }
208         }
209         return false;
210     }
211 
212     @Override
getPreferenceKey()213     public String getPreferenceKey() {
214         return KEY_REMOTE_VOLUME_GROUP;
215     }
216 
217     @Override
onDeviceListUpdate(List<MediaDevice> devices)218     public void onDeviceListUpdate(List<MediaDevice> devices) {
219         if (mPreferenceCategory == null) {
220             // Preference group is not ready.
221             return;
222         }
223         ThreadUtils.postOnMainThread(() -> {
224             initRemoteMediaSession();
225             refreshPreference();
226         });
227     }
228 
229     @Override
onSelectedDeviceStateChanged(MediaDevice device, int state)230     public void onSelectedDeviceStateChanged(MediaDevice device, int state) {
231     }
232 }
233