blob: d9cf637ffaf8f2e650c6d02b8e981e7ea2072d49 [file] [log] [blame]
Ady Abrahamf3e05312019-05-13 18:04:59 -07001/*
2 * Copyright (C) 2019 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
17package com.android.server.wm;
18
Long Ling1961cf12019-08-13 16:07:14 -070019import static android.hardware.display.DisplayManager.DeviceConfig.KEY_HIGH_REFRESH_RATE_BLACKLIST;
Michael Wrightf40ed272019-07-04 19:17:19 +010020
Ady Abrahamf3e05312019-05-13 18:04:59 -070021import android.annotation.NonNull;
Michael Wrightf40ed272019-07-04 19:17:19 +010022import android.annotation.Nullable;
23import android.content.res.Resources;
24import android.provider.DeviceConfig;
Ady Abrahamf3e05312019-05-13 18:04:59 -070025import android.util.ArraySet;
26
Michael Wrightf40ed272019-07-04 19:17:19 +010027import com.android.internal.R;
Ady Abrahamf3e05312019-05-13 18:04:59 -070028import com.android.internal.annotations.VisibleForTesting;
Michael Wrightf40ed272019-07-04 19:17:19 +010029import com.android.internal.os.BackgroundThread;
Adrian Roos1c2e9a12019-08-20 18:23:47 +020030import com.android.server.wm.utils.DeviceConfigInterface;
Michael Wrightf40ed272019-07-04 19:17:19 +010031
32import java.io.PrintWriter;
Ady Abrahamf3e05312019-05-13 18:04:59 -070033
34/**
35 * A Blacklist for packages that should force the display out of high refresh rate.
36 */
37class HighRefreshRateBlacklist {
38
Michael Wrightf40ed272019-07-04 19:17:19 +010039 private final ArraySet<String> mBlacklistedPackages = new ArraySet<>();
40 @NonNull
41 private final String[] mDefaultBlacklist;
42 private final Object mLock = new Object();
Ady Abrahamf3e05312019-05-13 18:04:59 -070043
Charles Chenb9f9e64d2019-11-14 17:13:21 +080044 private DeviceConfigInterface mDeviceConfig;
45 private OnPropertiesChangedListener mListener = new OnPropertiesChangedListener();
46
Michael Wrightf40ed272019-07-04 19:17:19 +010047 static HighRefreshRateBlacklist create(@NonNull Resources r) {
Adrian Roos1c2e9a12019-08-20 18:23:47 +020048 return new HighRefreshRateBlacklist(r, DeviceConfigInterface.REAL);
Ady Abrahamf3e05312019-05-13 18:04:59 -070049 }
50
51 @VisibleForTesting
Michael Wrightf40ed272019-07-04 19:17:19 +010052 HighRefreshRateBlacklist(Resources r, DeviceConfigInterface deviceConfig) {
53 mDefaultBlacklist = r.getStringArray(R.array.config_highRefreshRateBlacklist);
Charles Chenb9f9e64d2019-11-14 17:13:21 +080054 mDeviceConfig = deviceConfig;
55 mDeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_DISPLAY_MANAGER,
56 BackgroundThread.getExecutor(), mListener);
57 final String property = mDeviceConfig.getProperty(DeviceConfig.NAMESPACE_DISPLAY_MANAGER,
Michael Wrightf40ed272019-07-04 19:17:19 +010058 KEY_HIGH_REFRESH_RATE_BLACKLIST);
59 updateBlacklist(property);
60 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070061
Michael Wrightf40ed272019-07-04 19:17:19 +010062 private void updateBlacklist(@Nullable String property) {
63 synchronized (mLock) {
64 mBlacklistedPackages.clear();
65 if (property != null) {
66 String[] packages = property.split(",");
67 for (String pkg : packages) {
68 String pkgName = pkg.trim();
69 if (!pkgName.isEmpty()) {
70 mBlacklistedPackages.add(pkgName);
71 }
72 }
73 } else {
74 // If there's no config, or the config has been deleted, fallback to the device's
75 // default blacklist
76 for (String pkg : mDefaultBlacklist) {
77 mBlacklistedPackages.add(pkg);
78 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070079 }
80 }
81 }
82
83 boolean isBlacklisted(String packageName) {
Michael Wrightf40ed272019-07-04 19:17:19 +010084 synchronized (mLock) {
85 return mBlacklistedPackages.contains(packageName);
86 }
87 }
88 void dump(PrintWriter pw) {
89 pw.println("High Refresh Rate Blacklist");
90 pw.println(" Packages:");
91 synchronized (mLock) {
92 for (String pkg : mBlacklistedPackages) {
93 pw.println(" " + pkg);
94 }
95 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070096 }
97
Charles Chenb9f9e64d2019-11-14 17:13:21 +080098 /** Used to prevent WmTests leaking issues. */
99 @VisibleForTesting
100 void dispose() {
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200101 mDeviceConfig.removeOnPropertiesChangedListener(mListener);
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800102 mDeviceConfig = null;
103 mBlacklistedPackages.clear();
104 }
105
Michael Wrightf40ed272019-07-04 19:17:19 +0100106 private class OnPropertiesChangedListener implements DeviceConfig.OnPropertiesChangedListener {
107 public void onPropertiesChanged(@NonNull DeviceConfig.Properties properties) {
Linus Tufvesson6167bf22019-11-18 18:14:00 +0000108 if (properties.getKeyset().contains(KEY_HIGH_REFRESH_RATE_BLACKLIST)) {
109 updateBlacklist(
110 properties.getString(KEY_HIGH_REFRESH_RATE_BLACKLIST, null /*default*/));
111 }
Michael Wrightf40ed272019-07-04 19:17:19 +0100112 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700113 }
114}
Michael Wrightf40ed272019-07-04 19:17:19 +0100115