• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 android.media.cts;
18 
19 import android.media.MediaSession2;
20 import android.media.MediaSession2.ControllerInfo;
21 import android.media.MediaSession2Service;
22 import android.media.Session2CommandGroup;
23 import android.os.Process;
24 
25 import com.android.internal.annotations.GuardedBy;
26 
27 import java.util.List;
28 
29 /**
30  * Stub implementation of {@link MediaSession2Service} for testing.
31  */
32 public class StubMediaSession2Service extends MediaSession2Service {
33     /**
34      * ID of the session that this service will create.
35      */
36     private static final String ID = "StubMediaSession2Service";
37 
38     @GuardedBy("StubMediaSession2Service.class")
39     private static HandlerExecutor sHandlerExecutor;
40     @GuardedBy("StubMediaSession2Service.class")
41     private static StubMediaSession2Service sInstance;
42     @GuardedBy("StubMediaSession2Service.class")
43     private static TestInjector sTestInjector;
44 
setHandlerExecutor(HandlerExecutor handlerExecutor)45     public static void setHandlerExecutor(HandlerExecutor handlerExecutor) {
46         synchronized (StubMediaSession2Service.class) {
47             sHandlerExecutor = handlerExecutor;
48         }
49     }
50 
getInstance()51     public static StubMediaSession2Service getInstance() {
52         synchronized (StubMediaSession2Service.class) {
53             return sInstance;
54         }
55     }
56 
setTestInjector(TestInjector injector)57     public static void setTestInjector(TestInjector injector) {
58         synchronized (StubMediaSession2Service.class) {
59             sTestInjector = injector;
60         }
61     }
62 
63     @Override
onCreate()64     public void onCreate() {
65         super.onCreate();
66         synchronized (StubMediaSession2Service.class) {
67             sInstance = this;
68         }
69     }
70 
71     @Override
onDestroy()72     public void onDestroy() {
73         List<MediaSession2> sessions = getSessions();
74         for (MediaSession2 session : sessions) {
75             session.close();
76         }
77         synchronized (StubMediaSession2Service.class) {
78             if (sTestInjector != null) {
79                 sTestInjector.onServiceDestroyed();
80             }
81             sInstance = null;
82         }
83         super.onDestroy();
84     }
85 
86     @Override
onUpdateNotification(MediaSession2 session)87     public MediaNotification onUpdateNotification(MediaSession2 session) {
88         synchronized (StubMediaSession2Service.class) {
89             if (sTestInjector != null) {
90                 sTestInjector.onUpdateNotification(session);
91             }
92             sInstance = null;
93         }
94         return null;
95     }
96 
97     @Override
onGetSession(ControllerInfo controllerInfo)98     public MediaSession2 onGetSession(ControllerInfo controllerInfo) {
99         synchronized (StubMediaSession2Service.class) {
100             if (sTestInjector != null) {
101                 return sTestInjector.onGetSession(controllerInfo);
102             }
103         }
104         if (getSessions().size() > 0) {
105             return getSessions().get(0);
106         }
107         return new MediaSession2.Builder(this)
108                 .setId(ID)
109                 .setSessionCallback(sHandlerExecutor, new MediaSession2.SessionCallback() {
110                     @Override
111                     public Session2CommandGroup onConnect(MediaSession2 session,
112                             ControllerInfo controller) {
113                         if (controller.getUid() == Process.myUid()) {
114                             return new Session2CommandGroup.Builder().build();
115                         }
116                         return null;
117                     }
118                 }).build();
119     }
120 
121     public static abstract class TestInjector {
122         MediaSession2 onGetSession(ControllerInfo controllerInfo) {
123             return null;
124         }
125 
126         MediaNotification onUpdateNotification(MediaSession2 session) {
127             return null;
128         }
129 
130         void onServiceDestroyed() {
131             // no-op
132         }
133     }
134 }