diff options
author | Mike Lockwood <lockwood@android.com> | 2011-03-08 22:47:08 -0500 |
---|---|---|
committer | Mike Lockwood <lockwood@android.com> | 2011-03-08 22:47:08 -0500 |
commit | d591357524091254483849e37697255cc8fce2ad (patch) | |
tree | 914eabcaf84171f276213569a28fb1ec99fbe408 /packages | |
parent | 3a68b8338b431eb15d28e92f06452efbbda9493e (diff) | |
download | frameworks_base-d591357524091254483849e37697255cc8fce2ad.zip frameworks_base-d591357524091254483849e37697255cc8fce2ad.tar.gz frameworks_base-d591357524091254483849e37697255cc8fce2ad.tar.bz2 |
Close USB dialogs if their corresponding accessory or device has disconnected
BUG: 4060065
Change-Id: Ib517e5e4a5422f9f0c8fca601308cecf7743c5d1
Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'packages')
3 files changed, 106 insertions, 12 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbDisconnectedReceiver.java b/packages/SystemUI/src/com/android/systemui/usb/UsbDisconnectedReceiver.java new file mode 100644 index 0000000..1edebbb --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/usb/UsbDisconnectedReceiver.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.usb; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.hardware.usb.UsbAccessory; +import android.hardware.usb.UsbDevice; +import android.hardware.usb.UsbManager; + +// This class is used to close UsbPermissionsActivity and UsbResolverActivity +// if their device/accessory is disconnected while the dialog is still open +class UsbDisconnectedReceiver extends BroadcastReceiver { + private final Activity mActivity; + private UsbDevice mDevice; + private UsbAccessory mAccessory; + + public UsbDisconnectedReceiver(Activity activity, UsbDevice device) { + mActivity = activity; + mDevice = device; + + IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED); + activity.registerReceiver(this, filter); + } + + public UsbDisconnectedReceiver(Activity activity, UsbAccessory accessory) { + mActivity = activity; + mAccessory = accessory; + + IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED); + activity.registerReceiver(this, filter); + } + + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { + UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); + if (device != null && device.equals(mDevice)) { + mActivity.finish(); + } + } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) { + UsbAccessory accessory = + (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); + if (accessory != null && accessory.equals(mAccessory)) { + mActivity.finish(); + } + } + } +}
\ No newline at end of file diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java b/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java index 92c6d3d..f1784df 100644 --- a/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java +++ b/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java @@ -57,6 +57,7 @@ public class UsbPermissionActivity extends AlertActivity private String mPackageName; private int mUid; private boolean mPermissionGranted; + private UsbDisconnectedReceiver mDisconnectedReceiver; @Override public void onCreate(Bundle icicle) { @@ -85,8 +86,10 @@ public class UsbPermissionActivity extends AlertActivity ap.mTitle = appName; if (mDevice == null) { ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName); + mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); } else { ap.mMessage = getString(R.string.usb_device_permission_prompt, appName); + mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); } ap.mPositiveButtonText = getString(com.android.internal.R.string.ok); ap.mNegativeButtonText = getString(com.android.internal.R.string.cancel); @@ -142,6 +145,9 @@ public class UsbPermissionActivity extends AlertActivity Log.e(TAG, "IUsbService connection failed", e); } + if (mDisconnectedReceiver != null) { + unregisterReceiver(mDisconnectedReceiver); + } super.onDestroy(); } diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java b/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java index 1d7f70f..84d73dd 100644 --- a/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java +++ b/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java @@ -39,6 +39,10 @@ public class UsbResolverActivity extends ResolverActivity { public static final String TAG = "UsbResolverActivity"; public static final String EXTRA_RESOLVE_INFOS = "rlist"; + private UsbDevice mDevice; + private UsbAccessory mAccessory; + private UsbDisconnectedReceiver mDisconnectedReceiver; + @Override protected void onCreate(Bundle savedInstanceState) { Intent intent = getIntent(); @@ -57,6 +61,27 @@ public class UsbResolverActivity extends ResolverActivity { This is necessary because this activity is needed for the user to allow the application permission to access the 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); + } + } + + @Override + protected void onDestroy() { + if (mDisconnectedReceiver != null) { + unregisterReceiver(mDisconnectedReceiver); + } + super.onDestroy(); } protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) { @@ -64,28 +89,24 @@ public class UsbResolverActivity extends ResolverActivity { IBinder b = ServiceManager.getService(USB_SERVICE); IUsbManager service = IUsbManager.Stub.asInterface(b); int uid = ri.activityInfo.applicationInfo.uid; - String action = intent.getAction(); - if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { - UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); + if (mDevice != null) { // grant permission for the device - service.grantDevicePermission(device, uid); + service.grantDevicePermission(mDevice, uid); // set or clear default setting if (alwaysCheck) { - service.setDevicePackage(device, ri.activityInfo.packageName); + service.setDevicePackage(mDevice, ri.activityInfo.packageName); } else { - service.setDevicePackage(device, null); + service.setDevicePackage(mDevice, null); } - } else if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) { - UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra( - UsbManager.EXTRA_ACCESSORY); + } else if (mAccessory != null) { // grant permission for the accessory - service.grantAccessoryPermission(accessory, uid); + service.grantAccessoryPermission(mAccessory, uid); // set or clear default setting if (alwaysCheck) { - service.setAccessoryPackage(accessory, ri.activityInfo.packageName); + service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName); } else { - service.setAccessoryPackage(accessory, null); + service.setAccessoryPackage(mAccessory, null); } } |