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

https://github.com/aizuzi/platform_frameworks_base · Java · 136 lines · 103 code · 17 blank · 16 comment · 14 complexity · 69273b61128c38246d6098f7e1e3c70e MD5 · raw file

  1. /*
  2. * Copyright (C) 2012 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.app.AlertDialog;
  19. import android.content.BroadcastReceiver;
  20. import android.content.Context;
  21. import android.content.DialogInterface;
  22. import android.content.Intent;
  23. import android.content.IntentFilter;
  24. import android.hardware.usb.IUsbManager;
  25. import android.hardware.usb.UsbManager;
  26. import android.os.Bundle;
  27. import android.os.IBinder;
  28. import android.os.ServiceManager;
  29. import android.os.SystemProperties;
  30. import android.util.Log;
  31. import android.view.LayoutInflater;
  32. import android.view.View;
  33. import android.widget.CheckBox;
  34. import com.android.internal.app.AlertActivity;
  35. import com.android.internal.app.AlertController;
  36. import com.android.systemui.R;
  37. public class UsbDebuggingActivity extends AlertActivity
  38. implements DialogInterface.OnClickListener {
  39. private static final String TAG = "UsbDebuggingActivity";
  40. private CheckBox mAlwaysAllow;
  41. private UsbDisconnectedReceiver mDisconnectedReceiver;
  42. private String mKey;
  43. @Override
  44. public void onCreate(Bundle icicle) {
  45. super.onCreate(icicle);
  46. if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) {
  47. mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
  48. }
  49. Intent intent = getIntent();
  50. String fingerprints = intent.getStringExtra("fingerprints");
  51. mKey = intent.getStringExtra("key");
  52. if (fingerprints == null || mKey == null) {
  53. finish();
  54. return;
  55. }
  56. final AlertController.AlertParams ap = mAlertParams;
  57. ap.mTitle = getString(R.string.usb_debugging_title);
  58. ap.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
  59. ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
  60. ap.mPositiveButtonText = getString(android.R.string.ok);
  61. ap.mNegativeButtonText = getString(android.R.string.cancel);
  62. ap.mPositiveButtonListener = this;
  63. ap.mNegativeButtonListener = this;
  64. // add "always allow" checkbox
  65. LayoutInflater inflater = LayoutInflater.from(ap.mContext);
  66. View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
  67. mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
  68. mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
  69. ap.mView = checkbox;
  70. setupAlert();
  71. }
  72. private class UsbDisconnectedReceiver extends BroadcastReceiver {
  73. private final Activity mActivity;
  74. public UsbDisconnectedReceiver(Activity activity) {
  75. mActivity = activity;
  76. }
  77. @Override
  78. public void onReceive(Context content, Intent intent) {
  79. String action = intent.getAction();
  80. if (!UsbManager.ACTION_USB_STATE.equals(action)) {
  81. return;
  82. }
  83. boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
  84. if (!connected) {
  85. mActivity.finish();
  86. }
  87. }
  88. }
  89. @Override
  90. public void onStart() {
  91. super.onStart();
  92. IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
  93. registerReceiver(mDisconnectedReceiver, filter);
  94. }
  95. @Override
  96. protected void onStop() {
  97. if (mDisconnectedReceiver != null) {
  98. unregisterReceiver(mDisconnectedReceiver);
  99. }
  100. super.onStop();
  101. }
  102. @Override
  103. public void onClick(DialogInterface dialog, int which) {
  104. boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
  105. boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
  106. try {
  107. IBinder b = ServiceManager.getService(USB_SERVICE);
  108. IUsbManager service = IUsbManager.Stub.asInterface(b);
  109. if (allow) {
  110. service.allowUsbDebugging(alwaysAllow, mKey);
  111. } else {
  112. service.denyUsbDebugging();
  113. }
  114. } catch (Exception e) {
  115. Log.e(TAG, "Unable to notify Usb service", e);
  116. }
  117. finish();
  118. }
  119. }