Android O에서 충전 프롬프트의 몇 가지 추가 변경 사항

12946 단어 AudioAndroidO
네이티브 충전 알림음
문제: 설정 중 충전 알림음 켜기 옵션이 있지만 켜면 충전선을 꽂아도 충전음이 들리지 않는다. 실제 상황은 무선 충전이 필요할 때 소리를 낸다.
PowerManagerService.java에서 충전선을 삽입하면 업데이트 Is PoweredLocked ()
   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();
       }
       ......
  }

이어서 docked On Wireless Charger의 값이true일 경우play Wireless Charging Started Sound를 호출하여 충전 알림음 프레임워크/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의 업데이트 방법, 무선 충전만 가능한 충전 알림음 발견.
주: 유선에도 알림음이 있으면 온와이어스 Charging Started의 조건을 없앨 수 있지만, 이렇게 충전 알림음을 삽입하면 충전선을 뽑아도 알림음이 나온다.
유선 연결 충전 알림 소리 추가
기본 재생 방식
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: Notification Manager 서비스에서 브로드캐스트를 통해 프롬프트를 재생합니다.
수신: 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();  
                     }  
                 }  
            }               
       }

추가 방법 2: Battery Controller를 통해 브로드캐스트 재생 알림 소리 수신
수신: 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
            }

좋은 웹페이지 즐겨찾기