1 /* 2 * Copyright (C) 2006 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 android.compat.annotation.UnsupportedAppUsage; 20 21 /** 22 * Native implementation of the service manager. Most clients will only 23 * care about asInterface(). 24 * 25 * @hide 26 */ 27 public final class ServiceManagerNative { ServiceManagerNative()28 private ServiceManagerNative() {} 29 30 /** 31 * Cast a Binder object into a service manager interface, generating 32 * a proxy if needed. 33 * 34 * TODO: delete this method and have clients use 35 * IServiceManager.Stub.asInterface instead 36 */ 37 @UnsupportedAppUsage asInterface(IBinder obj)38 public static IServiceManager asInterface(IBinder obj) { 39 if (obj == null) { 40 return null; 41 } 42 43 // ServiceManager is never local 44 return new ServiceManagerProxy(obj); 45 } 46 } 47 48 // This class should be deleted and replaced with IServiceManager.Stub whenever 49 // mRemote is no longer used 50 class ServiceManagerProxy implements IServiceManager { ServiceManagerProxy(IBinder remote)51 public ServiceManagerProxy(IBinder remote) { 52 mRemote = remote; 53 mServiceManager = IServiceManager.Stub.asInterface( 54 Binder.allowBlocking(this.getNativeServiceManager())); 55 } 56 asBinder()57 public IBinder asBinder() { 58 return mRemote; 59 } 60 61 // TODO(b/355394904): This function has been deprecated, please use getService2 instead. 62 @UnsupportedAppUsage getService(String name)63 public IBinder getService(String name) throws RemoteException { 64 // Same as checkService (old versions of servicemanager had both methods). 65 return checkService2(name).getServiceWithMetadata().service; 66 } 67 getService2(String name)68 public Service getService2(String name) throws RemoteException { 69 // Same as checkService (old versions of servicemanager had both methods). 70 return checkService2(name); 71 } 72 73 // TODO(b/355394904): This function has been deprecated, please use checkService2 instead. 74 @UnsupportedAppUsage checkService(String name)75 public IBinder checkService(String name) throws RemoteException { 76 // Same as checkService (old versions of servicemanager had both methods). 77 return checkService2(name).getServiceWithMetadata().service; 78 } 79 checkService2(String name)80 public Service checkService2(String name) throws RemoteException { 81 return mServiceManager.checkService2(name); 82 } 83 addService(String name, IBinder service, boolean allowIsolated, int dumpPriority)84 public void addService(String name, IBinder service, boolean allowIsolated, int dumpPriority) 85 throws RemoteException { 86 mServiceManager.addService(name, service, allowIsolated, dumpPriority); 87 } 88 listServices(int dumpPriority)89 public String[] listServices(int dumpPriority) throws RemoteException { 90 return mServiceManager.listServices(dumpPriority); 91 } 92 registerForNotifications(String name, IServiceCallback cb)93 public void registerForNotifications(String name, IServiceCallback cb) 94 throws RemoteException { 95 mServiceManager.registerForNotifications(name, cb); 96 } 97 unregisterForNotifications(String name, IServiceCallback cb)98 public void unregisterForNotifications(String name, IServiceCallback cb) 99 throws RemoteException { 100 throw new RemoteException(); 101 } 102 isDeclared(String name)103 public boolean isDeclared(String name) throws RemoteException { 104 return mServiceManager.isDeclared(name); 105 } 106 getDeclaredInstances(String iface)107 public String[] getDeclaredInstances(String iface) throws RemoteException { 108 return mServiceManager.getDeclaredInstances(iface); 109 } 110 updatableViaApex(String name)111 public String updatableViaApex(String name) throws RemoteException { 112 return mServiceManager.updatableViaApex(name); 113 } 114 getUpdatableNames(String apexName)115 public String[] getUpdatableNames(String apexName) throws RemoteException { 116 return mServiceManager.getUpdatableNames(apexName); 117 } 118 getConnectionInfo(String name)119 public ConnectionInfo getConnectionInfo(String name) throws RemoteException { 120 return mServiceManager.getConnectionInfo(name); 121 } 122 registerClientCallback(String name, IBinder service, IClientCallback cb)123 public void registerClientCallback(String name, IBinder service, IClientCallback cb) 124 throws RemoteException { 125 throw new RemoteException(); 126 } 127 tryUnregisterService(String name, IBinder service)128 public void tryUnregisterService(String name, IBinder service) throws RemoteException { 129 throw new RemoteException(); 130 } 131 getServiceDebugInfo()132 public ServiceDebugInfo[] getServiceDebugInfo() throws RemoteException { 133 return mServiceManager.getServiceDebugInfo(); 134 } 135 136 /** 137 * Same as mServiceManager but used by apps. 138 * 139 * Once this can be removed, ServiceManagerProxy should be removed entirely. 140 */ 141 @UnsupportedAppUsage 142 private IBinder mRemote; 143 144 private IServiceManager mServiceManager; 145 getNativeServiceManager()146 private native IBinder getNativeServiceManager(); 147 } 148