Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 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.util.ArrayMap; |
| 20 | |
| 21 | /** |
| 22 | * This class is used in a similar way as ServiceManager, except the services registered here |
| 23 | * are not Binder objects and are only available in the same process. |
| 24 | * |
| 25 | * Once all services are converted to the SystemService interface, this class can be absorbed |
| 26 | * into SystemServiceManager. |
| 27 | */ |
| 28 | public final class LocalServices { |
| 29 | private LocalServices() {} |
| 30 | |
| 31 | private static final ArrayMap<Class<?>, Object> sLocalServiceObjects = |
| 32 | new ArrayMap<Class<?>, Object>(); |
| 33 | |
| 34 | /** |
| 35 | * Returns a local service instance that implements the specified interface. |
| 36 | * |
| 37 | * @param type The type of service. |
| 38 | * @return The service object. |
| 39 | */ |
| 40 | @SuppressWarnings("unchecked") |
| 41 | public static <T> T getService(Class<T> type) { |
| 42 | synchronized (sLocalServiceObjects) { |
| 43 | return (T) sLocalServiceObjects.get(type); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Adds a service instance of the specified interface to the global registry of local services. |
| 49 | */ |
| 50 | public static <T> void addService(Class<T> type, T service) { |
| 51 | synchronized (sLocalServiceObjects) { |
| 52 | if (sLocalServiceObjects.containsKey(type)) { |
| 53 | throw new IllegalStateException("Overriding service registration"); |
| 54 | } |
| 55 | sLocalServiceObjects.put(type, service); |
| 56 | } |
| 57 | } |
| 58 | } |