• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.os;
18 
19 import com.android.internal.os.BinderInternal;
20 
21 import android.util.Log;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 
26 /** @hide */
27 public 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 
getIServiceManager()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      */
getService(String name)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      */
addService(String name, IBinder service)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      */
addService(String name, IBinder service, boolean allowIsolated)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      */
checkService(String name)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      */
listServices()116     public static String[] listServices() throws RemoteException {
117         try {
118             return getIServiceManager().listServices();
119         } catch (RemoteException e) {
120             Log.e(TAG, "error in listServices", e);
121             return null;
122         }
123     }
124 
125     /**
126      * This is only intended to be called when the process is first being brought
127      * up and bound by the activity manager. There is only one thread in the process
128      * at that time, so no locking is done.
129      *
130      * @param cache the cache of service references
131      * @hide
132      */
initServiceCache(Map<String, IBinder> cache)133     public static void initServiceCache(Map<String, IBinder> cache) {
134         if (sCache.size() != 0) {
135             throw new IllegalStateException("setServiceCache may only be called once");
136         }
137         sCache.putAll(cache);
138     }
139 }
140