ANDROID4.0 HID Insert Hint

Source: Internet
Author: User

The concrete principle here does not say that I also did not straighten out the internet has a lot of articles are said very clearly here I directly on the point

Main modification Files

Frameworks/base/service/java/com/android/server/usb/usbservice.java

Frameworks/base/service/java/com/android/server/usb/usbhostmanager.java

First, change the public usbservice (context context) in Usbservice.java to this

    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 Usbhostmanager will not execute.

The next main step is to modify the Usbhostmanager when the hardware USB host Insert will execute Usbhostmanager in the usbdeviceadded function is unplugged will be executed usbdeviceremoved

When the device is plugged in to the USB JNI callback usbdeviceadded function will return the relevant information of this USB device but found that the class is 0 fortunately in int [] interfacevalues back the entire USB-related information Interfacevalues[1] corresponds to the corresponding class 3 corresponding to the USB HID 8 corresponds to the mass storage 9 pair is the USB hub tips pictures and information directly using the system comes with

The last modification is as follows

/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "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<string,usbdevice> mdevices = new Hashmap<string,us   Bdevice> (); 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<    String> dlist;        Public Usbhostmanager (context context, Usbsettingsmanager SettingsManager) {mcontext = context;        Msettingsmanager = SettingsManager; Mhostblacklist = Context.getresources (). Getstringarray (Com.android.internal.r.array.config_usbhostblacklis    T);        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 being accessible by applications */private Boolean isblacklisted (int cl Azz, 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 Devicena me, int vendorid, int productID, int deviceclass, int devicesubclass, int deviceprotocol,/* Array o            f Quintuples containing ID, class, subclass, protocol and number of endpoints for each interface */ Int[] interfacevalues,/* array of quadruples containing address, attributes, max packet size an D 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_notificat Ion_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<string> ();d List.add (        devicename);}                if (isblacklisted (devicename) | |        Isblacklisted (Deviceclass, Devicesubclass, Deviceprotocol)) {return; } synchronized (MLock) {if (Mdevices.get (devicename) = null) {SLOG.W (TAG, "Device Alre                Ady 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, Eva         L = 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 is BlacklisTed if (isblacklisted (Interfaceclass, Interfacesubclass, Interfaceprotocol)) {R                    Eturn; } interfaces[intf] = new Usbinterface (InterfaceID, Interfaceclass, interface                Subclass, 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, DEVICESUBCL            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<dlist.size (); i++) {if (Dlist.get (i). Equals (DeviceName)) {dlist.remove (i);/* Setus Bhostnotification (Com.android.internal.r.string.ext_media_nomedia_notification_title, COM.ANDROID.I Nternal.            R.string.ext_media_nomedia_notification_message, Com.android.internal.r.drawable.stat_sys_tether_usb,        False, False, null); */setusbhostnotification (0, 0, 0, false, false, NULL);            Synchronized (mLock) {Usbdevice device = Mdevices.remove (devicename);            if (device! = null) {msettingsmanager.devicedetached (device); }}} public void Systemready () {synchronized (MLock) {//Create a ' thread to ' into Na '            tive code to wait for USB host events.            This thread would call us back on usbdeviceadded and usbdeviceremoved. Runnable Runnable = new Runnable () {public void run () {MoniTorusbhostbus ();            }            };        New Thread (NULL, runnable, "Usbservice host Thread"). Start ();        }}/* Returns a list of all currently attached USB devices */public void getdevicelist (Bundle devices) { Synchronized (MLock) {for (String Name:mDevices.keySet ()) {devices.putparcelable (name, Mdev            Ices.get (name));        }}}/* Opens the specified USB device */public parcelfiledescriptor Opendevice (String devicename) { Synchronized (MLock) {if (isblacklisted (devicename)) {throw new SecurityException ("USB Dev            Ice is on a restricted bus ");            } Usbdevice device = Mdevices.get (devicename);                if (device = = null) {//If it is not in mdevices, it either does not exist or is blacklisted   throw new IllegalArgumentException ("Device" + devicename + "does not exist or is restricted");          } msettingsmanager.checkpermission (device);        Return Nativeopendevice (devicename);  }} public void dump (filedescriptor fd, PrintWriter pw) {synchronized (MLock) {pw.println ("USB            Host state: ");            For (String Name:mDevices.keySet ()) {pw.println ("" + Name + ":" + mdevices.get (name));                                                           }}} private synchronized void setusbhostnotification (int titleid, int messageId, int icon, Boolean visible, Boolean dismissable, Pendingintent pi) {if (!visible &am        p;& musbhostnotification = = null) {return; } Notificationmanager Notificationmanager = (notificationmanager) mcontext. Getsystemservice (Context .        Notification_service);        if (Notificationmanager = = null) {return;         } if (musbhostnotification! = null && visible) {/*    * Dismiss the previous notification-we ' re about to * re-use it.            */FINAL int notificationid = Musbhostnotification.icon;        Notificationmanager.cancel (Notificationid);            } if (visible) {Resources R = Resources.getsystem ();            Charsequence title = R.gettext (TitleID);            Charsequence message = R.gettext (messageId);                if (musbhostnotification = = null) {musbhostnotification = new Notification ();            Musbhostnotification.when = 0;            } musbhostnotification.defaults &= ~notification.default_sound;            if (dismissable) {musbhostnotification.flags = Notification.flag_auto_cancel;            } else {musbhostnotification.flags = notification.flag_ongoing_event;            } musbhostnotification.tickertext = title;       if (pi = = null) {Intent Intent = new Intent ();         Pi = pendingintent.getbroadcast (mcontext, 0, intent, 0);            } Musbhostnotification.icon = icon;        Musbhostnotification.setlatesteventinfo (Mcontext, title, message, PI);        } final int notificationid = Musbhostnotification.icon;        if (visible) {notificationmanager.notify (Notificationid, musbhostnotification);        } else {notificationmanager.cancel (Notificationid);    }} private native void Monitorusbhostbus (); Private native Parcelfiledescriptor Nativeopendevice (String devicename);}




ANDROID4.0 HID Insert Hint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.