Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [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 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.hardware.usb.IUsbManager; |
| 28 | import android.hardware.usb.UsbDevice; |
| 29 | import android.hardware.usb.UsbAccessory; |
| 30 | import android.hardware.usb.UsbManager; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
| 39 | import android.widget.CheckBox; |
| 40 | import android.widget.CompoundButton; |
| 41 | import android.widget.TextView; |
| 42 | |
| 43 | import com.android.internal.app.AlertActivity; |
| 44 | import com.android.internal.app.AlertController; |
| 45 | |
| 46 | import com.android.systemui.R; |
| 47 | |
| 48 | public class UsbPermissionActivity extends AlertActivity |
| 49 | implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { |
| 50 | |
| 51 | private static final String TAG = "UsbPermissionActivity"; |
| 52 | |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 53 | private CheckBox mAlwaysUse; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 54 | private TextView mClearDefaultHint; |
| 55 | private UsbDevice mDevice; |
| 56 | private UsbAccessory mAccessory; |
| 57 | private PendingIntent mPendingIntent; |
| 58 | private String mPackageName; |
| 59 | private int mUid; |
| 60 | private boolean mPermissionGranted; |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 61 | private UsbDisconnectedReceiver mDisconnectedReceiver; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 62 | |
| 63 | @Override |
| 64 | public void onCreate(Bundle icicle) { |
| 65 | super.onCreate(icicle); |
| 66 | |
| 67 | Intent intent = getIntent(); |
| 68 | mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); |
| 69 | mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); |
| 70 | mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT); |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 71 | mUid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 72 | mPackageName = intent.getStringExtra("package"); |
| 73 | |
| 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; |
| 86 | ap.mIcon = aInfo.loadIcon(packageManager); |
| 87 | ap.mTitle = appName; |
| 88 | if (mDevice == null) { |
| 89 | ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName); |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 90 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 91 | } else { |
| 92 | ap.mMessage = getString(R.string.usb_device_permission_prompt, appName); |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 93 | mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 94 | } |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 95 | ap.mPositiveButtonText = getString(android.R.string.ok); |
| 96 | ap.mNegativeButtonText = getString(android.R.string.cancel); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 97 | 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); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 104 | mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); |
Mike Lockwood | ad5f83e | 2011-03-15 16:04:12 -0400 | [diff] [blame] | 105 | if (mDevice == null) { |
| 106 | mAlwaysUse.setText(R.string.always_use_accessory); |
| 107 | } else { |
| 108 | mAlwaysUse.setText(R.string.always_use_device); |
| 109 | } |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 110 | mAlwaysUse.setOnCheckedChangeListener(this); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 111 | mClearDefaultHint = (TextView)ap.mView.findViewById( |
| 112 | com.android.internal.R.id.clearDefaultHint); |
| 113 | mClearDefaultHint.setVisibility(View.GONE); |
| 114 | |
| 115 | setupAlert(); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onDestroy() { |
| 121 | IBinder b = ServiceManager.getService(USB_SERVICE); |
| 122 | IUsbManager service = IUsbManager.Stub.asInterface(b); |
| 123 | |
| 124 | // send response via pending intent |
| 125 | Intent intent = new Intent(); |
| 126 | try { |
| 127 | if (mDevice != null) { |
| 128 | intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice); |
| 129 | if (mPermissionGranted) { |
| 130 | service.grantDevicePermission(mDevice, mUid); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 131 | if (mAlwaysUse.isChecked()) { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 132 | final int userId = UserHandle.getUserId(mUid); |
| 133 | service.setDevicePackage(mDevice, mPackageName, userId); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | if (mAccessory != null) { |
| 138 | intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory); |
| 139 | if (mPermissionGranted) { |
| 140 | service.grantAccessoryPermission(mAccessory, mUid); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 141 | if (mAlwaysUse.isChecked()) { |
Jeff Sharkey | fc3f24b | 2012-10-01 21:45:52 -0700 | [diff] [blame] | 142 | final int userId = UserHandle.getUserId(mUid); |
| 143 | service.setAccessoryPackage(mAccessory, mPackageName, userId); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted); |
| 148 | mPendingIntent.send(this, 0, intent); |
| 149 | } catch (PendingIntent.CanceledException e) { |
| 150 | Log.w(TAG, "PendingIntent was cancelled"); |
| 151 | } catch (RemoteException e) { |
| 152 | Log.e(TAG, "IUsbService connection failed", e); |
| 153 | } |
| 154 | |
Mike Lockwood | d591357 | 2011-03-08 22:47:08 -0500 | [diff] [blame] | 155 | if (mDisconnectedReceiver != null) { |
| 156 | unregisterReceiver(mDisconnectedReceiver); |
| 157 | } |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 158 | super.onDestroy(); |
| 159 | } |
| 160 | |
| 161 | public void onClick(DialogInterface dialog, int which) { |
| 162 | if (which == AlertDialog.BUTTON_POSITIVE) { |
| 163 | mPermissionGranted = true; |
| 164 | } |
| 165 | finish(); |
| 166 | } |
| 167 | |
| 168 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 169 | if (mClearDefaultHint == null) return; |
| 170 | |
| 171 | if(isChecked) { |
| 172 | mClearDefaultHint.setVisibility(View.VISIBLE); |
| 173 | } else { |
| 174 | mClearDefaultHint.setVisibility(View.GONE); |
| 175 | } |
| 176 | } |
| 177 | } |