android.os.IInterface Java Examples

The following examples show how to use android.os.IInterface. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceWrapper.java    From springreplugin with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: ProxyServiceFactory.java    From container with GNU General Public License v3.0 6 votes vote down vote up
@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;
					}
				}
			};
		}
	};
}
 
Example #3
Source File: AudioHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: ReflectionUtils.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
  * 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;
 }
 
Example #5
Source File: ProxyServiceFactory.java    From container with GNU General Public License v3.0 6 votes vote down vote up
@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;
					}
				}
			};
		}
	};
}
 
Example #6
Source File: PopupWindowManager.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/** 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;
       }
}
 
Example #7
Source File: AudioHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #8
Source File: BinderHookHandler.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #9
Source File: AudioHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #10
Source File: VrManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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);
        }
    });
}
 
Example #11
Source File: ManagedApplicationService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #12
Source File: PopupWindowManager.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/** 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;
       }
}
 
Example #13
Source File: Services.java    From deagle with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #14
Source File: ServiceWrapper.java    From springreplugin with Apache License 2.0 5 votes vote down vote up
@Override
public IInterface queryLocalInterface(String descriptor) {
    try {
        return getRemoteBinder().queryLocalInterface(descriptor);
    } catch (RemoteException e) {
        if (DEBUG) {
            Log.d(TAG, "queryLocalInterface()", e);
        }
    }
    return null;
}
 
Example #15
Source File: INotificationSideChannel.java    From letv with Apache License 2.0 5 votes vote down vote up
public static INotificationSideChannel asInterface(IBinder obj) {
    if (obj == null) {
        return null;
    }
    IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
    if (iin == null || !(iin instanceof INotificationSideChannel)) {
        return new Proxy(obj);
    }
    return (INotificationSideChannel) iin;
}
 
Example #16
Source File: IApplicationThreadCompat.java    From container with GNU General Public License v3.0 5 votes vote down vote up
public static void scheduleBindService(IInterface appThread, IBinder token, Intent intent, boolean rebind,
		int processState) throws RemoteException {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
		IApplicationThreadKitkat.scheduleBindService.call(appThread, token, intent, rebind, processState);
	} else {
		IApplicationThread.scheduleBindService.call(appThread, token, intent, rebind);
	}
}
 
Example #17
Source File: IApplicationThreadCompat.java    From container with GNU General Public License v3.0 5 votes vote down vote up
public static void scheduleServiceArgs(IInterface appThread, IBinder token, boolean taskRemoved,
		int startId, int flags, Intent args) throws RemoteException {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
		IApplicationThreadICSMR1.scheduleServiceArgs.call(appThread, token, taskRemoved, startId, flags, args);
	} else {
		IApplicationThread.scheduleServiceArgs.call(appThread, token, startId, flags, args);
	}
}
 
Example #18
Source File: IWatchFaceService.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
public static IWatchFaceService asInterface(IBinder obj) {
    if (obj == null) {
        return null;
    }
    IInterface iin = obj.queryLocalInterface("android.support.wearable.watchface.IWatchFaceService");
    if ((iin != null) && ((iin instanceof IWatchFaceService))) {
        return (IWatchFaceService) iin;
    }
    return new Proxy(obj);
}
 
Example #19
Source File: LocalAidlServices.java    From deagle with Apache License 2.0 5 votes vote down vote up
private static @Nullable <I extends IInterface> I bindLocked(final Context context, @NonNull final Class<I> service_interface, final Intent intent) {
	final IBinder binder;
	ServiceRecord record = sServices.get(service_interface);
	if (record == null) {
		final IBinder sys_binder = sDummyReceiver.peekService(context, intent);
		if (sys_binder != null) {	// The service is already bound by system, initiate a new binding via AMS.
			record = new ServiceRecord(service_interface, null, sys_binder);
			if (! context.bindService(intent, record, Context.BIND_AUTO_CREATE)) {
				Log.e(TAG, "Failed to bind service with " + intent);
				return null;
			}
		} else {	// Create the service instance locally
			final Service service = createService(context, intent.getComponent().getClassName());
			if (service == null) return null;
			binder = bindService(service, intent);
			if (binder == null) {
				destroyService(service);
				return null;
			}
			record = new ServiceRecord(service_interface, service, binder);
		}

		sServices.put(service_interface, record);
	}

	// Wrap with dynamic proxy, which is later used to differentiate instances in unbind().
	final ProxyHandler handler = new ProxyHandler(service_interface, record.binder);
	final Class[] interfaces = collectInterfacesToProxy(record.binder, service_interface);
	@SuppressWarnings("unchecked") final I instance = (I) Proxy.newProxyInstance(context.getClassLoader(), interfaces, handler);
	record.instances.add(instance);
	return instance;
}
 
Example #20
Source File: HookBinderDelegate.java    From container with GNU General Public License v3.0 5 votes vote down vote up
private static IInterface createStub(Class<?> stubClass, IBinder binder) {
    try {
        if (stubClass == null || binder == null) {
            return null;
        }
        Method asInterface = stubClass.getMethod("asInterface", IBinder.class);
        return (IInterface) asInterface.invoke(null, binder);
    } catch (Exception e) {
        Log.d(TAG, "Could not create stub " + stubClass.getName() + ". Cause: " + e);
        return null;
    }
}
 
Example #21
Source File: IResultReceiver.java    From letv with Apache License 2.0 5 votes vote down vote up
public static IResultReceiver asInterface(IBinder obj) {
    if (obj == null) {
        return null;
    }
    IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
    if (iin == null || !(iin instanceof IResultReceiver)) {
        return new Proxy(obj);
    }
    return (IResultReceiver) iin;
}
 
Example #22
Source File: Services.java    From deagle with Apache License 2.0 5 votes vote down vote up
public static <I extends IInterface> boolean unregister(final Class<I> service_interface, final I instance) {
	final IInterface existent = sRegistry.get(service_interface);
	if (existent == null) return false;
	if (instance != existent) throw new IllegalStateException(service_interface + " was registered with " + existent + ", but being unregistered with " + instance);
	sRegistry.remove(service_interface);
	return true;
}
 
Example #23
Source File: AIDLActivity.java    From letv with Apache License 2.0 5 votes vote down vote up
public static AIDLActivity asInterface(IBinder obj) {
    if (obj == null) {
        return null;
    }
    IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
    if (iin == null || !(iin instanceof AIDLActivity)) {
        return new Proxy(obj);
    }
    return (AIDLActivity) iin;
}
 
Example #24
Source File: AIDLService.java    From letv with Apache License 2.0 5 votes vote down vote up
public static AIDLService asInterface(IBinder obj) {
    if (obj == null) {
        return null;
    }
    IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
    if (iin == null || !(iin instanceof AIDLService)) {
        return new Proxy(obj);
    }
    return (AIDLService) iin;
}
 
Example #25
Source File: ICdeBinder.java    From letv with Apache License 2.0 5 votes vote down vote up
public static ICdeBinder asInterface(IBinder obj) {
    if (obj == null) {
        return null;
    }
    IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
    if (iin == null || !(iin instanceof ICdeBinder)) {
        return new Proxy(obj);
    }
    return (ICdeBinder) iin;
}
 
Example #26
Source File: GetIntentSender.java    From container with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object call(Object who, Method method, Object... args) throws Throwable {
    String creator = (String) args[1];
    args[1] = getHostPkg();
    String[] resolvedTypes = (String[]) args[6];
    int type = (int) args[0];
    if (args[5] instanceof Intent[]) {
        Intent[] intents = (Intent[]) args[5];
        if (intents.length > 0) {
            Intent intent = intents[intents.length - 1];
            if (resolvedTypes != null && resolvedTypes.length > 0) {
                intent.setDataAndType(intent.getData(), resolvedTypes[resolvedTypes.length - 1]);
            }
            Intent proxyIntent = redirectIntentSender(type, creator, intent);
            if (proxyIntent != null) {
                intents[intents.length - 1] = proxyIntent;
            }
        }
    }
    if (args.length > 7 && args[7] instanceof Integer) {
        args[7] = PendingIntent.FLAG_UPDATE_CURRENT;
    }
    IInterface sender = (IInterface) method.invoke(who, args);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 && sender != null && creator != null) {
        VActivityManager.get().addPendingIntent(sender.asBinder(), creator);
    }
    return sender;
}
 
Example #27
Source File: IDownloadService.java    From letv with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #28
Source File: PopupWindowManager.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/** @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;
}
 
Example #29
Source File: IApkManager.java    From letv with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #30
Source File: ILicenseResultListener.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}