【干货】Android系统定制基础篇:第十三部分(开放root权限、禁止应用旋转、隐藏状态栏和导航栏)

一、Android开放root权限

Android 5.1
1、修改 su 源码(system\extras\su\su.c),注释下面代码:

int main(int argc, char **argv)
{
    struct passwd *pw;
    uid_t uid, myuid;
    gid_t gid, gids[10];

    /* Until we have something better, only root and the shell can use su. */
    /* 注释下面代码
    myuid = getuid();
    if (myuid != AID_ROOT && myuid != AID_SHELL) {
        fprintf(stderr,"su: uid %d not allowed to su\n", myuid);
        return 1;
    }
    */

    if(argc < 2) {
        uid = gid = 0;
    } else {
        int gids_count = sizeof(gids)/sizeof(gids[0]);
        extract_uidgids(argv[1], &uid, &gid, gids, &gids_count);
        if(gids_count) {
            if(setgroups(gids_count, gids)) {
                fprintf(stderr, "su: failed to set groups\n");
                return 1;
            }
        }
    }

2、修改(system\core\include\private\android_filesystem_config.h)默认权限改为 『04755』

- { 04750, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },
+ { 04755, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },

3、编译

Android$ mmm system/extras/su

4、替换 su 程序

$ adb push su system/xbin/

5、修改权限

$ adb shell
$ chmod 04755 system/xbin/su

6、APP 中获取 root 权限

Process process = Runtime.getRuntime().exec("su");

Android 8.1

commit 47fd12ee992aac429a501b3d00fc3435ba67b137
Author: shenhb <shenhb@topband.com.cn>
Date:   Sun Dec 16 15:56:37 2018 +0800

    支持APP获取root权限

diff --git a/device/rockchip/common/BoardConfig.mk b/device/rockchip/common/BoardConfig.mk
index 85e083f..4ae7370 100755
--- a/device/rockchip/common/BoardConfig.mk
+++ b/device/rockchip/common/BoardConfig.mk
@@ -387,4 +387,7 @@ CAMERA_SUPPORT_AUTOFOCUS ?= false
 BOARD_USB_ALLOW_DEFAULT_MTP ?= false
 
 HIGH_RELIABLE_RECOVERY_OTA := false
-BOARD_USES_FULL_RECOVERY_IMAGE := false
\ No newline at end of file
+BOARD_USES_FULL_RECOVERY_IMAGE := false
+
+#root
+CUSTOM_ROOT ?= true
\ No newline at end of file
diff --git a/frameworks/base/cmds/app_process/Android.mk b/frameworks/base/cmds/app_process/Android.mk
old mode 100644
new mode 100755
index 72fe051..73b8600
--- a/frameworks/base/cmds/app_process/Android.mk
+++ b/frameworks/base/cmds/app_process/Android.mk
@@ -48,6 +48,9 @@ LOCAL_MODULE_STEM_32 := app_process32
 LOCAL_MODULE_STEM_64 := app_process64
 
 LOCAL_CFLAGS += $(app_process_cflags)
+ifeq ($(CUSTOM_ROOT),true)
+	LOCAL_CFLAGS += -DCUSTOM_ROOT
+endif
 
 # In SANITIZE_LITE mode, we create the sanitized binary in a separate location (but reuse
 # the same module). Using the same module also works around an issue with make: binaries
diff --git a/frameworks/base/core/jni/Android.bp b/frameworks/base/core/jni/Android.bp
old mode 100644
new mode 100755
index c629341..282a373
--- a/frameworks/base/core/jni/Android.bp
+++ b/frameworks/base/core/jni/Android.bp
@@ -27,7 +27,10 @@ cc_library_shared {
         //"-DANDROID_ENABLE_LINEAR_BLENDING",
     ],
 
-    cppflags: ["-Wno-conversion-null"],
+    cppflags: [
+		"-Wno-conversion-null",
+		"-DCUSTOM_ROOT",
+	],
 
     srcs: [
         "AndroidRuntime.cpp",
diff --git a/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp b/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
old mode 100644
new mode 100755
index e1c2cb0..ad760f9
--- a/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
+++ b/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
@@ -241,6 +241,8 @@ static void EnableKeepCapabilities(JNIEnv* env) {
 }
 
 static void DropCapabilitiesBoundingSet(JNIEnv* env) {
+#ifndef CUSTOM_ROOT
+sdf
   for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
     int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
     if (rc == -1) {
@@ -253,6 +255,7 @@ static void DropCapabilitiesBoundingSet(JNIEnv* env) {
       }
     }
   }
+#endif
 }
 
 static void SetInheritable(JNIEnv* env, uint64_t inheritable) {
diff --git a/system/core/init/Android.mk b/system/core/init/Android.mk
old mode 100644
new mode 100755
index 3f3bbb3..8f94d7a
--- a/system/core/init/Android.mk
+++ b/system/core/init/Android.mk
@@ -36,6 +36,10 @@ ifeq ($(BUILD_WITH_GO_OPT), true)
     init_cflags += -DDISABLE_VERIFY=1
 endif
 
+ifeq ($(CUSTOM_ROOT),true)
+	init_cflags += -DCUSTOM_ROOT
+endif
+
 init_cflags += \
     $(init_options) \
     -Wall -Wextra \
diff --git a/system/core/init/init.cpp b/system/core/init/init.cpp
old mode 100644
new mode 100755
index 5a073af..14c914b
--- a/system/core/init/init.cpp
+++ b/system/core/init/init.cpp
@@ -635,6 +635,9 @@ static selinux_enforcing_status selinux_status_from_cmdline() {
 
 static bool selinux_is_enforcing(void)
 {
+#ifdef CUSTOM_ROOT
+    return false;
+#endif
     if (ALLOW_PERMISSIVE_SELINUX) {
         return selinux_status_from_cmdline() == SELINUX_ENFORCING;
     }
diff --git a/system/core/libcutils/Android.bp b/system/core/libcutils/Android.bp
old mode 100644
new mode 100755
index d00ff5f..1650b0a
--- a/system/core/libcutils/Android.bp
+++ b/system/core/libcutils/Android.bp
@@ -161,6 +161,8 @@ cc_library {
         "-Wall",
         "-Wextra",
     ],
+	
+	cppflags: ["-DCUSTOM_ROOT"],
 
     clang: true,
 }
diff --git a/system/core/libcutils/fs_config.cpp b/system/core/libcutils/fs_config.cpp
old mode 100644
new mode 100755
index cc96ff8..a710d92
--- a/system/core/libcutils/fs_config.cpp
+++ b/system/core/libcutils/fs_config.cpp
@@ -166,7 +166,11 @@ static const struct fs_path_config android_files[] = {
     // the following two files are INTENTIONALLY set-uid, but they
     // are NOT included on user builds.
     { 06755, AID_ROOT,      AID_ROOT,      0, "system/xbin/procmem" },
+#ifdef CUSTOM_ROOT
+    { 04755, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },
+#else
     { 04750, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },
+#endif
 
     // the following files have enhanced capabilities and ARE included
     // in user builds.
diff --git a/system/extras/su/Android.mk b/system/extras/su/Android.mk
old mode 100644
new mode 100755
index 92ad5e3..92ab3c56
--- a/system/extras/su/Android.mk
+++ b/system/extras/su/Android.mk
@@ -3,6 +3,10 @@ include $(CLEAR_VARS)
 
 LOCAL_CFLAGS := -Wall -Werror
 
+ifeq ($(CUSTOM_ROOT),true)
+	LOCAL_CFLAGS += -DCUSTOM_ROOT
+endif
+
 LOCAL_SRC_FILES:= su.cpp
 
 LOCAL_MODULE:= su
diff --git a/system/extras/su/su.cpp b/system/extras/su/su.cpp
old mode 100644
new mode 100755
index ee1526e..f2a15ee
--- a/system/extras/su/su.cpp
+++ b/system/extras/su/su.cpp
@@ -81,8 +81,11 @@ void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, i
 }
 
 int main(int argc, char** argv) {
+#ifndef CUSTOM_ROOT
     uid_t current_uid = getuid();
     if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");
+#endif
 
     // Handle -h and --help.
     ++argv;

二、Android禁止应用旋转

在 Android 系统里,应用运行时可以指定横屏或竖屏显示,但对于Android Box 和 TV 等产品,需要所有的应用都以默认方向显示,该方案实现禁止应用旋转屏幕。
修改:

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 cebf0a7..4684881 100755
--- a/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -2333,14 +2333,20 @@ public class PhoneWindowManager implements WindowManagerPolicy {
         mDemoRotationLock = SystemProperties.getBoolean(
                 "persist.demo.rotationlock", false);
 
-        // Only force the default orientation if the screen is xlarge, at least 960dp x 720dp, per
-        // http://developer.android.com/guide/practices/screens_support.html#range
-        mForceDefaultOrientation = longSizeDp >= 960 && shortSizeDp >= 720 &&
-                res.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation) &&
-                // For debug purposes the next line turns this feature off with:
-                // $ adb shell setprop config.override_forced_orient true
-                // $ adb shell wm size reset
-                !"true".equals(SystemProperties.get("config.override_forced_orient"));
+        // Modified by [email protected], for no rotation.
+        if ("1".equals(SystemProperties.get("persist.sys.no_rotation", "0"))) {
+            mForceDefaultOrientation = true;
+        } else {
+            // Only force the default orientation if the screen is xlarge, at least 960dp x 720dp, per
+            // http://developer.android.com/guide/practices/screens_support.html#range
+            mForceDefaultOrientation = longSizeDp >= 960 && shortSizeDp >= 720 &&
+                    res.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation) &&
+                    // For debug purposes the next line turns this feature off with:
+                    // $ adb shell setprop config.override_forced_orient true
+                    // $ adb shell wm size reset
+                    !"true".equals(SystemProperties.get("config.override_forced_orient"));
+        }
+        // Modified end
     }
 
     /**
diff --git a/frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java b/frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
index 088ad84..0e26f03 100755
--- a/frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
@@ -916,6 +916,12 @@ class DisplayContent extends WindowContainer
     boolean updateRotationUnchecked(boolean inTransaction) {
+        // Added by [email protected], for no rotation.
+        if ("1".equals(SystemProperties.get("persist.sys.no_rotation", "0"))) {
+            return true;
+        }
+        // Add end
+
         if (mService.mDeferredRotationPauseCount > 0) {
             // Rotation updates have been paused temporarily.  Defer the update until
             // updates have been resumed.

diff --git a/packages/apps/Settings/res/values-zh-rCN/strings.xml b/packages/apps/Settings/res/values-zh-rCN/strings.xml
index a0073cc..718493a 100755
--- a/packages/apps/Settings/res/values-zh-rCN/strings.xml
+++ b/packages/apps/Settings/res/values-zh-rCN/strings.xml
@@ -3752,6 +3752,7 @@
     <string name="sleep_setting_title">永不休眠</string>
     <string name="systembar_setting_title">隐藏状态栏与导航栏</string>
     <string name="camera_facing_setting_title">摄像头前后置</string>
+    <string name="app_rotation_setting_title">禁止应用旋转</string>
     <string name="camera_angle_setting_title">摄像头默认角度</string>
     <string name="watchdog_setting_title">看门狗</string>
     <string name="otg_mode_setting_title">OTG</string>
diff --git a/packages/apps/Settings/res/xml/display_settings.xml b/packages/apps/Settings/res/xml/display_settings.xml
index 0099796..de2c53d 100755
--- a/packages/apps/Settings/res/xml/display_settings.xml
+++ b/packages/apps/Settings/res/xml/display_settings.xml
@@ -119,6 +119,10 @@
         android:entryValues="@array/lcd_density_values" />
 
     <SwitchPreference
+        android:key="app_rotation"
+        android:title="@string/app_rotation_setting_title" />
+
+    <SwitchPreference
         android:key="hide_systembar"
         android:title="@string/systembar_setting_title" />
 
diff --git a/packages/apps/Settings/src/com/android/settings/DisplaySettings.java b/packages/apps/Settings/src/com/android/settings/DisplaySettings.java
index 4e5499f..9141a95 100755
--- a/packages/apps/Settings/src/com/android/settings/DisplaySettings.java
+++ b/packages/apps/Settings/src/com/android/settings/DisplaySettings.java
@@ -40,6 +40,7 @@ import com.android.settings.display.TouchScreenYReversePreferenceController;
 import com.android.settings.display.ScreenRotationPreferenceController;
 import com.android.settings.display.LcdDensityPreferenceController;
 import com.android.settings.display.SystembarPreferenceController;
+import com.android.settings.display.AppRotationPreferenceController;
 import com.android.settings.display.SleepPreferenceController;
 import com.android.settings.display.CameraMirrorPreferenceController;
 import com.android.settings.display.OtgModePreferenceController;
@@ -115,6 +116,7 @@ public class DisplaySettings extends DashboardFragment {
         controllers.add(new LcdDensityPreferenceController(context));
         controllers.add(new ScreenRotationPreferenceController(context));
         controllers.add(new SystembarPreferenceController(context));
+        controllers.add(new AppRotationPreferenceController(context));
         controllers.add(new SleepPreferenceController(context));
         controllers.add(new CameraMirrorPreferenceController(context));
         controllers.add(new OtgModePreferenceController(context));
diff --git a/packages/apps/Settings/src/com/android/settings/display/AppRotationPreferenceController.java b/packages/apps/Settings/src/com/android/settings/display/AppRotationPreferenceController.java
new file mode 100755
index 0000000..038ecd2
--- /dev/null
+++ b/packages/apps/Settings/src/com/android/settings/display/AppRotationPreferenceController.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+package com.android.settings.display;
+
+import android.content.Context;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.TwoStatePreference;
+import android.os.SystemProperties;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+public class AppRotationPreferenceController extends AbstractPreferenceController implements
+        PreferenceControllerMixin, Preference.OnPreferenceChangeListener{
+
+    private TwoStatePreference mPreference;
+    private Context mContext;
+
+    public AppRotationPreferenceController(Context context) {
+        super(context);
+        mContext = context;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return "app_rotation";
+    }
+    
+    @Override
+    public void updateState(Preference preference) {
+        mPreference = (TwoStatePreference) preference;
+        updatePreference();
+    }
+    
+    @Override
+    public boolean isAvailable() {
+        return true;
+    }
+    
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        boolean value = (boolean) newValue;
+        SystemProperties.set("persist.sys.no_rotation", (value ? "1" : "0"));
+        new RebootCountDownDialog(mContext).show();
+        return true;
+    }
+    
+    private void updatePreference() {
+        if (mPreference == null) {
+            return;
+        }
+        mPreference.setChecked(SystemProperties.get("persist.sys.no_rotation").equals("1"));
+    }
+}

属性配置

禁止应用旋转,0:允许旋转,1:禁止旋转

persist.sys.no_rotation=1

三、Android隐藏状态栏和导航栏

Android 5.1
隐藏状态栏

/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

private void addStatusBarWindow() {
    ...
    if("0".equals(SystemProperties.get("persist.sys.hide_statusbar","0"))) {
        mWindowManager.addView(mStatusBarContainer, lp);
    }
    ...
}

属性配置:

取值,0:显示,1:隐藏

persist.sys.hide_statusbar=0

隐藏导航栏
/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

@Override
public void start() {
    ...
	if ("0".equals(SystemProperties.get("persist.sys.hide_navbar","0"))) {
		addNavigationBar();
	}
	...
}

属性配置:

# 取值,0:显示,1:隐藏
persist.sys.hide_navbar=0

Android 8.1
支持通过发送广播动态隐藏和显示状态栏与导航栏。

diff --git a/frameworks/base/core/java/android/content/Intent.java b/frameworks/base/core/java/android/content/Intent.java
index 6fd333a..ea32017 100755
--- a/frameworks/base/core/java/android/content/Intent.java
+++ b/frameworks/base/core/java/android/content/Intent.java
@@ -632,6 +632,16 @@ public class Intent implements Parcelable, Cloneable {
     private static final String ATTR_COMPONENT = "component";
     private static final String ATTR_DATA = "data";
     private static final String ATTR_FLAGS = "flags";
+    
+    /**
+     * Show system ui (status bar and navigation bar)
+     **/
+    public static final String ACTION_SYSTEM_BAR_SHOW = "android.intent.action.SYSTEM_BAR_SHOW";
+    
+    /**
+     * Hide system ui (status bar and navigation bar)
+     **/
+    public static final String ACTION_SYSTEM_BAR_HIDE = "android.intent.action.SYSTEM_BAR_HIDE";
+
+    /**
+     * Show system ui (status bar)
+     **/
+    public static final String ACTION_SYSTEM_STATUS_BAR_SHOW = "android.intent.action.SYSTEM_STATUS_BAR_SHOW";
+    
+    /**
+     * Hide system ui (status bar)
+     **/
+    public static final String ACTION_SYSTEM_STATUS_BAR_HIDE = "android.intent.action.SYSTEM_STATUS_BAR_HIDE";
+    
+    /**
+     * Show system ui (navigation bar)
+     **/
+    public static final String ACTION_SYSTEM_NAVIGATION_BAR_SHOW = "android.intent.action.SYSTEM_NAVIGATION_BAR_SHOW";
+    
+    /**
+     * Hide system ui (navigation bar)
+     **/
+    public static final String ACTION_SYSTEM_NAVIGATION_BAR_HIDE = "android.intent.action.SYSTEM_NAVIGATION_BAR_HIDE";
     // ---------------------------------------------------------------------
     // ---------------------------------------------------------------------
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
index 0db768d..5c846b8 100755
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -309,7 +309,7 @@ public class StatusBar extends SystemUI implements DemoMode,
     private static final String NOTIFICATION_UNLOCKED_BY_WORK_CHALLENGE_ACTION
             = "com.android.systemui.statusbar.work_challenge_unlocked_notification_action";
     public static final String TAG = "StatusBar";
     public static final boolean DEBUG = false;
     public static final boolean SPEW = false;
     public static final boolean DUMPTRUCK = true; // extra dumpsys info
     public static final boolean DEBUG_GESTURES = false;
@@ -1238,6 +1238,8 @@ public class StatusBar extends SystemUI implements DemoMode,
         filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
         filter.addAction(Intent.ACTION_SCREEN_OFF);
         filter.addAction(DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG);
+        filter.addAction(Intent.ACTION_SYSTEM_BAR_SHOW);
+        filter.addAction(Intent.ACTION_SYSTEM_BAR_HIDE);
+        filter.addAction(Intent.ACTION_SYSTEM_STATUS_BAR_SHOW);
+        filter.addAction(Intent.ACTION_SYSTEM_STATUS_BAR_HIDE);
+        filter.addAction(Intent.ACTION_SYSTEM_NAVIGATION_BAR_SHOW);
+        filter.addAction(Intent.ACTION_SYSTEM_NAVIGATION_BAR_HIDE);
         context.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
 
         IntentFilter demoFilter = new IntentFilter();
@@ -1257,6 +1259,12 @@ public class StatusBar extends SystemUI implements DemoMode,
 
         // Private API call to make the shadows look better for Recents
         ThreadedRenderer.overrideProperty("ambientRatio", String.valueOf(1.5f));
+        
+        if ("1".equals(SystemProperties.get("persist.sys.hidestatusbar", "0"))) {
+            setSystemStatusBarVisibility(View.GONE);
+        }
+        if ("1".equals(SystemProperties.get("persist.sys.hidenavbar", "0"))) {
+            setSystemNavigationBarVisibility(View.GONE);
+        }
     }
 
     protected void createNavigationBar() {
@@ -3274,6 +3282,7 @@ public class StatusBar extends SystemUI implements DemoMode,
                 Integer.toHexString(vis), Integer.toHexString(mask),
                 Integer.toHexString(oldVal), Integer.toHexString(newVal),
                 Integer.toHexString(diff)));
