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; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
| 26 | import android.hardware.usb.IUsbManager; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 27 | import android.hardware.usb.UsbAccessory; |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 28 | import android.hardware.usb.UsbDevice; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 29 | import android.hardware.usb.UsbManager; |
| 30 | import android.os.Bundle; |
| 31 | import android.os.IBinder; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 32 | import android.os.ServiceManager; |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | import android.view.LayoutInflater; |
| 36 | import android.view.View; |
| 37 | import android.widget.CheckBox; |
| 38 | import android.widget.CompoundButton; |
| 39 | import android.widget.TextView; |
| 40 | |
| 41 | import com.android.internal.app.AlertActivity; |
| 42 | import com.android.internal.app.AlertController; |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 43 | import com.android.systemui.R; |
| 44 | |
| 45 | public 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 Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 62 | Intent intent = getIntent(); |
| 63 | mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 64 | mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 65 | mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo"); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 66 | |
| 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. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame^] | 74 | ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName, |
| 75 | mAccessory.getDescription()); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 76 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); |
| 77 | } else { |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame^] | 78 | ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName, |
| 79 | mDevice.getProductName()); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 80 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); |
| 81 | } |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 82 | ap.mPositiveButtonText = getString(android.R.string.ok); |
| 83 | ap.mNegativeButtonText = getString(android.R.string.cancel); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 84 | 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 Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 92 | if (mDevice == null) { |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame^] | 93 | mAlwaysUse.setText(getString(R.string.always_use_accessory, appName, |
| 94 | mAccessory.getDescription())); |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 95 | } else { |
Philip P. Moltmann | 5a633c6 | 2017-11-09 15:55:24 -0800 | [diff] [blame^] | 96 | mAlwaysUse.setText(getString(R.string.always_use_device, appName, |
| 97 | mDevice.getProductName())); |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 98 | } |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 99 | 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 Lockwood | 8f6dce4 | 2011-03-14 20:17:13 -0400 | [diff] [blame] | 108 | @Override |
| 109 | protected void onDestroy() { |
| 110 | if (mDisconnectedReceiver != null) { |
| 111 | unregisterReceiver(mDisconnectedReceiver); |
| 112 | } |
| 113 | super.onDestroy(); |
| 114 | } |
| 115 | |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 116 | 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 Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 121 | final int uid = mResolveInfo.activityInfo.applicationInfo.uid; |
| 122 | final int userId = UserHandle.myUserId(); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 123 | 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 Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 134 | service.setDevicePackage( |
| 135 | mDevice, mResolveInfo.activityInfo.packageName, userId); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 136 | } else { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 137 | service.setDevicePackage(mDevice, null, userId); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 138 | } |
| 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 Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 147 | service.setAccessoryPackage( |
| 148 | mAccessory, 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.setAccessoryPackage(mAccessory, null, userId); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 155 | intent.setComponent( |
| 156 | new ComponentName(mResolveInfo.activityInfo.packageName, |
| 157 | mResolveInfo.activityInfo.name)); |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 158 | startActivityAsUser(intent, new UserHandle(userId)); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 159 | } 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 | } |