已開通新的博客,後續文字都會發到新博客
---
USB充電插拔與USB Debugging connect提示
在 packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java 找到關於 USB Debug Enable 的代碼:
Java代碼
- Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED,0);
此文件中,將根據用戶設置將其值保存到 Settings 數據庫中。別處將根據其值動態變化做出相應動作
經搜索,在 frameworks/base/services/java/com/android/server/NotificationManagerService.java 中存在利用該值判斷是否在狀態欄中進行通知。代碼如下:
別處將根據其值動態變化做出相應動作如狀態欄消息提示。
Java代碼
- 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();
- }
當激活時,在狀態欄中給出通知提示:
C-sharp代碼
- notificationManager.notify(
- com.android.internal.R.string.adb_active_notification_title,
- mAdbNotification);
通知的內容在資源字符串(英文)在字符串資源文件 frameworks/base/core/res/res/values/strings.xml 中,定義如下:
Xhtml代碼
- <!-- 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>
改變該 Settings 值將通過如下方式影響到實際使用:
在文件中 frameworks/base/services/java/com/android/server/SystemServer.java
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");
- }
- }
可見,當設置系統屬性 persist.service.adb.enable 的值時,將影響到 adbd 守護進程相應動作 ( 停止和開啓 ) ,這將影響到是否查看 log 等供開發者使用的 adb 功能。
Bug 案例分析:
Java代碼
- 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.notify"))) {
- Log.d(TAG, "return directly");
- return;
- }
- if (!mAdbNotificationShown) {
- //…省略部分代碼
- mAdbNotificationShown = true;
- notificationManager.notify(
- com.android.internal.R.string.adb_active_notification_title,
- mAdbNotification);
- }
- }
- } else if (mAdbNotificationShown) {
- //…省略部分代碼
- mAdbNotificationShown = false;
- notificationManager.cancel(
- com.android.internal.R.string.adb_active_notification_title);
- }
- }
- }
症狀:當插上 USB 線與 PC 相連再拔掉時,才顯示“ USB Debugging connected ”
分析:通過上述介紹的代碼搜索,只有 NotificationManagerService.java 纔會出現此提示,也就是說只要當監測到用戶修改 Settings 中的設置值時和接收到 intent (即 usb 插拔充電)時纔會調用 updateAdbNotification() 函數:
log信息:
Xhtml代碼
- 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
結合 log 看出, mBatteryPlugged 在usb線連上時 爲 1 (即 BATTERY_PLUGGED_AC ,不是2即 BatteryManager.BATTERY_PLUGGED_USB )而不能進入,在拔掉瞬間爲 2 ,則發出提示;在拔掉之後爲0。
mBatteryPlugged 的值來自於 Intent :
Java代碼
- mBatteryPlugged = intent.getIntExtra("plugged",0);
- updateAdbNotification();
它發起於下面代碼(見文件 frameworks/base/services/java/com/android/server/BatteryService.java )
C-sharp代碼
- private synchronized final void update() {
- native_update();//JNI層去讀取各種值
- 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;
- }
在文件中 com_android_server_BatteryService.cpp 中,函數:
Java代碼
- static void android_server_BatteryService_update(JNIEnv* env, jobject obj)
會從 sys 系統下讀取需要的值:
C-sharp代碼
- #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"
其中前 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 守護進程等)。