[高通MSM8909][Android8.1]虚拟导航栏增加音量加减按钮

开发平台基本信息

芯片: MSM8909
版本: Android 8.1
kernel: msm-3.18

问题描述

没有实体按键的机器,没办法快速调节音量大小,所以,需要在虚拟导航栏的左右两侧各添加一个按钮,一个控制音量增加,一个控制音量减少。

解决方法

效果图如下:在这里插入图片描述

1. 增加按钮图片资源

diff --git a/frameworks/base/packages/SystemUI/res/drawable/ic_increase_volume.xml b/frameworks/base/packages/SystemUI/res/drawable/ic_increase_volume.xml
new file mode 100755
index 0000000..1952a62
--- /dev/null
+++ b/frameworks/base/packages/SystemUI/res/drawable/ic_increase_volume.xml
@@ -0,0 +1,12 @@
+<vector android:height="20dp" android:viewportHeight="32"^M
+    android:viewportWidth="32" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M3,11.2309L3,20.3223C3,21.1672 3.5329,21.6157 4.3987,21.6719C6.3953,21.8013 9.8423,21.6719 9.8423,21.6719C9.8423,21.6719 14.474,24.5182 17.2825,26.7888C18.3299,27.6359 19.9198,27.5838 19.9198,25.766C19.9198,20.4699 19.9154,10.0464 19.9154,5.5095C19.9154,3.7479 18.3727,3.667 17.2132,4.5549C14.3441,6.7523 9.8423,9.9895 9.8423,9.9895L4.3987,9.9895C3.2982,9.9887 3,10.4731 3,11.2309Z"^M
+        android:strokeColor="#FFFFFF" android:strokeWidth="3"/>^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M22,23.9815C26.3586,23.9815 29.4304,20.2722 29.4304,15.9803C29.4304,11.6884 25.7152,8 22,8"^M
+        android:strokeColor="#FFFFFF" android:strokeWidth="3"/>^M
+    <path android:fillColor="#FFFFFF" android:fillType="nonZero"^M
+        android:pathData="M22,21C24.7614,21 27,18.7614 27,16C27,13.2386 24.7614,11 22,11C22,12.4064 22,17.6225 22,21Z"^M
+        android:strokeColor="#00000000" android:strokeWidth="1"/>^M
+</vector>
\ No newline at end of file
diff --git a/frameworks/base/packages/SystemUI/res/drawable/ic_increase_volume_dark.xml b/frameworks/base/packages/SystemUI/res/drawable/ic_increase_volume_dark.xml
new file mode 100755
index 0000000..44ba0b9
--- /dev/null
+++ b/frameworks/base/packages/SystemUI/res/drawable/ic_increase_volume_dark.xml
@@ -0,0 +1,12 @@
+<vector android:height="20dp" android:viewportHeight="32"^M
+    android:viewportWidth="32" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M3,11.2309L3,20.3223C3,21.1672 3.5329,21.6157 4.3987,21.6719C6.3953,21.8013 9.8423,21.6719 9.8423,21.6719C9.8423,21.6719 14.474,24.5182 17.2825,26.7888C18.3299,27.6359 19.9198,27.5838 19.9198,25.766C19.9198,20.4699 19.9154,10.0464 19.9154,5.5095C19.9154,3.7479 18.3727,3.667 17.2132,4.5549C14.3441,6.7523 9.8423,9.9895 9.8423,9.9895L4.3987,9.9895C3.2982,9.9887 3,10.4731 3,11.2309Z"^M
+        android:strokeColor="#5E5E5E" android:strokeWidth="3"/>^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M22,23.9815C26.3586,23.9815 29.4304,20.2722 29.4304,15.9803C29.4304,11.6884 25.7152,8 22,8"^M
+        android:strokeColor="#5E5E5E" android:strokeWidth="3"/>^M
+    <path android:fillColor="#5E5E5E" android:fillType="nonZero"^M
+        android:pathData="M22,21C24.7614,21 27,18.7614 27,16C27,13.2386 24.7614,11 22,11C22,12.4064 22,17.6225 22,21Z"^M
+        android:strokeColor="#00000000" android:strokeWidth="1"/>^M
+</vector>
\ No newline at end of file
diff --git a/frameworks/base/packages/SystemUI/res/drawable/ic_lower_volume.xml b/frameworks/base/packages/SystemUI/res/drawable/ic_lower_volume.xml
new file mode 100755
index 0000000..3055b7e
--- /dev/null
+++ b/frameworks/base/packages/SystemUI/res/drawable/ic_lower_volume.xml
@@ -0,0 +1,9 @@
+<vector android:height="20dp" android:viewportHeight="32"^M
+    android:viewportWidth="32" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M4,11.2309L4,20.3223C4,21.1672 4.5329,21.6157 5.3987,21.6719C7.3953,21.8013 10.8423,21.6719 10.8423,21.6719C10.8423,21.6719 15.474,24.5182 18.2825,26.7888C19.3299,27.6359 20.9198,27.5838 20.9198,25.766C20.9198,20.4699 20.9154,10.0464 20.9154,5.5095C20.9154,3.7479 19.3727,3.667 18.2132,4.5549C15.3441,6.7523 10.8423,9.9895 10.8423,9.9895L5.3987,9.9895C4.2982,9.9887 4,10.4731 4,11.2309Z"^M
+        android:strokeColor="#FFFFFF" android:strokeWidth="3"/>^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M23.7446,21.9616C26.6705,20.678 28.8996,19.1251 28.8996,15.8254C28.8996,12.5257 26.6705,10.9729 23.7446,9.69"^M
+        android:strokeColor="#FFFFFF" android:strokeWidth="3"/>^M
+</vector>
\ No newline at end of file
diff --git a/frameworks/base/packages/SystemUI/res/drawable/ic_lower_volume_dark.xml b/frameworks/base/packages/SystemUI/res/drawable/ic_lower_volume_dark.xml
new file mode 100755
index 0000000..81cff82
--- /dev/null
+++ b/frameworks/base/packages/SystemUI/res/drawable/ic_lower_volume_dark.xml
@@ -0,0 +1,9 @@
+<vector android:height="20dp" android:viewportHeight="32"^M
+    android:viewportWidth="32" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M4,11.2309L4,20.3223C4,21.1672 4.5329,21.6157 5.3987,21.6719C7.3953,21.8013 10.8423,21.6719 10.8423,21.6719C10.8423,21.6719 15.474,24.5182 18.2825,26.7888C19.3299,27.6359 20.9198,27.5838 20.9198,25.766C20.9198,20.4699 20.9154,10.0464 20.9154,5.5095C20.9154,3.7479 19.3727,3.667 18.2132,4.5549C15.3441,6.7523 10.8423,9.9895 10.8423,9.9895L5.3987,9.9895C4.2982,9.9887 4,10.4731 4,11.2309Z"^M
+        android:strokeColor="#5E5E5E" android:strokeWidth="3"/>^M
+    <path android:fillColor="#00000000" android:fillType="evenOdd"^M
+        android:pathData="M23.7446,21.9616C26.6705,20.678 28.8996,19.1251 28.8996,15.8254C28.8996,12.5257 26.6705,10.9729 23.7446,9.69"^M
+        android:strokeColor="#5E5E5E" android:strokeWidth="3"/>^M
+</vector>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/increase_volume.xml b/packages/SystemUI/res/layout/increase_volume.xml
new file mode 100755
index 0000000..eb3f3f5
--- /dev/null
+++ b/frameworks/base/packages/SystemUI/res/layout/increase_volume.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>^M
+<com.android.systemui.statusbar.policy.KeyButtonView^M
+xmlns:android="http://schemas.android.com/apk/res/android"^M
+xmlns:systemui="http://schemas.android.com/apk/res-auto"^M
+android:id="@+id/increase"^M
+android:layout_width="@dimen/navigation_key_width"^M
+android:layout_height="match_parent"^M
+android:layout_weight="0"^M
+systemui:keyCode="24"^M
+android:scaleType="center"^M
+android:contentDescription="@string/accessibility_home"^M
+android:paddingStart="@dimen/navigation_key_padding"^M
+android:paddingEnd="@dimen/navigation_key_padding"^M
+/>
\ No newline at end of file
diff --git a/frameworks/base/packages/SystemUI/res/layout/lower_volume.xml b/frameworks/base/packages/SystemUI/res/layout/lower_volume.xml
new file mode 100755
index 0000000..0f0149d
--- /dev/null
+++ b/frameworks/base/packages/SystemUI/res/layout/lower_volume.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>^M
+<com.android.systemui.statusbar.policy.KeyButtonView^M
+xmlns:android="http://schemas.android.com/apk/res/android"^M
+xmlns:systemui="http://schemas.android.com/apk/res-auto"^M
+android:id="@+id/lower"^M
+android:layout_width="@dimen/navigation_key_width"^M
+android:layout_height="match_parent"^M
+android:layout_weight="0"^M
+systemui:keyCode="25"^M
+android:scaleType="center"^M
+android:contentDescription="@string/accessibility_home"^M
+android:paddingStart="@dimen/navigation_key_padding"^M
+android:paddingEnd="@dimen/navigation_key_padding"^M
+/>

