blob: e117969c5993ffff0855743024c30f42f6ff4ef8 [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;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.hardware.usb.IUsbManager;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040027import android.hardware.usb.UsbAccessory;
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070028import android.hardware.usb.UsbDevice;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040029import android.hardware.usb.UsbManager;
30import android.os.Bundle;
31import android.os.IBinder;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040032import android.os.ServiceManager;
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070033import android.os.UserHandle;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040034import android.util.Log;
35import android.view.LayoutInflater;
36import android.view.View;
37import android.widget.CheckBox;
38import android.widget.CompoundButton;
39import android.widget.TextView;
40
41import com.android.internal.app.AlertActivity;
42import com.android.internal.app.AlertController;
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040043import com.android.systemui.R;
44
45public class UsbConfirmActivity extends AlertActivity
46 implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
47
48 private static final String TAG = "UsbConfirmActivity";
49
50 private CheckBox mAlwaysUse;
51 private TextView mClearDefaultHint;
52 private UsbDevice mDevice;
53 private UsbAccessory mAccessory;
54 private ResolveInfo mResolveInfo;
55 private boolean mPermissionGranted;
56 private UsbDisconnectedReceiver mDisconnectedReceiver;
57
58 @Override
59 public void onCreate(Bundle icicle) {
60 super.onCreate(icicle);
61
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070062 Intent intent = getIntent();
63 mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040064 mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070065 mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo");
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040066
67 PackageManager packageManager = getPackageManager();
68 String appName = mResolveInfo.loadLabel(packageManager).toString();
69
70 final AlertController.AlertParams ap = mAlertParams;
71 ap.mIcon = mResolveInfo.loadIcon(packageManager);
72 ap.mTitle = appName;
73 if (mDevice == null) {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -080074 ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName,
75 mAccessory.getDescription());
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040076 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
77 } else {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -080078 ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName,
79 mDevice.getProductName());
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040080 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
81 }
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040082 ap.mPositiveButtonText = getString(android.R.string.ok);
83 ap.mNegativeButtonText = getString(android.R.string.cancel);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040084 ap.mPositiveButtonListener = this;
85 ap.mNegativeButtonListener = this;
86
87 // add "always use" checkbox
88 LayoutInflater inflater = (LayoutInflater)getSystemService(
89 Context.LAYOUT_INFLATER_SERVICE);
90 ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
91 mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040092 if (mDevice == null) {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -080093 mAlwaysUse.setText(getString(R.string.always_use_accessory, appName,
94 mAccessory.getDescription()));
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040095 } else {
Philip P. Moltmann5a633c62017-11-09 15:55:24 -080096 mAlwaysUse.setText(getString(R.string.always_use_device, appName,
97 mDevice.getProductName()));
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040098 }
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040099 mAlwaysUse.setOnCheckedChangeListener(this);
100 mClearDefaultHint = (TextView)ap.mView.findViewById(
101 com.android.internal.R.id.clearDefaultHint);
102 mClearDefaultHint.setVisibility(View.GONE);
103
104 setupAlert();
105
106 }
107
Mike Lockwood8f6dce42011-03-14 20:17:13 -0400108 @Override
109 protected void onDestroy() {
110 if (mDisconnectedReceiver != null) {
111 unregisterReceiver(mDisconnectedReceiver);
112 }
113 super.onDestroy();
114 }
115
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400116 public void onClick(DialogInterface dialog, int which) {
117 if (which == AlertDialog.BUTTON_POSITIVE) {
118 try {
119 IBinder b = ServiceManager.getService(USB_SERVICE);
120 IUsbManager service = IUsbManager.Stub.asInterface(b);
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700121 final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
122 final int userId = UserHandle.myUserId();
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400123 boolean alwaysUse = mAlwaysUse.isChecked();
124 Intent intent = null;
125
126 if (mDevice != null) {
127 intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
128 intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
129
130 // grant permission for the device
131 service.grantDevicePermission(mDevice, uid);
132 // set or clear default setting
133 if (alwaysUse) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700134 service.setDevicePackage(
135 mDevice, mResolveInfo.activityInfo.packageName, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400136 } else {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700137 service.setDevicePackage(mDevice, null, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400138 }
139 } else if (mAccessory != null) {
140 intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
141 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
142
143 // grant permission for the accessory
144 service.grantAccessoryPermission(mAccessory, uid);
145 // set or clear default setting
146 if (alwaysUse) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700147 service.setAccessoryPackage(
148 mAccessory, mResolveInfo.activityInfo.packageName, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400149 } else {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700150 service.setAccessoryPackage(mAccessory, null, userId);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400151 }
152 }
153
154 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155 intent.setComponent(
156 new ComponentName(mResolveInfo.activityInfo.packageName,
157 mResolveInfo.activityInfo.name));
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700158 startActivityAsUser(intent, new UserHandle(userId));
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400159 } catch (Exception e) {
160 Log.e(TAG, "Unable to start activity", e);
161 }
162 }
163 finish();
164 }
165
166 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
167 if (mClearDefaultHint == null) return;
168
169 if(isChecked) {
170 mClearDefaultHint.setVisibility(View.VISIBLE);
171 } else {
172 mClearDefaultHint.setVisibility(View.GONE);
173 }
174 }
175}