Android10 framework实现所有APP全屏

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、adb命令可以实现
  • 二、修改思路
    • 1.思考adb是怎么修改的
    • 2.修改方式
  • 总结


前言

有些特殊安卓设备,需要让所有app是全屏的,隐藏systemui,可以在framework层修改

一、adb命令可以实现

adb 实现沉浸式状态栏 导航栏
adb shell settings put global policy_control immersive.full=*
immersive.full这个属性可以去下面的PolicyControl.java里面看一下,还有其他几个
alps/frameworks/base/services/core/java/com/android/server/wm/PolicyControl.java b/alps/frameworks/base/services/core/java/com/android/server/wm/PolicyControl.java

我们还可以单独控制哪些app不沉浸,其他程序沉浸:
adb shell settings put global policy_control immersive.full=apps,-com.xxx.xxx
如果想恢复到原来,运行下面的代码:
adb shell settings put global policy_control null

二、修改思路

1.思考adb是怎么修改的

adb shell settings put global policy_control immersive.full=*

这里很明显是修改settings的数据库,在framework目录下全局搜一下policy_control

alps/frameworks$ grep policy_control ./* -rns
./base/core/java/android/provider/Settings.java:13180:        public static final String POLICY_CONTROL = "policy_control";

得到POLICY_CONTROL 关键字,继续全局搜,看哪些类引用了

alps/frameworks$ grep POLICY_CONTROL ./* -rnw
./base/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java:1142:                Settings.Global.POLICY_CONTROL,
./base/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java:1143:                GlobalSettingsProto.POLICY_CONTROL);
./base/config/hiddenapi-greylist-max-o.txt:60473:Landroid/provider/Settings$Global;->POLICY_CONTROL:Ljava/lang/String;
./base/services/core/java/com/android/server/wm/PolicyControl.java:38: * Control by setting {@link Settings.Global#POLICY_CONTROL} to one or more name-value pairs.
./base/services/core/java/com/android/server/wm/PolicyControl.java:135:                    Settings.Global.POLICY_CONTROL,
./base/services/core/java/com/android/server/wm/WindowManagerService.java:719:                Settings.Global.getUriFor(Settings.Global.POLICY_CONTROL);
./base/core/tests/coretests/src/android/provider/SettingsBackupTest.java:395:                    Settings.Global.POLICY_CONTROL,
./base/core/java/android/provider/Settings.java:13180:        public static final String POLICY_CONTROL = "policy_control";

看到关键使用部分了
PolicyControl类和WindowManagerService类。继续分析,下面只贴一部分关键代码


这里有POLICY_CONTROL的uri,代码监听了值的变化
        private final Uri mImmersiveModeConfirmationsUri =
              Settings.Secure.getUriFor(Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS);
        private final Uri mPolicyControlUri =
                Settings.Global.getUriFor(Settings.Global.POLICY_CONTROL);
       
      
下面updateSystemUiSettings是变化后的监听

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            if (uri == null) {
                return;
            }

            if (mImmersiveModeConfirmationsUri.equals(uri) || mPolicyControlUri.equals(uri)) {
                updateSystemUiSettings();
                return;
            }
看看updateSystemUiSettings做了什么,和哪些位置引用了

void updateSystemUiSettings() {
            boolean changed;
            synchronized (mGlobalLock) {
                changed = ImmersiveModeConfirmation.loadSetting(mCurrentUserId, mContext)
                        || PolicyControl.reloadFromSetting(mContext);
            }
            if (changed) {
                updateRotation(false /* alwaysSendConfiguration */, false /* forceRelayout */);
            }
        }

  在systemReady方法中引用了 ,这里很重要,表明开机就会运行一次     
 UiThread.getHandler().post(mSettingsObserver::updateSystemUiSettings);
 
 关键位置 PolicyControl.reloadFromSetting(mContext);  
     
