1/*
2 * Copyright (C) 2007 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
17package android.os;
18
19import com.android.internal.os.BinderInternal;
20
21import android.util.Log;
22
23import java.util.HashMap;
24import java.util.Map;
25
26/** @hide */
27public final class ServiceManager {
28    private static final String TAG = "ServiceManager";
29
30    private static IServiceManager sServiceManager;
31    private static HashMap<String, IBinder> sCache = new HashMap<String, IBinder>();
32
33    private static IServiceManager getIServiceManager() {
34        if (sServiceManager != null) {
35            return sServiceManager;
36        }
37
38        // Find the service manager
39        sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject());
40        return sServiceManager;
41    }
42
43    /**
44     * Returns a reference to a service with the given name.
45     *
46     * @param name the name of the service to get
47     * @return a reference to the service, or <code>null</code> if the service doesn't exist
48     */
49    public static IBinder getService(String name) {
50        try {
51            IBinder service = sCache.get(name);
52            if (service != null) {
53                return service;
54            } else {
55                return getIServiceManager().getService(name);
56            }
57        } catch (RemoteException e) {
58            Log.e(TAG, "error in getService", e);
59        }
60        return null;
61    }
62
63    /**
64     * Place a new @a service called @a name into the service
65     * manager.
66     *
67     * @param name the name of the new service
68     * @param service the service object
69     */
70    public static void addService(String name, IBinder service) {
71        try {
72            getIServiceManager().addService(name, service, false);
73        } catch (RemoteException e) {
74            Log.e(TAG, "error in addService", e);
75        }
76    }
77
78    /**
79     * Place a new @a service called @a name into the service
80     * manager.
81     *
82     * @param name the name of the new service
83     * @param service the service object
84     * @param allowIsolated set to true to allow isolated sandboxed processes
85     * to access this service
86     */
87    public static void addService(String name, IBinder service, boolean allowIsolated) {
88        try {
89            getIServiceManager().addService(name, service, allowIsolated);
90        } catch (RemoteException e) {
91            Log.e(TAG, "error in addService", e);
92        }
93    }
94
95    /**
96     * Retrieve an existing service called @a name from the
97     * service manager.  Non-blocking.
98     */
99    public static IBinder checkService(String name) {
100        try {
101            IBinder service = sCache.get(name);
102            if (service != null) {
103                return service;
104            } else {
105                return getIServiceManager().checkService(name);
106            }
107        } catch (RemoteException e) {
108            Log.e(TAG, "error in checkService", e);
109            return null;
110        }
111    }
112
113    /**
114     * Return a list of all currently running services.
115     * @return an array of all currently running services, or <code>null</code> in
116     * case of an exception
117     */
118    public static String[] listServices() {
119        try {
120            return getIServiceManager().listServices();
121        } catch (RemoteException e) {
122            Log.e(TAG, "error in listServices", e);
123            return null;
124        }
125    }
126
127    /**
128     * This is only intended to be called when the process is first being brought
129     * up and bound by the activity manager. There is only one thread in the process
130     * at that time, so no locking is done.
131     *
132     * @param cache the cache of service references
133     * @hide
134     */
135    public static void initServiceCache(Map<String, IBinder> cache) {
136        if (sCache.size() != 0) {
137            throw new IllegalStateException("setServiceCache may only be called once");
138        }
139        sCache.putAll(cache);
140    }
141}
142