• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 android.media;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.annotation.SystemApi;
21 import android.annotation.SystemApi.Client;
22 import android.os.IBinder;
23 import android.os.ServiceManager;
24 
25 /**
26  * Provides a way to register and obtain the system service binder objects managed by the media
27  * service.
28  *
29  * <p> Only the media mainline module will be able to access an instance of this class.
30  * @hide
31  */
32 @SystemApi(client = Client.MODULE_LIBRARIES)
33 public class MediaServiceManager {
34     private static final String MEDIA_SESSION_SERVICE = "media_session";
35     private static final String MEDIA_TRANSCODING_SERVICE = "media.transcoding";
36     private static final String MEDIA_COMMUNICATION_SERVICE = "media_communication";
37 
38     /**
39      * @hide
40      */
MediaServiceManager()41     public MediaServiceManager() {}
42 
43     /**
44      * A class that exposes the methods to register and obtain each system service.
45      */
46     public static final class ServiceRegisterer {
47         private final String mServiceName;
48         private final boolean mLazyStart;
49 
50         /**
51          * @hide
52          */
ServiceRegisterer(String serviceName, boolean lazyStart)53         public ServiceRegisterer(String serviceName, boolean lazyStart) {
54             mServiceName = serviceName;
55             mLazyStart = lazyStart;
56         }
57 
58         /**
59          * @hide
60          */
ServiceRegisterer(String serviceName)61         public ServiceRegisterer(String serviceName) {
62             this(serviceName, false /*lazyStart*/);
63         }
64 
65         /**
66          * Get the system server binding object for MediaServiceManager.
67          *
68          * <p> This blocks until the service instance is ready.
69          * or a timeout happens, in which case it returns null.
70          */
71         @Nullable
get()72         public IBinder get() {
73             if (mLazyStart) {
74                 return ServiceManager.waitForService(mServiceName);
75             }
76             return ServiceManager.getService(mServiceName);
77         }
78     }
79 
80     /**
81      * Returns {@link ServiceRegisterer} for MEDIA_SESSION_SERVICE.
82      */
83     @NonNull
getMediaSessionServiceRegisterer()84     public ServiceRegisterer getMediaSessionServiceRegisterer() {
85         return new ServiceRegisterer(MEDIA_SESSION_SERVICE);
86     }
87 
88     /**
89      * Returns {@link ServiceRegisterer} for MEDIA_TRANSCODING_SERVICE.
90      */
91     @NonNull
getMediaTranscodingServiceRegisterer()92     public ServiceRegisterer getMediaTranscodingServiceRegisterer() {
93         return new ServiceRegisterer(MEDIA_TRANSCODING_SERVICE, true /*lazyStart*/);
94     }
95 
96     /**
97      * Returns {@link ServiceRegisterer} for MEDIA_COMMUNICATION_SERVICE.
98      */
99     @NonNull
getMediaCommunicationServiceRegisterer()100     public ServiceRegisterer getMediaCommunicationServiceRegisterer() {
101         return new ServiceRegisterer(MEDIA_COMMUNICATION_SERVICE);
102     }
103 }
104