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 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 44 | static HighRefreshRateBlacklist create(@NonNull Resources r) { |
| 45 | return new HighRefreshRateBlacklist(r, new DeviceConfigInterface() { |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 46 | @Override |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 47 | public @Nullable String getProperty(@NonNull String namespace, @NonNull String name) { |
| 48 | return DeviceConfig.getProperty(namespace, name); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 49 | } |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 50 | public void addOnPropertiesChangedListener(@NonNull String namespace, |
| 51 | @NonNull Executor executor, |
| 52 | @NonNull DeviceConfig.OnPropertiesChangedListener listener) { |
| 53 | DeviceConfig.addOnPropertiesChangedListener(namespace, executor, listener); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 54 | } |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | @VisibleForTesting |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 59 | HighRefreshRateBlacklist(Resources r, DeviceConfigInterface deviceConfig) { |
| 60 | mDefaultBlacklist = r.getStringArray(R.array.config_highRefreshRateBlacklist); |
Long Ling | 1961cf1 | 2019-08-13 16:07:14 -0700 | [diff] [blame^] | 61 | deviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 62 | BackgroundThread.getExecutor(), new OnPropertiesChangedListener()); |
Long Ling | 1961cf1 | 2019-08-13 16:07:14 -0700 | [diff] [blame^] | 63 | final String property = deviceConfig.getProperty(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 64 | KEY_HIGH_REFRESH_RATE_BLACKLIST); |
| 65 | updateBlacklist(property); |
| 66 | } |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 67 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 68 | 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | boolean isBlacklisted(String packageName) { |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 90 | 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 104 | interface DeviceConfigInterface { |
| 105 | @Nullable String getProperty(@NonNull String namespace, @NonNull String name); |
| 106 | void addOnPropertiesChangedListener(@NonNull String namespace, @NonNull Executor executor, |
| 107 | @NonNull DeviceConfig.OnPropertiesChangedListener listener); |
| 108 | } |
| 109 | |
| 110 | private class OnPropertiesChangedListener implements DeviceConfig.OnPropertiesChangedListener { |
| 111 | public void onPropertiesChanged(@NonNull DeviceConfig.Properties properties) { |
| 112 | updateBlacklist( |
| 113 | properties.getString(KEY_HIGH_REFRESH_RATE_BLACKLIST, null /*default*/)); |
| 114 | } |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 117 | |