1 /* 2 * Copyright (C) 2018 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.server.usb; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.ActivityNotFoundException; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.ResolveInfo; 26 import android.hardware.usb.UsbAccessory; 27 import android.hardware.usb.UsbDevice; 28 import android.hardware.usb.UsbManager; 29 import android.os.UserHandle; 30 import android.util.Slog; 31 32 import java.util.ArrayList; 33 34 /** 35 * UsbResolveActivityManager creates UI dialogs for user to pick or confirm handler for 36 * usb attach event. 37 * 38 * @hide 39 */ 40 public class UsbHandlerManager { 41 private static final String LOG_TAG = UsbHandlerManager.class.getSimpleName(); 42 43 private final Context mContext; 44 UsbHandlerManager(@onNull Context context)45 UsbHandlerManager(@NonNull Context context) { 46 mContext = context; 47 } 48 49 /** 50 * Shows dialog to user to allow them to optionally visit that URL for more 51 * information or software downloads if the attached USB accessory has a valid 52 * URL associated with it. 53 * 54 * @param accessory The accessory to confirm in the UI dialog 55 * @param user The user to start the UI dialog 56 */ showUsbAccessoryUriActivity(@onNull UsbAccessory accessory, @NonNull UserHandle user)57 void showUsbAccessoryUriActivity(@NonNull UsbAccessory accessory, 58 @NonNull UserHandle user) { 59 String uri = accessory.getUri(); 60 if (uri != null && uri.length() > 0) { 61 // display URI to user 62 Intent dialogIntent = createDialogIntent(); 63 dialogIntent.setComponent(ComponentName.unflattenFromString( 64 mContext.getResources().getString( 65 com.android.internal.R.string.config_usbAccessoryUriActivity))); 66 dialogIntent.putExtra(UsbManager.EXTRA_ACCESSORY, accessory); 67 dialogIntent.putExtra("uri", uri); 68 try { 69 mContext.startActivityAsUser(dialogIntent, user); 70 } catch (ActivityNotFoundException e) { 71 Slog.e(LOG_TAG, "unable to start UsbAccessoryUriActivity"); 72 } 73 } 74 } 75 76 /** 77 * Shows dialog to user to confirm the package to start when the USB device 78 * or accessory is attached and there is only one package claims to handle this 79 * USB device or accessory. 80 * 81 * @param rInfo The ResolveInfo of the package to confirm in the UI dialog 82 * @param device The USB device to confirm 83 * @param accessory The USB accessory to confirm 84 */ confirmUsbHandler(@onNull ResolveInfo rInfo, @Nullable UsbDevice device, @Nullable UsbAccessory accessory)85 void confirmUsbHandler(@NonNull ResolveInfo rInfo, @Nullable UsbDevice device, 86 @Nullable UsbAccessory accessory) { 87 Intent resolverIntent = createDialogIntent(); 88 // start UsbConfirmActivity if there is only one choice 89 resolverIntent.setComponent(ComponentName.unflattenFromString( 90 mContext.getResources().getString( 91 com.android.internal.R.string.config_usbConfirmActivity))); 92 resolverIntent.putExtra("rinfo", rInfo); 93 UserHandle user = 94 UserHandle.getUserHandleForUid(rInfo.activityInfo.applicationInfo.uid); 95 96 if (device != null) { 97 resolverIntent.putExtra(UsbManager.EXTRA_DEVICE, device); 98 } else { 99 resolverIntent.putExtra(UsbManager.EXTRA_ACCESSORY, accessory); 100 } 101 102 try { 103 mContext.startActivityAsUser(resolverIntent, user); 104 } catch (ActivityNotFoundException e) { 105 Slog.e(LOG_TAG, "unable to start activity " + resolverIntent, e); 106 } 107 } 108 109 /** 110 * Shows dialog to user to select the package to start when the USB device 111 * or accessory is attached and there are more than one package claim to handle this 112 * USB device or accessory. 113 * 114 * @param matches The available resolutions of the intent 115 * @param user The user to start UI dialog 116 * @param intent The intent to start the UI dialog 117 */ selectUsbHandler(@onNull ArrayList<ResolveInfo> matches, @NonNull UserHandle user, @NonNull Intent intent)118 void selectUsbHandler(@NonNull ArrayList<ResolveInfo> matches, 119 @NonNull UserHandle user, @NonNull Intent intent) { 120 Intent resolverIntent = createDialogIntent(); 121 resolverIntent.setComponent(ComponentName.unflattenFromString( 122 mContext.getResources().getString( 123 com.android.internal.R.string.config_usbResolverActivity))); 124 resolverIntent.putParcelableArrayListExtra("rlist", matches); 125 resolverIntent.putExtra(Intent.EXTRA_INTENT, intent); 126 127 try { 128 mContext.startActivityAsUser(resolverIntent, user); 129 } catch (ActivityNotFoundException e) { 130 Slog.e(LOG_TAG, "unable to start activity " + resolverIntent, e); 131 } 132 } 133 createDialogIntent()134 private Intent createDialogIntent() { 135 Intent intent = new Intent(); 136 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 137 return intent; 138 } 139 } 140