USB charging and plugging and USB debugging connect prompt

Source: Internet
Author: User

Find the code about USB debug enable in packages/apps/settings/src/COM/Android/settings/developmentsettings. Java:

1 Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED,  0 );  
Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 0);

The status bar message prompts you to dynamically change the value elsewhere.

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();  }  
        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:

notificationManager.notify(                              com.android.internal .R. string .adb_active_notification_title,                              mAdbNotification);  

  

notificationManager.notify(                            com.android.internal.R.string.adb_active_notification_title,                            mAdbNotification);

  

The content is defined as follows in the resource string (English) in the string resource file frameworks/base/CORE/RES/values/strings. xml:

<!-- Title of notification shown when ADB is actively connected to the phone. -->   < string   name = "adbactivenotificationtitle" > USB debugging connected </ string >   <!-- Message of notification shown when ADB is actively connected to the phone. -->   < string   name = "adbactivenotificationmessage" > A computer is connected to your phone. </ string >   

  

<!-- Title of notification shown when ADB is actively connected to the phone. --><string name="adbactivenotificationtitle">USB debugging connected</string><!-- Message of notification shown when ADB is actively connected to the phone. --><string name="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

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" );          }  }  

  

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:

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. Y ") {log. D (TAG, "Return directly"); return;} If (! Madbnotificationshown ){//... Omitting some code madbnotificationshown = true; icationicationmanager. Y (COM. Android. Internal. R. String. adb_active_notification_title, madbnotification) ;}} else if (madbnotificationshown ){//... Omitting some code madbicationicationshown = false; icationicationmanager. Cancel (COM. Android. Internal. R. String. adb_active_notification_title );}}}

  

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. Y ") {log. D (TAG, "Return directly"); return;} If (! Madbnotificationshown ){//... Omitting some code madbnotificationshown = true; icationicationmanager. Y (COM. Android. Internal. R. String. adb_active_notification_title, madbnotification) ;}} else if (madbnotificationshown ){//... Omitting some code madbicationicationshown = false; icationicationmanager. Cancel (COM. Android. Internal. R. String. adb_active_notification_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:

D/NotificationService( 1557):  mBatteryPlugged = 1   D/NotificationService( 1557): 2. mBatteryPlugged = 1   D/NotificationService( 1557): mBatteryPlugged = 1   D/NotificationService( 1557): 2. mBatteryPlugged = 1   D/NotificationService( 1557): mBatteryPlugged = 2   D/NotificationService( 1557): 2. mBatteryPlugged = 2   D/NotificationService( 1557): adb enabled, Battery Plugged usb  D/NotificationService( 1557): adb show notification  D/NotificationService( 1557): mBatteryPlugged = 0   D/NotificationService( 1557): 2. mBatteryPlugged = 0   D/NotificationService( 1557): adb cancel notification  D/NotificationService( 1557): mBatteryPlugged = 0   D/NotificationService( 1557): 2. mBatteryPlugged = 0   

  

D/NotificationService( 1557): mBatteryPlugged=1D/NotificationService( 1557): 2. mBatteryPlugged=1D/NotificationService( 1557): mBatteryPlugged=1D/NotificationService( 1557): 2. mBatteryPlugged=1D/NotificationService( 1557): mBatteryPlugged=2D/NotificationService( 1557): 2. mBatteryPlugged=2D/NotificationService( 1557): adb enabled, Battery Plugged usbD/NotificationService( 1557): adb show notificationD/NotificationService( 1557): mBatteryPlugged=0D/NotificationService( 1557): 2. mBatteryPlugged=0D/NotificationService( 1557): adb cancel notificationD/NotificationService( 1557): mBatteryPlugged=0D/NotificationService( 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:

mBatteryPlugged = intent.getIntExtra( "plugged" ,  0 );                  updateAdbNotification();  

  

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)

Private synchronized final void Update () {native_update (); // read various values at the JNI layer: Boolean logoutlier = false; long dischargeduration = 0; mbatterylevelcritical = mbatterylevel <= unknown; if (maconline) {mplugtype = batterymanager. battery_plugged_ac;} else if (musbonline) {mplugtype = batterymanager. battery_plugged_usb;} else {mplugtype = battery_plugged_none ;}

  

Private synchronized final void Update () {native_update (); // read various values at the JNI layer: Boolean logoutlier = false; long dischargeduration = 0; mbatterylevelcritical = mbatterylevel <= unknown; 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:

static   void  android_server_BatteryService_update(JNIEnv* env, jobject obj)  

  

static void android_server_BatteryService_update(JNIEnv* env, jobject obj)  

  

The required values are read from the Sys system:

#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_TECHNOLOGY_PATH "/sys/class/power_supply/battery/technology"   

  

#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_TECHNOLOGY_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.