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