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 17 package com.android.server; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.util.ArrayMap; 23 24 import java.util.Map; 25 import java.util.Objects; 26 27 /** 28 * The registry for in-process module interfaces. 29 * <p> 30 * In-process module interfaces should be named with the suffix {@code ManagerLocal} for 31 * consistency. 32 * 33 * @hide 34 */ 35 @SystemApi(client = SystemApi.Client.SYSTEM_SERVER) 36 public final class LocalManagerRegistry { LocalManagerRegistry()37 private LocalManagerRegistry() {} 38 39 @NonNull 40 private static final Map<Class<?>, Object> sManagers = new ArrayMap<>(); 41 42 /** 43 * Get a manager from the registry. 44 * 45 * @param managerClass the class that the manager implements 46 * @return the manager, or {@code null} if not found 47 */ 48 @Nullable 49 @SuppressWarnings("unchecked") getManager(@onNull Class<T> managerClass)50 public static <T> T getManager(@NonNull Class<T> managerClass) { 51 synchronized (sManagers) { 52 return (T) sManagers.get(managerClass); 53 } 54 } 55 56 /** 57 * Adds a manager to the registry. 58 * 59 * @param managerClass the class that the manager implements 60 * @param manager the manager 61 * @throws IllegalStateException if the manager class is already registered 62 */ addManager(@onNull Class<T> managerClass, @NonNull T manager)63 public static <T> void addManager(@NonNull Class<T> managerClass, @NonNull T manager) { 64 Objects.requireNonNull(managerClass, "managerClass"); 65 Objects.requireNonNull(manager, "manager"); 66 synchronized (sManagers) { 67 if (sManagers.containsKey(managerClass)) { 68 throw new IllegalStateException(managerClass.getName() + " is already registered"); 69 } 70 sManagers.put(managerClass, manager); 71 } 72 } 73 } 74