Reprint Please specify source: http://blog.csdn.net/droyon/article/details/45098027
1. Introduction to Android Background
Usbservice, which is created at system startup, in this file, and the USB state is closely related to the Operation class is Usbdevicemanager, most of the USB and the relevant ADB logic, in this class to do processing. Usbdevicemanager, we need to focus on three parts of the content. One, the configuration file. Second, private final ueventobserver mueventobserver = new Ueventobserver (), accept the Uevent event. Third, Usbhandler.
Where the configuration file, save the current USB state. Ueventobserver accepts USB events, push to Usbhandler to handle. Usbhandler is the key class used to handle USB events.
2, USB default state from where to get, the current default state is what?
The current state of the USB is stored in the Mcurrentfunctions member variable, which is initialized in the construction of the Usbhandler.
Mdefaultfunctions = Systemproperties.get ("Persist.sys.usb.config", "adb"); Check if USB mode needs to is overridden depending on OEM specific bootmode. Mdefaultfunctions = Processoemusboverride (mdefaultfunctions); Sanity Check the Sys.usb.config system property //This is necessary if we crashed while switching USB Configur ations String config = systemproperties.get ("Sys.usb.config", "none"); if (!config.equals (mdefaultfunctions)) { SLOG.W (TAG, "resetting config to persistent property:" + mdefaultfunctions ); Systemproperties.set ("Sys.usb.config", mdefaultfunctions); } Mcurrentfunctions = mdefaultfunctions;
3. When inserting USB, what is the general process of ejecting ADB and notification notification?
When USB is plugged in, our ueventobserver will receive the message and eventually push to Usbhandler.
Uevent Information:
04-17 14:20:03.352 V/usbdevicemanager ( 759): USB uevent: {usb_state=disconnected, SUBSYSTEM=ANDROID_USB, SEQNUM= 7528, Action=change, devpath=/devices/virtual/android_usb/android0}
Then execute the Usbhandler updatestate method.
Private final Ueventobserver mueventobserver = new Ueventobserver () { @Override public void Onuevent ( Ueventobserver.uevent event) { if (DEBUG) slog.v (TAG, "USB uevent:" + event.tostring ()); String state = Event.get ("Usb_state"); String accessory = Event.get ("accessory"); if (state = null) { mhandler.updatestate (state); } else if ("START". Equals (Accessory)) { if (DEBUG) SLOG.D ( TAG, "Got accessory Start"); Startaccessorymode ();}} ;
Updatestate method: Updates the mconnection state.
public void Updatestate (String state) {int connected, configured; if ("Disconnected". Equals (state)) {connected = 0; configured = 0; } else if ("CONNECTED". Equals (state)) {CONNECTED = 1; configured = 0; } else if ("Configured". Equals (state)) {connected = 1; configured = 1; } else {slog.e (TAG, "unknown state" + state); Return } removemessages (Msg_update_state); Message msg = Message.obtain (this, msg_update_state); MSG.ARG1 = connected; MSG.ARG2 = configured; Debounce disconnects to avoid problems bringing up USB tethering sendmessagedelayed (msg, (connected = = 0)? update_delay:0); }
Finally in the Usbhandler update status, and then eject the ADB and USB notification
Case Msg_update_state: mconnected = (Msg.arg1 = = 1); mconfigured = (Msg.arg2 = = 1); Updateusbnotification (); Updateadbnotification (); if (Containsfunction (mcurrentfunctions, usbmanager.usb_function_accessory)) { updatecurrentaccessory (); } else if (!mconnected) { //restore Defaults when USB is disconnected setenabledfunctions (mdefaultfunctions, false); } if (mbootcompleted) { updateusbstate (); Updateaudiosourcefunction (); } Break
4. What type of USB can be set?
public static final String usb_function_mass_storage = "Mass_storage"; /** * Name of the ADB USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} */public static final String usb_ FUNCTION_ADB = "ADB"; /** * Name of the RNDIS Ethernet USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} */public static final String usb_ Function_rndis = "RNDIS"; /** * Name of the MTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} */public static final String usb_ FUNCTION_MTP = "MTP"; /** * Name of the PTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} */public static final String usb_ FUNCTION_PTP = "PTP"; /** * Name of the charging USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} */PublicStatic final String usb_function_charging = "charging"; /** * Name of the audio source USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} */public static final String usb_ Function_audio_source = "Audio_source"; /** * Name of the accessory USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} */public static final String usb_ Function_accessory = "accessory";
These are their definitions. where ADB and charging cannot be compatible at the same time.
5. What is the process for setting up USB?
The class name of the upper layer that can set the USB type is called: Usbsettings, in which the user can set the current usbsettings status as MTP, PTP, and other USB states defined by the vendor.
Musbmanager.setcurrentfunction (function, true) is called when the user is operating;
Tring function = Usb_function_default; if (preference = = Mmtp && mmtp.ischecked ()) { function = usbmanager.usb_function_mtp; } else if (Preferen Ce = = MPTP && mptp.ischecked ()) { function = USBMANAGER.USB_FUNCTION_PTP; } else if (preference = = Mchargi ng && mcharging.ischecked ()) { function = usbmanager.usb_function_charging; } else if (preference = = Msdcard && msdcard.ischecked ()) { function = usbmanager.usb_function_mass_storage; } Operateinprogress = true; Musbmanager.setcurrentfunction (function, true); Updatetoggles (function);
Usbmanager will then call Usbservice's Setcurrentfunction
Usbservice.java
@Override public void Setcurrentfunction (String function, Boolean makedefault) {mcontext.enforcecallingorselfpermission (Android) Oid. Manifest.permission.MANAGE_USB, NULL); If attempt to change USB function while file transfer are restricted, ensure that//the current function is set To "none", and return. Usermanager Usermanager = (usermanager) mcontext.getsystemservice (Context.user_service); if (Usermanager.hasuserrestriction (Usermanager.disallow_usb_file_transfer)) {if (Mdevicemanager! = null) MDevi Cemanager.setcurrentfunctions ("None", false); Return } if (Mdevicemanager! = null) {mdevicemanager.setcurrentfunctions (function, makedefault); } else {throw new IllegalStateException ("USB device mode not supported"); } }
The setcurrentfunction in Usbdevicemanager will eventually be called. Therefore, the remaining steps can refer to 2.
Android USB Related Knowledge Summary