1 /* 2 * Copyright (C) 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 package android.os; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.annotation.SystemApi; 21 import android.annotation.SystemApi.Client; 22 23 /** 24 * Provides a way to register and obtain the system service binder objects managed by the stats 25 * service. 26 * 27 * <p> Only the statsd mainline module will be able to access an instance of this class. 28 * @hide 29 */ 30 @SystemApi(client = Client.MODULE_LIBRARIES) 31 public class StatsServiceManager { 32 /** 33 * @hide 34 */ StatsServiceManager()35 public StatsServiceManager() {} 36 37 /** 38 * A class that exposes the methods to register and obtain each system service. 39 */ 40 public static final class ServiceRegisterer { 41 private final String mServiceName; 42 43 /** 44 * @hide 45 */ ServiceRegisterer(String serviceName)46 public ServiceRegisterer(String serviceName) { 47 mServiceName = serviceName; 48 } 49 50 /** 51 * Get the system server binding object for StatsManagerService. 52 * 53 * <p> This blocks until the service instance is ready. 54 * or a timeout happens, in which case it returns null. 55 */ 56 @Nullable get()57 public IBinder get() { 58 return ServiceManager.getService(mServiceName); 59 } 60 61 /** 62 * Get the system server binding object for a service. 63 * 64 * <p>This blocks until the service instance is ready, 65 * or a timeout happens, in which case it throws {@link ServiceNotFoundException}. 66 */ 67 @Nullable getOrThrow()68 public IBinder getOrThrow() throws ServiceNotFoundException { 69 try { 70 return ServiceManager.getServiceOrThrow(mServiceName); 71 } catch (ServiceManager.ServiceNotFoundException e) { 72 throw new ServiceNotFoundException(mServiceName); 73 } 74 } 75 76 /** 77 * Get the system server binding object for a service. If the specified service is 78 * not available, it returns null. 79 */ 80 @Nullable tryGet()81 private IBinder tryGet() { 82 return ServiceManager.checkService(mServiceName); 83 } 84 } 85 86 /** 87 * See {@link ServiceRegisterer#getOrThrow()} 88 */ 89 public static class ServiceNotFoundException extends ServiceManager.ServiceNotFoundException { 90 /** 91 * Constructor 92 * 93 * @param name the name of the binder service that cannot be found. 94 */ ServiceNotFoundException(@onNull String name)95 public ServiceNotFoundException(@NonNull String name) { 96 super(name); 97 } 98 } 99 100 /** 101 * Returns {@link ServiceRegisterer} for the "statscompanion" service. 102 */ 103 @NonNull getStatsCompanionServiceRegisterer()104 public ServiceRegisterer getStatsCompanionServiceRegisterer() { 105 return new ServiceRegisterer("statscompanion"); 106 } 107 108 /** 109 * Returns {@link ServiceRegisterer} for the "statsmanager" service. 110 */ 111 @NonNull getStatsManagerServiceRegisterer()112 public ServiceRegisterer getStatsManagerServiceRegisterer() { 113 return new ServiceRegisterer("statsmanager"); 114 } 115 116 /** 117 * Returns {@link ServiceRegisterer} for the "statsd" service. 118 */ 119 @NonNull getStatsdServiceRegisterer()120 public ServiceRegisterer getStatsdServiceRegisterer() { 121 return new ServiceRegisterer("stats"); 122 } 123 } 124