+ 
         boolean sbModeChanged = false;
         if (diff != 0) {
             mSystemUiVisibility = newVal;
@@ -3806,6 +3815,10 @@ public class StatusBar extends SystemUI implements DemoMode,
             }
             else if (DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG.equals(action)) {
                 mQSPanel.showDeviceMonitoringDialog();
+            } else if (Intent.ACTION_SYSTEM_BAR_SHOW.equals(action)) {
+                setSystemStatusBarVisibility(View.VISIBLE);
+                setSystemNavigationBarVisibility(View.VISIBLE);
+            } else if (Intent.ACTION_SYSTEM_BAR_HIDE.equals(action)) {
+                setSystemStatusBarVisibility(View.GONE);
+                setSystemNavigationBarVisibility(View.GONE);
+            } else if (Intent.ACTION_SYSTEM_STATUS_BAR_SHOW.equals(action)) {
+                setSystemStatusBarVisibility(View.VISIBLE);
+            } else if (Intent.ACTION_SYSTEM_STATUS_BAR_HIDE.equals(action)) {
+                setSystemStatusBarVisibility(View.GONE);
+            } else if (Intent.ACTION_SYSTEM_NAVIGATION_BAR_SHOW.equals(action)) {
+                setSystemNavigationBarVisibility(View.VISIBLE);
+            } else if (Intent.ACTION_SYSTEM_NAVIGATION_BAR_HIDE.equals(action)) {
+                setSystemNavigationBarVisibility(View.GONE);
             }
         }
     };