2.修改配置文件

配置文件不仅仅要修改values目录下的,其他分辨率下的配置文件也要修改,不然如果系统运行在其他大屏的设备下,就会系统奔溃或者出现其他问题。

diff --git a/packages/SystemUI/res/values-sw372dp/config.xml b/packages/SystemUI/res/values-sw372dp/config.xml
old mode 100644
new mode 100755
index 07b797a..e375a4f
--- a/packages/SystemUI/res/values-sw372dp/config.xml
+++ b/packages/SystemUI/res/values-sw372dp/config.xml
@@ -21,5 +21,5 @@
      for different hardware and product builds. -->
 <resources>
     <!-- Nav bar button default ordering/layout -->
-    <string name="config_navBarLayout" translatable="false">left[.25W],back[.5WC];home;recent[.5WC],right[.25W]</string>
+    <string name="config_navBarLayout" translatable="false">lower;back;home;recent;increase</string>
 </resources>
diff --git a/packages/SystemUI/res/values-sw600dp/config.xml b/packages/SystemUI/res/values-sw600dp/config.xml
old mode 100644
new mode 100755
index 63b9d75..85dc580
--- a/packages/SystemUI/res/values-sw600dp/config.xml
+++ b/packages/SystemUI/res/values-sw600dp/config.xml
@@ -34,7 +34,7 @@
     <bool name="config_keyguardUserSwitcher">true</bool>
 
     <!-- Nav bar button default ordering/layout -->
