UsbResolverActivity.java revision 02eb8746de2d60563ec2751a34d20923192e4293
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.server.usb; 18 19import com.android.internal.app.ResolverActivity; 20 21import android.content.ActivityNotFoundException; 22import android.content.Intent; 23import android.content.pm.ResolveInfo; 24import android.hardware.IUsbManager; 25import android.hardware.UsbAccessory; 26import android.hardware.UsbDevice; 27import android.hardware.UsbManager; 28import android.os.Bundle; 29import android.os.IBinder; 30import android.os.Parcelable; 31import android.os.RemoteException; 32import android.os.ServiceManager; 33import android.util.Log; 34 35import java.util.ArrayList; 36 37/* Activity for choosing an application for a USB device or accessory */ 38public class UsbResolverActivity extends ResolverActivity { 39 public static final String TAG = "UsbResolverActivity"; 40 public static final String EXTRA_RESOLVE_INFOS = "rlist"; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 Intent intent = getIntent(); 45 Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT); 46 if (!(targetParcelable instanceof Intent)) { 47 Log.w("UsbResolverActivity", "Target is not an intent: " + targetParcelable); 48 finish(); 49 return; 50 } 51 Intent target = (Intent)targetParcelable; 52 ArrayList<ResolveInfo> rList = intent.getParcelableArrayListExtra(EXTRA_RESOLVE_INFOS); 53 Log.d(TAG, "rList.size() " + rList.size()); 54 CharSequence title = getResources().getText(com.android.internal.R.string.chooseUsbActivity); 55 super.onCreate(savedInstanceState, target, title, null, rList, 56 true, /* Set alwaysUseOption to true to enable "always use this app" checkbox. */ 57 true /* Set alwaysChoose to display activity when only one choice is available. 58 This is necessary because this activity is needed for the user to allow 59 the application permission to access the device */ 60 ); 61 } 62 63 protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) { 64 try { 65 IBinder b = ServiceManager.getService(USB_SERVICE); 66 IUsbManager service = IUsbManager.Stub.asInterface(b); 67 int uid = ri.activityInfo.applicationInfo.uid; 68 String action = intent.getAction(); 69 70 if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { 71 UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 72 // grant permission for the device 73 service.grantDevicePermission(device, uid); 74 // set or clear default setting 75 if (alwaysCheck) { 76 service.setDevicePackage(device, ri.activityInfo.packageName); 77 } else { 78 service.setDevicePackage(device, null); 79 } 80 } else if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) { 81 UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra( 82 UsbManager.EXTRA_ACCESSORY); 83 // grant permission for the accessory 84 service.grantAccessoryPermission(accessory, uid); 85 // set or clear default setting 86 if (alwaysCheck) { 87 service.setAccessoryPackage(accessory, ri.activityInfo.packageName); 88 } else { 89 service.setAccessoryPackage(accessory, null); 90 } 91 } 92 93 try { 94 startActivity(intent); 95 } catch (ActivityNotFoundException e) { 96 Log.e(TAG, "startActivity failed", e); 97 } 98 } catch (RemoteException e) { 99 Log.e(TAG, "onIntentSelected failed", e); 100 } 101 } 102} 103