Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Long Ling | 1961cf1 | 2019-08-13 16:07:14 -0700 | [diff] [blame] | 19 | import static android.hardware.display.DisplayManager.DeviceConfig.KEY_HIGH_REFRESH_RATE_BLACKLIST; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 20 | |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 22 | import android.annotation.Nullable; |
| 23 | import android.content.res.Resources; |
| 24 | import android.provider.DeviceConfig; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 25 | import android.util.ArraySet; |
| 26 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 27 | import com.android.internal.R; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 29 | import com.android.internal.os.BackgroundThread; |
| 30 | |
| 31 | import java.io.PrintWriter; |
| 32 | import java.util.concurrent.Executor; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A Blacklist for packages that should force the display out of high refresh rate. |
| 36 | */ |
| 37 | class HighRefreshRateBlacklist { |
| 38 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 39 | private final ArraySet<String> mBlacklistedPackages = new ArraySet<>(); |
| 40 | @NonNull |
| 41 | private final String[] mDefaultBlacklist; |
| 42 | private final Object mLock = new Object(); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 43 | |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame^] | 44 | private DeviceConfigInterface mDeviceConfig; |
| 45 | private OnPropertiesChangedListener mListener = new OnPropertiesChangedListener(); |
| 46 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 47 | static HighRefreshRateBlacklist create(@NonNull Resources r) { |
| 48 | return new HighRefreshRateBlacklist(r, new DeviceConfigInterface() { |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 49 | @Override |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 50 | public @Nullable String getProperty(@NonNull String namespace, @NonNull String name) { |
| 51 | return DeviceConfig.getProperty(namespace, name); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 52 | } |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame^] | 53 | |
| 54 | @Override |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 55 | public void addOnPropertiesChangedListener(@NonNull String namespace, |
| 56 | @NonNull Executor executor, |
| 57 | @NonNull DeviceConfig.OnPropertiesChangedListener listener) { |
| 58 | DeviceConfig.addOnPropertiesChangedListener(namespace, executor, listener); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 59 | } |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame^] | 60 | |
| 61 | @Override |
| 62 | public void removePropertiesChangedListener( |
| 63 | DeviceConfig.OnPropertiesChangedListener listener) { |
| 64 | DeviceConfig.removeOnPropertiesChangedListener(listener); |
| 65 | } |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 66 | }); |
| 67 | } |
| 68 | |
| 69 | @VisibleForTesting |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 70 | HighRefreshRateBlacklist(Resources r, DeviceConfigInterface deviceConfig) { |
| 71 | mDefaultBlacklist = r.getStringArray(R.array.config_highRefreshRateBlacklist); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame^] | 72 | mDeviceConfig = deviceConfig; |
| 73 | mDeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, |
| 74 | BackgroundThread.getExecutor(), mListener); |
| 75 | final String property = mDeviceConfig.getProperty(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 76 | KEY_HIGH_REFRESH_RATE_BLACKLIST); |
| 77 | updateBlacklist(property); |
| 78 | } |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 79 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 80 | 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | boolean isBlacklisted(String packageName) { |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 102 | 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame^] | 116 | /** Used to prevent WmTests leaking issues. */ |
| 117 | @VisibleForTesting |
| 118 | void dispose() { |
| 119 | mDeviceConfig.removePropertiesChangedListener(mListener); |
| 120 | mDeviceConfig = null; |
| 121 | mBlacklistedPackages.clear(); |
| 122 | } |
| 123 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 124 | 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 Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame^] | 128 | void removePropertiesChangedListener( |
| 129 | @NonNull DeviceConfig.OnPropertiesChangedListener listener); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 130 | } |
| 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 139 | |