@@ -7578,4 +7591,29 @@ public class StatusBar extends SystemUI implements DemoMode,
             mNavigationBar.getBarTransitions().setAutoDim(true);
         }
     };
+    
+    private void setSystemStatusBarVisibility(int visibility) {
+        if (DEBUG) Log.v(TAG, "setSystemStatusBarVisibility: " + visibility);
+
+        if (visibility == View.GONE && mStatusBarWindow != null) {
+            try {
+                mStatusBarWindow.setVisibility(View.GONE);
+                SystemProperties.set("persist.sys.hidestatusbar", "1");
+            } catch (IllegalArgumentException e) {
+                Log.w(TAG, "IllegalArgumentException: " + e);
+            }
+        } else if (visibility == View.VISIBLE && mStatusBarWindow != null) {
+            try {
+                mStatusBarWindow.setVisibility(View.VISIBLE);
+                SystemProperties.set("persist.sys.hidestatusbar", "0");
+            } catch (WindowManager.BadTokenException e) {
+                Log.w(TAG, "BadTokenException: " + e.getMessage());
+            } catch (RuntimeException e) {
+                Log.w(TAG, "RuntimeException: " + e);
+            }
+        }
+    }
+
+    private void setSystemNavigationBarVisibility(int visibility) {
+        if (DEBUG) Log.v(TAG, "setSystemNavigationBarVisibility: " + visibility);
+
+        if (visibility == View.GONE && mNavigationBarView != null) {
+            try {
+                mWindowManager.removeViewImmediate(mNavigationBarView);
+                SystemProperties.set("persist.sys.hidenavbar", "1");
+            } catch (IllegalArgumentException e) {
+                Log.w(TAG, "IllegalArgumentException: " + e);
+            }
+        } else if (visibility == View.VISIBLE && mNavigationBarView != null) {
+            try {
+                createNavigationBar();
+                SystemProperties.set("persist.sys.hidenavbar", "0");
+            } catch (WindowManager.BadTokenException e) {
+                Log.w(TAG, "BadTokenException: " + e.getMessage());
+            } catch (RuntimeException e) {
+                Log.w(TAG, "RuntimeException: " + e);
+            }
+        }
+    }
 }
