Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 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; |
Kenny Root | a5964c0 | 2018-01-23 20:08:39 +0900 | [diff] [blame] | 26 | import android.debug.IAdbManager; |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 27 | import android.hardware.usb.UsbManager; |
| 28 | import android.os.Bundle; |
| 29 | import android.os.IBinder; |
| 30 | import android.os.ServiceManager; |
Benoit Goby | 29a4b72 | 2013-04-17 17:34:30 -0700 | [diff] [blame] | 31 | import android.os.SystemProperties; |
Beverly | 5cdff95 | 2017-08-24 09:33:57 -0400 | [diff] [blame] | 32 | import android.util.EventLog; |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | import android.view.LayoutInflater; |
Beverly | 830d54f | 2017-08-16 14:46:54 -0400 | [diff] [blame] | 35 | import android.view.MotionEvent; |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 36 | import android.view.View; |
Beverly | 830d54f | 2017-08-16 14:46:54 -0400 | [diff] [blame] | 37 | import android.view.Window; |
| 38 | import android.view.WindowManager; |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 39 | import android.widget.CheckBox; |
Beverly | 830d54f | 2017-08-16 14:46:54 -0400 | [diff] [blame] | 40 | import android.widget.Toast; |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 41 | |
| 42 | import com.android.internal.app.AlertActivity; |
| 43 | import com.android.internal.app.AlertController; |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 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 |
| 55 | public void onCreate(Bundle icicle) { |
Beverly | 830d54f | 2017-08-16 14:46:54 -0400 | [diff] [blame] | 56 | Window window = getWindow(); |
Philip P. Moltmann | 66ce238 | 2018-10-09 13:46:11 -0700 | [diff] [blame] | 57 | window.addSystemFlags(WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); |
Beverly | 830d54f | 2017-08-16 14:46:54 -0400 | [diff] [blame] | 58 | window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); |
| 59 | |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 60 | super.onCreate(icicle); |
| 61 | |
Benoit Goby | 29a4b72 | 2013-04-17 17:34:30 -0700 | [diff] [blame] | 62 | if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) { |
| 63 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this); |
| 64 | } |
| 65 | |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 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); |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 77 | ap.mMessage = getString(R.string.usb_debugging_message, fingerprints); |
Nick Kralevich | f9d2a85 | 2019-01-11 14:28:39 -0800 | [diff] [blame] | 78 | ap.mPositiveButtonText = getString(R.string.usb_debugging_allow); |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 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; |
Tobias Thierer | 6783c30 | 2019-05-24 19:55:02 +0100 | [diff] [blame] | 89 | window.setCloseOnTouchOutside(false); |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 90 | |
| 91 | setupAlert(); |
Beverly | 830d54f | 2017-08-16 14:46:54 -0400 | [diff] [blame] | 92 | |
| 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) { |
Beverly | 5cdff95 | 2017-08-24 09:33:57 -0400 | [diff] [blame] | 100 | EventLog.writeEvent(0x534e4554, "62187985"); // safety net logging |
Beverly | 830d54f | 2017-08-16 14:46:54 -0400 | [diff] [blame] | 101 | 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 Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 116 | } |
| 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 Root | a5964c0 | 2018-01-23 20:08:39 +0900 | [diff] [blame] | 157 | IBinder b = ServiceManager.getService(ADB_SERVICE); |
| 158 | IAdbManager service = IAdbManager.Stub.asInterface(b); |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 159 | if (allow) { |
Kenny Root | a5964c0 | 2018-01-23 20:08:39 +0900 | [diff] [blame] | 160 | service.allowDebugging(alwaysAllow, mKey); |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 161 | } else { |
Kenny Root | a5964c0 | 2018-01-23 20:08:39 +0900 | [diff] [blame] | 162 | service.denyDebugging(); |
Benoit Goby | 4e68bd4 | 2012-04-25 18:06:00 -0700 | [diff] [blame] | 163 | } |
| 164 | } catch (Exception e) { |
| 165 | Log.e(TAG, "Unable to notify Usb service", e); |
| 166 | } |
| 167 | finish(); |
| 168 | } |
| 169 | } |