Android P 修改音量键控制与方向和UI一致
作者:互联网
frameworks/base/services/core/java/com/android/server/media/MediaSessionService.java
--- a/mt8788/frameworks/base/services/core/java/com/android/server/media/MediaSessionService.java
+++ b/mt8788/frameworks/base/services/core/java/com/android/server/media/MediaSessionService.java
@@ -81,7 +81,7 @@ import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.KeyEvent;
import android.view.ViewConfiguration;
-
+import android.view.WindowManager;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.DumpUtils;
import com.android.server.SystemService;
@@ -1434,13 +1434,30 @@ public class MediaSessionService extends SystemService implements Monitor {
boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
int direction = 0;
+ int rotation = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
+ int device_id = keyEvent.getDeviceId();
+ Log.d("addlog", "====================rotation = "+ rotation);
boolean isMute = false;
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
- direction = AudioManager.ADJUST_RAISE;
+ if(device_id==1){
+ direction = (rotation == 0 || rotation == 1) ? AudioManager.ADJUST_LOWER : AudioManager.ADJUST_RAISE;
+ }else{
+ direction = AudioManager.ADJUST_RAISE;
+ }
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
- direction = AudioManager.ADJUST_LOWER;
+ if(device_id==1){
+ direction = (rotation == 0 || rotation == 1) ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER;
+ }else{
+ direction = AudioManager.ADJUST_LOWER;
+ }
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
isMute = true;
注:Android O还需修改如下文件:
frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java
--- a/mt8167/frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java
+++ b/mt8167/frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java
@@ -1866,6 +1866,11 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
//Log.i(TAG, "Key down: repeat=" + event.getRepeatCount()
// + " flags=0x" + Integer.toHexString(event.getFlags()));
+ int rotation = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
+ int device_id = event.getDeviceId();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
@@ -1876,10 +1881,22 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
int direction = 0;
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
- direction = AudioManager.ADJUST_RAISE;
+ if(device_id==1){
+ direction = (rotation == 2 || rotation == 1) ? AudioManager.ADJUST_LOWER : AudioManager.ADJUST_RAISE;
+ }else{
+ direction = AudioManager.ADJUST_RAISE;
+ }
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
- direction = AudioManager.ADJUST_LOWER;
+ if(device_id==1){
+ direction = (rotation == 2 || rotation == 1) ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER;
+ }else{
+ direction = AudioManager.ADJUST_LOWER;
+ }
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
direction = AudioManager.ADJUST_TOGGLE_MUTE;
标签:java,direction,rotation,AudioManager,ADJUST,音量,UI,Android,android 来源: https://blog.csdn.net/jydzm/article/details/97369702