Search in sources :

Example 1 with IUsbManager

use of android.hardware.usb.IUsbManager in project platform_frameworks_base by android.

the class UsbConfirmActivity method onClick.

public void onClick(DialogInterface dialog, int which) {
    if (which == AlertDialog.BUTTON_POSITIVE) {
        try {
            IBinder b = ServiceManager.getService(USB_SERVICE);
            IUsbManager service = IUsbManager.Stub.asInterface(b);
            final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
            final int userId = UserHandle.myUserId();
            boolean alwaysUse = mAlwaysUse.isChecked();
            Intent intent = null;
            if (mDevice != null) {
                intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
                intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
                // grant permission for the device
                service.grantDevicePermission(mDevice, uid);
                // set or clear default setting
                if (alwaysUse) {
                    service.setDevicePackage(mDevice, mResolveInfo.activityInfo.packageName, userId);
                } else {
                    service.setDevicePackage(mDevice, null, userId);
                }
            } else if (mAccessory != null) {
                intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
                intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
                // grant permission for the accessory
                service.grantAccessoryPermission(mAccessory, uid);
                // set or clear default setting
                if (alwaysUse) {
                    service.setAccessoryPackage(mAccessory, mResolveInfo.activityInfo.packageName, userId);
                } else {
                    service.setAccessoryPackage(mAccessory, null, userId);
                }
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(new ComponentName(mResolveInfo.activityInfo.packageName, mResolveInfo.activityInfo.name));
            startActivityAsUser(intent, new UserHandle(userId));
        } catch (Exception e) {
            Log.e(TAG, "Unable to start activity", e);
        }
    }
    finish();
}
Also used : IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) Intent(android.content.Intent) ComponentName(android.content.ComponentName) IUsbManager(android.hardware.usb.IUsbManager)

Example 2 with IUsbManager

use of android.hardware.usb.IUsbManager in project platform_frameworks_base by android.

the class UsbDebuggingActivity method onClick.

@Override
public void onClick(DialogInterface dialog, int which) {
    boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
    boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
    try {
        IBinder b = ServiceManager.getService(USB_SERVICE);
        IUsbManager service = IUsbManager.Stub.asInterface(b);
        if (allow) {
            service.allowUsbDebugging(alwaysAllow, mKey);
        } else {
            service.denyUsbDebugging();
        }
    } catch (Exception e) {
        Log.e(TAG, "Unable to notify Usb service", e);
    }
    finish();
}
Also used : IBinder(android.os.IBinder) IUsbManager(android.hardware.usb.IUsbManager)

Example 3 with IUsbManager

use of android.hardware.usb.IUsbManager in project platform_frameworks_base by android.

the class UsbResolverActivity method onTargetSelected.

@Override
protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
    final ResolveInfo ri = target.getResolveInfo();
    try {
        IBinder b = ServiceManager.getService(USB_SERVICE);
        IUsbManager service = IUsbManager.Stub.asInterface(b);
        final int uid = ri.activityInfo.applicationInfo.uid;
        final int userId = UserHandle.myUserId();
        if (mDevice != null) {
            // grant permission for the device
            service.grantDevicePermission(mDevice, uid);
            // set or clear default setting
            if (alwaysCheck) {
                service.setDevicePackage(mDevice, ri.activityInfo.packageName, userId);
            } else {
                service.setDevicePackage(mDevice, null, userId);
            }
        } else if (mAccessory != null) {
            // grant permission for the accessory
            service.grantAccessoryPermission(mAccessory, uid);
            // set or clear default setting
            if (alwaysCheck) {
                service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName, userId);
            } else {
                service.setAccessoryPackage(mAccessory, null, userId);
            }
        }
        try {
            target.startAsUser(this, null, new UserHandle(userId));
        } catch (ActivityNotFoundException e) {
            Log.e(TAG, "startActivity failed", e);
        }
    } catch (RemoteException e) {
        Log.e(TAG, "onIntentSelected failed", e);
    }
    return true;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) IBinder(android.os.IBinder) ActivityNotFoundException(android.content.ActivityNotFoundException) UserHandle(android.os.UserHandle) RemoteException(android.os.RemoteException) IUsbManager(android.hardware.usb.IUsbManager)

