The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.util.Log; |
| 20 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 21 | import com.android.internal.os.BinderInternal; |
| 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 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 | /** |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 64 | * Returns a reference to a service with the given name, or throws |
| 65 | * {@link NullPointerException} if none is found. |
| 66 | * |
| 67 | * @hide |
| 68 | */ |
| 69 | public static IBinder getServiceOrThrow(String name) throws ServiceNotFoundException { |
| 70 | final IBinder binder = getService(name); |
| 71 | if (binder != null) { |
| 72 | return binder; |
| 73 | } else { |
| 74 | throw new ServiceNotFoundException(name); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 85 | public static void addService(String name, IBinder service) { |
| 86 | try { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 87 | getIServiceManager().addService(name, service, false); |
| 88 | } catch (RemoteException e) { |
| 89 | Log.e(TAG, "error in addService", e); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Place a new @a service called @a name into the service |
| 95 | * manager. |
| 96 | * |
| 97 | * @param name the name of the new service |
| 98 | * @param service the service object |
| 99 | * @param allowIsolated set to true to allow isolated sandboxed processes |
| 100 | * to access this service |
| 101 | */ |
| 102 | public static void addService(String name, IBinder service, boolean allowIsolated) { |
| 103 | try { |
| 104 | getIServiceManager().addService(name, service, allowIsolated); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } catch (RemoteException e) { |
| 106 | Log.e(TAG, "error in addService", e); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Retrieve an existing service called @a name from the |
| 112 | * service manager. Non-blocking. |
| 113 | */ |
| 114 | public static IBinder checkService(String name) { |
| 115 | try { |
| 116 | IBinder service = sCache.get(name); |
| 117 | if (service != null) { |
| 118 | return service; |
| 119 | } else { |
| 120 | return getIServiceManager().checkService(name); |
| 121 | } |
| 122 | } catch (RemoteException e) { |
| 123 | Log.e(TAG, "error in checkService", e); |
| 124 | return null; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Return a list of all currently running services. |
Umair Khan | 9ccebbf | 2016-01-06 19:47:20 +0530 | [diff] [blame] | 130 | * @return an array of all currently running services, or <code>null</code> in |
| 131 | * case of an exception |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | */ |
Umair Khan | 9ccebbf | 2016-01-06 19:47:20 +0530 | [diff] [blame] | 133 | public static String[] listServices() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | try { |
| 135 | return getIServiceManager().listServices(); |
| 136 | } catch (RemoteException e) { |
| 137 | Log.e(TAG, "error in listServices", e); |
| 138 | return null; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * This is only intended to be called when the process is first being brought |
| 144 | * up and bound by the activity manager. There is only one thread in the process |
| 145 | * at that time, so no locking is done. |
| 146 | * |
| 147 | * @param cache the cache of service references |
| 148 | * @hide |
| 149 | */ |
| 150 | public static void initServiceCache(Map<String, IBinder> cache) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 151 | if (sCache.size() != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | throw new IllegalStateException("setServiceCache may only be called once"); |
| 153 | } |
| 154 | sCache.putAll(cache); |
| 155 | } |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Exception thrown when no service published for given name. This might be |
| 159 | * thrown early during boot before certain services have published |
| 160 | * themselves. |
| 161 | * |
| 162 | * @hide |
| 163 | */ |
| 164 | public static class ServiceNotFoundException extends Exception { |
| 165 | public ServiceNotFoundException(String name) { |
| 166 | super("No service published for: " + name); |
| 167 | } |
| 168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |