下面列出了怎么用android.os.IInterface的API类实例代码及写法,或者点击链接到github查看源代码。
/** Register an {@link IRotationWatcher} with IWindowManager. */
protected static boolean watchRotations(IRotationWatcher watcher, final boolean watch) {
// NOTE: removeRotationWatcher is only available on Android 4.3 (API 18) and 4.4 (API 19 & 20).
final String methodName = ((watch) ? "watchRotation" : "removeRotationWatcher" );
final IInterface mWM = getIWindowManager();
if (null == mWM) return false;
try {
Method mMethod = mWM.getClass().getDeclaredMethod(
methodName, new Class[]{ IRotationWatcher.class });
if (mMethod == null) return false;
mMethod.setAccessible(true);
mMethod.invoke(mWM, watcher);
return true;
} catch (Throwable t) {
Log.e(TAG, "Cannot register " + IRotationWatcher.class.getSimpleName() , t);
return false;
}
}
/**
* Send an event to run as soon as the binder interface is available.
*
* @param event a {@link PendingEvent} to send.
*/
public void sendEvent(@NonNull PendingEvent event) {
IInterface iface;
synchronized (mLock) {
iface = mBoundInterface;
if (iface == null) {
mPendingEvent = event;
}
}
if (iface != null) {
try {
event.runEvent(iface);
} catch (RuntimeException | RemoteException ex) {
Slog.e(TAG, "Received exception from user service: ", ex);
}
}
}
private void callFocusedActivityChangedLocked() {
final ComponentName c = mCurrentVrModeComponent;
final boolean b = mRunning2dInVr;
final int pid = mVrAppProcessId;
mCurrentVrService.sendEvent(new PendingEvent() {
@Override
public void runEvent(IInterface service) throws RemoteException {
// Under specific (and unlikely) timing scenarios, when VrCore
// crashes and is rebound, focusedActivityChanged() may be
// called a 2nd time with the same arguments. IVrListeners
// should make sure to handle that scenario gracefully.
IVrListener l = (IVrListener) service;
l.focusedActivityChanged(c, b, pid);
}
});
}
public static boolean _dispatchMediaKeyEvent(KeyEvent event) {
try {
IInterface service = getService();
if (null == mDispatchMediaKeyEvent)
mDispatchMediaKeyEvent = service.getClass().getDeclaredMethod(
"dispatchMediaKeyEvent", KeyEvent.class);
if (null != mDispatchMediaKeyEvent) {
mDispatchMediaKeyEvent.setAccessible(true);
mDispatchMediaKeyEvent.invoke(service, event);
return true;
}
} catch (Throwable t) {
LOGE(TAG, "Error dispatchMediaKeyEvent()", t);
}
return false;
}
/**
* ServiceManager.getService(String name)
* Obtains an {@link IBinder} reference of a system service.
*/
public static IBinder getService(String name) {
try {
final IInterface mService = ReflectionUtils.getIServiceManager();
Method mMethod = mService.getClass().getDeclaredMethod(
"getService", new Class[] { String.class });
if (mMethod == null) return null;
mMethod.setAccessible(true);
return (IBinder) mMethod.invoke(mService, name);
} catch (Throwable e) {
LogUtils.LOGE("Reflection", "Error accessing ServiceManager.getService().", e);
}
return null;
}
@Override
@SuppressLint("PrivateApi")
public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
switch (method.getName()) {
case "queryLocalInterface":
Class iManager;
try {
iManager = Class.forName(String.valueOf(args[0]));
} catch (ClassNotFoundException e) {
e.printStackTrace();
return method.invoke(mOriginService, args);
}
ClassLoader classLoader = mOriginService.getClass().getClassLoader();
Class[] interfaces = new Class[]{IInterface.class, IBinder.class, iManager};
return Proxy.newProxyInstance(classLoader, interfaces, mHooker);
default:
return method.invoke(mOriginService, args);
}
}
public static Object _iaudioServiceMethod(String methodName, Object[] vals) {
try {
IInterface service = getService();
if (null != service) {
String methodMapName = methodMapName(service.getClass(), methodName);
if (!shouldTryMethod(methodMapName)) return null;
boolean hadMethod = mMethodMap.containsKey(methodMapName);
Method bMethod = (hadMethod) ? mMethodMap.get(methodMapName) :
service.getClass().getDeclaredMethod(methodName, fromObjects(vals));
if (null != bMethod) {
bMethod.setAccessible(true);
if (!hadMethod) mMethodMap.put(methodMapName, bMethod);
Object ret = bMethod.invoke(service, vals);
mMethodSuccessMap.put(methodMapName, null != ret);
if (null != ret) return ret;
}
}
} catch (Throwable t) {
LOGE(TAG, "Error invoking AudioService#" + methodName, t);
return null;
}
return new Object();
}
public static IBinder factory(Context context, String name, IBinder binder) {
String descriptor = null;
try {
descriptor = binder.getInterfaceDescriptor();
} catch (RemoteException e) {
if (DEBUG) {
Log.d(TAG, "getInterfaceDescriptor()", e);
}
}
android.os.IInterface iin = binder.queryLocalInterface(descriptor);
if (iin != null) {
/**
* If the requested interface has local implementation, meaning that
* it's living in the same process as the one who requests for it,
* return the binder directly since in such cases our wrapper does
* not help in any way.
*/
return binder;
}
return new ServiceWrapper(context, name, binder);
}
public void closeSystemDialogs(Context context, String reason) {
LOGI(TAG, "closeSystemDialogs(" + reason + ')');
IInterface wm = PopupWindowManager.getIWindowManager();
try {
if (null == wmCsd)
wmCsd = wm.getClass().getDeclaredMethod("closeSystemDialogs", String.class);
if (null != wmCsd) {
wmCsd.setAccessible(true);
wmCsd.invoke(wm, reason);
return;
}
} catch (Throwable t) {
LOGE(TAG, "Could not invoke IWindowManager#closeSystemDialogs");
}
// Backup is to send the intent ourselves.
Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
intent.putExtra("reason", reason);
context.sendBroadcast(intent);
}
@Override
public IBinder getService(final Context context, ClassLoader classLoader, IBinder binder) {
return new StubBinder(classLoader, binder) {
@Override
public InvocationHandler createHandler(Class<?> interfaceClass, final IInterface base) {
return new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(base, args);
} catch (InvocationTargetException e) {
if (e.getCause() != null) {
throw e.getCause();
}
throw e;
}
}
};
}
};
}
@Override
public IBinder getService(final Context context, ClassLoader classLoader, IBinder binder) {
return new StubBinder(classLoader, binder) {
@Override
public InvocationHandler createHandler(Class<?> interfaceClass, final IInterface base) {
return new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(base, args);
} catch (InvocationTargetException e) {
if (e.getCause() != null) {
throw e.getCause();
}
throw e;
}
}
};
}
};
}
/** Register an {@link IRotationWatcher} with IWindowManager. */
protected static boolean watchRotations(IRotationWatcher watcher, final boolean watch) {
// NOTE: removeRotationWatcher is only available on Android 4.3 (API 18) and 4.4 (API 19 & 20).
final String methodName = ((watch) ? "watchRotation" : "removeRotationWatcher" );
final IInterface mWM = getIWindowManager();
if (null == mWM) return false;
try {
Method mMethod = mWM.getClass().getDeclaredMethod(
methodName, new Class[]{ IRotationWatcher.class });
if (mMethod == null) return false;
mMethod.setAccessible(true);
mMethod.invoke(mWM, watcher);
return true;
} catch (Throwable t) {
Log.e(TAG, "Cannot register " + IRotationWatcher.class.getSimpleName() , t);
return false;
}
}
private ManagedServiceInfo getServiceFromTokenLocked(IInterface service) {
if (service == null) {
return null;
}
final IBinder token = service.asBinder();
final int N = mServices.size();
for (int i = 0; i < N; i++) {
final ManagedServiceInfo info = mServices.get(i);
if (info.service.asBinder() == token) return info;
}
return null;
}
protected boolean isServiceTokenValidLocked(IInterface service) {
if (service == null) {
return false;
}
ManagedServiceInfo info = getServiceFromTokenLocked(service);
if (info != null) {
return true;
}
return false;
}
protected ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
checkNotNull(service);
ManagedServiceInfo info = getServiceFromTokenLocked(service);
if (info != null) {
return info;
}
throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
+ service + " " + service.getClass());
}
public void registerService(IInterface service, ComponentName component, int userid) {
checkNotNull(service);
ManagedServiceInfo info = registerServiceImpl(service, component, userid);
if (info != null) {
onServiceAdded(info);
}
}
public ComponentName startService(IInterface caller, Intent service, String resolvedType, int userId) {
try {
return getService().startService(caller != null ? caller.asBinder() : null, service, resolvedType, userId);
} catch (RemoteException e) {
return VirtualRuntime.crash(e);
}
}
/**
* Removes a service from the list and unbinds.
*/
private void unregisterServiceImpl(IInterface service, int userid) {
ManagedServiceInfo info = removeServiceImpl(service, userid);
if (info != null && info.connection != null && !info.isGuest(this)) {
mContext.unbindService(info.connection);
}
}
public static ICustomTabsService asInterface(IBinder obj) {
if(obj == null) {
return null;
} else {
IInterface iin = obj.queryLocalInterface("android.support.customtabs.ICustomTabsService");
return (iin != null && iin instanceof ICustomTabsService?(ICustomTabsService)iin:new ICustomTabsService.Stub.Proxy(obj));
}
}
/**
* Compares the underlying Binder of the given two IInterface objects and returns true if
* they equals. null values are accepted.
*/
private boolean binderEquals(IInterface left, IInterface right) {
if (left == null) {
return right == null;
} else {
return right == null ? false : left.asBinder() == right.asBinder();
}
}
public static ICustomTabsService asInterface(IBinder obj) {
if(obj == null) {
return null;
} else {
IInterface iin = obj.queryLocalInterface("android.support.customtabs.ICustomTabsService");
return (iin != null && iin instanceof ICustomTabsService?(ICustomTabsService)iin:new ICustomTabsService.Stub.Proxy(obj));
}
}
public static ICustomTabsService asInterface(IBinder obj) {
if(obj == null) {
return null;
} else {
IInterface iin = obj.queryLocalInterface("android.support.customtabs.ICustomTabsService");
return (iin != null && iin instanceof ICustomTabsService?(ICustomTabsService)iin:new ICustomTabsService.Stub.Proxy(obj));
}
}
public static ICustomTabsCallback asInterface(IBinder obj) {
if(obj == null) {
return null;
} else {
IInterface iin = obj.queryLocalInterface("android.support.customtabs.ICustomTabsCallback");
return (iin != null && iin instanceof ICustomTabsCallback?(ICustomTabsCallback)iin:new ICustomTabsCallback.Stub.Proxy(obj));
}
}
public static <I extends IInterface> void register(final Class<I> service_interface, final I instance) {
final IInterface existent = sRegistry.get(service_interface);
if (existent != null) {
if (existent == instance) return;
throw new IllegalStateException(service_interface + " was already registered with " + existent.getClass());
}
sRegistry.put(service_interface, instance);
}
public static ICustomTabsService asInterface(IBinder obj) {
if(obj == null) {
return null;
} else {
IInterface iin = obj.queryLocalInterface("android.support.customtabs.ICustomTabsService");
return (iin != null && iin instanceof ICustomTabsService?(ICustomTabsService)iin:new ICustomTabsService.Stub.Proxy(obj));
}
}
/**
* Cast an IBinder object into an ILicenseResultListener interface,
* generating a proxy if needed.
*/
public static com.google.android.vending.licensing.ILicenseResultListener asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.google.android.vending.licensing.ILicenseResultListener))) {
return ((com.google.android.vending.licensing.ILicenseResultListener)iin);
}
return new com.google.android.vending.licensing.ILicenseResultListener.Stub.Proxy(obj);
}
/**
* Cast an IBinder object into an ILicenseResultListener interface,
* generating a proxy if needed.
*/
public static com.google.android.vending.licensing.ILicenseResultListener asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.google.android.vending.licensing.ILicenseResultListener))) {
return ((com.google.android.vending.licensing.ILicenseResultListener)iin);
}
return new com.google.android.vending.licensing.ILicenseResultListener.Stub.Proxy(obj);
}
public static IApkManager asInterface(IBinder obj) {
if (obj == null) {
return null;
}
IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (iin == null || !(iin instanceof IApkManager)) {
return new Proxy(obj);
}
return (IApkManager) iin;
}
/** @return An instance of android.view.IWindowManager. */
public synchronized static IInterface getIWindowManager() {
if (null == iWindowManager) {
iWindowManager = ReflectionUtils.getIInterface(
Context.WINDOW_SERVICE, "android.view.IWindowManager$Stub");
}
return iWindowManager;
}
public static IDownloadService asInterface(IBinder obj) {
if (obj == null) {
return null;
}
IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (iin == null || !(iin instanceof IDownloadService)) {
return new Proxy(obj);
}
return (IDownloadService) iin;
}