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