/packages/SystemUI/src/com/android/systemui/usb/UsbDisconnectedReceiver.java

https://github.com/aizuzi/platform_frameworks_base · Java · 67 lines · 42 code · 8 blank · 17 comment · 9 complexity · 58e8137451803033f2385d499724d6e5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.systemui.usb;
  17. import android.app.Activity;
  18. import android.content.BroadcastReceiver;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.content.IntentFilter;
  22. import android.hardware.usb.UsbAccessory;
  23. import android.hardware.usb.UsbDevice;
  24. import android.hardware.usb.UsbManager;
  25. // This class is used to close UsbPermissionsActivity and UsbResolverActivity
  26. // if their device/accessory is disconnected while the dialog is still open
  27. class UsbDisconnectedReceiver extends BroadcastReceiver {
  28. private final Activity mActivity;
  29. private UsbDevice mDevice;
  30. private UsbAccessory mAccessory;
  31. public UsbDisconnectedReceiver(Activity activity, UsbDevice device) {
  32. mActivity = activity;
  33. mDevice = device;
  34. IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
  35. activity.registerReceiver(this, filter);
  36. }
  37. public UsbDisconnectedReceiver(Activity activity, UsbAccessory accessory) {
  38. mActivity = activity;
  39. mAccessory = accessory;
  40. IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
  41. activity.registerReceiver(this, filter);
  42. }
  43. @Override
  44. public void onReceive(Context context, Intent intent) {
  45. String action = intent.getAction();
  46. if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
  47. UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  48. if (device != null && device.equals(mDevice)) {
  49. mActivity.finish();
  50. }
  51. } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
  52. UsbAccessory accessory =
  53. (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
  54. if (accessory != null && accessory.equals(mAccessory)) {
  55. mActivity.finish();
  56. }
  57. }
  58. }
  59. }