Search in sources :

Example 11 with IInterface

use of android.os.IInterface in project VirtualApp by asLody.

the class VActivityManagerService method attachClient.

private void attachClient(int pid, final IBinder clientBinder) {
    final IVClient client = IVClient.Stub.asInterface(clientBinder);
    if (client == null) {
        killProcess(pid);
        return;
    }
    IInterface thread = null;
    try {
        thread = ApplicationThreadNative.asInterface.call(client.getAppThread());
    } catch (RemoteException e) {
    // process has dead
    }
    if (thread == null) {
        killProcess(pid);
        return;
    }
    ProcessRecord app = null;
    try {
        IBinder token = client.getToken();
        if (token instanceof ProcessRecord) {
            app = (ProcessRecord) token;
        }
    } catch (RemoteException e) {
    // process has dead
    }
    if (app == null) {
        killProcess(pid);
        return;
    }
    try {
        final ProcessRecord record = app;
        clientBinder.linkToDeath(new DeathRecipient() {

            @Override
            public void binderDied() {
                clientBinder.unlinkToDeath(this, 0);
                onProcessDead(record);
            }
        }, 0);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    app.client = client;
    app.appThread = thread;
    app.pid = pid;
    synchronized (mProcessNames) {
        mProcessNames.put(app.processName, app.vuid, app);
        mPidsSelfLocked.put(app.pid, app);
    }
}
Also used : IBinder(android.os.IBinder) IInterface(android.os.IInterface) RemoteException(android.os.RemoteException) IVClient(com.lody.virtual.client.IVClient)

Example 12 with IInterface

use of android.os.IInterface in project android_frameworks_base by DirtyUnicorns.

the class ManagedServices method registerServiceLocked.

private void registerServiceLocked(final ComponentName name, final int userid, final boolean isSystem) {
    if (DEBUG)
        Slog.v(TAG, "registerService: " + name + " u=" + userid);
    final String servicesBindingTag = name.toString() + "/" + userid;
    if (mServicesBinding.contains(servicesBindingTag)) {
        // stop registering this thing already! we're working on it
        return;
    }
    mServicesBinding.add(servicesBindingTag);
    final int N = mServices.size();
    for (int i = N - 1; i >= 0; i--) {
        final ManagedServiceInfo info = mServices.get(i);
        if (name.equals(info.component) && info.userid == userid) {
            // cut old connections
            if (DEBUG)
                Slog.v(TAG, "    disconnecting old " + getCaption() + ": " + info.service);
            removeServiceLocked(i);
            if (info.connection != null) {
                mContext.unbindService(info.connection);
            }
        }
    }
    Intent intent = new Intent(mConfig.serviceInterface);
    intent.setComponent(name);
    intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
    final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mConfig.settingsAction), 0);
    intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
    ApplicationInfo appInfo = null;
    try {
        appInfo = mContext.getPackageManager().getApplicationInfo(name.getPackageName(), 0);
    } catch (NameNotFoundException e) {
    // Ignore if the package doesn't exist we won't be able to bind to the service.
    }
    final int targetSdkVersion = appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
    try {
        if (DEBUG)
            Slog.v(TAG, "binding: " + intent);
        ServiceConnection serviceConnection = new ServiceConnection() {

            IInterface mService;

            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                boolean added = false;
                ManagedServiceInfo info = null;
                synchronized (mMutex) {
                    mServicesBinding.remove(servicesBindingTag);
                    try {
                        mService = asInterface(binder);
                        info = newServiceInfo(mService, name, userid, isSystem, this, targetSdkVersion);
                        binder.linkToDeath(info, 0);
                        added = mServices.add(info);
                    } catch (RemoteException e) {
                    // already dead
                    }
                }
                if (added) {
                    onServiceAdded(info);
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Slog.v(TAG, getCaption() + " connection lost: " + name);
            }
        };
        if (!mContext.bindServiceAsUser(intent, serviceConnection, BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT, new UserHandle(userid))) {
            mServicesBinding.remove(servicesBindingTag);
            Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
            return;
        }
    } catch (SecurityException ex) {
        Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
        return;
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) IInterface(android.os.IInterface) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException)

Example 13 with IInterface

use of android.os.IInterface in project android_frameworks_base by DirtyUnicorns.

the class VrManagerService method updateCurrentVrServiceLocked.

/**
     * Send VR mode changes (if the mode state has changed), and update the bound/unbound state of
     * the currently selected VR listener service.  If the component selected for the VR listener
     * service has changed, unbind the previous listener and bind the new listener (if enabled).
     * <p/>
     * Note: Must be called while holding {@code mLock}.
     *
     * @param enabled new state for VR mode.
     * @param component new component to be bound as a VR listener.
     * @param userId user owning the component to be bound.
     * @param calling the component currently using VR mode, or null to leave unchanged.
     *
     * @return {@code true} if the component/user combination specified is valid.
     */
