blob: 98b4209ede0014554ba966e5c2589aa8889c8f7e [file] [log] [blame]
Mike Lockwood3a68b832011-03-08 10:08:59 -05001/*
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
17package com.android.systemui.usb;
18
Mike Lockwood3a68b832011-03-08 10:08:59 -050019import android.app.AlertDialog;
20import android.app.PendingIntent;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
Paul McLeand3364532019-09-23 08:28:41 -060024import android.content.PermissionChecker;
Mike Lockwood3a68b832011-03-08 10:08:59 -050025import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
27import android.hardware.usb.IUsbManager;
Mike Lockwood3a68b832011-03-08 10:08:59 -050028import android.hardware.usb.UsbAccessory;
John Spurlockde84f0e2013-06-12 12:41:00 -040029import android.hardware.usb.UsbDevice;
Mike Lockwood3a68b832011-03-08 10:08:59 -050030import android.hardware.usb.UsbManager;
31import android.os.Bundle;
32import android.os.IBinder;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070035import android.os.UserHandle;
Mike Lockwood3a68b832011-03-08 10:08:59 -050036import android.util.Log;
37import android.view.LayoutInflater;
38import android.view.View;
39import android.widget.CheckBox;
40import android.widget.CompoundButton;
41import android.widget.TextView;
42
43import com.android.internal.app.AlertActivity;
44import com.android.internal.app.AlertController;
Mike Lockwood3a68b832011-03-08 10:08:59 -050045import com.android.systemui.R;
46
47public class UsbPermissionActivity extends AlertActivity
48 implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
49
50 private static final String TAG = "UsbPermissionActivity";
51
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040052 private CheckBox mAlwaysUse;
Mike Lockwood3a68b832011-03-08 10:08:59 -050053 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 Lockwoodd5913572011-03-08 22:47:08 -050060 private UsbDisconnectedReceiver mDisconnectedReceiver;
Mike Lockwood3a68b832011-03-08 10:08:59 -050061
62 @Override
63 public void onCreate(Bundle icicle) {
64 super.onCreate(icicle);
65
lgcheng2992e1c2018-08-14 15:14:31 -070066 Intent intent = getIntent();
Mike Lockwood3a68b832011-03-08 10:08:59 -050067 mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
68 mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
69 mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT);
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070070 mUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
lgcheng2992e1c2018-08-14 15:14:31 -070071 mPackageName = intent.getStringExtra(UsbManager.EXTRA_PACKAGE);
72 boolean canBeDefault = intent.getBooleanExtra(UsbManager.EXTRA_CAN_BE_DEFAULT, false);
Mike Lockwood3a68b832011-03-08 10:08:59 -050073
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 Lockwood3a68b832011-03-08 10:08:59 -050086 ap.mTitle = appName;
Paul McLean6b9b18f2019-11-21 12:42:10 -070087 boolean useRecordWarning = false;
Mike Lockwood3a68b832011-03-08 10:08:59 -050088 if (mDevice == null) {
Paul McLeand3364532019-09-23 08:28:41 -060089 // Accessory Case
90
Philip P. Moltmann5a633c62017-11-09 15:55:24 -080091 ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName,
92 mAccessory.getDescription());
Mike Lockwoodd5913572011-03-08 22:47:08 -050093 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
Mike Lockwood3a68b832011-03-08 10:08:59 -050094 } else {
Paul McLeand3364532019-09-23 08:28:41 -060095 boolean hasRecordPermission =
Svet Ganov249810d2019-09-23 21:32:08 -070096 PermissionChecker.checkPermissionForPreflight(
Paul McLeand3364532019-09-23 08:28:41 -060097 this, android.Manifest.permission.RECORD_AUDIO, -1, aInfo.uid,
98 mPackageName)
99 == android.content.pm.PackageManager.PERMISSION_GRANTED;
100 boolean isAudioCaptureDevice = mDevice.getHasAudioCapture();
Paul McLean6b9b18f2019-11-21 12:42:10 -0700101 useRecordWarning = isAudioCaptureDevice && !hasRecordPermission;
Paul McLeand3364532019-09-23 08:28:41 -0600102
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 Lockwoodd5913572011-03-08 22:47:08 -0500107 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
Paul McLean6b9b18f2019-11-21 12:42:10 -0700108
Mike Lockwood3a68b832011-03-08 10:08:59 -0500109 }
Paul McLeand3364532019-09-23 08:28:41 -0600110
Mike Lockwoodad5f83e2011-03-15 16:04:12 -0400111 ap.mPositiveButtonText = getString(android.R.string.ok);
112 ap.mNegativeButtonText = getString(android.R.string.cancel);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500113 ap.mPositiveButtonListener = this;
114 ap.mNegativeButtonListener = this;
115
Paul McLean6b9b18f2019-11-21 12:42:10 -0700116 // Don't show the "always use" checkbox if the USB/Record warning is in effect
117 if (!useRecordWarning && canBeDefault && (mDevice != null || mAccessory != null)) {
lgcheng2992e1c2018-08-14 15:14:31 -0700118 // 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. Moltmann5a633c62017-11-09 15:55:24 -0800129 }
lgcheng2992e1c2018-08-14 15:14:31 -0700130 mAlwaysUse.setOnCheckedChangeListener(this);
131
132 mClearDefaultHint = (TextView)ap.mView.findViewById(
133 com.android.internal.R.id.clearDefaultHint);
134 mClearDefaultHint.setVisibility(View.GONE);
Mike Lockwoodad5f83e2011-03-15 16:04:12 -0400135 }
Mike Lockwood3a68b832011-03-08 10:08:59 -0500136
137 setupAlert();
Philip P. Moltmann5a633c62017-11-09 15:55:24 -0800138 }
139
Mike Lockwood3a68b832011-03-08 10:08:59 -0500140 @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. Moltmann8653c7a2017-11-16 08:48:38 -0800152 if (mAlwaysUse != null && mAlwaysUse.isChecked()) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700153 final int userId = UserHandle.getUserId(mUid);
154 service.setDevicePackage(mDevice, mPackageName, userId);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500155 }
156 }
157 }
158 if (mAccessory != null) {
159 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
160 if (mPermissionGranted) {
161 service.grantAccessoryPermission(mAccessory, mUid);
Philip P. Moltmann8653c7a2017-11-16 08:48:38 -0800162 if (mAlwaysUse != null && mAlwaysUse.isChecked()) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700163 final int userId = UserHandle.getUserId(mUid);
164 service.setAccessoryPackage(mAccessory, mPackageName, userId);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500165 }
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 Lockwoodd5913572011-03-08 22:47:08 -0500176 if (mDisconnectedReceiver != null) {
177 unregisterReceiver(mDisconnectedReceiver);
178 }
Mike Lockwood3a68b832011-03-08 10:08:59 -0500179 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}