USB charging and plugging and USB debugging connect prompt
Find the code about USB debug enable in packages/apps/settings/src/COM/Android/settings/developmentsettings. Java:
Java code
- Settings. Secure. putint (getcontentresolver (), settings. Secure. adb_enabled, 0 );
In this file, the value is saved to the settings database according to user settings. Action will be taken elsewhere based on its value changes dynamically
After searching, the value in frameworks/base/services/Java/COM/Android/Server/icationicationmanagerservice. Java is used to determine whether a notification is sent in the status bar. The Code is as follows:
The status bar message prompts you to dynamically change the value elsewhere.
Java code
- Void observe (){
- Contentresolver resolver = mcontext. getcontentresolver ();
- Resolver. registercontentobserver (settings. Secure. geturifor (
- Settings. Secure. adb_enabled), false, this );
- Update ();
- }
- @ Override public void onchange (Boolean selfchange ){
- Update ();
- }
- Public void Update (){
- Contentresolver resolver = mcontext. getcontentresolver ();
- Madbenabled = settings. Secure. getint (Resolver,
- Settings. Secure. adb_enabled,
0 )! = 0;
- Updateadbnotification ();
- }
When activated, a notification prompt is displayed in the status bar:
C-sharp code
- Icationicationmanager. Notify (
- Com. Android. Internal. R. String. adb_active_icationication_title,
- Madbnotification );
The notification content is defined as follows in the resource string (English) in the string resource file frameworks/base/CORE/RES/values/strings. xml:
XHTML code
- <! -- Title of notification shown when ADB is actively connected to the phone. -->
- <Stringname = "adbactivenotificationtitle"> USB debugging connected </string>
- <! -- Message of notification shown when ADB is actively connected to the phone. -->
- <Stringname = "adbactivenotificationmessage"> A computer is connected to your phone. </string>
Changing the settings value will affect the actual usage as follows:
In the file, frameworks/base/services/Java/COM/Android/Server/systemserver. Java
Java code
- Private class adbsettingsobserver extends contentobserver {
- Public adbsettingsobserver (){
- Super (null );
- }
- @ Override
- Public void onchange (Boolean selfchange ){
- Boolean enableadb = (settings. Secure. getint (mcontentresolver,
- Settings. Secure. adb_enabled,
0)> 0 );
- // Setting this secure property will start or stop adbd
- Systemproperties. Set ("Persist. Service. ADB. Enable", enableadb? "1": "0 ");
- }
- }
It can be seen that when the system property persist is set. service. ADB. when the value of enable is set, the corresponding actions of the adbd daemon (STOP and enable) are affected. This will affect whether to view logs and other ADB functions for developers.
Bug case analysis:
Java code
- Private void updateadbnotification (){
- Log. D (TAG, "2. mbatteryplugged =" + mbatteryplugged );
- If (madbenabled & mbatteryplugged = batterymanager. batterypluggedusb ){
- Log. D (TAG, "ADB enabled, battery plugged USB ");
- If ("0". Equals (systemproperties. Get ("Persist. ADB. Policy "))){
- Log. D (TAG, "Return directly ");
- Return;
- }
- If (! Madbnotificationshown ){
- //... Partial Code omitted
- Madbicationicationshown = true;
- Icationicationmanager. Notify (
- Com. Android. Internal. R. String. adb_active_icationication_title,
- Madbnotification );
- }
- }
- } Else if (madbicationicationshown ){
- //... Partial Code omitted
- Madbicationicationshown = false;
- Icationicationmanager. Cancel (
- Com. Android. Internal. R. String. adb_active_icationication_title );
- }
- }
- }
Symptom: "USB debugging connected" is displayed only when the USB cable is connected to the PC and unplugged"
Analysis: Through the code search described above, only icationmanagerservice is available. this prompt appears in Java, that is, the updateadbnotification () function is called only when the user changes the settings value and receives the intent (USB plugging and charging:
Log information:
XHTML code
- D/icationicationservice (1557 ):
Mbatteryplugged = 1
- D/icationicationservice (1557): 2.
Mbatteryplugged = 1
- D/icationicationservice (1557 ):
Mbatteryplugged = 1
- D/icationicationservice (1557): 2.
Mbatteryplugged = 1
- D/icationicationservice (1557 ):
Mbatteryplugged = 2
- D/icationicationservice (1557): 2.
Mbatteryplugged = 2
- D/icationicationservice (1557): ADB enabled, battery plugged USB
- D/icationicationservice (1557): ADB show notification
- D/icationicationservice (1557 ):
Mbatteryplugged = 0
- D/icationicationservice (1557): 2.
Mbatteryplugged = 0
- D/icationicationservice (1557): ADB cancel notification
- D/icationicationservice (1557 ):
Mbatteryplugged = 0
- D/icationicationservice (1557): 2.
Mbatteryplugged = 0
According to the log, the mbatteryplugged value is 1 (battery_plugged_ac, not 2 (batterymanager) when connected to the USB cable. battery_plugged_usb). If the value is 2 at the moment of unplugging, a prompt is sent. If the value is 0 after unplugging.
The value of mbatteryplugged comes from intent:
Java code
- Mbatteryplugged = intent. getintextra ("plugged", 0 );
- Updateadbnotification ();
It is initiated in the following code (see the file frameworks/base/services/Java/COM/Android/Server/batteryservice. Java)
C-sharp code
- Private synchronized final void Update (){
- Native_update (); // read various values at the JNI Layer
- Boolean logoutlier = false;
- Long dischargeduration = 0;
- Mbatterylevelcritical = mbatterylevel <= critical_battery_level;
- If (maconline ){
- Mplugtype = batterymanager. battery_plugged_ac;
- } Else if (musbonline ){
- Mplugtype = batterymanager. battery_plugged_usb;
- } Else {
- Mplugtype = battery_plugged_none;
- }
In the file com_android_server_batteryservice.cpp, function:
Java code
- Static void android_server_batteryservice_update (jnienv * ENV, jobject OBJ)
The required values are read from the Sys system:
C-sharp code
- # Define ac_online_path "/sys/class/Power_Supply/AC/online"
- # Define usb_online_path "/sys/class/Power_Supply/USB/online"
- # Define battery_status_path "/sys/class/Power_Supply/battery/status"
- # Define battery_health_path "/sys/class/Power_Supply/battery/Health"
- # Define battery_present_path "/sys/class/Power_Supply/battery/present"
- # Define battery_capacity_path "/sys/class/Power_Supply/battery/capacity"
- # Define battery_voltage_path "/sys/class/Power_Supply/battery/batt_vol"
- # Define battery_temperature_path "/sys/class/Power_Supply/battery/batt_temp"
- # Define battery_policy_path "/sys/class/Power_Supply/battery/technology"
The first two items indicate whether AC or USB is charged.
Therefore, the problem occurs when identifying USB charging information errors at the underlying layer of the system, resulting in displaying USB debugging connect information at the wrong time.
Summary:
When the system's batteryservice (batteryservice. Java) calls the JNI layer (com_android_server_batteryservice.cpp), it obtains the charging (such as USB charging) and battery information through the Sys system file, and then sends it out through intent. Icationicationmanagerservice. Java after receiving the broadcast information, the corresponding prompt information is taken when the analysis is USB charging.
In addition, icationicationmanagerservice also monitors whether the user in settings has modified the set value and takes the corresponding action (whether to update the prompt information, whether to stop or enable the adbd Daemon ).