Example 4 with IUsbManager

use of android.hardware.usb.IUsbManager in project android_frameworks_base by ParanoidAndroid.

the class UsbPermissionActivity method onDestroy.

@Override
public void onDestroy() {
    IBinder b = ServiceManager.getService(USB_SERVICE);
    IUsbManager service = IUsbManager.Stub.asInterface(b);
    // send response via pending intent
    Intent intent = new Intent();
    try {
        if (mDevice != null) {
            intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
            if (mPermissionGranted) {
                service.grantDevicePermission(mDevice, mUid);
                if (mAlwaysUse.isChecked()) {
                    final int userId = UserHandle.getUserId(mUid);
                    service.setDevicePackage(mDevice, mPackageName, userId);
                }
            }
        }
        if (mAccessory != null) {
            intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
            if (mPermissionGranted) {
                service.grantAccessoryPermission(mAccessory, mUid);
                if (mAlwaysUse.isChecked()) {
                    final int userId = UserHandle.getUserId(mUid);
                    service.setAccessoryPackage(mAccessory, mPackageName, userId);
                }
            }
        }
        intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
        mPendingIntent.send(this, 0, intent);
    } catch (PendingIntent.CanceledException e) {
        Log.w(TAG, "PendingIntent was cancelled");
    } catch (RemoteException e) {
        Log.e(TAG, "IUsbService connection failed", e);
    }
    if (mDisconnectedReceiver != null) {
        unregisterReceiver(mDisconnectedReceiver);
    }
    super.onDestroy();
}
Also used : IBinder(android.os.IBinder) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException) IUsbManager(android.hardware.usb.IUsbManager)

Example 5 with IUsbManager

use of android.hardware.usb.IUsbManager in project android_frameworks_base by ParanoidAndroid.

the class UsbConfirmActivity method onClick.

public void onClick(DialogInterface dialog, int which) {
    if (which == AlertDialog.BUTTON_POSITIVE) {
        try {
            IBinder b = ServiceManager.getService(USB_SERVICE);
            IUsbManager service = IUsbManager.Stub.asInterface(b);
            final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
            final int userId = UserHandle.myUserId();
            boolean alwaysUse = mAlwaysUse.isChecked();
            Intent intent = null;
            if (mDevice != null) {
                intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
                intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
                // grant permission for the device
                service.grantDevicePermission(mDevice, uid);
                // set or clear default setting
                if (alwaysUse) {
                    service.setDevicePackage(mDevice, mResolveInfo.activityInfo.packageName, userId);
                } else {
                    service.setDevicePackage(mDevice, null, userId);
                }
            } else if (mAccessory != null) {
                intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
                intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
                // grant permission for the accessory
                service.grantAccessoryPermission(mAccessory, uid);
                // set or clear default setting
                if (alwaysUse) {
                    service.setAccessoryPackage(mAccessory, mResolveInfo.activityInfo.packageName, userId);
                } else {
                    service.setAccessoryPackage(mAccessory, null, userId);
                }
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(new ComponentName(mResolveInfo.activityInfo.packageName, mResolveInfo.activityInfo.name));
            startActivityAsUser(intent, new UserHandle(userId));
        } catch (Exception e) {
            Log.e(TAG, "Unable to start activity", e);
        }
    }
    finish();
}
Also used : IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) Intent(android.content.Intent) ComponentName(android.content.ComponentName) IUsbManager(android.hardware.usb.IUsbManager)

Aggregations

IUsbManager (android.hardware.usb.IUsbManager)30 IBinder (android.os.IBinder)24 RemoteException (android.os.RemoteException)18 Intent (android.content.Intent)12 UserHandle (android.os.UserHandle)12 PendingIntent (android.app.PendingIntent)6 ActivityNotFoundException (android.content.ActivityNotFoundException)6 ComponentName (android.content.ComponentName)6 ResolveInfo (android.content.pm.ResolveInfo)5