private boolean updateCurrentVrServiceLocked(boolean enabled, @NonNull ComponentName component, int userId, ComponentName calling) {
    boolean sendUpdatedCaller = false;
    final long identity = Binder.clearCallingIdentity();
    try {
        boolean validUserComponent = (mComponentObserver.isValid(component, userId) == EnabledComponentsObserver.NO_ERROR);
        boolean goingIntoVrMode = validUserComponent && enabled;
        if (!mVrModeEnabled && !goingIntoVrMode) {
            // Disabled -> Disabled transition does nothing.
            return validUserComponent;
        }
        String oldVrServicePackage = mCurrentVrService != null ? mCurrentVrService.getComponent().getPackageName() : null;
        final int oldUserId = mCurrentVrModeUser;
        // Notify system services and VR HAL of mode change.
        changeVrModeLocked(goingIntoVrMode);
        boolean nothingChanged = false;
        if (!goingIntoVrMode) {
            // Not going into VR mode, unbind whatever is running
            if (mCurrentVrService != null) {
                Slog.i(TAG, "Leaving VR mode, disconnecting " + mCurrentVrService.getComponent() + " for user " + mCurrentVrService.getUserId());
                mCurrentVrService.disconnect();
                mCurrentVrService = null;
            } else {
                nothingChanged = true;
            }
        } else {
            // Going into VR mode
            if (mCurrentVrService != null) {
                // selection.
                if (mCurrentVrService.disconnectIfNotMatching(component, userId)) {
                    Slog.i(TAG, "VR mode component changed to " + component + ", disconnecting " + mCurrentVrService.getComponent() + " for user " + mCurrentVrService.getUserId());
                    createAndConnectService(component, userId);
                    sendUpdatedCaller = true;
                } else {
                    nothingChanged = true;
                }
            // The service with the correct component/user is already bound, do nothing.
            } else {
                // Nothing was previously running, bind a new service for the latest
                // component/user selection.
                createAndConnectService(component, userId);
                sendUpdatedCaller = true;
            }
        }
        if (calling != null && !Objects.equals(calling, mCurrentVrModeComponent)) {
            mCurrentVrModeComponent = calling;
            sendUpdatedCaller = true;
        }
        if (mCurrentVrModeUser != userId) {
            mCurrentVrModeUser = userId;
            sendUpdatedCaller = true;
        }
        String newVrServicePackage = mCurrentVrService != null ? mCurrentVrService.getComponent().getPackageName() : null;
        final int newUserId = mCurrentVrModeUser;
        // Update AppOps settings that change state when entering/exiting VR mode, or changing
        // the current VrListenerService.
        updateDependentAppOpsLocked(newVrServicePackage, newUserId, oldVrServicePackage, oldUserId);
        if (mCurrentVrService != null && sendUpdatedCaller) {
            final ComponentName c = mCurrentVrModeComponent;
            mCurrentVrService.sendEvent(new PendingEvent() {

                @Override
                public void runEvent(IInterface service) throws RemoteException {
                    IVrListener l = (IVrListener) service;
                    l.focusedActivityChanged(c);
                }
            });
        }
        if (!nothingChanged) {
            logStateLocked();
        }
        return validUserComponent;
    } finally {
        Binder.restoreCallingIdentity(identity);
    }
}
Also used : PendingEvent(com.android.server.utils.ManagedApplicationService.PendingEvent) IInterface(android.os.IInterface) ComponentName(android.content.ComponentName) IVrListener(android.service.vr.IVrListener) RemoteException(android.os.RemoteException)

Example 14 with IInterface

use of android.os.IInterface in project android_frameworks_base by DirtyUnicorns.

the class ManagedApplicationService method connect.

/**
     * Asynchronously bind to the application service if not bound.
     */
