MTK平台相对比较少用于台式机研发上面,由于移动终端和平板都有物理按键,所以很少有添加虚拟按键的需求,但是对于一些台式机,可能没有物理的音量键,而且大屏操作时也希望导航栏上能有快捷关机键,因此有了这样的需求。网上其他的方案均存在问题,可以参考此方案快速导入。
由于13上默认导航栏不居中,因此首先修改导航栏居中显示,然后在添加按键,涉及修改的文件有
- .../android/server/policy/PhoneWindowManager.java | 5 +++
- .../android/launcher3/taskbar/TaskbarManager.java | 5 ++-
- .../apps/SystemUI/res/drawable-xxhdpi/ic_power.png | Bin 0 -> 2112 bytes
- .../SystemUI/res/drawable-xxhdpi/ic_volume.png | Bin 0 -> 1957 bytes
- .../SystemUI/res/drawable-xxxhdpi/ic_power.png | Bin 0 -> 2112 bytes
- .../SystemUI/res/drawable-xxxhdpi/ic_volume.png | Bin 0 -> 1957 bytes
- .../packages/apps/SystemUI/res/layout/power.xml | 28 +++++++++++++++
- .../packages/apps/SystemUI/res/layout/volume.xml | 27 +++++++++++++++
- .../apps/SystemUI/res/values-sw400dp/config.xml | 3 ++
- .../apps/SystemUI/res/values-sw600dp/config.xml | 2 +-
- .../apps/SystemUI/res/values-sw900dp/config.xml | 2 +-
- .../packages/apps/SystemUI/res/values/dimens.xml | 2 ++
- .../packages/apps/SystemUI/res/values/strings.xml | 2 ++
- .../shared/recents/utilities/Utilities.java | 4 +++
- .../systemui/navigationbar/NavigationBar.java | 38 ++++++++++++++++++++-
- .../navigationbar/NavigationBarInflaterView.java | 6 ++++
- .../systemui/navigationbar/NavigationBarView.java | 25 +++++++++++++-
- diff --git a/vendor/mediatek/proprietary/packages/apps/Launcher3/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java b/vendor/mediatek/proprietary/packages/apps/Launcher3/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
- index 7433669..fb200e5 100644
- --- a/vendor/mediatek/proprietary/packages/apps/Launcher3/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
- +++ b/vendor/mediatek/proprietary/packages/apps/Launcher3/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
- @@ -52,6 +52,7 @@ import com.android.systemui.unfold.UnfoldTransitionProgressProvider;
- import com.android.systemui.unfold.util.ScopedUnfoldTransitionProgressProvider;
-
- import java.io.PrintWriter;
- +import android.os.SystemProperties;
-
- /**
- * Class to manage taskbar lifecycle
- @@ -262,7 +263,9 @@ public class TaskbarManager {
-
- private void recreateTaskbar() {
- destroyExistingTaskbar();
- -
- + if (SystemProperties.get("persist.sys.taskbar.enable","0").equals("0")){
- + return ;
- + }
- DeviceProfile dp =
- mUserUnlocked ? LauncherAppState.getIDP(mContext).getDeviceProfile(mContext) : null;
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/shared/src/com/android/systemui/shared/recents/utilities/Utilities.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/shared/src/com/android/systemui/shared/recents/utilities/Utilities.java
- index 56326e3..88201b4 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/shared/src/com/android/systemui/shared/recents/utilities/Utilities.java
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/shared/src/com/android/systemui/shared/recents/utilities/Utilities.java
- @@ -31,6 +31,7 @@ import android.os.Message;
- import android.util.DisplayMetrics;
- import android.view.Surface;
- import android.view.WindowManager;
- +import android.os.SystemProperties;
-
- /* Common code */
- public class Utilities {
- @@ -128,6 +129,9 @@ public class Utilities {
- /** @return whether or not {@param context} represents that of a large screen device or not */
- @TargetApi(Build.VERSION_CODES.R)
- public static boolean isTablet(Context context) {
- + if (SystemProperties.get("persist.sys.taskbar.enable","0").equals("0")){
- + return false;
- + }
- final WindowManager windowManager = context.getSystemService(WindowManager.class);
- final Rect bounds = windowManager.getCurrentWindowMetrics().getBounds();
布局文件:
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/power.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/power.xml
- new file mode 100644
- index 0000000..bcb177c
- --- /dev/null
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/power.xml
- @@ -0,0 +1,28 @@
- +"1.0" encoding="utf-8"?>
- +
- +<com.android.systemui.navigationbar.buttons.KeyButtonView
- + xmlns:android="http://schemas.android.com/apk/res/android"
- + xmlns:systemui="http://schemas.android.com/apk/res-auto"
- + android:id="@+id/power"
- + android:layout_width="@dimen/navigation_key_width"
- + android:layout_height="match_parent"
- + android:layout_weight="0"
- + android:scaleType="center"
- + android:contentDescription="@string/accessibility_power"
- + android:paddingStart="@dimen/navigation_key_padding"
- + android:paddingEnd="@dimen/navigation_key_padding"
- + />
- +
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/volume.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/volume.xml
- new file mode 100644
- index 0000000..ed6c939
- --- /dev/null
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/volume.xml
- @@ -0,0 +1,27 @@
- +"1.0" encoding="utf-8"?>
- +
- +<com.android.systemui.navigationbar.buttons.KeyButtonView
- + xmlns:android="http://schemas.android.com/apk/res/android"
- + xmlns:systemui="http://schemas.android.com/apk/res-auto"
- + android:id="@+id/volume"
- + android:layout_width="@dimen/navigation_key_width"
- + android:layout_height="match_parent"
- + android:layout_weight="0"
- + android:scaleType="center"
- + android:contentDescription="@string/accessibility_volume"
- + android:paddingStart="@dimen/navigation_key_padding"
- + android:paddingEnd="@dimen/navigation_key_padding"
- + />
资源文件(根据自己分辨率放到指定路径就行了):
vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable-xxhdpi/ic_power.png
vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable-xxhdpi/ic_volume.png
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw400dp/config.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw400dp/config.xml
- new file mode 100644
- index 0000000..0cc515c
- --- /dev/null
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw400dp/config.xml
- @@ -0,0 +1,3 @@
- +<resources>
- + <string name="config_navBarLayout" translatable="false">left;poweroff,back,home,recent,volume;rightstring>
- +resources>
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw600dp/config.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw600dp/config.xml
- index 36cc0ad..ba3a959 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw600dp/config.xml
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw600dp/config.xml
- @@ -27,7 +27,7 @@
- <bool name="config_quickSettingsMediaLandscapeCollapsed">falsebool>
-
-
- - <string name="config_navBarLayout" translatable="false">left;back,home,recent;rightstring>
- + <string name="config_navBarLayout" translatable="false">left;power,back,home,recent,volume;rightstring>
-
-
- <integer name="navigation_bar_deadzone_orientation">0integer>
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw900dp/config.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw900dp/config.xml
- index 221b013..99f2d56 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw900dp/config.xml
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values-sw900dp/config.xml
- @@ -19,6 +19,6 @@
- <resources>
-
-
- - <string name="config_navBarLayout" translatable="false">back,home,left;space;right,recentstring>
- + <string name="config_navBarLayout" translatable="false">power,back,home,left;space;right,recent,volumestring>
-
- resources>
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/dimens.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/dimens.xml
- index b6a2815..3b09c06 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/dimens.xml
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/dimens.xml
- @@ -49,6 +49,8 @@
- <dimen name="navigation_edge_arrow_min_y">64dpdimen>
-
- <dimen name="navigation_edge_finger_offset">48dpdimen>
- + <dimen name="navigation_key_width_sw600dp_land">162dpdimen>
- + <dimen name="navigation_key_padding_sw600dp_land">42dpdimen>
-
-
- <dimen name="status_bar_icon_size">@*android:dimen/status_bar_icon_sizedimen>
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/strings.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/strings.xml
- index e4607b3..b6c2866 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/strings.xml
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/strings.xml
- @@ -2545,4 +2545,6 @@
- =1 {# notification}
- other {# notifications}
- }string>
- + <string name="accessibility_power">Powerstring>
- + <string name="accessibility_volume">Volumestring>
- resources>
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarInflaterView.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarInflaterView.java
- index 59bb227..ed9e3fc 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarInflaterView.java
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarInflaterView.java
- @@ -68,6 +68,8 @@ public class NavigationBarInflaterView extends FrameLayout
- public static final String RIGHT = "right";
- public static final String CONTEXTUAL = "contextual";
- public static final String IME_SWITCHER = "ime_switcher";
- + public static final String POWER = "power";
- + public static final String VOLUME = "volume";
-
- public static final String GRAVITY_SEPARATOR = ";";
- public static final String BUTTON_SEPARATOR = ",";
- @@ -387,6 +389,10 @@ public class NavigationBarInflaterView extends FrameLayout
- v = inflater.inflate(R.layout.home_handle, parent, false);
- } else if (IME_SWITCHER.equals(button)) {
- v = inflater.inflate(R.layout.ime_switcher, parent, false);
- + } else if(POWER.equals(button)) {
- + v = inflater.inflate(R.layout.power, parent, false);
- + } else if(VOLUME.equals(button)) {
- + v = inflater.inflate(R.layout.volume, parent, false);
- } else if (button.startsWith(KEY)) {
- String uri = extractImage(button);
- int code = extractKeycode(button);
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java
- index 3fc9afe..11d9e7f 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java
- @@ -92,7 +92,7 @@ import java.util.function.Consumer;
-
- /** */
- public class NavigationBarView extends FrameLayout {
- - final static boolean DEBUG = false;
- + final static boolean DEBUG = true;
- final static String TAG = "NavBarView";
-
- final static boolean ALTERNATE_CAR_MODE_UI = false;
- @@ -119,6 +119,9 @@ public class NavigationBarView extends FrameLayout {
- private Context mLightContext;
- private int mLightIconColor;
- private int mDarkIconColor;
- + private KeyButtonDrawable mPowerIcon;
- + private KeyButtonDrawable mVolumeIcon;
- +
-
- private EdgeBackGestureHandler mEdgeBackGestureHandler;
- private final DeadZone mDeadZone;
- @@ -316,6 +319,8 @@ public class NavigationBarView extends FrameLayout {
- mButtonDispatchers.put(R.id.ime_switcher, imeSwitcherButton);
- mButtonDispatchers.put(R.id.accessibility_button, accessibilityButton);
- mButtonDispatchers.put(R.id.menu_container, mContextualButtonGroup);
- + mButtonDispatchers.put(R.id.power, new ButtonDispatcher(R.id.power));
- + mButtonDispatchers.put(R.id.volume, new ButtonDispatcher(R.id.volume));
- mDeadZone = new DeadZone(this);
- }
-
- @@ -412,6 +417,14 @@ public class NavigationBarView extends FrameLayout {
- return mButtonDispatchers.get(R.id.accessibility_button);
- }
-
- + public ButtonDispatcher getPowerButton() {
- + return mButtonDispatchers.get(R.id.power);
- + }
- +
- + public ButtonDispatcher getVolumeButton() {
- + return mButtonDispatchers.get(R.id.volume);
- + }
- +
- public RotationContextButton getRotateSuggestionButton() {
- return (RotationContextButton) mButtonDispatchers.get(R.id.rotate_suggestion);
- }
- @@ -456,6 +469,8 @@ public class NavigationBarView extends FrameLayout {
- if (orientationChange || densityChange || dirChange) {
- mBackIcon = getBackDrawable();
- }
- + mPowerIcon = getDrawable(R.drawable.ic_power);
- + mVolumeIcon = getDrawable(R.drawable.ic_volume);
- }
-
- /**
- @@ -594,11 +609,15 @@ public class NavigationBarView extends FrameLayout {
- KeyButtonDrawable backIcon = mBackIcon;
- orientBackButton(backIcon);
- KeyButtonDrawable homeIcon = mHomeDefaultIcon;
- + KeyButtonDrawable powerIcon = mPowerIcon;
- + KeyButtonDrawable volumeIcon = mVolumeIcon;
- if (!mUseCarModeUi) {
- orientHomeButton(homeIcon);
- }
- getHomeButton().setImageDrawable(homeIcon);
- getBackButton().setImageDrawable(backIcon);
- + getPowerButton().setImageDrawable(powerIcon);
- + getVolumeButton().setImageDrawable(volumeIcon);
-
- updateRecentsIcon();
-
- @@ -652,6 +671,8 @@ public class NavigationBarView extends FrameLayout {
- getHomeButton().setVisibility(disableHome ? View.INVISIBLE : View.VISIBLE);
- getRecentsButton().setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
- getHomeHandle().setVisibility(disableHomeHandle ? View.INVISIBLE : View.VISIBLE);
- + getPowerButton().setVisibility(false ? View.INVISIBLE : View.VISIBLE);
- + getVolumeButton().setVisibility(false ? View.INVISIBLE : View.VISIBLE);
- notifyActiveTouchRegions();
- }
-
- @@ -1122,6 +1143,8 @@ public class NavigationBarView extends FrameLayout {
-
- dumpButton(pw, "back", getBackButton());
- dumpButton(pw, "home", getHomeButton());
- + dumpButton(pw, "volume", getVolumeButton());
- + dumpButton(pw, "power", getPowerButton());
- dumpButton(pw, "handle", getHomeHandle());
- dumpButton(pw, "rcnt", getRecentsButton());
- dumpButton(pw, "rota", getRotateSuggestionButton());
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java b/vendor/mediatek/proprietary/packages/apps/Syst
- emUI/src/com/android/systemui/navigationbar/NavigationBar.java
- index 91b01ef..9634cb0 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
- @@ -1787,11 +1787,15 @@ public class NavigationBar extends ViewController
implements - Region region = new Region();
- Map
touchRegionCache = mView.getButtonTouchRegionCache(); - updateButtonLocation(
- + region, touchRegionCache, mView.getPowerButton(), inScreenSpace, useNearestRegion);
- + updateButtonLocation(
- region, touchRegionCache, mView.getBackButton(), inScreenSpace, useNearestRegion);
- updateButtonLocation(
- region, touchRegionCache, mView.getHomeButton(), inScreenSpace, useNearestRegion);
- updateButtonLocation(region, touchRegionCache, mView.getRecentsButton(), inScreenSpace,
- useNearestRegion);
- + updateButtonLocation(
- + region, touchRegionCache, mView.getVolumeButton(), inScreenSpace, useNearestRegion);
- updateButtonLocation(region, touchRegionCache, mView.getImeSwitchButton(), inScreenSpace,
- useNearestRegion);
- updateButtonLocation(
6.按键发送广播或调用相关服务
注:打叉的地方修改成自己喜欢的名字即可
- diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
- index aa38b78..91b01ef 100644
- --- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
- +++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
- @@ -158,6 +158,7 @@ import com.android.systemui.util.DeviceConfigProxy;
- import com.android.systemui.util.ViewController;
- import com.android.wm.shell.back.BackAnimation;
- import com.android.wm.shell.pip.Pip;
- +import android.media.AudioManager;
-
- import java.io.PrintWriter;
- import java.util.Locale;
- @@ -177,7 +178,7 @@ import dagger.Lazy;
- public class NavigationBar extends ViewController
implements Callbacks { -
- public static final String TAG = "NavigationBar";
- - private static final boolean DEBUG = false;
- + private static final boolean DEBUG = true;
- private static final String EXTRA_DISABLE_STATE = "disabled_state";
- private static final String EXTRA_DISABLE2_STATE = "disabled2_state";
- private static final String EXTRA_APPEARANCE = "appearance";
- @@ -1260,6 +1261,13 @@ public class NavigationBar extends ViewController
implements - accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
- updateAccessibilityStateFlags();
-
- + ButtonDispatcher powerButton = mView.getPowerButton();
- + powerButton.setOnClickListener(this::powerClick);
- + powerButton.setOnTouchListener(this::powerTouch);
- +
- + ButtonDispatcher volumeButton = mView.getVolumeButton();
- + volumeButton.setOnClickListener(this::volumeClick);
- +
- ButtonDispatcher imeSwitcherButton = mView.getImeSwitchButton();
- imeSwitcherButton.setOnClickListener(this::onImeSwitcherClick);
-
- @@ -1450,6 +1458,30 @@ public class NavigationBar extends ViewController
implements - return false;
- }
-
- + private boolean powerTouch(View v, MotionEvent event) {
- + if (event.getAction() == MotionEvent.ACTION_UP) {
- + Log.i("xxxxx","powerTouch--sendBroadcast");
- + Intent intent=new Intent("com.xxxxx.systemui.power");
- + mContext.sendBroadcast(intent);
- + }
- + return false;
- + }
- +
- + private void powerClick(View v) {
- + Log.i("xxxx","powerClick--sendBroadcast");
- + Intent intent=new Intent("com.xxxxx.systemui.power");
- + mContext.sendBroadcast(intent);
- + }
- +
- + private void volumeClick(View v) {
- + Log.i("xxxxx","volumeClick--sendBroadcast");
- + AudioManager am = getContext().getSystemService(AudioManager.class);
- + am.adjustSuggestedStreamVolume(
- + AudioManager.ADJUST_SAME,
- + AudioManager.STREAM_MUSIC,
- + AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
- + }
- +
- private void onAccessibilityClick(View v) {
- final Display display = v.getDisplay();
- mAccessibilityManager.notifyAccessibilityButtonClicked(
注:打叉的地方修改成自己喜欢的名字即可
- diff --git a/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java b/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
- index 0b61a84..a3d9745 100644
- --- a/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
- +++ b/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
- @@ -2071,6 +2071,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
- filter = new IntentFilter();
- filter.addAction(Intent.ACTION_DREAMING_STARTED);
- filter.addAction(Intent.ACTION_DREAMING_STOPPED);
- + filter.addAction("com.xxxx.systemui.power");
- context.registerReceiver(mDreamReceiver, filter);
-
- // register for multiuser-relevant broadcasts
- @@ -4449,6 +4450,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
- BroadcastReceiver mDreamReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- + Log.i("xxxx", "mDreamReceiver--onReceive");
- if (Intent.ACTION_DREAMING_STARTED.equals(intent.getAction())) {
- if (mKeyguardDelegate != null) {
- mKeyguardDelegate.onDreamingStarted();
- @@ -4457,6 +4459,9 @@ public class PhoneWindowManager implements WindowManagerPolicy {
- if (mKeyguardDelegate != null) {
- mKeyguardDelegate.onDreamingStopped();
- }
- + } else if("com.xxxx.systemui.power".equals(intent.getAction())) {
- + Log.i("xxxxx", "Receive a soft poweroff broadcast.");
- + showGlobalActionsInternal();
- }
- }
- };
以上修改完成后,整体编译然后烧录即可实现虚拟按键添加及事件响应