1. 程式人生 > >Android O 交換虛擬按鍵位置

Android O 交換虛擬按鍵位置

1.找到虛擬按鍵

     overlay中直接進行修改)

       <string name="config_navBarLayout" translatable="false">left[.5W],back[1WC];home;recent[1WC],right[.5W]</string>

2.找到載入虛擬按鍵的方法

    getDefaultLayout方法中return mContext.getString(R.string.config_navBarLayout);

3.需要進行控制思路很簡單隻需要在getDefaultLayout方法中加個判斷語句進行控制即可,麻煩的是在設定中加入控制選項並更新SystemUI

3.1 在設定中的顯示設定加控制虛擬按鍵交換的選項並新增控制功能

3.1.1   在vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values/arrays.xml中新增

+    <string-array name="nav_back_key_entries">
+        <item>Back key to the left </item>
+        <item>Back key to the right</item>
+    </string-array>
+
+    <string-array name="nav_back_key_values" translatable="false">
+        <item>1</item>
+        <item>2</item>
+    </string-array>
3.1.2 在vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values/strings.xml中新增

+    <string name="nav_bar_key_right_title">Set back key</string>
3.1.3 在vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/display_settings.xml中新增

+        <ListPreference 
+                android:key="nav_bar_key_right"
+                android:title="@string/nav_bar_key_right_title"
+               android:entries="@array/nav_back_key_entries"
+               android:entryValues="@array/nav_back_key_values" />
3.1.4 在vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/DisplaySettings.java中新增
@@ -38,6 +38,7 @@ import com.android.settings.display.ThemePreferenceController;
 import com.android.settings.display.TimeoutPreferenceController;
 import com.android.settings.display.VrDisplayPreferenceController;
 import com.android.settings.display.WallpaperPreferenceController;
+import com.android.settings.display.NavigationController;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
 import com.android.settingslib.core.AbstractPreferenceController;
@@ -97,6 +98,7 @@ public class DisplaySettings extends DashboardFragment {
             Context context, Lifecycle lifecycle) {
         final List<AbstractPreferenceController> controllers = new ArrayList<>();
         controllers.add(new AutoBrightnessPreferenceController(context, KEY_AUTO_BRIGHTNESS));
+        controllers.add(new NavigationController(context));
         controllers.add(new AutoRotatePreferenceController(context, lifecycle));
         controllers.add(new CameraGesturePreferenceController(context));
         controllers.add(new FontSizePreferenceController(context));
3.1.5 新建類 vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/display/NavigationController.java
@@ -0,0 +1,72 @@
+package com.android.settings.display;
+import android.content.Context;
+import android.provider.Settings;
+import android.support.v7.preference.Preference;
+import com.android.settings.R;
+import android.util.Log;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.core.AbstractPreferenceController;
+import android.support.v7.preference.PreferenceScreen;
+import android.support.v7.preference.ListPreference;
+import android.os.SystemProperties;
+
+public class NavigationController extends AbstractPreferenceController implements
+        PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
+    private final String NavBarKey ="nav_bar_key_right";
+    private ListPreference mNavBackKeyRight;
+   
+    public NavigationController (Context context){
+     super(context);
+    }
+    @Override
+    public boolean isAvailable() {
+        return SystemProperties.get("ro.miki_backkey_change").equals("1");
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return NavBarKey;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        if (!isAvailable()) {
+            removePreference(screen, NavBarKey);
+            return;
+        }
+        mNavBackKeyRight = (ListPreference) screen.findPreference(
+                NavBarKey);
+        if (mNavBackKeyRight != null) {
+            int defaultValue = Settings.System.getInt(mContext.getContentResolver(), "navigation_bar_back_right", 1);
+            mNavBackKeyRight.setValue(String.valueOf(defaultValue));
+            mNavBackKeyRight.setSummary(mContext.getResources().getStringArray(R.array.nav_back_key_entries)[defaultValue - 1]);
+            mNavBackKeyRight.setOnPreferenceChangeListener(this);
+        }
+    }
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        try {
+            final int value = Integer.parseInt((String) newValue);
+           String summary = "";
+            switch (value) {
+                 case 1:
+                     Log.e("jiaz","case 1");
+                     summary = mContext.getResources().getStringArray(R.array.nav_back_key_entries)[0];
+                      break;
+                  case 2:
+                     Log.e("jiaz","case 2");
+                      summary = mContext.getResources().getStringArray(R.array.nav_back_key_entries)[1];
+                      break;
+                 default:
+                      summary = mContext.getResources().getStringArray(R.array.nav_back_key_entries)[0];
+                      break;
+            }
+           mNavBackKeyRight.setSummary(summary);
+           Settings.System.putInt(mContext.getContentResolver(), "navigation_bar_back_right", value);
+       } catch (NumberFormatException e) {
+            Log.e("jiaz", "could not persist night mode setting", e);
+            return false;
+        }
+        return true;
+    }
+}

