Android4.0 hid insertion prompt
The specific principle is not mentioned here. I have not straightened out. Many articles on the Internet have clearly stated that I will directly focus on this.
Main File Modification
Frameworks/base/service/java/com/android/server/usb service. java
Frameworks/base/service/java/com/android/server/usb/UsbHostManager. java
Change the public USB Service (Context context) in USB service. java
public UsbService(Context context) { mContext = context; mSettingsManager = new UsbSettingsManager(context); PackageManager pm = mContext.getPackageManager(); //if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {//modify by hclydao mHostManager = new UsbHostManager(context, mSettingsManager); //} if (new File("/sys/class/android_usb").exists()) { mDeviceManager = new UsbDeviceManager(context, mSettingsManager); } }
Otherwise, the USB hostmanager will not execute
The next step is to modify the USB hostmanager.When the hardware usb hostDuring the insertion, the USB deviceadded function in USB hostmanager will be executed and the USB deviceremoved function will be executed.
When the device is inserted, usb jni will call back the usb deviceadded function. At this time, information about the usb device will be returned, but it is found that the class is 0. In int [] interfaceValues, the whole usb information is returned. interfaceValues [1] the corresponding class 3 corresponds to usb hid 8, and mass storage 9 corresponds to the usb hub prompt image and information directly used by the system.
The last modification is as follows:
/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions an * limitations under the License. */package com.android.server.usb;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.hardware.usb.IUsbManager;import android.hardware.usb.UsbConstants;import android.hardware.usb.UsbDevice;import android.hardware.usb.UsbEndpoint;import android.hardware.usb.UsbInterface;import android.hardware.usb.UsbManager;import android.net.Uri;import android.os.Binder;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.Parcelable;import android.os.ParcelFileDescriptor;import android.os.UEventObserver;import android.provider.Settings;import android.util.Slog;import java.io.File;import java.io.FileDescriptor;import java.io.FileReader;import java.io.PrintWriter;import java.util.HashMap;import java.util.List;import android.app.Notification;import android.app.NotificationManager;import android.content.res.Resources;import java.util.ArrayList;/** * UsbHostManager manages USB state in host mode. */public class UsbHostManager { private static final String TAG = UsbHostManager.class.getSimpleName(); private static final boolean LOG = false; // contains all connected USB devices private final HashMap
mDevices = new HashMap
(); // USB busses to exclude from USB host support private final String[] mHostBlacklist; private final Context mContext; private final Object mLock = new Object(); private final UsbSettingsManager mSettingsManager;private Notification mUsbHostNotification;private ArrayList
dlist; public UsbHostManager(Context context, UsbSettingsManager settingsManager) { mContext = context; mSettingsManager = settingsManager; mHostBlacklist = context.getResources().getStringArray( com.android.internal.R.array.config_usbHostBlacklist); } private boolean isBlackListed(String deviceName) { int count = mHostBlacklist.length; for (int i = 0; i < count; i++) { if (deviceName.startsWith(mHostBlacklist[i])) { return true; } } return false; } /* returns true if the USB device should not be accessible by applications */ private boolean isBlackListed(int clazz, int subClass, int protocol) { // blacklist hubs if (clazz == UsbConstants.USB_CLASS_HUB) return true; // blacklist HID boot devices (mouse and keyboard) if (clazz == UsbConstants.USB_CLASS_HID && subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) { return true; } return false; } /* Called from JNI in monitorUsbHostBus() to report new USB devices */ private void usbDeviceAdded(String deviceName, int vendorID, int productID, int deviceClass, int deviceSubclass, int deviceProtocol, /* array of quintuples containing id, class, subclass, protocol and number of endpoints for each interface */ int[] interfaceValues, /* array of quadruples containing address, attributes, max packet size and interval for each endpoint */ int[] endpointValues) {if((interfaceValues != null) && (interfaceValues[1] == UsbConstants.USB_CLASS_HID)) {//add by hclydao setUsbHostNotification( com.android.internal.R.string.usb_storage_notification_title, com.android.internal.R.string.usb_storage_notification_title, com.android.internal.R.drawable.stat_sys_tether_usb, true, true, null);if(dlist == null)dlist = new ArrayList
();dlist.add(deviceName);} if (isBlackListed(deviceName) || isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) { return; } synchronized (mLock) { if (mDevices.get(deviceName) != null) { Slog.w(TAG, "device already on mDevices list: " + deviceName); return; } int numInterfaces = interfaceValues.length / 5; Parcelable[] interfaces = new UsbInterface[numInterfaces]; try { // repackage interfaceValues as an array of UsbInterface int intf, endp, ival = 0, eval = 0; for (intf = 0; intf < numInterfaces; intf++) { int interfaceId = interfaceValues[ival++]; int interfaceClass = interfaceValues[ival++]; int interfaceSubclass = interfaceValues[ival++]; int interfaceProtocol = interfaceValues[ival++]; int numEndpoints = interfaceValues[ival++]; Parcelable[] endpoints = new UsbEndpoint[numEndpoints]; for (endp = 0; endp < numEndpoints; endp++) { int address = endpointValues[eval++]; int attributes = endpointValues[eval++]; int maxPacketSize = endpointValues[eval++]; int interval = endpointValues[eval++]; endpoints[endp] = new UsbEndpoint(address, attributes, maxPacketSize, interval); } // don't allow if any interfaces are blacklisted if (isBlackListed(interfaceClass, interfaceSubclass, interfaceProtocol)) { return; } interfaces[intf] = new UsbInterface(interfaceId, interfaceClass, interfaceSubclass, interfaceProtocol, endpoints); } } catch (Exception e) { // beware of index out of bound exceptions, which might happen if // a device does not set bNumEndpoints correctly Slog.e(TAG, "error parsing USB descriptors", e); return; } UsbDevice device = new UsbDevice(deviceName, vendorID, productID, deviceClass, deviceSubclass, deviceProtocol, interfaces); mDevices.put(deviceName, device); mSettingsManager.deviceAttached(device); } } /* Called from JNI in monitorUsbHostBus to report USB device removal */ private void usbDeviceRemoved(String deviceName) {if(dlist != null) {for(int i = 0;i