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); 73 } catch (RemoteException e) { 74 Log.e(TAG, "error in addService", e); 75 } 76 } 77 78 /** 79 * Retrieve an existing service called @a name from the 80 * service manager. Non-blocking. 81 */ 82 public static IBinder checkService(String name) { 83 try { 84 IBinder service = sCache.get(name); 85 if (service != null) { 86 return service; 87 } else { 88 return getIServiceManager().checkService(name); 89 } 90 } catch (RemoteException e) { 91 Log.e(TAG, "error in checkService", e); 92 return null; 93 } 94 } 95 96 /** 97 * Return a list of all currently running services. 98 */ 99 public static String[] listServices() throws RemoteException { 100 try { 101 return getIServiceManager().listServices(); 102 } catch (RemoteException e) { 103 Log.e(TAG, "error in listServices", e); 104 return null; 105 } 106 } 107 108 /** 109 * This is only intended to be called when the process is first being brought 110 * up and bound by the activity manager. There is only one thread in the process 111 * at that time, so no locking is done. 112 * 113 * @param cache the cache of service references 114 * @hide 115 */ 116 public static void initServiceCache(Map<String, IBinder> cache) { 117 if (sCache.size() != 0) { 118 throw new IllegalStateException("setServiceCache may only be called once"); 119 } 120 sCache.putAll(cache); 121 } 122} 123