UsbResolverActivitypublic class UsbResolverActivity extends com.android.internal.app.ResolverActivity
Fields Summary |
---|
public static final String | TAG | public static final String | EXTRA_RESOLVE_INFOS | private android.hardware.usb.UsbDevice | mDevice | private android.hardware.usb.UsbAccessory | mAccessory | private UsbDisconnectedReceiver | mDisconnectedReceiver |
Methods Summary |
---|
protected void | onCreate(android.os.Bundle savedInstanceState)
Intent intent = getIntent();
Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
if (!(targetParcelable instanceof Intent)) {
Log.w("UsbResolverActivity", "Target is not an intent: " + targetParcelable);
finish();
return;
}
Intent target = (Intent)targetParcelable;
ArrayList<ResolveInfo> rList = intent.getParcelableArrayListExtra(EXTRA_RESOLVE_INFOS);
CharSequence title = getResources().getText(com.android.internal.R.string.chooseUsbActivity);
super.onCreate(savedInstanceState, target, title, null, rList,
true /* Set alwaysUseOption to true to enable "always use this app" checkbox. */ );
CheckBox alwaysUse = (CheckBox)findViewById(com.android.internal.R.id.alwaysUse);
if (alwaysUse != null) {
if (mDevice == null) {
alwaysUse.setText(R.string.always_use_accessory);
} else {
alwaysUse.setText(R.string.always_use_device);
}
}
mDevice = (UsbDevice)target.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (mDevice != null) {
mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
} else {
mAccessory = (UsbAccessory)target.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (mAccessory == null) {
Log.e(TAG, "no device or accessory");
finish();
return;
}
mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
}
| protected void | onDestroy()
if (mDisconnectedReceiver != null) {
unregisterReceiver(mDisconnectedReceiver);
}
super.onDestroy();
| protected void | onIntentSelected(android.content.pm.ResolveInfo ri, android.content.Intent intent, boolean alwaysCheck)
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 {
startActivityAsUser(intent, new UserHandle(userId));
} catch (ActivityNotFoundException e) {
Log.e(TAG, "startActivity failed", e);
}
} catch (RemoteException e) {
Log.e(TAG, "onIntentSelected failed", e);
}
|
|