blob: 286b7c049fc73bd3e7de787c6d509bcf594bbb85 [file] [log] [blame]
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -04001/*
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 Lockwoodbce6f8f2011-03-13 17:26:52 -040019import android.app.AlertDialog;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
Paul McLean79ce5e02019-10-15 14:53:39 -060024import android.content.PermissionChecker;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040025import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.hardware.usb.IUsbManager;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040028import android.hardware.usb.UsbAccessory;
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070029import android.hardware.usb.UsbDevice;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040030import android.hardware.usb.UsbManager;
31import android.os.Bundle;
32import android.os.IBinder;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040033import android.os.ServiceManager;
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070034import android.os.UserHandle;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040035import android.util.Log;
36import android.view.LayoutInflater;
37import android.view.View;
38import android.widget.CheckBox;
39import android.widget.CompoundButton;
40import android.widget.TextView;
41
42import com.android.internal.app.AlertActivity;
43import com.android.internal.app.AlertController;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040044import com.android.systemui.R;
45
46public 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 Sharkeyfc3f24b2012-10-01 21:45:52 -070063 Intent intent = getIntent();
64 mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040065 mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070066 mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo");
Paul McLean79ce5e02019-10-15 14:53:39 -060067 String packageName = intent.getStringExtra(UsbManager.EXTRA_PACKAGE);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040068
69 PackageManager packageManager = getPackageManager();
70 String appName = mResolveInfo.loadLabel(packageManager).toString();
71
72 final AlertController.AlertParams ap = mAlertParams;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040073 ap.mTitle = appName;
Paul McLeanc90234a2019-11-25 14:30:17 -070074 boolean useRecordWarning = false;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040075 if (mDevice == null) {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -080076 ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName,
77 mAccessory.getDescription());
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040078 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
79 } else {
Paul McLean79ce5e02019-10-15 14:53:39 -060080 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
81 boolean hasRecordPermission =
82 PermissionChecker.checkPermissionForPreflight(
83 this, android.Manifest.permission.RECORD_AUDIO, -1, uid,
84 packageName)
85 == android.content.pm.PackageManager.PERMISSION_GRANTED;
86 boolean isAudioCaptureDevice = mDevice.getHasAudioCapture();
Paul McLeanc90234a2019-11-25 14:30:17 -070087 useRecordWarning = isAudioCaptureDevice && !hasRecordPermission;
Paul McLean79ce5e02019-10-15 14:53:39 -060088
89 int strID = useRecordWarning
90 ? R.string.usb_device_confirm_prompt_warn
91 : R.string.usb_device_confirm_prompt;
92
93 ap.mMessage = getString(strID, appName, mDevice.getProductName());
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040094 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
95 }
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040096 ap.mPositiveButtonText = getString(android.R.string.ok);
97 ap.mNegativeButtonText = getString(android.R.string.cancel);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040098 ap.mPositiveButtonListener = this;
99 ap.mNegativeButtonListener = this;
100
101 // add "always use" checkbox
Paul McLeanc90234a2019-11-25 14:30:17 -0700102 if (!useRecordWarning) {
103 LayoutInflater inflater = (LayoutInflater) getSystemService(
104 Context.LAYOUT_INFLATER_SERVICE);
105 ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
106 mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
107 if (mDevice == null) {
108 mAlwaysUse.setText(getString(R.string.always_use_accessory, appName,
109 mAccessory.getDescription()));
110 } else {
111 mAlwaysUse.setText(getString(R.string.always_use_device, appName,
112 mDevice.getProductName()));
113 }
114 mAlwaysUse.setOnCheckedChangeListener(this);
115 mClearDefaultHint = (TextView) ap.mView.findViewById(
116 com.android.internal.R.id.clearDefaultHint);
117 mClearDefaultHint.setVisibility(View.GONE);
Mike Lockwoodad5f83e2011-03-15 16:04:12 -0400118 }
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400119 setupAlert();
120
121 }
122
Mike Lockwood8f6dce42011-03-14 20:17:13 -0400123 @Override
124 protected void onDestroy() {
125 if (mDisconnectedReceiver != null) {
126 unregisterReceiver(mDisconnectedReceiver);
127 }
128 super.onDestroy();
129 }
130
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400131 public void onClick(DialogInterface dialog, int which) {
132 if (which == AlertDialog.BUTTON_POSITIVE) {
133 try {
134 IBinder b = ServiceManager.getService(USB_SERVICE);
135 IUsbManager service = IUsbManager.Stub.asInterface(b);
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700136 final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
137 final int userId = UserHandle.myUserId();
Paul McLeanc90234a2019-11-25 14:30:17 -0700138 boolean alwaysUse = mAlwaysUse != null ? mAlwaysUse.isChecked() : false;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400139 Intent intent = null;
140
141 if (mDevice != null) {
142 intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
143 intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
144
145 // grant permission for the device
146 service.grantDevicePermission(mDevice, uid);
147 // set or clear default setting
148 if (alwaysUse) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700149 service.setDevicePackage(
150 mDevice, mResolveInfo.activityInfo.packageName, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400151 } else {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700152 service.setDevicePackage(mDevice, null, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400153 }
154 } else if (mAccessory != null) {
155 intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
156 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
157
158 // grant permission for the accessory
159 service.grantAccessoryPermission(mAccessory, uid);
160 // set or clear default setting
161 if (alwaysUse) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700162 service.setAccessoryPackage(
163 mAccessory, mResolveInfo.activityInfo.packageName, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400164 } else {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700165 service.setAccessoryPackage(mAccessory, null, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400166 }
167 }
168
169 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
170 intent.setComponent(
171 new ComponentName(mResolveInfo.activityInfo.packageName,
172 mResolveInfo.activityInfo.name));
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700173 startActivityAsUser(intent, new UserHandle(userId));
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400174 } catch (Exception e) {
175 Log.e(TAG, "Unable to start activity", e);
176 }
177 }
178 finish();
179 }
180
181 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
182 if (mClearDefaultHint == null) return;
183
184 if(isChecked) {
185 mClearDefaultHint.setVisibility(View.VISIBLE);
186 } else {
187 mClearDefaultHint.setVisibility(View.GONE);
188 }
189 }
190}