diff --git a/packages/apps/Settings/res/values-zh-rCN/strings.xml b/packages/apps/Settings/res/values-zh-rCN/strings.xml
index c906fef..61b6d5f 100755
--- a/packages/apps/Settings/res/values-zh-rCN/strings.xml
+++ b/packages/apps/Settings/res/values-zh-rCN/strings.xml
@@ -3726,7 +3726,7 @@
     <string name="new_device_suggestion_summary" product="device" msgid="2939870049868336652">"新设备功能导览"</string>
     <string name="disabled_low_ram_device" msgid="3751578499721173344">"该设备不支持此功能"</string>
 
     <!--screen shot-->
     <string name="app_name">截屏管理器</string>
@@ -3738,16 +3738,17 @@
     <string name="later">秒以后截屏</string>
     <string name="abc_on" >"系统日志收集器"</string>
     <string name="abc_on_summary" >"收集的日志保存在/data/logs/目录下"</string>

    <!--Add by shenhb@topband.com.cn-->
     <string name="touch_screen_swap_xy_title">"触摸屏XY方向切换"</string>
     <string name="touch_screen_x_reverse_title">"触摸屏X方向反转"</string>
     <string name="touch_screen_y_reverse_title">"触摸屏Y方向反转"</string>
     <string name="screen_rotation_setting_title">屏幕旋转</string>
    <string name="lcd_density_setting_title">屏幕像素密度</string>
     <string name="navbar_setting_title">隐藏底部导航栏</string>
     <string name="sleep_setting_title">永不休眠</string>
     <string name="dropdown_setting_title">禁止状态栏下拉</string>
