• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 static android.provider.DeviceConfig.NAMESPACE_WINDOW_MANAGER;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.content.pm.ApplicationInfo;
24 import android.os.Build;
25 import android.provider.DeviceConfig;
26 import android.util.Slog;
27 
28 import com.android.internal.annotations.GuardedBy;
29 import com.android.internal.annotations.VisibleForTesting;
30 
31 import java.util.HashSet;
32 import java.util.Locale;
33 import java.util.concurrent.Executor;
34 import java.util.function.Supplier;
35 
36 /**
37  * Handles filtering the list of package that don't use the system splash screen.
38  * The list is backed by a {@link DeviceConfig} property.
39  * <p>
40  * An application can manually opt-out of the exception list by setting the &lt;meta-data
41  * {@value OPT_OUT_METADATA_FLAG}="true"/&gt; in the <code>&lt;application&gt;</code> section of the
42  * manifest.
43  */
44 class SplashScreenExceptionList {
45 
46     private static final boolean DEBUG = Build.isDebuggable();
47     private static final String LOG_TAG = "SplashScreenExceptionList";
48     private static final String KEY_SPLASH_SCREEN_EXCEPTION_LIST = "splash_screen_exception_list";
49     private static final String NAMESPACE = NAMESPACE_WINDOW_MANAGER;
50     private static final String OPT_OUT_METADATA_FLAG = "android.splashscreen.exception_opt_out";
51 
52     @GuardedBy("mLock")
53     private final HashSet<String> mDeviceConfigExcludedPackages = new HashSet<>();
54     private final Object mLock = new Object();
55 
56     @VisibleForTesting
57     final DeviceConfig.OnPropertiesChangedListener mOnPropertiesChangedListener;
58 
SplashScreenExceptionList(@onNull Executor executor)59     SplashScreenExceptionList(@NonNull Executor executor) {
60         updateDeviceConfig(DeviceConfig.getString(NAMESPACE, KEY_SPLASH_SCREEN_EXCEPTION_LIST, ""));
61         mOnPropertiesChangedListener = properties -> updateDeviceConfig(
62                 properties.getString(KEY_SPLASH_SCREEN_EXCEPTION_LIST, ""));
63         DeviceConfig.addOnPropertiesChangedListener(NAMESPACE, executor,
64                 mOnPropertiesChangedListener);
65     }
66 
updateDeviceConfig(String values)67     private void updateDeviceConfig(String values) {
68         parseDeviceConfigPackageList(values);
69     }
70 
71     /**
72      * Returns true if the packageName is in the list and the target sdk is before S.
73      *
74      * @param packageName  The package name of the application to check
75      * @param targetSdk    The target sdk of the application
76      * @param infoSupplier A {@link Supplier} that returns an {@link ApplicationInfo} used to
77      *                     check if the application wants to opt-out of the exception list in the
78      *                     manifest metadata. Evaluated only if the application is in the exception
79      *                     list.
80      */
81     @SuppressWarnings("AndroidFrameworkCompatChange") // Target sdk check
isException(@onNull String packageName, int targetSdk, @Nullable Supplier<ApplicationInfo> infoSupplier)82     public boolean isException(@NonNull String packageName, int targetSdk,
83             @Nullable Supplier<ApplicationInfo> infoSupplier) {
84         if (targetSdk >= Build.VERSION_CODES.S) {
85             return false;
86         }
87 
88         synchronized (mLock) {
89             if (DEBUG) {
90                 Slog.v(LOG_TAG, String.format(Locale.US,
91                         "SplashScreen checking exception for package %s (target sdk:%d) -> %s",
92                         packageName, targetSdk,
93                         mDeviceConfigExcludedPackages.contains(packageName)));
94             }
95             if (!mDeviceConfigExcludedPackages.contains(packageName)) {
96                 return false;
97             }
98         }
99         return !isOptedOut(infoSupplier);
100     }
101 
102     /**
103      * An application can manually opt-out of the exception list by setting the meta-data
104      * {@value OPT_OUT_METADATA_FLAG} = true in the <code>application</code> section of the manifest
105      */
isOptedOut(@ullable Supplier<ApplicationInfo> infoProvider)106     private static boolean isOptedOut(@Nullable Supplier<ApplicationInfo> infoProvider) {
107         if (infoProvider == null) {
108             return false;
109         }
110         ApplicationInfo info = infoProvider.get();
111         return info != null && info.metaData != null && info.metaData.getBoolean(
112                 OPT_OUT_METADATA_FLAG, false);
113     }
114 
parseDeviceConfigPackageList(String rawList)115     private void parseDeviceConfigPackageList(String rawList) {
116         synchronized (mLock) {
117             mDeviceConfigExcludedPackages.clear();
118             String[] packages = rawList.split(",");
119             for (String packageName : packages) {
120                 String packageNameTrimmed = packageName.trim();
121                 if (!packageNameTrimmed.isEmpty()) {
122                     mDeviceConfigExcludedPackages.add(packageNameTrimmed);
123                 }
124             }
125         }
126     }
127 }
128