欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android rom开发:动态显示隐藏底部导航栏,默认显示3s后消失

程序员文章站 2024-02-26 22:45:28
...

时节如流,2019年快要结束了,立个flag,2020.1.1发布2019年个人总结,今天先来一篇技术干货。

项目需求要求动态显示/隐藏底部导航栏,默认显示3s后消失。设备默认是横屏显示。

思路:

PhoneWindowManager.java里面检测手势操作–>调用IStatusBarService.aidl里面的
接口方法–>StatusBarManagerService.java实现了IStatusBarService.aidl并且持有IStatusBar.aidl的实例对象–>CommandQueue.java实现了IStatusBar.aidl–>BaseStatusBar.java实现了CommandQueue.Callbacks–>PhoneStatusBar.java继承了BaseStatusBar.java

**方法:

新增show hide方法,通过层层调用,最终操作到PhoneStatusBar.java里面来控制显示和隐藏导航栏,注意在SystemGesturesPointerEventListener.java里面新增onSwipeFromLeft()向右滑的接口用来手动隐藏导航栏。

主要修改文件:
modified:   frameworks/base/core/java/com/android/internal/statusbar/IStatusBar.aidl
modified:   frameworks/base/core/java/com/android/internal/statusbar/IStatusBarService.aidl
modified:   frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
modified:   frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
modified:   frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java
modified:   frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
modified:   frameworks/base/policy/src/com/android/internal/policy/impl/SystemGesturesPointerEventListener.java
modified:   frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java

全部改动如下:

git diff frameworks
diff --git a/frameworks/base/core/java/com/android/internal/statusbar/IStatusBar.aidl b/frameworks/base/core/java/com/android/internal/statusbar/IStatusBar.aidl
index a3c0db4f34..18040da902 100644
--- a/frameworks/base/core/java/com/android/internal/statusbar/IStatusBar.aidl
+++ b/frameworks/base/core/java/com/android/internal/statusbar/IStatusBar.aidl
@@ -43,5 +43,7 @@ oneway interface IStatusBar
     void preloadRecentApps();
     void cancelPreloadRecentApps();
     void showScreenPinningRequest();
+    void showNavigationBar();
+    void hideNavigationBar();
 }
 
diff --git a/frameworks/base/core/java/com/android/internal/statusbar/IStatusBarService.aidl b/frameworks/base/core/java/com/android/internal/statusbar/IStatusBarService.aidl
index 40c009f3a4..2a2dd5616b 100644
--- a/frameworks/base/core/java/com/android/internal/statusbar/IStatusBarService.aidl
+++ b/frameworks/base/core/java/com/android/internal/statusbar/IStatusBarService.aidl
@@ -61,4 +61,6 @@ interface IStatusBarService
     void toggleRecentApps();
     void preloadRecentApps();
     void cancelPreloadRecentApps();
+    void showNavigationBar();
+    void hideNavigationBar();
 }
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
index 0b1b883810..61f1483f79 100644
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
@@ -57,6 +57,8 @@ public class CommandQueue extends IStatusBar.Stub {
     private static final int MSG_NOTIFICATION_LIGHT_OFF     = 16 << MSG_SHIFT;
     private static final int MSG_NOTIFICATION_LIGHT_PULSE   = 17 << MSG_SHIFT;
     private static final int MSG_SHOW_SCREEN_PIN_REQUEST    = 18 << MSG_SHIFT;
+    private static final int MSG_SHOW_NAVIGATIONBAR         = 19 << MSG_SHIFT;
+    private static final int MSG_HIDE_NAVIGATIONBAR         = 20 << MSG_SHIFT;
 
     public static final int FLAG_EXCLUDE_NONE = 0;
     public static final int FLAG_EXCLUDE_SEARCH_PANEL = 1 << 0;
@@ -84,6 +86,8 @@ public class CommandQueue extends IStatusBar.Stub {
         public void animateCollapsePanels(int flags);
         public void animateExpandSettingsPanel();
         public void setSystemUiVisibility(int vis, int mask);
+        public void showNavigationBar();
+        public void hideNavigationBar();
         public void topAppWindowChanged(boolean visible);
         public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
                 boolean showImeSwitcher);
@@ -150,6 +154,20 @@ public class CommandQueue extends IStatusBar.Stub {
         }
     }
 
