blob: f3bc2176194aecb4d2e180745860d51eaa3fe926 [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;
30
31import java.io.PrintWriter;
32import java.util.concurrent.Executor;
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) {
48 return new HighRefreshRateBlacklist(r, new DeviceConfigInterface() {
Ady Abrahamf3e05312019-05-13 18:04:59 -070049 @Override
Michael Wrightf40ed272019-07-04 19:17:19 +010050 public @Nullable String getProperty(@NonNull String namespace, @NonNull String name) {
51 return DeviceConfig.getProperty(namespace, name);
Ady Abrahamf3e05312019-05-13 18:04:59 -070052 }
Charles Chenb9f9e64d2019-11-14 17:13:21 +080053
54 @Override
Michael Wrightf40ed272019-07-04 19:17:19 +010055 public void addOnPropertiesChangedListener(@NonNull String namespace,
56 @NonNull Executor executor,
57 @NonNull DeviceConfig.OnPropertiesChangedListener listener) {
58 DeviceConfig.addOnPropertiesChangedListener(namespace, executor, listener);
Ady Abrahamf3e05312019-05-13 18:04:59 -070059 }
Charles Chenb9f9e64d2019-11-14 17:13:21 +080060
61 @Override
62 public void removePropertiesChangedListener(
63 DeviceConfig.OnPropertiesChangedListener listener) {
64 DeviceConfig.removeOnPropertiesChangedListener(listener);
65 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070066 });
67 }
68
69 @VisibleForTesting
Michael Wrightf40ed272019-07-04 19:17:19 +010070 HighRefreshRateBlacklist(Resources r, DeviceConfigInterface deviceConfig) {
71 mDefaultBlacklist = r.getStringArray(R.array.config_highRefreshRateBlacklist);
Charles Chenb9f9e64d2019-11-14 17:13:21 +080072 mDeviceConfig = deviceConfig;
73 mDeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_DISPLAY_MANAGER,
74 BackgroundThread.getExecutor(), mListener);
75 final String property = mDeviceConfig.getProperty(DeviceConfig.NAMESPACE_DISPLAY_MANAGER,
Michael Wrightf40ed272019-07-04 19:17:19 +010076 KEY_HIGH_REFRESH_RATE_BLACKLIST);
77 updateBlacklist(property);
78 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070079
Michael Wrightf40ed272019-07-04 19:17:19 +010080 private void updateBlacklist(@Nullable String property) {
81 synchronized (mLock) {
82 mBlacklistedPackages.clear();
83 if (property != null) {
84 String[] packages = property.split(",");
85 for (String pkg : packages) {
86 String pkgName = pkg.trim();
87 if (!pkgName.isEmpty()) {
88 mBlacklistedPackages.add(pkgName);
89 }
90 }
91 } else {
92 // If there's no config, or the config has been deleted, fallback to the device's
93 // default blacklist
94 for (String pkg : mDefaultBlacklist) {
95 mBlacklistedPackages.add(pkg);
96 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070097 }
98 }
99 }
100
101 boolean isBlacklisted(String packageName) {
Michael Wrightf40ed272019-07-04 19:17:19 +0100102 synchronized (mLock) {
103 return mBlacklistedPackages.contains(packageName);
104 }
105 }
106 void dump(PrintWriter pw) {
107 pw.println("High Refresh Rate Blacklist");
108 pw.println(" Packages:");
109 synchronized (mLock) {
110 for (String pkg : mBlacklistedPackages) {
111 pw.println(" " + pkg);
112 }
113 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700114 }
115
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800116 /** Used to prevent WmTests leaking issues. */
117 @VisibleForTesting
118 void dispose() {
119 mDeviceConfig.removePropertiesChangedListener(mListener);
120 mDeviceConfig = null;
121 mBlacklistedPackages.clear();
122 }
123
Michael Wrightf40ed272019-07-04 19:17:19 +0100124 interface DeviceConfigInterface {
125 @Nullable String getProperty(@NonNull String namespace, @NonNull String name);
126 void addOnPropertiesChangedListener(@NonNull String namespace, @NonNull Executor executor,
127 @NonNull DeviceConfig.OnPropertiesChangedListener listener);
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800128 void removePropertiesChangedListener(
129 @NonNull DeviceConfig.OnPropertiesChangedListener listener);
Michael Wrightf40ed272019-07-04 19:17:19 +0100130 }
131
132 private class OnPropertiesChangedListener implements DeviceConfig.OnPropertiesChangedListener {
133 public void onPropertiesChanged(@NonNull DeviceConfig.Properties properties) {
134 updateBlacklist(
135 properties.getString(KEY_HIGH_REFRESH_RATE_BLACKLIST, null /*default*/));
136 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700137 }
138}
Michael Wrightf40ed272019-07-04 19:17:19 +0100139