Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [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 | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.content.Intent; |
Paul McLean | 79ce5e0 | 2019-10-15 14:53:39 -0600 | [diff] [blame] | 24 | import android.content.PermissionChecker; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ResolveInfo; |
| 27 | import android.hardware.usb.IUsbManager; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 28 | import android.hardware.usb.UsbAccessory; |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 29 | import android.hardware.usb.UsbDevice; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 30 | import android.hardware.usb.UsbManager; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.IBinder; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 33 | import android.os.ServiceManager; |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 34 | import android.os.UserHandle; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.LayoutInflater; |
| 37 | import android.view.View; |
| 38 | import android.widget.CheckBox; |
| 39 | import android.widget.CompoundButton; |
| 40 | import android.widget.TextView; |
| 41 | |
| 42 | import com.android.internal.app.AlertActivity; |
| 43 | import com.android.internal.app.AlertController; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 44 | import com.android.systemui.R; |
| 45 | |
| 46 | public class UsbConfirmActivity extends AlertActivity |
| 47 | implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { |
| 48 | |
| 49 | private static final String TAG = "UsbConfirmActivity"; |
| 50 | |
| 51 | private CheckBox mAlwaysUse; |
| 52 | private TextView mClearDefaultHint; |
| 53 | private UsbDevice mDevice; |
| 54 | private UsbAccessory mAccessory; |
| 55 | private ResolveInfo mResolveInfo; |
| 56 | private boolean mPermissionGranted; |
| 57 | private UsbDisconnectedReceiver mDisconnectedReceiver; |
| 58 | |
| 59 | @Override |
| 60 | public void onCreate(Bundle icicle) { |
| 61 | super.onCreate(icicle); |
| 62 | |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 63 | Intent intent = getIntent(); |
| 64 | mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 65 | mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 66 | mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo"); |
Paul McLean | 79ce5e0 | 2019-10-15 14:53:39 -0600 | [diff] [blame] | 67 | String packageName = intent.getStringExtra(UsbManager.EXTRA_PACKAGE); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 68 | |
| 69 | PackageManager packageManager = getPackageManager(); |
| 70 | String appName = mResolveInfo.loadLabel(packageManager).toString(); |
| 71 | |
| 72 | final AlertController.AlertParams ap = mAlertParams; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 73 | ap.mTitle = appName; |
| 74 | if (mDevice == null) { |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame] | 75 | ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName, |
| 76 | mAccessory.getDescription()); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 77 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); |
| 78 | } else { |
Paul McLean | 79ce5e0 | 2019-10-15 14:53:39 -0600 | [diff] [blame] | 79 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 80 | boolean hasRecordPermission = |
| 81 | PermissionChecker.checkPermissionForPreflight( |
| 82 | this, android.Manifest.permission.RECORD_AUDIO, -1, uid, |
| 83 | packageName) |
| 84 | == android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 85 | boolean isAudioCaptureDevice = mDevice.getHasAudioCapture(); |
| 86 | boolean useRecordWarning = isAudioCaptureDevice && !hasRecordPermission; |
| 87 | |
| 88 | int strID = useRecordWarning |
| 89 | ? R.string.usb_device_confirm_prompt_warn |
| 90 | : R.string.usb_device_confirm_prompt; |
| 91 | |
| 92 | ap.mMessage = getString(strID, appName, mDevice.getProductName()); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 93 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); |
| 94 | } |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 95 | ap.mPositiveButtonText = getString(android.R.string.ok); |
| 96 | ap.mNegativeButtonText = getString(android.R.string.cancel); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 97 | ap.mPositiveButtonListener = this; |
| 98 | ap.mNegativeButtonListener = this; |
| 99 | |
| 100 | // add "always use" checkbox |
| 101 | LayoutInflater inflater = (LayoutInflater)getSystemService( |
| 102 | Context.LAYOUT_INFLATER_SERVICE); |
| 103 | ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); |
| 104 | mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 105 | if (mDevice == null) { |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame] | 106 | mAlwaysUse.setText(getString(R.string.always_use_accessory, appName, |
| 107 | mAccessory.getDescription())); |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 108 | } else { |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame] | 109 | mAlwaysUse.setText(getString(R.string.always_use_device, appName, |
| 110 | mDevice.getProductName())); |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 111 | } |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 112 | mAlwaysUse.setOnCheckedChangeListener(this); |
| 113 | mClearDefaultHint = (TextView)ap.mView.findViewById( |
| 114 | com.android.internal.R.id.clearDefaultHint); |
| 115 | mClearDefaultHint.setVisibility(View.GONE); |
| 116 | |
| 117 | setupAlert(); |
| 118 | |
| 119 | } |
| 120 | |
Mike Lockwood | 8f6dce4 | 2011-03-14 20:17:13 -0400 | [diff] [blame] | 121 | @Override |
| 122 | protected void onDestroy() { |
| 123 | if (mDisconnectedReceiver != null) { |
| 124 | unregisterReceiver(mDisconnectedReceiver); |
| 125 | } |
| 126 | super.onDestroy(); |
| 127 | } |
| 128 | |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 129 | public void onClick(DialogInterface dialog, int which) { |
| 130 | if (which == AlertDialog.BUTTON_POSITIVE) { |
| 131 | try { |
| 132 | IBinder b = ServiceManager.getService(USB_SERVICE); |
| 133 | IUsbManager service = IUsbManager.Stub.asInterface(b); |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 134 | final int uid = mResolveInfo.activityInfo.applicationInfo.uid; |
| 135 | final int userId = UserHandle.myUserId(); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 136 | boolean alwaysUse = mAlwaysUse.isChecked(); |
| 137 | Intent intent = null; |
| 138 | |
| 139 | if (mDevice != null) { |
| 140 | intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
| 141 | intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice); |
| 142 | |
| 143 | // grant permission for the device |
| 144 | service.grantDevicePermission(mDevice, uid); |
| 145 | // set or clear default setting |
| 146 | if (alwaysUse) { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 147 | service.setDevicePackage( |
| 148 | mDevice, mResolveInfo.activityInfo.packageName, userId); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 149 | } else { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 150 | service.setDevicePackage(mDevice, null, userId); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 151 | } |
| 152 | } else if (mAccessory != null) { |
| 153 | intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); |
| 154 | intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory); |
| 155 | |
| 156 | // grant permission for the accessory |
| 157 | service.grantAccessoryPermission(mAccessory, uid); |
| 158 | // set or clear default setting |
| 159 | if (alwaysUse) { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 160 | service.setAccessoryPackage( |
| 161 | mAccessory, mResolveInfo.activityInfo.packageName, userId); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 162 | } else { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 163 | service.setAccessoryPackage(mAccessory, null, userId); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 168 | intent.setComponent( |
| 169 | new ComponentName(mResolveInfo.activityInfo.packageName, |
| 170 | mResolveInfo.activityInfo.name)); |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 171 | startActivityAsUser(intent, new UserHandle(userId)); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 172 | } catch (Exception e) { |
| 173 | Log.e(TAG, "Unable to start activity", e); |
| 174 | } |
| 175 | } |
| 176 | finish(); |
| 177 | } |
| 178 | |
| 179 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 180 | if (mClearDefaultHint == null) return; |
| 181 | |
| 182 | if(isChecked) { |
| 183 | mClearDefaultHint.setVisibility(View.VISIBLE); |
| 184 | } else { |
| 185 | mClearDefaultHint.setVisibility(View.GONE); |
| 186 | } |
| 187 | } |
| 188 | } |