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