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.SystemApi; 20 import android.annotation.SystemApi.Client; 21 import android.app.StatsManager; 22 import android.app.SystemServiceRegistry; 23 import android.content.Context; 24 25 /** 26 * Class for performing registration for all stats services 27 * 28 * @hide 29 */ 30 @SystemApi(client = Client.MODULE_LIBRARIES) 31 public class StatsFrameworkInitializer { StatsFrameworkInitializer()32 private StatsFrameworkInitializer() { 33 } 34 35 private static volatile StatsServiceManager sStatsServiceManager; 36 37 /** 38 * Sets an instance of {@link StatsServiceManager} that allows 39 * the statsd mainline module to register/obtain stats binder services. This is called 40 * by the platform during the system initialization. 41 * 42 * @param statsServiceManager instance of {@link StatsServiceManager} that allows 43 * the statsd mainline module to register/obtain statsd binder services. 44 */ setStatsServiceManager( @onNull StatsServiceManager statsServiceManager)45 public static void setStatsServiceManager( 46 @NonNull StatsServiceManager statsServiceManager) { 47 if (sStatsServiceManager != null) { 48 throw new IllegalStateException("setStatsServiceManager called twice!"); 49 } 50 51 if (statsServiceManager == null) { 52 throw new NullPointerException("statsServiceManager is null"); 53 } 54 55 sStatsServiceManager = statsServiceManager; 56 } 57 58 /** @hide */ getStatsServiceManager()59 public static StatsServiceManager getStatsServiceManager() { 60 return sStatsServiceManager; 61 } 62 63 /** 64 * Called by {@link SystemServiceRegistry}'s static initializer and registers all statsd 65 * services to {@link Context}, so that {@link Context#getSystemService} can return them. 66 * 67 * @throws IllegalStateException if this is called from anywhere besides 68 * {@link SystemServiceRegistry} 69 */ registerServiceWrappers()70 public static void registerServiceWrappers() { 71 SystemServiceRegistry.registerContextAwareService( 72 Context.STATS_MANAGER, 73 StatsManager.class, 74 context -> new StatsManager(context) 75 ); 76 } 77 } 78