USB充電插拔和usb Debugging connect提示

已開通新的博客,後續文字都會發到新博客

http://www.0xfree.top

---

USB充電插拔與USB Debugging connect提示

 

在 packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java 找到關於 USB Debug Enable 的代碼:

Java代碼

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

 

此文件中,將根據用戶設置將其值保存到 Settings 數據庫中。別處將根據其值動態變化做出相應動作

經搜索,在 frameworks/base/services/java/com/android/server/NotificationManagerService.java 中存在利用該值判斷是否在狀態欄中進行通知。代碼如下:

別處將根據其值動態變化做出相應動作如狀態欄消息提示。

 

Java代碼

  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. }

 

當激活時,在狀態欄中給出通知提示:

C-sharp代碼

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

 

通知的內容在資源字符串(英文)在字符串資源文件 frameworks/base/core/res/res/values/strings.xml 中,定義如下:

Xhtml代碼

  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>

 

改變該 Settings 值將通過如下方式影響到實際使用:

在文件中 frameworks/base/services/java/com/android/server/SystemServer.java

 

 

Java代碼

  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. }

 

 

可見,當設置系統屬性 persist.service.adb.enable 的值時,將影響到 adbd 守護進程相應動作 ( 停止和開啓 ) ,這將影響到是否查看 log 等供開發者使用的 adb 功能。

 

 

Bug 案例分析:

 

 

Java代碼

  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.notify"))) {
  6. Log.d(TAG, "return directly");
  7. return;
  8. }
  9. if (!mAdbNotificationShown) {
  10. //…省略部分代碼
  11. mAdbNotificationShown = true;
  12. notificationManager.notify(
  13. com.android.internal.R.string.adb_active_notification_title,
  14. mAdbNotification);
  15. }
  16. }
  17.  
  18. } else if (mAdbNotificationShown) {
  19. //…省略部分代碼
  20. mAdbNotificationShown = false;
  21. notificationManager.cancel(
  22. com.android.internal.R.string.adb_active_notification_title);
  23. }
  24. }
  25. }

 

 

症狀:當插上 USB 線與 PC 相連再拔掉時,才顯示“ USB Debugging connected ”

分析:通過上述介紹的代碼搜索,只有 NotificationManagerService.java 纔會出現此提示,也就是說只要當監測到用戶修改 Settings 中的設置值時和接收到 intent (即 usb 插拔充電)時纔會調用 updateAdbNotification() 函數:

log信息:

 

 

Xhtml代碼

  1. D/NotificationService( 1557): mBatteryPlugged=1
  2. D/NotificationService( 1557): 2. mBatteryPlugged=1
  3. D/NotificationService( 1557): mBatteryPlugged=1
  4. D/NotificationService( 1557): 2. mBatteryPlugged=1
  5. D/NotificationService( 1557): mBatteryPlugged=2
  6. D/NotificationService( 1557): 2. mBatteryPlugged=2
  7. D/NotificationService( 1557): adb enabled, Battery Plugged usb
  8. D/NotificationService( 1557): adb show notification
  9. D/NotificationService( 1557): mBatteryPlugged=0
  10. D/NotificationService( 1557): 2. mBatteryPlugged=0
  11. D/NotificationService( 1557): adb cancel notification
  12. D/NotificationService( 1557): mBatteryPlugged=0
  13. D/NotificationService( 1557): 2. mBatteryPlugged=0

 

 

結合 log 看出, mBatteryPlugged 在usb線連上時 爲 1 (即 BATTERY_PLUGGED_AC ,不是2即 BatteryManager.BATTERY_PLUGGED_USB )而不能進入,在拔掉瞬間爲 2 ,則發出提示;在拔掉之後爲0。

mBatteryPlugged 的值來自於 Intent :

 

Java代碼

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

 

它發起於下面代碼(見文件 frameworks/base/services/java/com/android/server/BatteryService.java )

 

C-sharp代碼

  1. private synchronized final void update() {
  2. native_update();//JNI層去讀取各種值
  3. boolean logOutlier = false;
  4. long dischargeDuration = 0;
  5.  
  6. mBatteryLevelCritical = mBatteryLevel <= CRITICAL_BATTERY_LEVEL;
  7. if (mAcOnline) {
  8. mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
  9. } else if (mUsbOnline) {
  10. mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
  11. } else {
  12. mPlugType = BATTERY_PLUGGED_NONE;
  13. }

 

在文件中 com_android_server_BatteryService.cpp 中,函數:

 

Java代碼

  1. static void android_server_BatteryService_update(JNIEnv* env, jobject obj)

 

會從 sys 系統下讀取需要的值:

 

C-sharp代碼

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

 

其中前 2 項標明瞭是 AC 還是 USB 充電。

因此,問題產生在系統底層識別 usb 充電信息錯誤,導致錯誤的時刻去顯示 USB Debugging connect 信息。

總結:

當系統的 BatteryService ( BatteryService.java )調用 JNI 層( com_android_server_BatteryService.cpp ),通過 sys 系統文件獲得充電(如 usb 充電)及電池信息,然後通過 intent 發送出去。 NotificationManagerService.java 在接收到廣播信息後,分析是 usb 充電則採取相應的提示信息。

另外, NotificationManagerService 還監聽着 Settings 裏用戶是否修改了設置的值,採取相應動作(是否更新提示信息、是否停掉或開啓 adbd 守護進程等)。

發佈了36 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章