Skip to content

Commit 7b7feee

Browse files
benoitgobyAndroid (Google) Code Review
authored andcommitted
Merge "Add UsbDebuggingManager and UsbDebuggingActivity" into jb-mr1-dev
2 parents a811e08 + 4e68bd4 commit 7b7feee

File tree

7 files changed

+520
-0
lines changed

7 files changed

+520
-0
lines changed

core/java/android/hardware/usb/IUsbManager.aidl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,12 @@ interface IUsbManager
8787

8888
/* Sets the file path for USB mass storage backing file. */
8989
void setMassStorageBackingFile(String path);
90+
91+
/* Allow USB debugging from the attached host. If alwaysAllow is true, add the
92+
* the public key to list of host keys that the user has approved.
93+
*/
94+
void allowUsbDebugging(boolean alwaysAllow, String publicKey);
95+
96+
/* Deny USB debugging from the attached host */
97+
void denyUsbDebugging();
9098
}

packages/SystemUI/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@
132132
android:excludeFromRecents="true">
133133
</activity>
134134

135+
<!-- started from UsbDebuggingManager -->
136+
<activity android:name=".usb.UsbDebuggingActivity"
137+
android:permission="android.permission.MANAGE_USB"
138+
android:theme="@*android:style/Theme.Holo.Dialog.Alert"
139+
android:finishOnCloseSystemDialogs="true"
140+
android:excludeFromRecents="true">
141+
</activity>
142+
135143
<!-- started from NetworkPolicyManagerService -->
136144
<activity
137145
android:name=".net.NetworkOverLimitActivity"

packages/SystemUI/res/values/strings.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@
154154
<!-- Checkbox label for USB accessory dialogs. [CHAR LIMIT=50] -->
155155
<string name="always_use_accessory">Use by default for this USB accessory</string>
156156

157+
<!-- Title of confirmation dialog for USB debugging -->
158+
<string name="usb_debugging_title">Allow USB Debugging?</string>
159+
160+
<!-- Message of confirmation dialog for USB debugging -->
161+
<string name="usb_debugging_message">Allow USB Debugging from this computer?\nYour RSA key fingerprint is\n<xliff:g id="fingerprint">%1$s</xliff:g></string>
162+
163+
<!-- Option to always allow USB debugging from the attached computer -->
164+
<string name="usb_debugging_always">Always allow this computer</string>
165+
157166
<!-- Checkbox label for application compatibility mode ON (zooming app to look like it's running
158167
on a phone). [CHAR LIMIT=25] -->
159168
<string name="compat_mode_on">Zoom to fill screen</string>
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
17+
package com.android.systemui.usb;
18+
19+
import android.app.Activity;
20+
import android.app.AlertDialog;
21+
import android.content.BroadcastReceiver;
22+
import android.content.Context;
23+
import android.content.DialogInterface;
24+
import android.content.Intent;
25+
import android.content.IntentFilter;
26+
import android.graphics.Typeface;
27+
import android.hardware.usb.IUsbManager;
28+
import android.hardware.usb.UsbDevice;
29+
import android.hardware.usb.UsbManager;
30+
import android.os.Bundle;
31+
import android.os.IBinder;
32+
import android.os.ServiceManager;
33+
import android.util.Log;
34+
import android.view.LayoutInflater;
35+
import android.view.View;
36+
import android.widget.CheckBox;
37+
import android.widget.LinearLayout;
38+
import android.widget.TextView;
39+
40+
import com.android.internal.app.AlertActivity;
41+
import com.android.internal.app.AlertController;
42+
43+
import com.android.systemui.R;
44+
45+
public class UsbDebuggingActivity extends AlertActivity
46+
implements DialogInterface.OnClickListener {
47+
private static final String TAG = "UsbDebuggingActivity";
48+
49+
private CheckBox mAlwaysAllow;
50+
private UsbDisconnectedReceiver mDisconnectedReceiver;
51+
private String mKey;
52+
53+
@Override
54+
public void onCreate(Bundle icicle) {
55+
super.onCreate(icicle);
56+
57+
mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
58+
Intent intent = getIntent();
59+
String fingerprints = intent.getStringExtra("fingerprints");
60+
mKey = intent.getStringExtra("key");
61+
62+
if (fingerprints == null || mKey == null) {
63+
finish();
64+
return;
65+
}
66+
67+
final AlertController.AlertParams ap = mAlertParams;
68+
ap.mTitle = getString(R.string.usb_debugging_title);
69+
ap.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
70+
ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
71+
ap.mPositiveButtonText = getString(android.R.string.ok);
72+
ap.mNegativeButtonText = getString(android.R.string.cancel);
73+
ap.mPositiveButtonListener = this;
74+
ap.mNegativeButtonListener = this;
75+
76+
// add "always allow" checkbox
77+
LayoutInflater inflater = LayoutInflater.from(ap.mContext);
78+
View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
79+
mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
80+
mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
81+
ap.mView = checkbox;
82+
83+
setupAlert();
84+
}
85+
86+
private class UsbDisconnectedReceiver extends BroadcastReceiver {
87+
private final Activity mActivity;
88+
public UsbDisconnectedReceiver(Activity activity) {
89+
mActivity = activity;
90+
}
91+
92+
@Override
93+
public void onReceive(Context content, Intent intent) {
94+
String action = intent.getAction();
95+
if (!UsbManager.ACTION_USB_STATE.equals(action)) {
96+
return;
97+
}
98+
boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
99+
if (!connected) {
100+
mActivity.finish();
101+
}
102+
}
103+
}
104+
105+
@Override
106+
public void onStart() {
107+
super.onStart();
108+
IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
109+
registerReceiver(mDisconnectedReceiver, filter);
110+
}
111+
112+
@Override
113+
protected void onStop() {
114+
if (mDisconnectedReceiver != null) {
115+
unregisterReceiver(mDisconnectedReceiver);
116+
}
117+
super.onStop();
118+
}
119+
120+
@Override
121+
public void onClick(DialogInterface dialog, int which) {
122+
boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
123+
boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
124+
try {
125+
IBinder b = ServiceManager.getService(USB_SERVICE);
126+
IUsbManager service = IUsbManager.Stub.asInterface(b);
127+
if (allow) {
128+
service.allowUsbDebugging(alwaysAllow, mKey);
129+
} else {
130+
service.denyUsbDebugging();
131+
}
132+
} catch (Exception e) {
133+
Log.e(TAG, "Unable to notify Usb service", e);
134+
}
135+
finish();
136+
}
137+
}

0 commit comments

Comments
 (0)