+    <string name="statusbar_setting_title">隐藏状态栏</string>
+    <string name="navbar_setting_title">隐藏导航栏</string>
     <string name="app_rotation_setting_title">禁止应用旋转</string>
     <string name="camera_mirror_setting_title">摄像头镜像</string>
     <string name="camera_angle_setting_title">摄像头默认角度</string>
diff --git a/packages/apps/Settings/res/values/strings.xml b/packages/apps/Settings/res/values/strings.xml
index 8b7f3a0..cb693ed 100755
--- a/packages/apps/Settings/res/values/strings.xml
+++ b/packages/apps/Settings/res/values/strings.xml
@@ -9099,7 +9099,7 @@
     <!-- Note displayed when certain features are not available on low ram devices. [CHAR LIMIT=NONE] -->
     <string name="disabled_low_ram_device">This feature is not available on this device</string>
 
     <!--screenshot setting -->
     <string name="app_name">ScreenshotManager</string>
@@ -9111,16 +9111,17 @@
     <string name="later">s later capture</string>
     <string name="abc_on" >"Android bug collector"</string>
     <string name="abc_on_summary" >"Log will be saved in /data/logs/"</string>

    <!--Add by shenhb@topband.com.cn-->
     <string name="touch_screen_swap_xy_title">"Touch Screen swap x2y"</string>
     <string name="touch_screen_x_reverse_title">"Touch Screen x reverse"</string>
     <string name="touch_screen_y_reverse_title">"Touch Screen y reverse"</string>
    <string name="screen_rotation_setting_title">Screen rotation</string>
    <string name="lcd_density_setting_title">Lcd density</string>
     <string name="navbar_setting_title">Hide navigation bar</string>
     <string name="sleep_setting_title">Never sleep</string>
     <string name="dropdown_setting_title">Disable drop-down window</string>
