OSDN Git Service

DO NOT MERGE Backporting potential usb tapjacking precaution.
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / usb / UsbDebuggingActivity.java
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.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;
43
44 public class UsbDebuggingActivity extends AlertActivity
45                                   implements DialogInterface.OnClickListener {
46     private static final String TAG = "UsbDebuggingActivity";
47
48     private CheckBox mAlwaysAllow;
49     private UsbDisconnectedReceiver mDisconnectedReceiver;
50     private String mKey;
51
52     @Override
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);
57
58         super.onCreate(icicle);
59
60         if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) {
61             mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
62         }
63
64         Intent intent = getIntent();
65         String fingerprints = intent.getStringExtra("fingerprints");
66         mKey = intent.getStringExtra("key");
67
68         if (fingerprints == null || mKey == null) {
69             finish();
70             return;
71         }
72
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;
80
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));
86         ap.mView = checkbox;
87
88         setupAlert();
89
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() {
93
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();
102                     }
103                     return true;
104                 }
105                 return false;
106             }
107         };
108         mAlert.getButton(BUTTON_POSITIVE).setOnTouchListener(filterTouchListener);
109     }
110
111     private class UsbDisconnectedReceiver extends BroadcastReceiver {
112         private final Activity mActivity;
113         public UsbDisconnectedReceiver(Activity activity) {
114             mActivity = activity;
115         }
116
117         @Override
118         public void onReceive(Context content, Intent intent) {
119             String action = intent.getAction();
120             if (!UsbManager.ACTION_USB_STATE.equals(action)) {
121                 return;
122             }
123             boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
124             if (!connected) {
125                 mActivity.finish();
126             }
127         }
128     }
129
130     @Override
131     public void onStart() {
132         super.onStart();
133         IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
134         registerReceiver(mDisconnectedReceiver, filter);
135     }
136
137     @Override
138     protected void onStop() {
139         if (mDisconnectedReceiver != null) {
140             unregisterReceiver(mDisconnectedReceiver);
141         }
142         super.onStop();
143     }
144
145     @Override
146     public void onClick(DialogInterface dialog, int which) {
147         boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
148         boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
149         try {
150             IBinder b = ServiceManager.getService(USB_SERVICE);
151             IUsbManager service = IUsbManager.Stub.asInterface(b);
152             if (allow) {
153                 service.allowUsbDebugging(alwaysAllow, mKey);
154             } else {
155                 service.denyUsbDebugging();
156             }
157         } catch (Exception e) {
158             Log.e(TAG, "Unable to notify Usb service", e);
159         }
160         finish();
161     }
162 }