+    public void showNavigationBar() {
+        synchronized (mList) {
+            mHandler.removeMessages(MSG_SHOW_NAVIGATIONBAR);
+            mHandler.sendEmptyMessage(MSG_SHOW_NAVIGATIONBAR);
+        }
+    }
+
+    public void hideNavigationBar() {
+        synchronized (mList) {
+            mHandler.removeMessages(MSG_HIDE_NAVIGATIONBAR);
+            mHandler.sendEmptyMessage(MSG_HIDE_NAVIGATIONBAR);
+        }
+    }
+
     public void setSystemUiVisibility(int vis, int mask) {
         synchronized (mList) {
             mHandler.removeMessages(MSG_SET_SYSTEMUI_VISIBILITY);
@@ -328,6 +346,12 @@ public class CommandQueue extends IStatusBar.Stub {
                 case MSG_SHOW_SCREEN_PIN_REQUEST:
                     mCallbacks.showScreenPinningRequest();
                     break;
+                case MSG_SHOW_NAVIGATIONBAR:
+                    mCallbacks.showNavigationBar();
+                    break;
+                case MSG_HIDE_NAVIGATIONBAR:
+                    mCallbacks.hideNavigationBar();
+                    break;
             }
         }
     }
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index d716b90ee4..ad450c181f 100644
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -619,6 +619,11 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
 
         addNavigationBar();
 
+        if (IS_ENABLE_AUTO_HIDE) {
+            mHideHandler.removeMessages(MSG_HIDE_NAVIGATIONBAR);
+            mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_NAVIGATIONBAR, 3000);
+        }
+
         // Lastly, call to the icon policy to install/update all the icons.
         mIconPolicy = new PhoneStatusBarPolicy(mContext, mCastController);
         mSettingsObserver.onChange(false); // set up
@@ -1291,6 +1296,87 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
         }
     }
 
+    //动态隐藏/显示导航栏,默认显示3s后消失
+    @Override
+    public void hideNavigationBar() {
+        if (!IS_ENABLE_AUTO_HIDE) {
+            return;
+        }
+
+        if (mNavigationBarView != null) {
+            mWindowManager.removeView(mNavigationBarView);
+            mNavigationBarView = null;
+        }
+    }
+
+    @Override
+    public void showNavigationBar() {
+        if (!IS_ENABLE_AUTO_HIDE || mNavigationBarView != null) {
+            return;
+        }
+        forceAddNavigationBar();
+    }
+
+    private void forceAddNavigationBar() {
+        mNavigationBarView = (NavigationBarView) View.inflate(mContext, R.layout.navigation_bar, null);
+        mNavigationBarView.setDisabledFlags(mDisabled);
+        mNavigationBarView.setBar(this);
+        addNavigationBar(true);
+    }
+
+    private void addNavigationBar(boolean forceReset) {
+        if (mNavigationBarView == null) {
+            return;
+        }
+
+        prepareNavigationBarView(forceReset);
+        mWindowManager.addView(mNavigationBarView, getNavigationBarLayoutParams());
+    }
+
+    private void prepareNavigationBarView(boolean forceReset) {
+        mNavigationBarView.reorient();
+        mNavigationBarView.getRecentsButton().setOnClickListener(mRecentsClickListener);
+        mNavigationBarView.getRecentsButton().setOnTouchListener(mRecentsPreloadOnTouchListener);
+        mNavigationBarView.getRecentsButton().setLongClickable(true);
+        mNavigationBarView.getRecentsButton().setOnLongClickListener(mLongPressBackRecentsListener);
+        mNavigationBarView.getBackButton().setLongClickable(true);
+        mNavigationBarView.getBackButton().setOnLongClickListener(mLongPressBackRecentsListener);
+        mNavigationBarView.getHomeButton().setOnTouchListener(mHomeActionListener);
+
+        if (forceReset) {
+            mNavigationBarMode = 0;
+            int newVal = mSystemUiVisibility;
+            mSystemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
+            setSystemUiVisibility(newVal, /*SYSTEM_UI_VISIBILITY_MASK*/0xffffffff);
+            int hints = mNavigationIconHints;
+            mNavigationIconHints = 0;
+            setNavigationIconHints(hints);
+        }
+
+        updateSearchPanel();
+
+        mHideHandler.removeMessages(MSG_HIDE_NAVIGATIONBAR);
+        mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_NAVIGATIONBAR, 3000);
+    }
+
+    private static final int MSG_HIDE_NAVIGATIONBAR = 0;
+    private static final boolean IS_ENABLE_AUTO_HIDE = true;
+    private HideHandler mHideHandler = new HideHandler();
+    private final class HideHandler extends Handler {
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_HIDE_NAVIGATIONBAR:
+                    hideNavigationBar();
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+    //动态隐藏/显示导航栏,默认显示3s后消失
+
     private void prepareNavigationBarView() {
         mNavigationBarView.reorient();
 
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java
index 08732e5be4..19f40c1a89 100644
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java
@@ -182,4 +182,12 @@ public class TvStatusBar extends BaseStatusBar {
     @Override
     public void showScreenPinningRequest() {
     }
+
+    @Override
+    public void showNavigationBar() {
+    }
+
+    @Override
+    public void hideNavigationBar() {
+    }
 }
diff --git a/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index cf2e543c15..e0f1267871 100644
--- a/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -1514,6 +1514,14 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                     public void onSwipeFromRight() {
                         if (mNavigationBar != null && !mNavigationBarOnBottom) {
                             requestTransientBars(mNavigationBar);
+                        } else {
+                            showNavigationBar(true);
+                        }
+                    }
+                    @Override
+                    public void onSwipeFromLeft() {
+                        if (mNavigationBar != null) {
+                            showNavigationBar(false);
                         }
                     }
                     @Override
@@ -1562,6 +1570,26 @@ public class PhoneWindowManager implements WindowManagerPolicy {
 
     }
 
+    private void showNavigationBar(final boolean isShowNavigationBar) {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    IStatusBarService statusbar = getStatusBarService();
+                    if (statusbar != null) {
+                        if (isShowNavigationBar) {
+                            statusbar.showNavigationBar();
+                        } else {
+                            statusbar.hideNavigationBar();
+                        }
+                    }
+                } catch (RemoteException e) {
+                    mStatusBarService = null;
+                }
+            }
+        });
+    }
+
     private void updateKeyAssignments() {
         final boolean hasMenu = (mDeviceHardwareKeys & KEY_MASK_MENU) != 0;
         final boolean hasHome = (mDeviceHardwareKeys & KEY_MASK_HOME) != 0;
diff --git a/frameworks/base/policy/src/com/android/internal/policy/impl/SystemGesturesPointerEventListener.java b/frameworks/base/policy/src/com/android/internal/policy/impl/SystemGesturesPointerEventListener.java
index 4ff9315707..82eff598dc 100644
--- a/frameworks/base/policy/src/com/android/internal/policy/impl/SystemGesturesPointerEventListener.java
+++ b/frameworks/base/policy/src/com/android/internal/policy/impl/SystemGesturesPointerEventListener.java
@@ -36,6 +36,7 @@ public class SystemGesturesPointerEventListener implements PointerEventListener
     private static final int SWIPE_FROM_TOP = 1;
     private static final int SWIPE_FROM_BOTTOM = 2;
     private static final int SWIPE_FROM_RIGHT = 3;
+    private static final int SWIPE_FROM_LEFT = 4;
 
     private final int mSwipeStartThreshold;
     private final int mSwipeDistanceThreshold;
@@ -99,6 +100,9 @@ public class SystemGesturesPointerEventListener implements PointerEventListener
                     } else if (swipe == SWIPE_FROM_RIGHT) {
                         if (DEBUG) Slog.d(TAG, "Firing onSwipeFromRight");
                         mCallbacks.onSwipeFromRight();
+                    } else if (swipe == SWIPE_FROM_LEFT) {
+                        if (DEBUG) Slog.d(TAG, "Firing onSwipeFromLeft");
+                        mCallbacks.onSwipeFromLeft();
                     }
                 }
                 break;