public void connect() {
    synchronized (mLock) {
        if (mConnection != null || mPendingConnection != null) {
            // We're already connected or are trying to connect
            return;
        }
        final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mSettingsAction), 0);
        final Intent intent = new Intent().setComponent(mComponent).putExtra(Intent.EXTRA_CLIENT_LABEL, mClientLabel).putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
        final ServiceConnection serviceConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                IInterface iface = null;
                PendingEvent pendingEvent = null;
                synchronized (mLock) {
                    if (mPendingConnection == this) {
                        // No longer pending, remove from pending connection
                        mPendingConnection = null;
                        mConnection = this;
                    } else {
                        // Service connection wasn't pending, must have been disconnected
                        mContext.unbindService(this);
                        return;
                    }
                    try {
                        iBinder.linkToDeath(mDeathRecipient, 0);
                        mBoundInterface = mChecker.asInterface(iBinder);
                        if (!mChecker.checkType(mBoundInterface)) {
                            // Received an invalid binder, disconnect
                            mContext.unbindService(this);
                            mBoundInterface = null;
                        }
                        iface = mBoundInterface;
                        pendingEvent = mPendingEvent;
                        mPendingEvent = null;
                    } catch (RemoteException e) {
                        // DOA
                        Slog.w(TAG, "Unable to bind service: " + intent, e);
                        mBoundInterface = null;
                    }
                }
                if (iface != null && pendingEvent != null) {
                    try {
                        pendingEvent.runEvent(iface);
                    } catch (RuntimeException | RemoteException ex) {
                        Slog.e(TAG, "Received exception from user service: ", ex);
                    }
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Slog.w(TAG, "Service disconnected: " + intent);
                mConnection = null;
                mBoundInterface = null;
            }
        };
        mPendingConnection = serviceConnection;
        try {
            if (!mContext.bindServiceAsUser(intent, serviceConnection, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, new UserHandle(mUserId))) {
                Slog.w(TAG, "Unable to bind service: " + intent);
            }
        } catch (SecurityException e) {
            Slog.w(TAG, "Unable to bind service: " + intent, e);
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) IInterface(android.os.IInterface) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException)

Example 15 with IInterface

use of android.os.IInterface in project android_frameworks_base by AOSPA.

the class ManagedServices method registerServiceLocked.

private void registerServiceLocked(final ComponentName name, final int userid, final boolean isSystem) {
    if (DEBUG)
        Slog.v(TAG, "registerService: " + name + " u=" + userid);
    final String servicesBindingTag = name.toString() + "/" + userid;
    if (mServicesBinding.contains(servicesBindingTag)) {
        // stop registering this thing already! we're working on it
        return;
    }
    mServicesBinding.add(servicesBindingTag);
    final int N = mServices.size();
    for (int i = N - 1; i >= 0; i--) {
        final ManagedServiceInfo info = mServices.get(i);
        if (name.equals(info.component) && info.userid == userid) {
            // cut old connections
            if (DEBUG)
                Slog.v(TAG, "    disconnecting old " + getCaption() + ": " + info.service);
            removeServiceLocked(i);
            if (info.connection != null) {
                mContext.unbindService(info.connection);
            }
        }
    }
    Intent intent = new Intent(mConfig.serviceInterface);
    intent.setComponent(name);
    intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
    final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mConfig.settingsAction), 0);
    intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
    ApplicationInfo appInfo = null;
    try {
        appInfo = mContext.getPackageManager().getApplicationInfo(name.getPackageName(), 0);
    } catch (NameNotFoundException e) {
    // Ignore if the package doesn't exist we won't be able to bind to the service.
    }
    final int targetSdkVersion = appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
    try {
        if (DEBUG)
            Slog.v(TAG, "binding: " + intent);
        ServiceConnection serviceConnection = new ServiceConnection() {

            IInterface mService;

            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                boolean added = false;
                ManagedServiceInfo info = null;
                synchronized (mMutex) {
                    mServicesBinding.remove(servicesBindingTag);
                    try {
                        mService = asInterface(binder);
                        info = newServiceInfo(mService, name, userid, isSystem, this, targetSdkVersion);
                        binder.linkToDeath(info, 0);
                        added = mServices.add(info);
                    } catch (RemoteException e) {
                    // already dead
                    }
                }
                if (added) {
                    onServiceAdded(info);
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Slog.v(TAG, getCaption() + " connection lost: " + name);
            }
        };
        if (!mContext.bindServiceAsUser(intent, serviceConnection, BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT, new UserHandle(userid))) {
            mServicesBinding.remove(servicesBindingTag);
            Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
            return;
        }
    } catch (SecurityException ex) {
        Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
        return;
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) IInterface(android.os.IInterface) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException)

Aggregations

IInterface (android.os.IInterface)28 RemoteException (android.os.RemoteException)16 ComponentName (android.content.ComponentName)15 Intent (android.content.Intent)14 IBinder (android.os.IBinder)12 PendingIntent (android.app.PendingIntent)11 ServiceConnection (android.content.ServiceConnection)9 UserHandle (android.os.UserHandle)9 ApplicationInfo (android.content.pm.ApplicationInfo)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 IVrListener (android.service.vr.IVrListener)4 PendingEvent (com.android.server.utils.ManagedApplicationService.PendingEvent)4 ServiceInfo (android.content.pm.ServiceInfo)3 ProviderInfo (android.content.pm.ProviderInfo)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 IServiceConnection (android.app.IServiceConnection)1 ContentProviderClient (android.content.ContentProviderClient)1 ContentResolver (android.content.ContentResolver)1 IIntentReceiver (android.content.IIntentReceiver)1 IntentFilter (android.content.IntentFilter)1