-    <string name="config_navBarLayout" translatable="false">left;back,home,recent;right</string>
+    <string name="config_navBarLayout" translatable="false">lower;back;home;recent;increase</string>
 
     <!-- Animation duration when using long press on recents to dock -->
     <integer name="long_press_dock_anim_duration">290</integer>
diff --git a/packages/SystemUI/res/values-sw900dp/config.xml b/packages/SystemUI/res/values-sw900dp/config.xml
old mode 100644
new mode 100755
index 221b013..33e0d65
--- a/packages/SystemUI/res/values-sw900dp/config.xml
+++ b/packages/SystemUI/res/values-sw900dp/config.xml
@@ -19,6 +19,6 @@
 <resources>
 
     <!-- Nav bar button default ordering/layout -->
-    <string name="config_navBarLayout" translatable="false">back,home,left;space;right,recent</string>
+    <string name="config_navBarLayout" translatable="false">lower;back;home;recent;increase</string>
 
 </resources>
diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml
index de159f0..a27250a 100755
--- a/packages/SystemUI/res/values/config.xml
+++ b/packages/SystemUI/res/values/config.xml
@@ -343,7 +343,7 @@
     <string name="config_systemUIFactoryComponent" translatable="false">com.android.systemui.SystemUIFactory</string>
 
     <!-- Nav bar button default ordering/layout -->
-    <string name="config_navBarLayout" translatable="false">left[.5W],back[1WC];home;recent[1WC],right[.5W]</string>
+    <string name="config_navBarLayout" translatable="false">lower;back;home;recent;increase</string>
 
     <bool name="quick_settings_show_full_alarm">false</bool>


