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.AlertDialog; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.hardware.usb.IUsbManager; 27 import android.hardware.usb.UsbAccessory; 28 import android.hardware.usb.UsbDevice; 29 import android.hardware.usb.UsbManager; 30 import android.os.Bundle; 31 import android.os.IBinder; 32 import android.os.ServiceManager; 33 import android.os.UserHandle; 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; 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 onCreate(Bundle icicle)59 public void onCreate(Bundle icicle) { 60 super.onCreate(icicle); 61 62 Intent intent = getIntent(); 63 mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 64 mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); 65 mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo"); 66 67 PackageManager packageManager = getPackageManager(); 68 String appName = mResolveInfo.loadLabel(packageManager).toString(); 69 70 final AlertController.AlertParams ap = mAlertParams; 71 ap.mTitle = appName; 72 if (mDevice == null) { 73 ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName, 74 mAccessory.getDescription()); 75 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); 76 } else { 77 ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName, 78 mDevice.getProductName()); 79 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); 80 } 81 ap.mPositiveButtonText = getString(android.R.string.ok); 82 ap.mNegativeButtonText = getString(android.R.string.cancel); 83 ap.mPositiveButtonListener = this; 84 ap.mNegativeButtonListener = this; 85 86 // add "always use" checkbox 87 LayoutInflater inflater = (LayoutInflater)getSystemService( 88 Context.LAYOUT_INFLATER_SERVICE); 89 ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); 90 mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); 91 if (mDevice == null) { 92 mAlwaysUse.setText(getString(R.string.always_use_accessory, appName, 93 mAccessory.getDescription())); 94 } else { 95 mAlwaysUse.setText(getString(R.string.always_use_device, appName, 96 mDevice.getProductName())); 97 } 98 mAlwaysUse.setOnCheckedChangeListener(this); 99 mClearDefaultHint = (TextView)ap.mView.findViewById( 100 com.android.internal.R.id.clearDefaultHint); 101 mClearDefaultHint.setVisibility(View.GONE); 102 103 setupAlert(); 104 105 } 106 107 @Override onDestroy()108 protected void onDestroy() { 109 if (mDisconnectedReceiver != null) { 110 unregisterReceiver(mDisconnectedReceiver); 111 } 112 super.onDestroy(); 113 } 114 onClick(DialogInterface dialog, int which)115 public void onClick(DialogInterface dialog, int which) { 116 if (which == AlertDialog.BUTTON_POSITIVE) { 117 try { 118 IBinder b = ServiceManager.getService(USB_SERVICE); 119 IUsbManager service = IUsbManager.Stub.asInterface(b); 120 final int uid = mResolveInfo.activityInfo.applicationInfo.uid; 121 final int userId = UserHandle.myUserId(); 122 boolean alwaysUse = mAlwaysUse.isChecked(); 123 Intent intent = null; 124 125 if (mDevice != null) { 126 intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); 127 intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice); 128 129 // grant permission for the device 130 service.grantDevicePermission(mDevice, uid); 131 // set or clear default setting 132 if (alwaysUse) { 133 service.setDevicePackage( 134 mDevice, mResolveInfo.activityInfo.packageName, userId); 135 } else { 136 service.setDevicePackage(mDevice, null, userId); 137 } 138 } else if (mAccessory != null) { 139 intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); 140 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory); 141 142 // grant permission for the accessory 143 service.grantAccessoryPermission(mAccessory, uid); 144 // set or clear default setting 145 if (alwaysUse) { 146 service.setAccessoryPackage( 147 mAccessory, mResolveInfo.activityInfo.packageName, userId); 148 } else { 149 service.setAccessoryPackage(mAccessory, null, userId); 150 } 151 } 152 153 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 154 intent.setComponent( 155 new ComponentName(mResolveInfo.activityInfo.packageName, 156 mResolveInfo.activityInfo.name)); 157 startActivityAsUser(intent, new UserHandle(userId)); 158 } catch (Exception e) { 159 Log.e(TAG, "Unable to start activity", e); 160 } 161 } 162 finish(); 163 } 164 onCheckedChanged(CompoundButton buttonView, boolean isChecked)165 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 166 if (mClearDefaultHint == null) return; 167 168 if(isChecked) { 169 mClearDefaultHint.setVisibility(View.VISIBLE); 170 } else { 171 mClearDefaultHint.setVisibility(View.GONE); 172 } 173 } 174 } 175