static boolean reloadFromSetting(Context context) {
        if (DEBUG) Slog.d(TAG, "reloadFromSetting()");
        String value = null;
        try {
            value = Settings.Global.getStringForUser(context.getContentResolver(),
                    Settings.Global.POLICY_CONTROL,
                    UserHandle.USER_CURRENT);
            if (sSettingValue == value || sSettingValue != null && sSettingValue.equals(value)) {
                return false;
            }
            setFilters(value);
            sSettingValue = value;
        } catch (Throwable t) {
            Slog.w(TAG, "Error loading policy control, value=" + value, t);
            return false;
        }
        return true;
    } 
	这里就是读取settings数据库中的值了

2.修改方式

继续分析reloadFromSetting,读取到值后做了什么,setFilters(value);
sImmersiveStatusFilter ,sImmersiveNavigationFilter 这两个就是对应状态栏和导航栏全屏的过滤,
可以看上面的getSystemUiVisibility方法和getWindowFlags方法

private static void setFilters(String value) {
        if (DEBUG) Slog.d(TAG, "setFilters: " + value);
        sImmersiveStatusFilter = null;
        sImmersiveNavigationFilter = null;
        sImmersivePreconfirmationsFilter = null;
        if (value != null) {
            String[] nvps = value.split(":");
            for (String nvp : nvps) {
                int i = nvp.indexOf('=');
                if (i == -1) continue;
                String n = nvp.substring(0, i);
                String v = nvp.substring(i + 1);
                if (n.equals(NAME_IMMERSIVE_FULL)) {
                    Filter f = Filter.parse(v);
                    sImmersiveStatusFilter = sImmersiveNavigationFilter = f;
                    if (sImmersivePreconfirmationsFilter == null) {
                        sImmersivePreconfirmationsFilter = f;
                    }
                } else if (n.equals(NAME_IMMERSIVE_STATUS)) {
                    Filter f = Filter.parse(v);
                    sImmersiveStatusFilter = f;
                } else if (n.equals(NAME_IMMERSIVE_NAVIGATION)) {
                    Filter f = Filter.parse(v);
                    sImmersiveNavigationFilter = f;
                    if (sImmersivePreconfirmationsFilter == null) {
                        sImmersivePreconfirmationsFilter = f;
                    }
                } else if (n.equals(NAME_IMMERSIVE_PRECONFIRMATIONS)) {
                    Filter f = Filter.parse(v);
                    sImmersivePreconfirmationsFilter = f;
                }
            }
        }
        if (DEBUG) {
            Slog.d(TAG, "immersiveStatusFilter: " + sImmersiveStatusFilter);
            Slog.d(TAG, "immersiveNavigationFilter: " + sImmersiveNavigationFilter);
            Slog.d(TAG, "immersivePreconfirmationsFilter: " + sImmersivePreconfirmationsFilter);
        }
    }

继续看关键解析部分 Filter f = Filter.parse(v); Filter类定义再最下面,这种一看就明白,一个白名单,一个黑名单,判断哪些包名能不能全屏,关注这句代码
boolean isApp = attrs.type >= WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW
&& attrs.type <= WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;

字面意思,判断是不是app,这里我暂时没有研究过,但是想到有一些服务是没有界面的。可能是比如输入法之类的,可能是这种的判断,获取是判断当前窗口 ,参考下面的链接
https://blog.csdn.net/u012739527/article/details/124011617

private static class Filter {
        private static final String ALL = "*";
        private static final String APPS = "apps";

        private final ArraySet<String> mWhitelist;
        private final ArraySet<String> mBlacklist;

        private Filter(ArraySet<String> whitelist, ArraySet<String> blacklist) {
            mWhitelist = whitelist;
            mBlacklist = blacklist;
        }

        boolean matches(LayoutParams attrs) {
            if (attrs == null) return false;
            boolean isApp = attrs.type >= WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW
                    && attrs.type <= WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
            if (isApp && mBlacklist.contains(APPS)) return false;
            if (onBlacklist(attrs.packageName)) return false;
            if (isApp && mWhitelist.contains(APPS)) return true;
            return onWhitelist(attrs.packageName);
        }