3. 修改配置逻辑

--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
@@ -52,6 +52,8 @@ public class NavigationBarInflaterView extends FrameLayout
         implements Tunable, PluginListener<NavBarButtonProvider> {
 
     private static final String TAG = "NavBarInflater";
+    public static final String LOWER = "lower";
+    public static final String INCREASE = "increase";
 
     public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
     public static final String NAV_BAR_LEFT = "sysui_nav_bar_left";
@@ -218,11 +220,16 @@ public class NavigationBarInflaterView extends FrameLayout
         if (newLayout == null) {
             newLayout = getDefaultLayout();
         }
-        String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
-        String[] start = sets[0].split(BUTTON_SEPARATOR);
-        String[] center = sets[1].split(BUTTON_SEPARATOR);
-        String[] end = sets[2].split(BUTTON_SEPARATOR);
+        String[] sets = newLayout.split(GRAVITY_SEPARATOR, 5);
+               String[] lower = sets[0].split(BUTTON_SEPARATOR);
+        String[] start = sets[1].split(BUTTON_SEPARATOR);
+        String[] center = sets[2].split(BUTTON_SEPARATOR);
+        String[] end = sets[3].split(BUTTON_SEPARATOR);
+               String[] increase = sets[4].split(BUTTON_SEPARATOR);
         // Inflate these in start to end order or accessibility traversal will be messed up.
+               inflateButtons(lower, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
+        inflateButtons(lower, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
+               
         inflateButtons(start, mRot0.findViewById(R.id.ends_group), isRot0Landscape, true);
         inflateButtons(start, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, true);
 
@@ -234,6 +241,9 @@ public class NavigationBarInflaterView extends FrameLayout
 
         inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
         inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
+               
+               inflateButtons(increase, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
+        inflateButtons(increase, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
     }
 
     private void addGravitySpacer(LinearLayout layout) {
@@ -326,6 +336,10 @@ public class NavigationBarInflaterView extends FrameLayout
             v = inflater.inflate(R.layout.home, parent, false);
         } else if (BACK.equals(button)) {
             v = inflater.inflate(R.layout.back, parent, false);
+        }else if (LOWER.equals(button)) {
+            v = inflater.inflate(R.layout.lower_volume, parent, false);
+        }else if (INCREASE.equals(button)) {
+            v = inflater.inflate(R.layout.increase_volume, parent, false);
         } else if (RECENT.equals(button)) {
             v = inflater.inflate(R.layout.recent_apps, parent, false);
         } else if (MENU_IME.equals(button)) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
index 50e801c..6ffdc19 100755
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -93,6 +93,8 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
     private KeyButtonDrawable mImeIcon;
     private KeyButtonDrawable mMenuIcon;
     private KeyButtonDrawable mAccessibilityIcon;
+       private KeyButtonDrawable mLowerVolumeIcon;
+       private KeyButtonDrawable mIncreaseVolumeIcon;
 
     private GestureHelper mGestureHelper;
     private DeadZone mDeadZone;
@@ -214,15 +216,25 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
         updateIcons(context, Configuration.EMPTY, mConfiguration);
 
         mBarTransitions = new NavigationBarTransitions(this);
-
+        
+               mButtonDispatchers.put(R.id.lower, new ButtonDispatcher(R.id.lower));
         mButtonDispatchers.put(R.id.back, new ButtonDispatcher(R.id.back));
         mButtonDispatchers.put(R.id.home, new ButtonDispatcher(R.id.home));
         mButtonDispatchers.put(R.id.recent_apps, new ButtonDispatcher(R.id.recent_apps));
+               mButtonDispatchers.put(R.id.increase, new ButtonDispatcher(R.id.increase));
         mButtonDispatchers.put(R.id.menu, new ButtonDispatcher(R.id.menu));
         mButtonDispatchers.put(R.id.ime_switcher, new ButtonDispatcher(R.id.ime_switcher));
         mButtonDispatchers.put(R.id.accessibility_button,
                 new ButtonDispatcher(R.id.accessibility_button));
     }
+       
+       public ButtonDispatcher getLower_VolumeButton() {
+        return mButtonDispatchers.get(R.id.lower);
+    }
+       
+       public ButtonDispatcher getIncrease_VolumeButton() {
+        return mButtonDispatchers.get(R.id.increase);
+    }
 
     public BarTransitions getBarTransitions() {
         return mBarTransitions;
@@ -331,6 +343,10 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
             mRecentIcon = getDrawable(ctx,
                     R.drawable.ic_sysbar_recent, R.drawable.ic_sysbar_recent);
             mMenuIcon = getDrawable(ctx, R.drawable.ic_sysbar_menu, R.drawable.ic_sysbar_menu);
+                       mLowerVolumeIcon = getDrawable(ctx,
+                    R.drawable.ic_lower_volume, R.drawable.ic_lower_volume);
+                       mIncreaseVolumeIcon = getDrawable(ctx,
+                    R.drawable.ic_increase_volume, R.drawable.ic_increase_volume);
             mAccessibilityIcon = getDrawable(ctx, R.drawable.ic_sysbar_accessibility_button,
                     R.drawable.ic_sysbar_accessibility_button);
 
@@ -423,6 +439,8 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
                         != 0);
         getImeSwitchButton().setVisibility(showImeButton ? View.VISIBLE : View.INVISIBLE);
         getImeSwitchButton().setImageDrawable(mImeIcon);
+               getLower_VolumeButton().setImageDrawable(mLowerVolumeIcon);
+               getIncrease_VolumeButton().setImageDrawable(mIncreaseVolumeIcon);
 
         // Update menu button in case the IME state has changed.
         setMenuVisibility(mShowMenu, true);
@@ -452,6 +470,9 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
                         || ((disabledFlags & View.STATUS_BAR_DISABLE_RECENT) != 0);
         final boolean disableBack = ((disabledFlags & View.STATUS_BAR_DISABLE_BACK) != 0)
                 && ((mNavigationIconHints & StatusBarManager.NAVIGATION_HINT_BACK_ALT) == 0);
+                               
+           boolean disableLowerVolume = true;
+               boolean disableIncreaseVolume = true;
 
         ViewGroup navButtons = (ViewGroup) getCurrentView().findViewById(R.id.nav_buttons);
         if (navButtons != null) {
@@ -467,10 +488,18 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
             // Unless home is hidden, then in DPM locked mode and no exit available.
             disableRecent = false;
         }
+               
+               String internalModel = SystemProperties.get("ro.internal.model","unknown");
+       //  这里根据型号判断,不需要的设备就不显示
+        if(internalModel.equals("custom")){
+              disableLowerVolume = false;
+              disableIncreaseVolume = false;
+        }
 
         getBackButton().setVisibility(disableBack      ? View.INVISIBLE : View.VISIBLE);
         getHomeButton().setVisibility(disableHome      ? View.INVISIBLE : View.VISIBLE);
         getRecentsButton().setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
+               getLower_VolumeButton().setVisibility(disableLowerVolume ? View.GONE : View.VISIBLE);
+               getIncrease_VolumeButton().setVisibility(disableIncreaseVolume ? View.GONE : View.VISIBLE);
     }
 
     private boolean inLockTask() {

4. 延伸说明

从上面的补丁看,貌似没看到音量键的点击事件,那么是怎么做到,点击左边按钮减少音量,点击右边按钮增加音量的呢?其实,很简单,就是在按钮布局中,有这么一句话:systemui:keyCode="25";这句话的意思就是,点击的时候,模拟按键,上报按键事件是25;按键值与对应事件在Keyevent.java这个类中声明,路径是frameworks/base/core/java/android/view/KeyEvent.java;在这个类中搜索24、25对应的事件,刚好是对应音量的加、减。

    /** Key code constant: Directional Pad Center key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_CENTER     = 23;
    /** Key code constant: Volume Up key.
     * Adjusts the speaker volume up. */
    public static final int KEYCODE_VOLUME_UP       = 24;
    /** Key code constant: Volume Down key.
     * Adjusts the speaker volume down. */
    public static final int KEYCODE_VOLUME_DOWN     = 25;
    /** Key code constant: Power key. */
    public static final int KEYCODE_POWER           = 26;


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值