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.Nullable; 21 import android.annotation.SystemApi; 22 import android.annotation.SystemApi.Client; 23 24 /** 25 * Provides a way to register and obtain the system service binder objects managed by the profiling 26 * service. 27 * 28 * <p> Only the profiling mainline module will be able to access an instance of this class. 29 * @hide 30 */ 31 @FlaggedApi(Flags.FLAG_TELEMETRY_APIS_FRAMEWORK_INITIALIZATION) 32 @SystemApi(client = Client.MODULE_LIBRARIES) 33 public class ProfilingServiceManager { 34 35 /** @hide */ ProfilingServiceManager()36 public ProfilingServiceManager() {} 37 38 /** 39 * A class that exposes the methods to register and obtain each system service. 40 */ 41 public static final class ServiceRegisterer { 42 private final String mServiceName; 43 44 /** @hide */ ServiceRegisterer(String serviceName)45 public ServiceRegisterer(String serviceName) { 46 mServiceName = serviceName; 47 } 48 49 /** 50 * Get the system server binding object for ProfilingService. 51 * 52 * <p> This blocks until the service instance is ready. 53 * or a timeout happens, in which case it returns null. 54 */ 55 @Nullable get()56 public IBinder get() { 57 return ServiceManager.getService(mServiceName); 58 } 59 60 /** 61 * Get the system server binding object for a service. 62 * 63 * <p>This blocks until the service instance is ready, 64 * or a timeout happens, in which case it throws {@link ServiceNotFoundException}. 65 */ 66 @Nullable getOrThrow()67 public IBinder getOrThrow() throws ServiceNotFoundException { 68 try { 69 return ServiceManager.getServiceOrThrow(mServiceName); 70 } catch (ServiceManager.ServiceNotFoundException e) { 71 throw new ServiceNotFoundException(mServiceName); 72 } 73 } 74 } 75 76 /** 77 * See {@link ServiceRegisterer#getOrThrow()} 78 */ 79 public static class ServiceNotFoundException extends ServiceManager.ServiceNotFoundException { 80 /** 81 * Constructor 82 * 83 * @param name the name of the binder service that cannot be found. 84 */ ServiceNotFoundException(@onNull String name)85 public ServiceNotFoundException(@NonNull String name) { 86 super(name); 87 } 88 } 89 90 /** 91 * Returns {@link ServiceRegisterer} for the "profiling" service. 92 */ 93 @NonNull getProfilingServiceRegisterer()94 public ServiceRegisterer getProfilingServiceRegisterer() { 95 return new ServiceRegisterer("profiling_service"); 96 } 97 } 98