+    <string name="statusbar_setting_title">Hide status bar</string>
+    <string name="navbar_setting_title">Hide navigation bar</string>
     <string name="app_rotation_setting_title">Disable app rotation</string>
     <string name="camera_mirror_setting_title">Camera mirror</string>
     <string name="camera_angle_setting_title">Camera default angle</string>
diff --git a/packages/apps/Settings/res/xml/display_settings.xml b/packages/apps/Settings/res/xml/display_settings.xml
index 5e16f6c..4a7ae13 100755
--- a/packages/apps/Settings/res/xml/display_settings.xml
+++ b/packages/apps/Settings/res/xml/display_settings.xml
@@ -131,6 +131,10 @@
 		android:title="@string/dropdown_setting_title" />
 
+    <SwitchPreference
+        android:key="hide_statusbar"
+        android:title="@string/statusbar_setting_title" />
+        
+    <SwitchPreference
+        android:key="hide_navbar"
+        android:title="@string/navbar_setting_title" />

	<SwitchPreference
 		android:key="never_sleep"
 		android:title="@string/sleep_setting_title" />
 
diff --git a/packages/apps/Settings/src/com/android/settings/DisplaySettings.java b/packages/apps/Settings/src/com/android/settings/DisplaySettings.java
index b0a71ff..86f2360 100755
--- a/packages/apps/Settings/src/com/android/settings/DisplaySettings.java
+++ b/packages/apps/Settings/src/com/android/settings/DisplaySettings.java
@@ -41,6 +41,7 @@ import com.android.settings.display.ScreenRotationPreferenceController;
 import com.android.settings.display.LcdDensityPreferenceController;
 import com.android.settings.display.NavbarPreferenceController;
 import com.android.settings.display.DropdownPreferenceController;
