• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.debug.IAdbManager;
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.EventLog;
33 import android.util.Log;
34 import android.view.LayoutInflater;
35 import android.view.MotionEvent;
36 import android.view.View;
37 import android.view.Window;
38 import android.view.WindowManager;
39 import android.widget.CheckBox;
40 import android.widget.Toast;
41 
42 import com.android.internal.app.AlertActivity;
43 import com.android.internal.app.AlertController;
44 import com.android.systemui.R;
45 
46 public 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
onCreate(Bundle icicle)55     public void onCreate(Bundle icicle) {
56         Window window = getWindow();
57         window.addSystemFlags(WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
58         window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
59 
60         super.onCreate(icicle);
61 
62         if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) {
63             mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
64         }
65 
66         Intent intent = getIntent();
67         String fingerprints = intent.getStringExtra("fingerprints");
68         mKey = intent.getStringExtra("key");
69 
70         if (fingerprints == null || mKey == null) {
71             finish();
72             return;
73         }
74 
75         final AlertController.AlertParams ap = mAlertParams;
76         ap.mTitle = getString(R.string.usb_debugging_title);
77         ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
78         ap.mPositiveButtonText = getString(R.string.usb_debugging_allow);
79         ap.mNegativeButtonText = getString(android.R.string.cancel);
80         ap.mPositiveButtonListener = this;
81         ap.mNegativeButtonListener = this;
82 
83         // add "always allow" checkbox
84         LayoutInflater inflater = LayoutInflater.from(ap.mContext);
85         View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
86         mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
87         mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
88         ap.mView = checkbox;
89 
90         setupAlert();
91 
92         // adding touch listener on affirmative button - checks if window is obscured
93         // if obscured, do not let user give permissions (could be tapjacking involved)
94         final View.OnTouchListener filterTouchListener = (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                     EventLog.writeEvent(0x534e4554, "62187985"); // safety net logging
100                     Toast.makeText(v.getContext(),
101                             R.string.touch_filtered_warning,
102                             Toast.LENGTH_SHORT).show();
103                 }
104                 return true;
105             }
106             return false;
107         };
108         mAlert.getButton(BUTTON_POSITIVE).setOnTouchListener(filterTouchListener);
109 
110     }
111 
112     @Override
onWindowAttributesChanged(WindowManager.LayoutParams params)113     public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
114         super.onWindowAttributesChanged(params);
115     }
116 
117     private class UsbDisconnectedReceiver extends BroadcastReceiver {
118         private final Activity mActivity;
UsbDisconnectedReceiver(Activity activity)119         public UsbDisconnectedReceiver(Activity activity) {
120             mActivity = activity;
121         }
122 
123         @Override
onReceive(Context content, Intent intent)124         public void onReceive(Context content, Intent intent) {
125             String action = intent.getAction();
126             if (!UsbManager.ACTION_USB_STATE.equals(action)) {
127                 return;
128             }
129             boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
130             if (!connected) {
131                 mActivity.finish();
132             }
133         }
134     }
135 
136     @Override
onStart()137     public void onStart() {
138         super.onStart();
139         IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
140         registerReceiver(mDisconnectedReceiver, filter);
141     }
142 
143     @Override
onStop()144     protected void onStop() {
145         if (mDisconnectedReceiver != null) {
146             unregisterReceiver(mDisconnectedReceiver);
147         }
148         super.onStop();
149     }
150 
151     @Override
onClick(DialogInterface dialog, int which)152     public void onClick(DialogInterface dialog, int which) {
153         boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
154         boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
155         try {
156             IBinder b = ServiceManager.getService(ADB_SERVICE);
157             IAdbManager service = IAdbManager.Stub.asInterface(b);
158             if (allow) {
159                 service.allowDebugging(alwaysAllow, mKey);
160             } else {
161                 service.denyDebugging();
162             }
163         } catch (Exception e) {
164             Log.e(TAG, "Unable to notify Usb service", e);
165         }
166         finish();
167     }
168 }
169