2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.usb;
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.hardware.usb.IUsbManager;
27 import android.hardware.usb.UsbManager;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.os.ServiceManager;
31 import android.os.SystemProperties;
32 import android.util.Log;
33 import android.view.LayoutInflater;
34 import android.view.MotionEvent;
35 import android.view.View;
36 import android.view.Window;
37 import android.view.WindowManager;
38 import android.widget.CheckBox;
39 import com.android.internal.app.AlertActivity;
40 import com.android.internal.app.AlertController;
41 import com.android.systemui.R;
42 import android.widget.Toast;
44 public class UsbDebuggingActivity extends AlertActivity
45 implements DialogInterface.OnClickListener {
46 private static final String TAG = "UsbDebuggingActivity";
48 private CheckBox mAlwaysAllow;
49 private UsbDisconnectedReceiver mDisconnectedReceiver;
53 public void onCreate(Bundle icicle) {
54 Window window = getWindow();
55 window.addPrivateFlags(WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
56 window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
58 super.onCreate(icicle);
60 if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) {
61 mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
64 Intent intent = getIntent();
65 String fingerprints = intent.getStringExtra("fingerprints");
66 mKey = intent.getStringExtra("key");
68 if (fingerprints == null || mKey == null) {
73 final AlertController.AlertParams ap = mAlertParams;
74 ap.mTitle = getString(R.string.usb_debugging_title);
75 ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
76 ap.mPositiveButtonText = getString(android.R.string.ok);
77 ap.mNegativeButtonText = getString(android.R.string.cancel);
78 ap.mPositiveButtonListener = this;
79 ap.mNegativeButtonListener = this;
81 // add "always allow" checkbox
82 LayoutInflater inflater = LayoutInflater.from(ap.mContext);
83 View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
84 mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
85 mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
90 // adding touch listener on affirmative button - checks if window is obscured
91 // if obscured, do not let user give permissions (could be tapjacking involved)
92 final View.OnTouchListener filterTouchListener = new View.OnTouchListener() {
94 public boolean onTouch(View v, MotionEvent event) {
95 // Filter obscured touches by consuming them.
96 if (((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0)
97 || ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED) != 0)) {
98 if (event.getAction() == MotionEvent.ACTION_UP) {
99 Toast.makeText(v.getContext(),
100 R.string.touch_filtered_warning,
101 Toast.LENGTH_SHORT).show();
108 mAlert.getButton(BUTTON_POSITIVE).setOnTouchListener(filterTouchListener);
111 private class UsbDisconnectedReceiver extends BroadcastReceiver {
112 private final Activity mActivity;
113 public UsbDisconnectedReceiver(Activity activity) {
114 mActivity = activity;
118 public void onReceive(Context content, Intent intent) {
119 String action = intent.getAction();
120 if (!UsbManager.ACTION_USB_STATE.equals(action)) {
123 boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
131 public void onStart() {
133 IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
134 registerReceiver(mDisconnectedReceiver, filter);
138 protected void onStop() {
139 if (mDisconnectedReceiver != null) {
140 unregisterReceiver(mDisconnectedReceiver);
146 public void onClick(DialogInterface dialog, int which) {
147 boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
148 boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
150 IBinder b = ServiceManager.getService(USB_SERVICE);
151 IUsbManager service = IUsbManager.Stub.asInterface(b);
153 service.allowUsbDebugging(alwaysAllow, mKey);
155 service.denyUsbDebugging();
157 } catch (Exception e) {
158 Log.e(TAG, "Unable to notify Usb service", e);