1 /*
2  * Copyright (C) 2024 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.os;
17 
18 import android.annotation.FlaggedApi;
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.profiling.Flags;
25 
26 /**
27  * Class for performing registration for profiling service.
28  *
29  * @hide
30  */
31 @FlaggedApi(Flags.FLAG_TELEMETRY_APIS)
32 @SystemApi(client = Client.MODULE_LIBRARIES)
33 public class ProfilingFrameworkInitializer {
34 
ProfilingFrameworkInitializer()35     private ProfilingFrameworkInitializer() {}
36 
37     private static volatile ProfilingServiceManager sProfilingServiceManager;
38 
39     /**
40      * Sets an instance of {@link ProfilingServiceManager} that allows the profiling module to
41      * register/obtain profiling binder services. This is called by the platform during the system
42      * initialization.
43      *
44      * @param profilingServiceManager instance of {@link ProfilingServiceManager} that allows the
45      * profiling module to register/obtain profiling binder services.
46      */
setProfilingServiceManager( @onNull ProfilingServiceManager profilingServiceManager)47     public static void setProfilingServiceManager(
48             @NonNull ProfilingServiceManager profilingServiceManager) {
49         if (sProfilingServiceManager != null) {
50             throw new IllegalStateException("setProfilingServiceManager called twice!");
51         }
52 
53         if (profilingServiceManager == null) {
54             throw new NullPointerException("profilingServiceManager is null");
55         }
56 
57         sProfilingServiceManager = profilingServiceManager;
58     }
59 
60     /** @hide */
getProfilingServiceManager()61     public static ProfilingServiceManager getProfilingServiceManager() {
62         return sProfilingServiceManager;
63     }
64 
65     /**
66      * Called by {@link SystemServiceRegistry}'s static initializer and registers profiling
67      * services to {@link Context}, so that {@link Context#getSystemService} can return them.
68      *
69      * @throws IllegalStateException if this is called from anywhere besides
70      * {@link SystemServiceRegistry}
71      */
registerServiceWrappers()72     public static void registerServiceWrappers() {
73         SystemServiceRegistry.registerContextAwareService(
74                 Context.PROFILING_SERVICE,
75                 ProfilingManager.class,
76                 context -> new ProfilingManager(context)
77         );
78     }
79 }
80