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 
17 package android.media;
18 
19 import android.annotation.NonNull;
20 import android.app.SystemServiceRegistry;
21 import android.content.Context;
22 import android.media.session.MediaSessionManager;
23 
24 import com.android.internal.util.Preconditions;
25 
26 import java.util.Objects;
27 
28 /**
29  * Class for performing registration for all media services
30  *
31  * TODO (b/160513103): This class is still needed on platform side until
32  * MEDIA_SESSION_SERVICE is moved onto com.android.media apex.
33  * Once that's done, we can move the code that registers the service onto the
34  * MediaFrameworkInitializer class on the apex.
35  *
36  * @hide
37  */
38 public class MediaFrameworkPlatformInitializer {
MediaFrameworkPlatformInitializer()39     private MediaFrameworkPlatformInitializer() {
40     }
41 
42     private static volatile MediaServiceManager sMediaServiceManager;
43 
44     /**
45      * Sets an instance of {@link MediaServiceManager} that allows
46      * the media mainline module to register/obtain media binder services. This is called
47      * by the platform during the system initialization.
48      *
49      * @param mediaServiceManager instance of {@link MediaServiceManager} that allows
50      * the media mainline module to register/obtain media binder services.
51      */
setMediaServiceManager( @onNull MediaServiceManager mediaServiceManager)52     public static void setMediaServiceManager(
53             @NonNull MediaServiceManager mediaServiceManager) {
54         Preconditions.checkState(sMediaServiceManager == null,
55                 "setMediaServiceManager called twice!");
56         sMediaServiceManager = Objects.requireNonNull(mediaServiceManager);
57     }
58 
59     /** @hide */
getMediaServiceManager()60     public static MediaServiceManager getMediaServiceManager() {
61         return sMediaServiceManager;
62     }
63 
64     /**
65      * Called by {@link SystemServiceRegistry}'s static initializer and registers all media
66      * services to {@link Context}, so that {@link Context#getSystemService} can return them.
67      *
68      * @throws IllegalStateException if this is called from anywhere besides
69      * {@link SystemServiceRegistry}
70      */
registerServiceWrappers()71     public static void registerServiceWrappers() {
72         SystemServiceRegistry.registerContextAwareService(
73                 Context.MEDIA_SESSION_SERVICE,
74                 MediaSessionManager.class,
75                 context -> new MediaSessionManager(context)
76         );
77     }
78 }
79