@@ -185,6 +189,10 @@ public class SystemGesturesPointerEventListener implements PointerEventListener
                 && elapsed < SWIPE_TIMEOUT_MS) {
             return SWIPE_FROM_RIGHT;
         }
+        if (fromX >= 1100 && x < screenWidth && x > fromX + mSwipeDistanceThreshold
+                && elapsed < SWIPE_TIMEOUT_MS) {
+            return SWIPE_FROM_LEFT;
+        }
         return SWIPE_NONE;
     }
 
@@ -192,6 +200,7 @@ public class SystemGesturesPointerEventListener implements PointerEventListener
         void onSwipeFromTop();
         void onSwipeFromBottom();
         void onSwipeFromRight();
+        void onSwipeFromLeft();
         void onDebug();
     }
 }
diff --git a/frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java b/frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
index cf2ed07425..f8d17c109b 100644
--- a/frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
@@ -378,6 +378,40 @@ public class StatusBarManagerService extends IStatusBarService.Stub {
         }
     }
 
+    @Override
+    public void showNavigationBar() {
+        enforceStatusBar();
+        synchronized(mLock) {
+            mHandler.post(new Runnable() {
+                public void run() {
+                    if (mBar != null) {
+                        try {
+                            mBar.showNavigationBar();
+                        } catch (RemoteException ex) {
+                       }
+                   }
+                }
+            });
+        }
+    }
+
+    @Override
+    public void hideNavigationBar() {
+        enforceStatusBar();
+        synchronized(mLock) {
+            mHandler.post(new Runnable() {
+                public void run() {
+                    if (mBar != null) {
+                        try {
+                            mBar.hideNavigationBar();
+                        } catch (RemoteException ex) {
+                       }
+                   }
+                }
+            });
+        }
+    }
+
     private void updateUiVisibilityLocked(final int vis, final int mask) {
         if (mSystemUiVisibility != vis) {
             mSystemUiVisibility = vis;

参考链接:
https://blog.csdn.net/way_ping_li/article/details/45727335