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);
}
}
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;
}
}
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);
}
}
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);
}
}
}
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;
}
}
Aggregations