• 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 package com.android.settings.media;
17 
18 import android.media.session.MediaController;
19 import android.media.session.MediaSessionManager;
20 import android.media.session.PlaybackState;
21 import android.text.TextUtils;
22 import android.util.Log;
23 
24 import androidx.annotation.Nullable;
25 
26 import com.android.settings.sound.MediaOutputPreferenceController;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Utilities that can be shared between {@link MediaOutputIndicatorWorker} and
33  * {@link MediaOutputPreferenceController}.
34  */
35 public class MediaOutputUtils {
36 
37     private static final String TAG = "MediaOutputUtils";
38     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
39 
40     /**
41      *  Returns a {@link MediaController} that state is playing and type is local playback,
42      *  and also have active sessions.
43      */
44     @Nullable
getActiveLocalMediaController( MediaSessionManager mediaSessionManager)45     public static MediaController getActiveLocalMediaController(
46             MediaSessionManager mediaSessionManager) {
47 
48         MediaController localController = null;
49         final List<String> remoteMediaSessionLists = new ArrayList<>();
50         for (MediaController controller : mediaSessionManager.getActiveSessions(null)) {
51             final MediaController.PlaybackInfo pi = controller.getPlaybackInfo();
52             if (pi == null) {
53                 // do nothing
54                 continue;
55             }
56             final PlaybackState playbackState = controller.getPlaybackState();
57             if (playbackState == null) {
58                 // do nothing
59                 continue;
60             }
61             if (DEBUG) {
62                 Log.d(TAG, "getActiveLocalMediaController() package name : "
63                         + controller.getPackageName()
64                         + ", play back type : " + pi.getPlaybackType() + ", play back state : "
65                         + playbackState.getState());
66             }
67             if (playbackState.getState() == PlaybackState.STATE_STOPPED
68                     || playbackState.getState() == PlaybackState.STATE_NONE
69                     || playbackState.getState() == PlaybackState.STATE_ERROR) {
70                 // do nothing
71                 continue;
72             }
73             if (pi.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE) {
74                 if (localController != null && TextUtils.equals(localController.getPackageName(),
75                         controller.getPackageName())) {
76                     localController = null;
77                 }
78                 if (!remoteMediaSessionLists.contains(controller.getPackageName())) {
79                     remoteMediaSessionLists.add(controller.getPackageName());
80                 }
81                 continue;
82             }
83             if (pi.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL) {
84                 if (localController == null
85                         && !remoteMediaSessionLists.contains(controller.getPackageName())) {
86                     localController = controller;
87                 }
88             }
89         }
90         return localController;
91     }
92 }
93