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.annotation.SystemApi;
21 import android.annotation.SystemApi.Client;
22 import android.app.SystemServiceRegistry;
23 import android.content.Context;
24 import android.os.Build;
25 
26 import com.android.modules.annotation.MinSdk;
27 import com.android.modules.utils.build.SdkLevel;
28 
29 /**
30  * Class for performing registration for all media services on com.android.media apex.
31  *
32  * @hide
33  */
34 @MinSdk(Build.VERSION_CODES.S)
35 @SystemApi(client = Client.MODULE_LIBRARIES)
36 public class MediaFrameworkInitializer {
MediaFrameworkInitializer()37     private MediaFrameworkInitializer() {
38     }
39 
40     private static volatile MediaServiceManager sMediaServiceManager;
41 
42     /**
43      * Sets an instance of {@link MediaServiceManager} that allows
44      * the media mainline module to register/obtain media binder services. This is called
45      * by the platform during the system initialization.
46      *
47      * @param mediaServiceManager instance of {@link MediaServiceManager} that allows
48      * the media mainline module to register/obtain media binder services.
49      */
setMediaServiceManager( @onNull MediaServiceManager mediaServiceManager)50     public static void setMediaServiceManager(
51             @NonNull MediaServiceManager mediaServiceManager) {
52         if (sMediaServiceManager != null) {
53             throw new IllegalStateException("setMediaServiceManager called twice!");
54         }
55 
56         if (mediaServiceManager == null) {
57             throw new NullPointerException("mediaServiceManager is null!");
58         }
59 
60         sMediaServiceManager = mediaServiceManager;
61     }
62 
63     /** @hide */
getMediaServiceManager()64     public static MediaServiceManager getMediaServiceManager() {
65         return sMediaServiceManager;
66     }
67 
68     /**
69      * Called by {@link SystemServiceRegistry}'s static initializer and registers all media
70      * services to {@link Context}, so that {@link Context#getSystemService} can return them.
71      *
72      * @throws IllegalStateException if this is called from anywhere besides
73      * {@link SystemServiceRegistry}
74      */
registerServiceWrappers()75     public static void registerServiceWrappers() {
76         SystemServiceRegistry.registerContextAwareService(
77                 Context.MEDIA_TRANSCODING_SERVICE,
78                 MediaTranscodingManager.class,
79                 context -> new MediaTranscodingManager(context)
80         );
81         if (SdkLevel.isAtLeastS()) {
82             SystemServiceRegistry.registerContextAwareService(
83                     Context.MEDIA_COMMUNICATION_SERVICE,
84                     MediaCommunicationManager.class,
85                     context -> new MediaCommunicationManager(context)
86             );
87         }
88     }
89 }
90