USB rechargeable plug-in and USB debugging connect prompt

Source: Internet
Author: User
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
  1. 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
  1. Void observe (){
  2. Contentresolver resolver = mcontext. getcontentresolver ();
  3. Resolver. registercontentobserver (settings. Secure. geturifor (
  4. Settings. Secure. adb_enabled), false, this );
  5. Update ();
  6. }
  7. @ Override public void onchange (Boolean selfchange ){
  8. Update ();
  9. }
  10. Public void Update (){
  11. Contentresolver resolver = mcontext. getcontentresolver ();
  12. Madbenabled = settings. Secure. getint (Resolver,
  13. Settings. Secure. adb_enabled,
    0 )! = 0;
  14. Updateadbnotification ();
  15. }

When activated, a notification prompt is displayed in the status bar:

C-sharp code
  1. Icationicationmanager. Notify (
  2. Com. Android. Internal. R. String. adb_active_icationication_title,
  3. 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
  1. <! -- Title of notification shown when ADB is actively connected to the phone. -->
  2. <Stringname = "adbactivenotificationtitle"> USB debugging connected </string>
  3. <! -- Message of notification shown when ADB is actively connected to the phone. -->
  4. <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
  1. Private class adbsettingsobserver extends contentobserver {
  2. Public adbsettingsobserver (){
  3. Super (null );
  4. }
  5. @ Override
  6. Public void onchange (Boolean selfchange ){
  7. Boolean enableadb = (settings. Secure. getint (mcontentresolver,
  8. Settings. Secure. adb_enabled,
    0)> 0 );
  9. // Setting this secure property will start or stop adbd
  10. Systemproperties. Set ("Persist. Service. ADB. Enable", enableadb? "1": "0 ");
  11. }
  12. }

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
  1. Private void updateadbnotification (){
  2. Log. D (TAG, "2. mbatteryplugged =" + mbatteryplugged );
  3. If (madbenabled & mbatteryplugged = batterymanager. batterypluggedusb ){
  4. Log. D (TAG, "ADB enabled, battery plugged USB ");
  5. If ("0". Equals (systemproperties. Get ("Persist. ADB. Policy "))){
  6. Log. D (TAG, "Return directly ");
  7. Return;
  8. }
  9. If (! Madbnotificationshown ){
  10. //... Partial Code omitted
  11. Madbicationicationshown = true;
  12. Icationicationmanager. Notify (
  13. Com. Android. Internal. R. String. adb_active_icationication_title,
  14. Madbnotification );
  15. }
  16. }
  17. } Else if (madbicationicationshown ){
  18. //... Partial Code omitted
  19. Madbicationicationshown = false;
  20. Icationicationmanager. Cancel (
  21. Com. Android. Internal. R. String. adb_active_icationication_title );
  22. }
  23. }
  24. }

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
  1. D/icationicationservice (1557 ):
    Mbatteryplugged = 1
  2. D/icationicationservice (1557): 2.
    Mbatteryplugged = 1
  3. D/icationicationservice (1557 ):
    Mbatteryplugged = 1
  4. D/icationicationservice (1557): 2.
    Mbatteryplugged = 1
  5. D/icationicationservice (1557 ):
    Mbatteryplugged = 2
  6. D/icationicationservice (1557): 2.
    Mbatteryplugged = 2
  7. D/icationicationservice (1557): ADB enabled, battery plugged USB
  8. D/icationicationservice (1557): ADB show notification
  9. D/icationicationservice (1557 ):
    Mbatteryplugged = 0
  10. D/icationicationservice (1557): 2.
    Mbatteryplugged = 0
  11. D/icationicationservice (1557): ADB cancel notification
  12. D/icationicationservice (1557 ):
    Mbatteryplugged = 0
  13. 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
  1. Mbatteryplugged = intent. getintextra ("plugged", 0 );
  2. Updateadbnotification ();

It is initiated in the following code (see the file frameworks/base/services/Java/COM/Android/Server/batteryservice. Java)

  C-sharp code
  1. Private synchronized final void Update (){
  2. Native_update (); // read various values at the JNI Layer
  3. Boolean logoutlier = false;
  4. Long dischargeduration = 0;
  5. Mbatterylevelcritical = mbatterylevel <= critical_battery_level;
  6. If (maconline ){
  7. Mplugtype = batterymanager. battery_plugged_ac;
  8. } Else if (musbonline ){
  9. Mplugtype = batterymanager. battery_plugged_usb;
  10. } Else {
  11. Mplugtype = battery_plugged_none;
  12. }

In the file com_android_server_batteryservice.cpp, function:

  Java code
  1. Static void android_server_batteryservice_update (jnienv * ENV, jobject OBJ)

The required values are read from the Sys system:

  C-sharp code
  1. # Define ac_online_path "/sys/class/Power_Supply/AC/online"
  2. # Define usb_online_path "/sys/class/Power_Supply/USB/online"
  3. # Define battery_status_path "/sys/class/Power_Supply/battery/status"
  4. # Define battery_health_path "/sys/class/Power_Supply/battery/Health"
  5. # Define battery_present_path "/sys/class/Power_Supply/battery/present"
  6. # Define battery_capacity_path "/sys/class/Power_Supply/battery/capacity"
  7. # Define battery_voltage_path "/sys/class/Power_Supply/battery/batt_vol"
  8. # Define battery_temperature_path "/sys/class/Power_Supply/battery/batt_temp"
  9. # 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 ).

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.