3.1.6 在vendor/mediatek/proprietary/packages/apps/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java中國新增
@@ -2325,6 +2325,8 @@ class DatabaseHelper extends SQLiteOpenHelper {
                     R.bool.def_dim_screen);
             loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
                     R.integer.def_screen_off_timeout);
+            loadIntegerSetting(stmt, "navigation_bar_back_right",
+                    R.integer.def_nav_bar_back_right);
3.1.7 在vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java中新增
@@ -104,7 +104,7 @@ public class NavigationBarFragment extends Fragment implements Callbacks {
     protected AssistManager mAssistManager;
 
     private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
+    private BkRightContentObserver mBkRightObserver;
     private int mNavigationIconHints = 0;
     private int mNavigationBarMode;
     private AccessibilityManager mAccessibilityManager;
@@ -147,7 +147,9 @@ public class NavigationBarFragment extends Fragment implements Callbacks {
         mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
                 mMagnificationObserver, UserHandle.USER_ALL);
+        //add by jiaz
+        mBkRightObserver = new BkRightContentObserver(getContext().getMainThreadHandler());
+        mContentResolver.registerContentObserver(Settings.System.getUriFor("navigation_bar_back_right"),true,mBkRightObserver);
         if (savedInstanceState != null) {
             mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
         }
@@ -640,7 +642,22 @@ public class NavigationBarFragment extends Fragment implements Callbacks {
             NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
         }
     }
+    private  class BkRightContentObserver extends ContentObserver {
+        public BkRightContentObserver(Handler handler) {
+            super(handler);
+        }
+        @Override
+        public void onChange(boolean selfChange) {
+            Handler h = getView().getHandler();
+            Message msg = Message.obtain(h, () -> {
+            //jiaz
+                mNavigationBarView.onBKChanged();
+                repositionNavigationBar();
+          });
+            msg.setAsynchronous(true);
+            h.sendMessageAtFrontOfQueue(msg);
+        }
+     }
3.1.7 在vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java新增
@@ -47,6 +47,7 @@ import java.util.List;
 import java.util.Objects;
 import android.os.SystemProperties;//add by jiaz for change backkey
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
+import android.provider.Settings;
 
 public class NavigationBarInflaterView extends FrameLayout
         implements Tunable, PluginListener<NavBarButtonProvider> {
@@ -134,7 +135,8 @@ public class NavigationBarInflaterView extends FrameLayout
     }
 
     protected String getDefaultLayout() {
+        int bkRight = Settings.System.getInt(mContext.getContentResolver(), "navigation_bar_back_right", 1);
+        if(bkRight == 2){
+             return mContext.getString(R.string.config_navBarLayout_right);
             }else{
              return mContext.getString(R.string.config_navBarLayout);
@@ -436,4 +438,8 @@ public class NavigationBarInflaterView extends FrameLayout
         clearViews();
         inflateLayout(mCurrentLayout);
     }
+    public void changeBK(){
+        clearViews();
+        inflateLayout(getDefaultLayout());
+    }
 }

3.1.8 在vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java新增
@@ -883,7 +883,9 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
     public interface OnVerticalChangedListener {
         void onVerticalChanged(boolean isVertical);
     }
+    public void onBKChanged(){
+       ((NavigationBarInflaterView) findViewById(R.id.navigation_inflater)).changeBK();
+    }
     private final Consumer<Boolean> mDockedListener = exists -> mHandler.post(() -> {
         mDockedStackExists = exists;
         updateRecentsIcon();
3.1.9 在vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/config.xml中新增
@@ -327,7 +327,7 @@
 
     <!-- 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_right" translatable="false">left[.5W],recent[1WC];home;back[1WC],right[.5W]</string>