blob: 4d12cc9ebcff57137e820ce2e0f3839d1e63be0a [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;
Kenny Roota5964c02018-01-23 20:08:39 +090026import android.debug.IAdbManager;
Benoit Goby4e68bd42012-04-25 18:06:00 -070027import android.hardware.usb.UsbManager;
28import android.os.Bundle;
29import android.os.IBinder;
30import android.os.ServiceManager;
Benoit Goby29a4b722013-04-17 17:34:30 -070031import android.os.SystemProperties;
Beverly5cdff952017-08-24 09:33:57 -040032import android.util.EventLog;
Benoit Goby4e68bd42012-04-25 18:06:00 -070033import android.util.Log;
34import android.view.LayoutInflater;
Beverly830d54f2017-08-16 14:46:54 -040035import android.view.MotionEvent;
Benoit Goby4e68bd42012-04-25 18:06:00 -070036import android.view.View;
Beverly830d54f2017-08-16 14:46:54 -040037import android.view.Window;
38import android.view.WindowManager;
Benoit Goby4e68bd42012-04-25 18:06:00 -070039import android.widget.CheckBox;
Beverly830d54f2017-08-16 14:46:54 -040040import android.widget.Toast;
Benoit Goby4e68bd42012-04-25 18:06:00 -070041
42import com.android.internal.app.AlertActivity;
43import com.android.internal.app.AlertController;
Benoit Goby4e68bd42012-04-25 18:06:00 -070044import 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) {
Beverly830d54f2017-08-16 14:46:54 -040056 Window window = getWindow();
Philip P. Moltmann66ce2382018-10-09 13:46:11 -070057 window.addSystemFlags(WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
Beverly830d54f2017-08-16 14:46:54 -040058 window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
59
Benoit Goby4e68bd42012-04-25 18:06:00 -070060 super.onCreate(icicle);
61
Benoit Goby29a4b722013-04-17 17:34:30 -070062 if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) {
63 mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
64 }
65
Benoit Goby4e68bd42012-04-25 18:06:00 -070066 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);
Benoit Goby4e68bd42012-04-25 18:06:00 -070077 ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
Nick Kralevichf9d2a852019-01-11 14:28:39 -080078 ap.mPositiveButtonText = getString(R.string.usb_debugging_allow);
Benoit Goby4e68bd42012-04-25 18:06:00 -070079 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;
Tobias Thierer6783c302019-05-24 19:55:02 +010089 window.setCloseOnTouchOutside(false);
Benoit Goby4e68bd42012-04-25 18:06:00 -070090
91 setupAlert();
Beverly830d54f2017-08-16 14:46:54 -040092
93 // adding touch listener on affirmative button - checks if window is obscured
94 // if obscured, do not let user give permissions (could be tapjacking involved)
95 final View.OnTouchListener filterTouchListener = (View v, MotionEvent event) -> {
96 // Filter obscured touches by consuming them.
97 if (((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0)
98 || ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED) != 0)) {
99 if (event.getAction() == MotionEvent.ACTION_UP) {
Beverly5cdff952017-08-24 09:33:57 -0400100 EventLog.writeEvent(0x534e4554, "62187985"); // safety net logging
Beverly830d54f2017-08-16 14:46:54 -0400101 Toast.makeText(v.getContext(),
102 R.string.touch_filtered_warning,
103 Toast.LENGTH_SHORT).show();
104 }
105 return true;
106 }
107 return false;
108 };
109 mAlert.getButton(BUTTON_POSITIVE).setOnTouchListener(filterTouchListener);
110
111 }
112
113 @Override
114 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
115 super.onWindowAttributesChanged(params);
Benoit Goby4e68bd42012-04-25 18:06:00 -0700116 }
117
118 private class UsbDisconnectedReceiver extends BroadcastReceiver {
119 private final Activity mActivity;
120 public UsbDisconnectedReceiver(Activity activity) {
121 mActivity = activity;
122 }
123
124 @Override
125 public void onReceive(Context content, Intent intent) {
126 String action = intent.getAction();
127 if (!UsbManager.ACTION_USB_STATE.equals(action)) {
128 return;
129 }
130 boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
131 if (!connected) {
132 mActivity.finish();
133 }
134 }
135 }
136
137 @Override
138 public void onStart() {
139 super.onStart();
140 IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
141 registerReceiver(mDisconnectedReceiver, filter);
142 }
143
144 @Override
145 protected void onStop() {
146 if (mDisconnectedReceiver != null) {
147 unregisterReceiver(mDisconnectedReceiver);
148 }
149 super.onStop();
150 }
151
152 @Override
153 public void onClick(DialogInterface dialog, int which) {
154 boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
155 boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
156 try {
Kenny Roota5964c02018-01-23 20:08:39 +0900157 IBinder b = ServiceManager.getService(ADB_SERVICE);
158 IAdbManager service = IAdbManager.Stub.asInterface(b);
Benoit Goby4e68bd42012-04-25 18:06:00 -0700159 if (allow) {
Kenny Roota5964c02018-01-23 20:08:39 +0900160 service.allowDebugging(alwaysAllow, mKey);
Benoit Goby4e68bd42012-04-25 18:06:00 -0700161 } else {
Kenny Roota5964c02018-01-23 20:08:39 +0900162 service.denyDebugging();
Benoit Goby4e68bd42012-04-25 18:06:00 -0700163 }
164 } catch (Exception e) {
165 Log.e(TAG, "Unable to notify Usb service", e);
166 }
167 finish();
168 }
169}