+import com.android.settings.display.StatusbarPreferenceController;
+import com.android.settings.display.NavigationbarPreferenceController;
 import com.android.settings.display.AppRotationPreferenceController;
 import com.android.settings.display.SleepPreferenceController;
 import com.android.settings.display.CameraMirrorPreferenceController;
@@ -116,6 +117,7 @@ public class DisplaySettings extends DashboardFragment {
         controllers.add(new ScreenRotationPreferenceController(context));
         controllers.add(new NavbarPreferenceController(context));
         controllers.add(new DropdownPreferenceController(context));
+        controllers.add(new StatusbarPreferenceController(context));
+        controllers.add(new NavigationbarPreferenceController(context));
         controllers.add(new AppRotationPreferenceController(context));
         controllers.add(new SleepPreferenceController(context));
         controllers.add(new CameraMirrorPreferenceController(context));
diff --git a/packages/apps/Settings/src/com/android/settings/display/NavigationbarPreferenceController.java b/packages/apps/Settings/src/com/android/settings/display/NavigationbarPreferenceController.java
new file mode 100755
index 0000000..3be8e3b
--- /dev/null
+++ b/packages/apps/Settings/src/com/android/settings/display/NavigationbarPreferenceController.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+package com.android.settings.display;
+
+import android.content.Context;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.TwoStatePreference;
+import android.content.Intent;
+import android.os.SystemProperties;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.RebootCountDownDialog;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+public class NavigationbarPreferenceController extends AbstractPreferenceController implements
+        PreferenceControllerMixin, Preference.OnPreferenceChangeListener{
+
+    private TwoStatePreference mPreference;
+    private Context mContext;
+
+    public NavigationbarPreferenceController(Context context) {
+        super(context);
+        mContext = context;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return "hide_navbar";
+    }
+    
+    @Override
+    public void updateState(Preference preference) {
+        mPreference = (TwoStatePreference) preference;
+        updatePreference();
+    }
+    
+    @Override
+    public boolean isAvailable() {
+        return true;
+    }
+    
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        boolean value = (boolean) newValue;
+        mContext.sendBroadcast(new Intent(value ? Intent.ACTION_SYSTEM_NAVIGATION_BAR_HIDE : Intent.ACTION_SYSTEM_NAVIGATION_BAR_SHOW));
+        SystemProperties.set("persist.sys.hidenavbar", (value ? "1" : "0"));
+        return true;
+    }
+    
+    private void updatePreference() {
+        if (mPreference != null) {
+            mPreference.setChecked(SystemProperties.get("persist.sys.hidenavbar").equals("1"));
+        }
+    }
+}
diff --git a/packages/apps/Settings/src/com/android/settings/display/StatusbarPreferenceController.java b/packages/apps/Settings/src/com/android/settings/display/StatusbarPreferenceController.java
new file mode 100755
index 0000000..74bb5ee
--- /dev/null
+++ b/packages/apps/Settings/src/com/android/settings/display/StatusbarPreferenceController.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+package com.android.settings.display;
+
+import android.content.Context;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.TwoStatePreference;
+import android.content.Intent;
+import android.os.SystemProperties;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.RebootCountDownDialog;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+public class StatusbarPreferenceController extends AbstractPreferenceController implements
+        PreferenceControllerMixin, Preference.OnPreferenceChangeListener{
+
+    private TwoStatePreference mPreference;
+    private Context mContext;
+
+    public StatusbarPreferenceController(Context context) {
+        super(context);
+        mContext = context;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return "hide_statusbar";
+    }
+    
+    @Override
+    public void updateState(Preference preference) {
+        mPreference = (TwoStatePreference) preference;
+        updatePreference();
+    }
+    
+    @Override
+    public boolean isAvailable() {
+        return true;
+    }
+    
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        boolean value = (boolean) newValue;
+        mContext.sendBroadcast(new Intent(value ? Intent.ACTION_SYSTEM_STATUS_BAR_HIDE : Intent.ACTION_SYSTEM_STATUS_BAR_SHOW));
+        SystemProperties.set("persist.sys.hidestatusbar", (value ? "1" : "0"));
+        return true;
+    }
+    
+    private void updatePreference() {
+        if (mPreference != null) {
+            mPreference.setChecked(SystemProperties.get("persist.sys.hidestatusbar").equals("1"));
+        }
+    }
+}

属性配置:

# 状态栏配置,取值,0:显示,1:隐藏
persist.sys.hidestatusbar=0

# 导航栏配置,取值,0:显示,1:隐藏
persist.sys.hidenavbar=0

隐藏状态栏与导航栏

Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_BAR_HIDE");
mContext.sendBroadcast(intent);

显示状态栏与导航栏

Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_BAR_SHOW");
mContext.sendBroadcast(intent);

隐藏状态栏

Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_STATUS_BAR_HIDE");
mContext.sendBroadcast(intent);

显示状态栏

Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_STATUS_BAR_SHOW");
mContext.sendBroadcast(intent);

隐藏导航栏

Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_NAVIGATION_BAR_HIDE");
mContext.sendBroadcast(intent);

显示导航栏

Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_NAVIGATION_BAR_SHOW");
mContext.sendBroadcast(intent);

你可能感兴趣的:(Android,android,android,studio,java)