Android O 中充电提示音的几种添加更改方式

原生充电提示音

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

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

接着如果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);
      }

查看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      }

添加方式一:在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);

接收广播播放提示音

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;  
            final String soundPath = Settings.Global.getString(getContext().getContentResolver(),  
                            Settings.Global.WIRELESS_CHARGING_STARTED_SOUND); 

             if (enabled && soundPath != null) {  
                 final Uri soundUri = Uri.parse("file://" + soundPath);  
                 if (soundUri != null) {  
                     final Ringtone sfx = RingtoneManager.getRingtone(getContext(), soundUri);  
                     if (sfx != null) {  
                         sfx.setStreamType(AudioManager.STREAM_SYSTEM);  
                         sfx.play();  
                     }  
                 }  
            }               
       }

添加方式二:通过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);
            ......
        }

接收广播播放提示音

        @Override
        public void onReceive(Context context, Intent intent) {
            onReceiveInternal(intent);
        }

        @VisibleForTesting
        public void onReceiveInternal(Intent intent) {
            synchronized (mLock) {
            ......
                else if ( Intent.ACTION_POWER_CONNECTED.equals(action)) {
                //play charging tone
                //替换为定制的InsertUSB.ogg提示音
                final String soundPath = "/system/media/audio/ui/InsertUSB.ogg";
                final boolean enabled = Settings.Global.getInt(getContext().getContentResolver(),  
                            Settings.Global.CHARGING_SOUNDS_ENABLED, 1) != 0; 
                if (enabled && soundPath != null  ) {
                    final Uri soundUri = Uri.parse("file://" + soundPath);
                    if (soundUri != null) {
                        final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
                        if (sfx != null) {
                            AudioAttributes mAudioAttributes = new AudioAttributes.Builder(sfx.getAudioAttributes()).setLegacyStreamType(AudioManager.STREAM_SYSTEM).build();
                            sfx.setAudioAttributes(mAudioAttributes);
                            sfx.play();
                        }
                    }
                }
                //play charging tone
            }

你可能感兴趣的:(Audio,AndroidO)