blob: fd99ef389bec13b282105a73f31fb89ca81ee73c [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;
74 if (mDevice == null) {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -080075 ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName,
76 mAccessory.getDescription());
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040077 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
78 } else {
Paul McLean79ce5e02019-10-15 14:53:39 -060079 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 Lockwoodbce6f8f2011-03-13 17:26:52 -040093 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
94 }
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040095 ap.mPositiveButtonText = getString(android.R.string.ok);
96 ap.mNegativeButtonText = getString(android.R.string.cancel);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040097 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 Lockwoodad5f83e2011-03-15 16:04:12 -0400105 if (mDevice == null) {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -0800106 mAlwaysUse.setText(getString(R.string.always_use_accessory, appName,
107 mAccessory.getDescription()));
Mike Lockwoodad5f83e2011-03-15 16:04:12 -0400108 } else {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -0800109 mAlwaysUse.setText(getString(R.string.always_use_device, appName,
110 mDevice.getProductName()));
Mike Lockwoodad5f83e2011-03-15 16:04:12 -0400111 }
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400112 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 Lockwood8f6dce42011-03-14 20:17:13 -0400121 @Override
122 protected void onDestroy() {
123 if (mDisconnectedReceiver != null) {
124 unregisterReceiver(mDisconnectedReceiver);
125 }
126 super.onDestroy();
127 }
128
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400129 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 Sharkeyfc3f24b2012-10-01 21:45:52 -0700134 final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
135 final int userId = UserHandle.myUserId();
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400136 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 Sharkeyfc3f24b2012-10-01 21:45:52 -0700147 service.setDevicePackage(
148 mDevice, mResolveInfo.activityInfo.packageName, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400149 } else {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700150 service.setDevicePackage(mDevice, null, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400151 }
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 Sharkeyfc3f24b2012-10-01 21:45:52 -0700160 service.setAccessoryPackage(
161 mAccessory, mResolveInfo.activityInfo.packageName, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400162 } else {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700163 service.setAccessoryPackage(mAccessory, null, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400164 }
165 }
166
167 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
168 intent.setComponent(
169 new ComponentName(mResolveInfo.activityInfo.packageName,
170 mResolveInfo.activityInfo.name));
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700171 startActivityAsUser(intent, new UserHandle(userId));
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400172 } 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}