blob: c562c72f13dfb5b6cc1b109dad9dc175b903c760 [file] [log] [blame]
Kevin Crossand4285492016-11-28 18:40:43 -08001/*
2 * Copyright (C) 2016 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 */
16package android.car.usb.handler;
17
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -070018import static android.content.pm.PackageManager.PERMISSION_GRANTED;
19
20import android.Manifest;
Nicholas Sauer3a3a4922018-09-05 07:50:03 -070021import android.annotation.Nullable;
Kevin Crossand4285492016-11-28 18:40:43 -080022import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
Kevin Crossand4285492016-11-28 18:40:43 -080025import android.content.pm.ActivityInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.PackageManager.NameNotFoundException;
28import android.content.pm.ResolveInfo;
29import android.content.res.XmlResourceParser;
30import android.hardware.usb.UsbDevice;
31import android.hardware.usb.UsbDeviceConnection;
Kevin Crossand4285492016-11-28 18:40:43 -080032import android.hardware.usb.UsbManager;
33import android.os.Handler;
34import android.os.HandlerThread;
Kevin Crossand4285492016-11-28 18:40:43 -080035import android.os.Looper;
36import android.os.Message;
Kevin Crossand4285492016-11-28 18:40:43 -080037import android.util.Log;
Nicholas Sauer3a3a4922018-09-05 07:50:03 -070038
Kevin Crossand4285492016-11-28 18:40:43 -080039import com.android.internal.util.XmlUtils;
Nicholas Sauer3a3a4922018-09-05 07:50:03 -070040
41import org.xmlpull.v1.XmlPullParser;
42
Kevin Crossand4285492016-11-28 18:40:43 -080043import java.io.IOException;
44import java.util.ArrayList;
Kevin Crossand4285492016-11-28 18:40:43 -080045import java.util.List;
Kevin Crossand4285492016-11-28 18:40:43 -080046
47/** Resolves supported handlers for USB device. */
48public final class UsbDeviceHandlerResolver {
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -070049
Kevin Crossand4285492016-11-28 18:40:43 -080050 private static final String TAG = UsbDeviceHandlerResolver.class.getSimpleName();
51 private static final boolean LOCAL_LOGD = true;
52
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -070053 private static final String AOAP_HANDLE_PERMISSION =
54 "android.car.permission.CAR_HANDLE_USB_AOAP_DEVICE";
55
Kevin Crossand4285492016-11-28 18:40:43 -080056 /**
57 * Callbacks for device resolver.
58 */
59 public interface UsbDeviceHandlerResolverCallback {
60 /** Handlers are resolved */
61 void onHandlersResolveCompleted(
62 UsbDevice device, List<UsbDeviceSettings> availableSettings);
63 /** Device was dispatched */
64 void onDeviceDispatched();
65 }
66
67 private final UsbManager mUsbManager;
68 private final PackageManager mPackageManager;
69 private final UsbDeviceHandlerResolverCallback mDeviceCallback;
70 private final Context mContext;
71 private final HandlerThread mHandlerThread;
72 private final UsbDeviceResolverHandler mHandler;
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -070073 private final AoapServiceManager mAoapServiceManager;
Kevin Crossand4285492016-11-28 18:40:43 -080074
75 public UsbDeviceHandlerResolver(UsbManager manager, Context context,
76 UsbDeviceHandlerResolverCallback deviceListener) {
77 mUsbManager = manager;
78 mContext = context;
79 mDeviceCallback = deviceListener;
80 mHandlerThread = new HandlerThread(TAG);
81 mHandlerThread.start();
82 mHandler = new UsbDeviceResolverHandler(mHandlerThread.getLooper());
83 mPackageManager = context.getPackageManager();
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -070084 mAoapServiceManager = new AoapServiceManager(mContext.getApplicationContext());
Kevin Crossand4285492016-11-28 18:40:43 -080085 }
86
87 /**
88 * Releases current object.
89 */
90 public void release() {
91 if (mHandlerThread != null) {
92 mHandlerThread.quitSafely();
93 }
94 }
95
96 /**
97 * Resolves handlers for USB device.
98 */
99 public void resolve(UsbDevice device) {
100 mHandler.requestResolveHandlers(device);
101 }
102
103 /**
104 * Dispatches device to component.
105 */
106 public boolean dispatch(UsbDevice device, ComponentName component, boolean inAoap) {
107 if (LOCAL_LOGD) {
108 Log.d(TAG, "dispatch: " + device + " component: " + component + " inAoap: " + inAoap);
109 }
110
111 ActivityInfo activityInfo;
112 try {
113 activityInfo = mPackageManager.getActivityInfo(component, PackageManager.GET_META_DATA);
114 } catch (NameNotFoundException e) {
115 Log.e(TAG, "Activity not found: " + component);
116 return false;
117 }
118
119 Intent intent = createDeviceAttachedIntent(device);
120 if (inAoap) {
121 if (AoapInterface.isDeviceInAoapMode(device)) {
122 mDeviceCallback.onDeviceDispatched();
123 } else {
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700124 UsbDeviceFilter filter =
Kevin Crossand4285492016-11-28 18:40:43 -0800125 packageMatches(activityInfo, intent.getAction(), device, true);
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700126
Kevin Crossand4285492016-11-28 18:40:43 -0800127 if (filter != null) {
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700128 mHandlerThread.getThreadHandler().post(() -> {
129 if (mAoapServiceManager.canSwitchDeviceToAoap(device,
130 ComponentName.unflattenFromString(filter.mAoapService))) {
131 requestAoapSwitch(device, filter);
132 } else {
133 Log.i(TAG, "Ignore AOAP switch for device " + device
134 + " handled by " + filter.mAoapService);
135 }
136 });
Pavel Maltseve6ce3db2019-05-10 11:40:04 -0700137 mDeviceCallback.onDeviceDispatched();
Kevin Crossand4285492016-11-28 18:40:43 -0800138 return true;
139 }
140 }
141 }
142
143 intent.setComponent(component);
144 mUsbManager.grantPermission(device, activityInfo.applicationInfo.uid);
145
146 mContext.startActivity(intent);
147 mHandler.requestCompleteDeviceDispatch();
148 return true;
149 }
150
151 private static Intent createDeviceAttachedIntent(UsbDevice device) {
152 Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
153 intent.putExtra(UsbManager.EXTRA_DEVICE, device);
154 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155 return intent;
156 }
157
158 private void doHandleResolveHandlers(UsbDevice device) {
159 if (LOCAL_LOGD) {
160 Log.d(TAG, "doHandleResolveHandlers: " + device);
161 }
162
163 Intent intent = createDeviceAttachedIntent(device);
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700164 List<UsbHandlerPackage> matches = getDeviceMatches(device, intent, false);
Kevin Crossand4285492016-11-28 18:40:43 -0800165 if (LOCAL_LOGD) {
166 Log.d(TAG, "matches size: " + matches.size());
167 }
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700168 List<UsbDeviceSettings> settings = new ArrayList<>();
169 for (UsbHandlerPackage pkg : matches) {
170 settings.add(createSettings(device, pkg));
Kevin Crossand4285492016-11-28 18:40:43 -0800171 }
Kevin Crossand4285492016-11-28 18:40:43 -0800172
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700173 UsbDeviceConnection devConnection = UsbUtil.openConnection(mUsbManager, device);
174 if (devConnection != null && AoapInterface.isSupported(mContext, device, devConnection)) {
175 for (UsbHandlerPackage pkg : getDeviceMatches(device, intent, true)) {
176 if (mAoapServiceManager.isDeviceSupported(device, pkg.mAoapService)) {
177 settings.add(createSettings(device, pkg));
178 }
Kevin Crossand4285492016-11-28 18:40:43 -0800179 }
Kevin Crossand4285492016-11-28 18:40:43 -0800180 }
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700181
182 deviceProbingComplete(device, settings);
Kevin Crossand4285492016-11-28 18:40:43 -0800183 }
184
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700185 private UsbDeviceSettings createSettings(UsbDevice device, UsbHandlerPackage pkg) {
186 UsbDeviceSettings settings = UsbDeviceSettings.constructSettings(device);
187 settings.setHandler(pkg.mActivity);
188 settings.setAoap(pkg.mAoapService != null);
189 return settings;
190 }
191
192 private void requestAoapSwitch(UsbDevice device, UsbDeviceFilter filter) {
Kevin Crossand4285492016-11-28 18:40:43 -0800193 UsbDeviceConnection connection = UsbUtil.openConnection(mUsbManager, device);
Nicholas Sauer3a3a4922018-09-05 07:50:03 -0700194 if (connection == null) {
195 Log.e(TAG, "Failed to connect to usb device.");
196 return;
197 }
Kevin Crossand4285492016-11-28 18:40:43 -0800198 try {
199 UsbUtil.sendAoapAccessoryStart(
200 connection,
201 filter.mAoapManufacturer,
202 filter.mAoapModel,
203 filter.mAoapDescription,
204 filter.mAoapVersion,
205 filter.mAoapUri,
206 filter.mAoapSerial);
207 } catch (IOException e) {
208 Log.w(TAG, "Failed to switch device into AOAP mode", e);
209 }
210 connection.close();
211 }
212
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700213 private void deviceProbingComplete(UsbDevice device, List<UsbDeviceSettings> settings) {
Kevin Crossand4285492016-11-28 18:40:43 -0800214 if (LOCAL_LOGD) {
215 Log.d(TAG, "deviceProbingComplete");
216 }
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700217 mDeviceCallback.onHandlersResolveCompleted(device, settings);
Kevin Crossand4285492016-11-28 18:40:43 -0800218 }
219
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700220 private List<UsbHandlerPackage> getDeviceMatches(
Kevin Crossand4285492016-11-28 18:40:43 -0800221 UsbDevice device, Intent intent, boolean forAoap) {
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700222 List<UsbHandlerPackage> matches = new ArrayList<>();
Kevin Crossand4285492016-11-28 18:40:43 -0800223 List<ResolveInfo> resolveInfos =
224 mPackageManager.queryIntentActivities(intent, PackageManager.GET_META_DATA);
225 for (ResolveInfo resolveInfo : resolveInfos) {
Pavel Maltsevf6862a02019-04-01 17:24:20 -0700226 final String packageName = resolveInfo.activityInfo.packageName;
227 if (forAoap && !hasAoapPermission(packageName)) {
228 Log.w(TAG, "Package " + packageName + " does not hold "
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700229 + AOAP_HANDLE_PERMISSION + " permission. Ignore the package.");
230 continue;
231 }
232
233 UsbDeviceFilter filter = packageMatches(resolveInfo.activityInfo,
Kevin Crossand4285492016-11-28 18:40:43 -0800234 intent.getAction(), device, forAoap);
235 if (filter != null) {
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700236 ActivityInfo ai = resolveInfo.activityInfo;
237 ComponentName activity = new ComponentName(ai.packageName, ai.name);
238 ComponentName aoapService = filter.mAoapService == null
239 ? null : ComponentName.unflattenFromString(filter.mAoapService);
240
241 if (aoapService != null && !checkServiceRequiresPermission(aoapService)) {
242 continue;
243 }
244
245 if (aoapService != null || !forAoap) {
246 matches.add(new UsbHandlerPackage(activity, aoapService));
247 }
Kevin Crossand4285492016-11-28 18:40:43 -0800248 }
249 }
250 return matches;
251 }
252
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700253 private boolean checkServiceRequiresPermission(ComponentName serviceName) {
254 Intent intent = new Intent();
255 intent.setComponent(serviceName);
256 boolean found = false;
257 for (ResolveInfo info : mPackageManager.queryIntentServices(intent, 0)) {
258 if (info.serviceInfo != null) {
259 found = true;
260 if ((Manifest.permission.MANAGE_USB.equals(info.serviceInfo.permission))) {
261 return true;
262 }
263 }
264 }
265 if (found) {
266 Log.w(TAG, "Component " + serviceName + " must be protected with "
267 + Manifest.permission.MANAGE_USB + " permission");
268 } else {
269 Log.w(TAG, "Component " + serviceName + " not found");
270 }
271 return false;
272 }
273
274 private boolean hasAoapPermission(String packageName) {
275 return mPackageManager
276 .checkPermission(AOAP_HANDLE_PERMISSION, packageName) == PERMISSION_GRANTED;
277 }
278
279 private UsbDeviceFilter packageMatches(ActivityInfo ai, String metaDataName, UsbDevice device,
Kevin Crossand4285492016-11-28 18:40:43 -0800280 boolean forAoap) {
281 if (LOCAL_LOGD) {
282 Log.d(TAG, "packageMatches ai: " + ai + "metaDataName: " + metaDataName + " forAoap: "
283 + forAoap);
284 }
285 String filterTagName = forAoap ? "usb-aoap-accessory" : "usb-device";
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700286 try (XmlResourceParser parser = ai.loadXmlMetaData(mPackageManager, metaDataName)) {
Kevin Crossand4285492016-11-28 18:40:43 -0800287 if (parser == null) {
288 Log.w(TAG, "no meta-data for " + ai);
289 return null;
290 }
291
292 XmlUtils.nextElement(parser);
293 while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
294 String tagName = parser.getName();
295 if (device != null && filterTagName.equals(tagName)) {
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700296 UsbDeviceFilter filter = UsbDeviceFilter.read(parser, forAoap);
Kevin Crossand4285492016-11-28 18:40:43 -0800297 if (forAoap || filter.matches(device)) {
298 return filter;
299 }
300 }
301 XmlUtils.nextElement(parser);
302 }
303 } catch (Exception e) {
304 Log.w(TAG, "Unable to load component info " + ai.toString(), e);
Kevin Crossand4285492016-11-28 18:40:43 -0800305 }
306 return null;
307 }
308
309 private class UsbDeviceResolverHandler extends Handler {
310 private static final int MSG_RESOLVE_HANDLERS = 0;
Kevin Crossand4285492016-11-28 18:40:43 -0800311 private static final int MSG_COMPLETE_DISPATCH = 3;
312
Kevin Crossand4285492016-11-28 18:40:43 -0800313 private UsbDeviceResolverHandler(Looper looper) {
314 super(looper);
315 }
316
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700317 void requestResolveHandlers(UsbDevice device) {
Kevin Crossand4285492016-11-28 18:40:43 -0800318 Message msg = obtainMessage(MSG_RESOLVE_HANDLERS, device);
319 sendMessage(msg);
320 }
321
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700322 void requestCompleteDeviceDispatch() {
Kevin Crossand4285492016-11-28 18:40:43 -0800323 sendEmptyMessage(MSG_COMPLETE_DISPATCH);
324 }
325
326 @Override
327 public void handleMessage(Message msg) {
328 switch (msg.what) {
329 case MSG_RESOLVE_HANDLERS:
330 doHandleResolveHandlers((UsbDevice) msg.obj);
331 break;
Kevin Crossand4285492016-11-28 18:40:43 -0800332 case MSG_COMPLETE_DISPATCH:
333 mDeviceCallback.onDeviceDispatched();
334 break;
335 default:
336 Log.w(TAG, "Unsupported message: " + msg);
337 }
338 }
339 }
Pavel Maltsev6b49b9b2019-03-14 10:14:24 -0700340
341 private static class UsbHandlerPackage {
342 final ComponentName mActivity;
343 final @Nullable ComponentName mAoapService;
344
345 UsbHandlerPackage(ComponentName activity, @Nullable ComponentName aoapService) {
346 mActivity = activity;
347 mAoapService = aoapService;
348 }
349 }
Kevin Crossand4285492016-11-28 18:40:43 -0800350}