UsbConfirmActivity.java revision bce6f8f249ff2b65df9ed790cc460053ab8eccec
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 17package com.android.systemui.usb; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.content.ComponentName; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.Intent; 25import android.content.pm.ApplicationInfo; 26import android.content.pm.PackageManager; 27import android.content.pm.ResolveInfo; 28import android.hardware.usb.IUsbManager; 29import android.hardware.usb.UsbDevice; 30import android.hardware.usb.UsbAccessory; 31import android.hardware.usb.UsbManager; 32import android.os.Bundle; 33import android.os.IBinder; 34import android.os.RemoteException; 35import android.os.ServiceManager; 36import android.util.Log; 37import android.view.LayoutInflater; 38import android.view.View; 39import android.widget.CheckBox; 40import android.widget.CompoundButton; 41import android.widget.TextView; 42 43import com.android.internal.app.AlertActivity; 44import com.android.internal.app.AlertController; 45 46import com.android.systemui.R; 47 48public class UsbConfirmActivity extends AlertActivity 49 implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { 50 51 private static final String TAG = "UsbConfirmActivity"; 52 53 private CheckBox mAlwaysUse; 54 private TextView mClearDefaultHint; 55 private UsbDevice mDevice; 56 private UsbAccessory mAccessory; 57 private ResolveInfo mResolveInfo; 58 private boolean mPermissionGranted; 59 private UsbDisconnectedReceiver mDisconnectedReceiver; 60 61 @Override 62 public void onCreate(Bundle icicle) { 63 super.onCreate(icicle); 64 65 Intent intent = getIntent(); 66 mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 67 mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); 68 mResolveInfo = (ResolveInfo)intent.getParcelableExtra("rinfo"); 69 70 PackageManager packageManager = getPackageManager(); 71 String appName = mResolveInfo.loadLabel(packageManager).toString(); 72 73 final AlertController.AlertParams ap = mAlertParams; 74 ap.mIcon = mResolveInfo.loadIcon(packageManager); 75 ap.mTitle = appName; 76 if (mDevice == null) { 77 ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName); 78 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); 79 } else { 80 ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName); 81 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); 82 } 83 ap.mPositiveButtonText = getString(com.android.internal.R.string.ok); 84 ap.mNegativeButtonText = getString(com.android.internal.R.string.cancel); 85 ap.mPositiveButtonListener = this; 86 ap.mNegativeButtonListener = this; 87 88 // add "always use" checkbox 89 LayoutInflater inflater = (LayoutInflater)getSystemService( 90 Context.LAYOUT_INFLATER_SERVICE); 91 ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); 92 mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); 93 mAlwaysUse.setText(com.android.internal.R.string.alwaysUse); 94 mAlwaysUse.setOnCheckedChangeListener(this); 95 mClearDefaultHint = (TextView)ap.mView.findViewById( 96 com.android.internal.R.id.clearDefaultHint); 97 mClearDefaultHint.setVisibility(View.GONE); 98 99 setupAlert(); 100 101 } 102 103 public void onClick(DialogInterface dialog, int which) { 104 if (which == AlertDialog.BUTTON_POSITIVE) { 105 try { 106 IBinder b = ServiceManager.getService(USB_SERVICE); 107 IUsbManager service = IUsbManager.Stub.asInterface(b); 108 int uid = mResolveInfo.activityInfo.applicationInfo.uid; 109 boolean alwaysUse = mAlwaysUse.isChecked(); 110 Intent intent = null; 111 112 if (mDevice != null) { 113 intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); 114 intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice); 115 116 // grant permission for the device 117 service.grantDevicePermission(mDevice, uid); 118 // set or clear default setting 119 if (alwaysUse) { 120 service.setDevicePackage(mDevice, mResolveInfo.activityInfo.packageName); 121 } else { 122 service.setDevicePackage(mDevice, null); 123 } 124 } else if (mAccessory != null) { 125 intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); 126 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory); 127 128 // grant permission for the accessory 129 service.grantAccessoryPermission(mAccessory, uid); 130 // set or clear default setting 131 if (alwaysUse) { 132 service.setAccessoryPackage(mAccessory, 133 mResolveInfo.activityInfo.packageName); 134 } else { 135 service.setAccessoryPackage(mAccessory, null); 136 } 137 } 138 139 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 140 intent.setComponent( 141 new ComponentName(mResolveInfo.activityInfo.packageName, 142 mResolveInfo.activityInfo.name)); 143 startActivity(intent); 144 } catch (Exception e) { 145 Log.e(TAG, "Unable to start activity", e); 146 } 147 } 148 finish(); 149 } 150 151 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 152 if (mClearDefaultHint == null) return; 153 154 if(isChecked) { 155 mClearDefaultHint.setVisibility(View.VISIBLE); 156 } else { 157 mClearDefaultHint.setVisibility(View.GONE); 158 } 159 } 160} 161