Android 添加充拔电提示音

原生充电提示音

问题:在设置中有充电提示音的打开选项,但打开后,插上充电线也是听不到充电音
实际情况:需要无线充电的时候才会发出声音

PowerManagerService.java中插拔充电线会执行updateIsPoweredLocked()

   private void updateIsPoweredLocked(int dirty) {
       ...... 
       // Update wireless dock detection state.
       final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update(
                          mIsPowered, mPlugType, mBatteryLevel);
       ......
       // Tell the notifier whether wireless charging has started so that
       // it can provide feedback to the user.
       if (dockedOnWirelessCharger) {
            mNotifier.onWirelessChargingStarted();
       }
       ......
  }
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

接着如果dockedOnWirelessCharger的值为true调用playWirelessChargingStartedSound 播放充电提示音
frameworks/base/services/core/java/com/android/server/power/Notifier.java

     /**
       * Called when wireless charging has started so as to provide user feedback.
       */
      public void onWirelessChargingStarted() {
          if (DEBUG) {
              Slog.d(TAG, "onWirelessChargingStarted");
          }

          mSuspendBlocker.acquire();
          Message msg = mHandler.obtainMessage(MSG_WIRELESS_CHARGING_STARTED);
          msg.setAsynchronous(true);
          mHandler.sendMessage(msg);
      }
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

查看android/frameworks/base/services/core/java/com/android/server/power/WirelessChargerDetector.java的update方法,发现只有无线充电的情况下才有充电提示音。

注:如果要有线也有提示音可以将onWirelessChargingStarted的条件去掉,但是这样插入充电播放提示音,拔掉充电线也会有提示音。

添加有线连接充电的提示音

原生的播放方式

android/frameworks/base/services/core/java/com/android/server/power/Notifier.java

     private void playWirelessChargingStartedSound() {
701          final boolean enabled = Settings.Global.getInt(mContext.getContentResolver(),
702                  Settings.Global.CHARGING_SOUNDS_ENABLED, 1) != 0;
703          final String soundPath = Settings.Global.getString(mContext.getContentResolver(),
704                  Settings.Global.WIRELESS_CHARGING_STARTED_SOUND);
705          if (enabled && soundPath != null) {
706              final Uri soundUri = Uri.parse("file://" + soundPath);
707              if (soundUri != null) {
708                  final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
709                  if (sfx != null) {
710                      sfx.setStreamType(AudioManager.STREAM_SYSTEM);
711                      sfx.play();
712                  }
713              }
714          }
715  
716          mSuspendBlocker.release();
717      }
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

添加方式一:在NotificationManagerService中通过接收广播播放提示音

接收:Intent.ACTION_POWER_CONNECTED
android/frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java
在onStart()中注册广播

     @Override
1340     public void onStart() {
         ......
         // register for various Intents
1371          IntentFilter filter = new IntentFilter();
1372          filter.addAction(Intent.ACTION_SCREEN_ON);
1373          filter.addAction(Intent.ACTION_SCREEN_OFF);
1374          filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
1375          filter.addAction(Intent.ACTION_USER_PRESENT);
1376          filter.addAction(Intent.ACTION_USER_STOPPED);
1377          filter.addAction(Intent.ACTION_USER_SWITCHED);
              filter.addAction(Intent.ACTION_POWER_CONNECTED);//添加Intent Action
              ......
              getContext().registerReceiver(mIntentReceiver, filter);
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

接收广播播放提示音

private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        ......
        }else if (action.equals(Intent.ACTION_POWER_CONNECTED)){
            final boolean enabled = Settings.Global.getInt(getContext().getContentResolver(),  
                            Settings.Global.CHARGING_SOUNDS_ENABLED, 1) != 0;          

            if (enabled) {  
                 final Uri soundUri = Uri.parse("file://" + "/system/media/audio/ui/WirelessChargingStarted.ogg");  
                 if (soundUri != null) {  
                     final Ringtone sfx = RingtoneManager.getRingtone(getContext(), soundUri);  
                     if (sfx != null) {  
                         sfx.setStreamType(AudioManager.STREAM_SYSTEM);  
                         sfx.play();  
                     }  
                 }  
            }               
       }

   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

添加方式二:通过BatteryController接收广播播放提示音

接收:Intent.ACTION_POWER_CONNECTED
android/frameworks/base/services/core/java/com/android/server/job/controllers/BatteryController.java
在startTracking()中注册广播

        public void startTracking() {
            IntentFilter filter = new IntentFilter();
            // Battery health.
            filter.addAction(Intent.ACTION_BATTERY_LOW);
            filter.addAction(Intent.ACTION_BATTERY_OKAY);
            // Charging/not charging.
            filter.addAction(BatteryManager.ACTION_CHARGING);
            filter.addAction(BatteryManager.ACTION_DISCHARGING);

            filter.addAction(Intent.ACTION_POWER_CONNECTED);

            mContext.registerReceiver(this, filter);
            ......
        }

添加音频文件

路径: /frameworks/base/data/sounds/AudioPackageGo.mk

PRODUCT_COPY_FILES += \
     $(LOCAL_PATH)/notifications/ogg/Alya.ogg:system/media/audio/notifications/Alya.ogg \
     $(LOCAL_PATH)/notifications/ogg/Argon.ogg:system/media/audio/notifications/Argon.ogg \
     $(LOCAL_PATH)/notifications/Canopus.ogg:system/media/audio/notifications/Canopus.ogg \
     $(LOCAL_PATH)/effects/ogg/LowBattery.ogg:system/media/audio/ui/LowBattery.ogg \
+    $(LOCAL_PATH)/effects/ogg/WirelessChargingStarted.ogg:system/media/audio/ui/WirelessChargingStarted.ogg \

你可能感兴趣的:(系统修改)