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; |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 30 | import com.android.server.wm.utils.DeviceConfigInterface; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 31 | |
| 32 | import java.io.PrintWriter; |
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) { |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 48 | return new HighRefreshRateBlacklist(r, DeviceConfigInterface.REAL); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @VisibleForTesting |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 52 | HighRefreshRateBlacklist(Resources r, DeviceConfigInterface deviceConfig) { |
| 53 | mDefaultBlacklist = r.getStringArray(R.array.config_highRefreshRateBlacklist); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 54 | mDeviceConfig = deviceConfig; |
| 55 | mDeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, |
| 56 | BackgroundThread.getExecutor(), mListener); |
| 57 | final String property = mDeviceConfig.getProperty(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 58 | KEY_HIGH_REFRESH_RATE_BLACKLIST); |
| 59 | updateBlacklist(property); |
| 60 | } |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 61 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 62 | 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | boolean isBlacklisted(String packageName) { |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 84 | 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 98 | /** Used to prevent WmTests leaking issues. */ |
| 99 | @VisibleForTesting |
| 100 | void dispose() { |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 101 | mDeviceConfig.removeOnPropertiesChangedListener(mListener); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 102 | mDeviceConfig = null; |
| 103 | mBlacklistedPackages.clear(); |
| 104 | } |
| 105 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 106 | private class OnPropertiesChangedListener implements DeviceConfig.OnPropertiesChangedListener { |
| 107 | public void onPropertiesChanged(@NonNull DeviceConfig.Properties properties) { |
Linus Tufvesson | 6167bf2 | 2019-11-18 18:14:00 +0000 | [diff] [blame^] | 108 | if (properties.getKeyset().contains(KEY_HIGH_REFRESH_RATE_BLACKLIST)) { |
| 109 | updateBlacklist( |
| 110 | properties.getString(KEY_HIGH_REFRESH_RATE_BLACKLIST, null /*default*/)); |
| 111 | } |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 112 | } |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 115 | |