1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wm; 18 19 import android.app.ActivityManager; 20 import android.content.Context; 21 import android.os.UserHandle; 22 import android.provider.Settings; 23 import android.util.ArraySet; 24 import android.util.Slog; 25 import android.view.View; 26 import android.view.WindowManager; 27 import android.view.WindowManager.LayoutParams; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 31 import java.io.PrintWriter; 32 import java.io.StringWriter; 33 34 /** 35 * Runtime adjustments applied to the global window policy. 36 * 37 * This includes forcing immersive mode behavior for one or both system bars (based on a package 38 * list) and permanently disabling immersive mode confirmations for specific packages. 39 * 40 * Control by setting {@link Settings.Global#POLICY_CONTROL} to one or more name-value pairs. 41 * e.g. 42 * to force immersive mode everywhere: 43 * "immersive.full=*" 44 * to force transient status for all apps except a specific package: 45 * "immersive.status=apps,-com.package" 46 * to disable the immersive mode confirmations for specific packages: 47 * "immersive.preconfirms=com.package.one,com.package.two" 48 * 49 * Separate multiple name-value pairs with ':' 50 * e.g. "immersive.status=apps:immersive.preconfirms=*" 51 */ 52 class PolicyControl { 53 private static final String TAG = "PolicyControl"; 54 private static final boolean DEBUG = false; 55 56 @VisibleForTesting 57 static final String NAME_IMMERSIVE_FULL = "immersive.full"; 58 private static final String NAME_IMMERSIVE_STATUS = "immersive.status"; 59 private static final String NAME_IMMERSIVE_NAVIGATION = "immersive.navigation"; 60 private static final String NAME_IMMERSIVE_PRECONFIRMATIONS = "immersive.preconfirms"; 61 62 private static String sSettingValue; 63 private static Filter sImmersivePreconfirmationsFilter; 64 private static Filter sImmersiveStatusFilter; 65 private static Filter sImmersiveNavigationFilter; 66 getSystemUiVisibility(WindowState win, LayoutParams attrs)67 static int getSystemUiVisibility(WindowState win, LayoutParams attrs) { 68 attrs = attrs != null ? attrs : win.getAttrs(); 69 int vis = win != null ? win.getSystemUiVisibility() 70 : (attrs.systemUiVisibility | attrs.subtreeSystemUiVisibility); 71 if (sImmersiveStatusFilter != null && sImmersiveStatusFilter.matches(attrs)) { 72 vis |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 73 | View.SYSTEM_UI_FLAG_FULLSCREEN; 74 if (attrs.isFullscreen()) { 75 vis |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; 76 } 77 vis &= ~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE 78 | View.STATUS_BAR_TRANSLUCENT); 79 } 80 if (sImmersiveNavigationFilter != null && sImmersiveNavigationFilter.matches(attrs)) { 81 vis |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 82 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 83 if (attrs.isFullscreen()) { 84 vis |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; 85 } 86 vis &= ~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE 87 | View.NAVIGATION_BAR_TRANSLUCENT); 88 } 89 return vis; 90 } 91 getWindowFlags(WindowState win, LayoutParams attrs)92 static int getWindowFlags(WindowState win, LayoutParams attrs) { 93 attrs = attrs != null ? attrs : win.getAttrs(); 94 int flags = attrs.flags; 95 if (sImmersiveStatusFilter != null && sImmersiveStatusFilter.matches(attrs)) { 96 flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 97 flags &= ~(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS 98 | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 99 } 100 if (sImmersiveNavigationFilter != null && sImmersiveNavigationFilter.matches(attrs)) { 101 flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; 102 } 103 return flags; 104 } 105 adjustClearableFlags(WindowState win, int clearableFlags)106 static int adjustClearableFlags(WindowState win, int clearableFlags) { 107 final LayoutParams attrs = win != null ? win.getAttrs() : null; 108 if (sImmersiveStatusFilter != null && sImmersiveStatusFilter.matches(attrs)) { 109 clearableFlags &= ~View.SYSTEM_UI_FLAG_FULLSCREEN; 110 } 111 return clearableFlags; 112 } 113 disableImmersiveConfirmation(String pkg)114 static boolean disableImmersiveConfirmation(String pkg) { 115 return (sImmersivePreconfirmationsFilter != null 116 && sImmersivePreconfirmationsFilter.matches(pkg)) 117 || ActivityManager.isRunningInTestHarness(); 118 } 119 reloadFromSetting(Context context)120 static boolean reloadFromSetting(Context context) { 121 if (DEBUG) Slog.d(TAG, "reloadFromSetting()"); 122 String value = null; 123 try { 124 value = Settings.Global.getStringForUser(context.getContentResolver(), 125 Settings.Global.POLICY_CONTROL, 126 UserHandle.USER_CURRENT); 127 if (sSettingValue == value || sSettingValue != null && sSettingValue.equals(value)) { 128 return false; 129 } 130 setFilters(value); 131 sSettingValue = value; 132 } catch (Throwable t) { 133 Slog.w(TAG, "Error loading policy control, value=" + value, t); 134 return false; 135 } 136 return true; 137 } 138 dump(String prefix, PrintWriter pw)139 static void dump(String prefix, PrintWriter pw) { 140 dump("sImmersiveStatusFilter", sImmersiveStatusFilter, prefix, pw); 141 dump("sImmersiveNavigationFilter", sImmersiveNavigationFilter, prefix, pw); 142 dump("sImmersivePreconfirmationsFilter", sImmersivePreconfirmationsFilter, prefix, pw); 143 } 144 dump(String name, Filter filter, String prefix, PrintWriter pw)145 private static void dump(String name, Filter filter, String prefix, PrintWriter pw) { 146 pw.print(prefix); pw.print("PolicyControl."); pw.print(name); pw.print('='); 147 if (filter == null) { 148 pw.println("null"); 149 } else { 150 filter.dump(pw); pw.println(); 151 } 152 } 153 154 @VisibleForTesting setFilters(String value)155 static void setFilters(String value) { 156 if (DEBUG) Slog.d(TAG, "setFilters: " + value); 157 sImmersiveStatusFilter = null; 158 sImmersiveNavigationFilter = null; 159 sImmersivePreconfirmationsFilter = null; 160 if (value != null) { 161 String[] nvps = value.split(":"); 162 for (String nvp : nvps) { 163 int i = nvp.indexOf('='); 164 if (i == -1) continue; 165 String n = nvp.substring(0, i); 166 String v = nvp.substring(i + 1); 167 if (n.equals(NAME_IMMERSIVE_FULL)) { 168 Filter f = Filter.parse(v); 169 sImmersiveStatusFilter = sImmersiveNavigationFilter = f; 170 if (sImmersivePreconfirmationsFilter == null) { 171 sImmersivePreconfirmationsFilter = f; 172 } 173 } else if (n.equals(NAME_IMMERSIVE_STATUS)) { 174 Filter f = Filter.parse(v); 175 sImmersiveStatusFilter = f; 176 } else if (n.equals(NAME_IMMERSIVE_NAVIGATION)) { 177 Filter f = Filter.parse(v); 178 sImmersiveNavigationFilter = f; 179 if (sImmersivePreconfirmationsFilter == null) { 180 sImmersivePreconfirmationsFilter = f; 181 } 182 } else if (n.equals(NAME_IMMERSIVE_PRECONFIRMATIONS)) { 183 Filter f = Filter.parse(v); 184 sImmersivePreconfirmationsFilter = f; 185 } 186 } 187 } 188 if (DEBUG) { 189 Slog.d(TAG, "immersiveStatusFilter: " + sImmersiveStatusFilter); 190 Slog.d(TAG, "immersiveNavigationFilter: " + sImmersiveNavigationFilter); 191 Slog.d(TAG, "immersivePreconfirmationsFilter: " + sImmersivePreconfirmationsFilter); 192 } 193 } 194 195 private static class Filter { 196 private static final String ALL = "*"; 197 private static final String APPS = "apps"; 198 199 private final ArraySet<String> mWhitelist; 200 private final ArraySet<String> mBlacklist; 201 Filter(ArraySet<String> whitelist, ArraySet<String> blacklist)202 private Filter(ArraySet<String> whitelist, ArraySet<String> blacklist) { 203 mWhitelist = whitelist; 204 mBlacklist = blacklist; 205 } 206 matches(LayoutParams attrs)207 boolean matches(LayoutParams attrs) { 208 if (attrs == null) return false; 209 boolean isApp = attrs.type >= WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW 210 && attrs.type <= WindowManager.LayoutParams.LAST_APPLICATION_WINDOW; 211 if (isApp && mBlacklist.contains(APPS)) return false; 212 if (onBlacklist(attrs.packageName)) return false; 213 if (isApp && mWhitelist.contains(APPS)) return true; 214 return onWhitelist(attrs.packageName); 215 } 216 matches(String packageName)217 boolean matches(String packageName) { 218 return !onBlacklist(packageName) && onWhitelist(packageName); 219 } 220 onBlacklist(String packageName)221 private boolean onBlacklist(String packageName) { 222 return mBlacklist.contains(packageName) || mBlacklist.contains(ALL); 223 } 224 onWhitelist(String packageName)225 private boolean onWhitelist(String packageName) { 226 return mWhitelist.contains(ALL) || mWhitelist.contains(packageName); 227 } 228 dump(PrintWriter pw)229 void dump(PrintWriter pw) { 230 pw.print("Filter["); 231 dump("whitelist", mWhitelist, pw); pw.print(','); 232 dump("blacklist", mBlacklist, pw); pw.print(']'); 233 } 234 dump(String name, ArraySet<String> set, PrintWriter pw)235 private void dump(String name, ArraySet<String> set, PrintWriter pw) { 236 pw.print(name); pw.print("=("); 237 final int n = set.size(); 238 for (int i = 0; i < n; i++) { 239 if (i > 0) pw.print(','); 240 pw.print(set.valueAt(i)); 241 } 242 pw.print(')'); 243 } 244 245 @Override toString()246 public String toString() { 247 StringWriter sw = new StringWriter(); 248 dump(new PrintWriter(sw, true)); 249 return sw.toString(); 250 } 251 252 // value = comma-delimited list of tokens, where token = (package name|apps|*) 253 // e.g. "com.package1", or "apps, com.android.keyguard" or "*" parse(String value)254 static Filter parse(String value) { 255 if (value == null) return null; 256 ArraySet<String> whitelist = new ArraySet<String>(); 257 ArraySet<String> blacklist = new ArraySet<String>(); 258 for (String token : value.split(",")) { 259 token = token.trim(); 260 if (token.startsWith("-") && token.length() > 1) { 261 token = token.substring(1); 262 blacklist.add(token); 263 } else { 264 whitelist.add(token); 265 } 266 } 267 return new Filter(whitelist, blacklist); 268 } 269 } 270 } 271