        boolean matches(String packageName) {
            return !onBlacklist(packageName) && onWhitelist(packageName);
        }

        private boolean onBlacklist(String packageName) {
            return mBlacklist.contains(packageName) || mBlacklist.contains(ALL);
        }

        private boolean onWhitelist(String packageName) {
            return mWhitelist.contains(ALL) || mWhitelist.contains(packageName);
        }

        void dump(PrintWriter pw) {
            pw.print("Filter[");
            dump("whitelist", mWhitelist, pw); pw.print(',');
            dump("blacklist", mBlacklist, pw); pw.print(']');
        }

        private void dump(String name, ArraySet<String> set, PrintWriter pw) {
            pw.print(name); pw.print("=(");
            final int n = set.size();
            for (int i = 0; i < n; i++) {
                if (i > 0) pw.print(',');
                pw.print(set.valueAt(i));
            }
            pw.print(')');
        }

        @Override
        public String toString() {
            StringWriter sw = new StringWriter();
            dump(new PrintWriter(sw, true));
            return sw.toString();
        }

        // value = comma-delimited list of tokens, where token = (package name|apps|*)
        // e.g. "com.package1", or "apps, com.android.keyguard" or "*"
        static Filter parse(String value) {
            if (value == null) return null;
            ArraySet<String> whitelist = new ArraySet<String>();
            ArraySet<String> blacklist = new ArraySet<String>();
            for (String token : value.split(",")) {
                token = token.trim();
                if (token.startsWith("-") && token.length() > 1) {
                    token = token.substring(1);
                    blacklist.add(token);
                } else {
                    whitelist.add(token);
                }
            }
            return new Filter(whitelist, blacklist);
        }
    }

到这里修改就简单了,只需要在开机之前给赋值就行,然后第一次要加载这个值,可以在SettingsProvider中修改

diff --git a/alps/vendor/mediatek/proprietary/packages/apps/SettingsProvider/res/values/defaults.xml b/alps/vendor/mediatek/proprietary/packages/apps/SettingsProvider/res/values/defaults.xml
@@ -56,6 +56,8 @@
 
     <string name="default_input_method" translatable="false">com.sohu.inputmethod.sogou.tv/.SogouIME</string>
     <string name="enabled_input_methods" translatable="false">com.sohu.inputmethod.sogou.tv/.SogouIME</string>
+       
+    <string name="def_policy_str" translatable="false">immersive.full=*</string>
  
 
     <!-- Default value for whether or not to pulse the notification LED when there is a
diff --git a/alps/vendor/mediatek/proprietary/packages/apps/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/alps/vendor/mediatek/proprietary/packages/apps/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -2470,7 +2470,10 @@ class DatabaseHelper extends SQLiteOpenHelper {
         try {
             stmt = db.compileStatement("INSERT OR IGNORE INTO global(name,value)"
                     + " VALUES(?,?);");
-
+                                       
+                       loadStringSetting(stmt, Settings.Global.POLICY_CONTROL,
+                    R.string.def_policy_str);
+                                       
             // --- Previously in 'system'
             loadBooleanSetting(stmt, Settings.Global.AIRPLANE_MODE_ON,
                     R.bool.def_airplane_mode_on);

还可以在中PolicyControl 多修改一处,if (isApp) return true;这个判断,只要是app,全部全屏,
但是上面的SettingsProvider的值仍要修改,因为在reloadFromSetting中有判空

class PolicyControl {
             if (attrs == null) return false;
             boolean isApp = attrs.type >= WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW
                     && attrs.type <= WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
+            if (isApp) return true;
             if (isApp && mBlacklist.contains(APPS)) return false;
             if (onBlacklist(attrs.packageName)) return false;
             if (isApp && mWhitelist.contains(APPS)) return true;


总结

简单来说就是修改Settings.Global.POLICY_CONTROL中的值,如果有部分白名单的,可以只添加一部分app的包名,目前的修改是基于Android10的

你可能感兴趣的:(android系统,android,adb,java)