blob: 271db2895eac4b40b22056198f46e7cf5d15192f [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 Ling5a570b32019-08-13 16:07:14 -070019import static android.hardware.display.DisplayManager.DeviceConfig.KEY_HIGH_REFRESH_RATE_BLACKLIST;
Michael Wright6a51b352019-07-04 19:17:19 +010020
Ady Abrahamf3e05312019-05-13 18:04:59 -070021import android.annotation.NonNull;
Michael Wright6a51b352019-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 Wright6a51b352019-07-04 19:17:19 +010027import com.android.internal.R;
Ady Abrahamf3e05312019-05-13 18:04:59 -070028import com.android.internal.annotations.VisibleForTesting;
Michael Wright6a51b352019-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 Wright6a51b352019-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
Michael Wright6a51b352019-07-04 19:17:19 +010044 static HighRefreshRateBlacklist create(@NonNull Resources r) {
45 return new HighRefreshRateBlacklist(r, new DeviceConfigInterface() {
Ady Abrahamf3e05312019-05-13 18:04:59 -070046 @Override
Michael Wright6a51b352019-07-04 19:17:19 +010047 public @Nullable String getProperty(@NonNull String namespace, @NonNull String name) {
48 return DeviceConfig.getProperty(namespace, name);
Ady Abrahamf3e05312019-05-13 18:04:59 -070049 }
Michael Wright6a51b352019-07-04 19:17:19 +010050 public void addOnPropertyChangedListener(@NonNull String namespace,
51 @NonNull Executor executor,
52 @NonNull DeviceConfig.OnPropertyChangedListener listener) {
53 DeviceConfig.addOnPropertyChangedListener(namespace, executor, listener);
Ady Abrahamf3e05312019-05-13 18:04:59 -070054 }
55 });
56 }
57
58 @VisibleForTesting
Michael Wright6a51b352019-07-04 19:17:19 +010059 HighRefreshRateBlacklist(Resources r, DeviceConfigInterface deviceConfig) {
60 mDefaultBlacklist = r.getStringArray(R.array.config_highRefreshRateBlacklist);
Long Ling5a570b32019-08-13 16:07:14 -070061 deviceConfig.addOnPropertyChangedListener(DeviceConfig.NAMESPACE_DISPLAY_MANAGER,
Michael Wright6a51b352019-07-04 19:17:19 +010062 BackgroundThread.getExecutor(), new OnPropertyChangedListener());
Long Ling5a570b32019-08-13 16:07:14 -070063 final String property = deviceConfig.getProperty(DeviceConfig.NAMESPACE_DISPLAY_MANAGER,
Michael Wright6a51b352019-07-04 19:17:19 +010064 KEY_HIGH_REFRESH_RATE_BLACKLIST);
65 updateBlacklist(property);
66 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070067
Michael Wright6a51b352019-07-04 19:17:19 +010068 private void updateBlacklist(@Nullable String property) {
69 synchronized (mLock) {
70 mBlacklistedPackages.clear();
71 if (property != null) {
72 String[] packages = property.split(",");
73 for (String pkg : packages) {
74 String pkgName = pkg.trim();
75 if (!pkgName.isEmpty()) {
76 mBlacklistedPackages.add(pkgName);
77 }
78 }
79 } else {
80 // If there's no config, or the config has been deleted, fallback to the device's
81 // default blacklist
82 for (String pkg : mDefaultBlacklist) {
83 mBlacklistedPackages.add(pkg);
84 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070085 }
86 }
87 }
88
89 boolean isBlacklisted(String packageName) {
Michael Wright6a51b352019-07-04 19:17:19 +010090 synchronized (mLock) {
91 return mBlacklistedPackages.contains(packageName);
92 }
93 }
94 void dump(PrintWriter pw) {
95 pw.println("High Refresh Rate Blacklist");
96 pw.println(" Packages:");
97 synchronized (mLock) {
98 for (String pkg : mBlacklistedPackages) {
99 pw.println(" " + pkg);
100 }
101 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700102 }
103
Michael Wright6a51b352019-07-04 19:17:19 +0100104 interface DeviceConfigInterface {
105 @Nullable String getProperty(@NonNull String namespace, @NonNull String name);
106 void addOnPropertyChangedListener(@NonNull String namespace, @NonNull Executor executor,
107 @NonNull DeviceConfig.OnPropertyChangedListener listener);
108 }
109
110 private class OnPropertyChangedListener implements DeviceConfig.OnPropertyChangedListener {
111 public void onPropertyChanged(@NonNull String namespace, @NonNull String name,
112 @Nullable String value) {
Long Ling0113bc02019-08-13 16:07:14 -0700113 if (KEY_HIGH_REFRESH_RATE_BLACKLIST.equals(name)) {
114 updateBlacklist(value);
115 }
Michael Wright6a51b352019-07-04 19:17:19 +0100116 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700117 }
118}
Michael Wright6a51b352019-07-04 19:17:19 +0100119