Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| 20 | import android.app.PendingIntent; |
| 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.content.Intent; |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 24 | import android.content.PermissionChecker; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.hardware.usb.IUsbManager; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 28 | import android.hardware.usb.UsbAccessory; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 29 | import android.hardware.usb.UsbDevice; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 30 | import android.hardware.usb.UsbManager; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
| 39 | import android.widget.CheckBox; |
| 40 | import android.widget.CompoundButton; |
| 41 | import android.widget.TextView; |
| 42 | |
| 43 | import com.android.internal.app.AlertActivity; |
| 44 | import com.android.internal.app.AlertController; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 45 | import com.android.systemui.R; |
| 46 | |
| 47 | public class UsbPermissionActivity extends AlertActivity |
| 48 | implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { |
| 49 | |
| 50 | private static final String TAG = "UsbPermissionActivity"; |
| 51 | |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 52 | private CheckBox mAlwaysUse; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 53 | private TextView mClearDefaultHint; |
| 54 | private UsbDevice mDevice; |
| 55 | private UsbAccessory mAccessory; |
| 56 | private PendingIntent mPendingIntent; |
| 57 | private String mPackageName; |
| 58 | private int mUid; |
| 59 | private boolean mPermissionGranted; |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 60 | private UsbDisconnectedReceiver mDisconnectedReceiver; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 61 | |
| 62 | @Override |
| 63 | public void onCreate(Bundle icicle) { |
| 64 | super.onCreate(icicle); |
| 65 | |
lgcheng | 2992e1c | 2018-08-14 15:14:31 -0700 | [diff] [blame] | 66 | Intent intent = getIntent(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 67 | mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); |
| 68 | mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); |
| 69 | mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT); |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 70 | mUid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
lgcheng | 2992e1c | 2018-08-14 15:14:31 -0700 | [diff] [blame] | 71 | mPackageName = intent.getStringExtra(UsbManager.EXTRA_PACKAGE); |
| 72 | boolean canBeDefault = intent.getBooleanExtra(UsbManager.EXTRA_CAN_BE_DEFAULT, false); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 73 | |
| 74 | PackageManager packageManager = getPackageManager(); |
| 75 | ApplicationInfo aInfo; |
| 76 | try { |
| 77 | aInfo = packageManager.getApplicationInfo(mPackageName, 0); |
| 78 | } catch (PackageManager.NameNotFoundException e) { |
| 79 | Log.e(TAG, "unable to look up package name", e); |
| 80 | finish(); |
| 81 | return; |
| 82 | } |
| 83 | String appName = aInfo.loadLabel(packageManager).toString(); |
| 84 | |
| 85 | final AlertController.AlertParams ap = mAlertParams; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 86 | ap.mTitle = appName; |
Paul McLean | 6b9b18f | 2019-11-21 12:42:10 -0700 | [diff] [blame] | 87 | boolean useRecordWarning = false; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 88 | if (mDevice == null) { |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 89 | // Accessory Case |
| 90 | |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame] | 91 | ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName, |
| 92 | mAccessory.getDescription()); |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 93 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 94 | } else { |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 95 | boolean hasRecordPermission = |
Svet Ganov | 249810d | 2019-09-23 21:32:08 -0700 | [diff] [blame] | 96 | PermissionChecker.checkPermissionForPreflight( |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 97 | this, android.Manifest.permission.RECORD_AUDIO, -1, aInfo.uid, |
| 98 | mPackageName) |
| 99 | == android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 100 | boolean isAudioCaptureDevice = mDevice.getHasAudioCapture(); |
Paul McLean | 6b9b18f | 2019-11-21 12:42:10 -0700 | [diff] [blame] | 101 | useRecordWarning = isAudioCaptureDevice && !hasRecordPermission; |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 102 | |
| 103 | int strID = useRecordWarning |
| 104 | ? R.string.usb_device_permission_prompt_warn |
| 105 | : R.string.usb_device_permission_prompt; |
| 106 | ap.mMessage = getString(strID, appName, mDevice.getProductName()); |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 107 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); |
Paul McLean | 6b9b18f | 2019-11-21 12:42:10 -0700 | [diff] [blame] | 108 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 109 | } |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 110 | |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 111 | ap.mPositiveButtonText = getString(android.R.string.ok); |
| 112 | ap.mNegativeButtonText = getString(android.R.string.cancel); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 113 | ap.mPositiveButtonListener = this; |
| 114 | ap.mNegativeButtonListener = this; |
| 115 | |
Paul McLean | 6b9b18f | 2019-11-21 12:42:10 -0700 | [diff] [blame] | 116 | // Don't show the "always use" checkbox if the USB/Record warning is in effect |
| 117 | if (!useRecordWarning && canBeDefault && (mDevice != null || mAccessory != null)) { |
lgcheng | 2992e1c | 2018-08-14 15:14:31 -0700 | [diff] [blame] | 118 | // add "open when" checkbox |
| 119 | LayoutInflater inflater = (LayoutInflater) getSystemService( |
| 120 | Context.LAYOUT_INFLATER_SERVICE); |
| 121 | ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); |
| 122 | mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse); |
| 123 | if (mDevice == null) { |
| 124 | mAlwaysUse.setText(getString(R.string.always_use_accessory, appName, |
| 125 | mAccessory.getDescription())); |
| 126 | } else { |
| 127 | mAlwaysUse.setText(getString(R.string.always_use_device, appName, |
| 128 | mDevice.getProductName())); |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame] | 129 | } |
lgcheng | 2992e1c | 2018-08-14 15:14:31 -0700 | [diff] [blame] | 130 | mAlwaysUse.setOnCheckedChangeListener(this); |
| 131 | |
| 132 | mClearDefaultHint = (TextView)ap.mView.findViewById( |
| 133 | com.android.internal.R.id.clearDefaultHint); |
| 134 | mClearDefaultHint.setVisibility(View.GONE); |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 135 | } |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 136 | |
| 137 | setupAlert(); |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 140 | @Override |
| 141 | public void onDestroy() { |
| 142 | IBinder b = ServiceManager.getService(USB_SERVICE); |
| 143 | IUsbManager service = IUsbManager.Stub.asInterface(b); |
| 144 | |
| 145 | // send response via pending intent |
| 146 | Intent intent = new Intent(); |
| 147 | try { |
| 148 | if (mDevice != null) { |
| 149 | intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice); |
| 150 | if (mPermissionGranted) { |
| 151 | service.grantDevicePermission(mDevice, mUid); |
Philip P. Moltmann | 8653c7a | 2017-11-16 08:48:38 -0800 | [diff] [blame] | 152 | if (mAlwaysUse != null && mAlwaysUse.isChecked()) { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 153 | final int userId = UserHandle.getUserId(mUid); |
| 154 | service.setDevicePackage(mDevice, mPackageName, userId); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | if (mAccessory != null) { |
| 159 | intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory); |
| 160 | if (mPermissionGranted) { |
| 161 | service.grantAccessoryPermission(mAccessory, mUid); |
Philip P. Moltmann | 8653c7a | 2017-11-16 08:48:38 -0800 | [diff] [blame] | 162 | if (mAlwaysUse != null && mAlwaysUse.isChecked()) { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 163 | final int userId = UserHandle.getUserId(mUid); |
| 164 | service.setAccessoryPackage(mAccessory, mPackageName, userId); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted); |
| 169 | mPendingIntent.send(this, 0, intent); |
| 170 | } catch (PendingIntent.CanceledException e) { |
| 171 | Log.w(TAG, "PendingIntent was cancelled"); |
| 172 | } catch (RemoteException e) { |
| 173 | Log.e(TAG, "IUsbService connection failed", e); |
| 174 | } |
| 175 | |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 176 | if (mDisconnectedReceiver != null) { |
| 177 | unregisterReceiver(mDisconnectedReceiver); |
| 178 | } |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 179 | super.onDestroy(); |
| 180 | } |
| 181 | |
| 182 | public void onClick(DialogInterface dialog, int which) { |
| 183 | if (which == AlertDialog.BUTTON_POSITIVE) { |
| 184 | mPermissionGranted = true; |
| 185 | } |
| 186 | finish(); |
| 187 | } |
| 188 | |
| 189 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 190 | if (mClearDefaultHint == null) return; |
| 191 | |
| 192 | if(isChecked) { |
| 193 | mClearDefaultHint.setVisibility(View.VISIBLE); |
| 194 | } else { |
| 195 | mClearDefaultHint.setVisibility(View.GONE); |
| 196 | } |
| 197 | } |
| 198 | } |