Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [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.compat; |
| 18 | |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 19 | import android.compat.Compatibility.ChangeConfig; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 20 | import android.content.pm.ApplicationInfo; |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 21 | import android.os.Environment; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 22 | import android.text.TextUtils; |
| 23 | import android.util.LongArray; |
| 24 | import android.util.LongSparseArray; |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 25 | import android.util.Slog; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 26 | |
| 27 | import com.android.internal.annotations.GuardedBy; |
| 28 | import com.android.internal.annotations.VisibleForTesting; |
Andrei Onea | ea997f2 | 2019-09-12 18:08:59 +0100 | [diff] [blame] | 29 | import com.android.internal.compat.CompatibilityChangeConfig; |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 30 | import com.android.internal.compat.CompatibilityChangeInfo; |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 31 | import com.android.server.compat.config.Change; |
| 32 | import com.android.server.compat.config.XmlParser; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 33 | |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 34 | import org.xmlpull.v1.XmlPullParserException; |
| 35 | |
| 36 | import java.io.BufferedInputStream; |
| 37 | import java.io.File; |
| 38 | import java.io.FileInputStream; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
Andrei Onea | c61bf0c | 2019-07-10 16:43:09 +0100 | [diff] [blame] | 41 | import java.io.PrintWriter; |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 42 | import java.util.HashSet; |
| 43 | import java.util.Set; |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 44 | |
| 45 | import javax.xml.datatype.DatatypeConfigurationException; |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 46 | |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 47 | /** |
| 48 | * This class maintains state relating to platform compatibility changes. |
| 49 | * |
| 50 | * <p>It stores the default configuration for each change, and any per-package overrides that have |
| 51 | * been configured. |
| 52 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 53 | final class CompatConfig { |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 54 | |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 55 | private static final String TAG = "CompatConfig"; |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 56 | |
| 57 | private static final CompatConfig sInstance = new CompatConfig().initConfigFromLib( |
| 58 | Environment.buildPath( |
atrost | 7aa64a7 | 2019-08-29 14:22:13 +0100 | [diff] [blame] | 59 | Environment.getRootDirectory(), "etc", "compatconfig")); |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 60 | |
| 61 | @GuardedBy("mChanges") |
| 62 | private final LongSparseArray<CompatChange> mChanges = new LongSparseArray<>(); |
| 63 | |
| 64 | @VisibleForTesting |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 65 | CompatConfig() { |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return The static instance of this class to be used within the system server. |
| 70 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 71 | static CompatConfig get() { |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 72 | return sInstance; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Add a change. This is intended to be used by code that reads change config from the |
| 77 | * filesystem. This should be done at system startup time. |
| 78 | * |
| 79 | * @param change The change to add. Any change with the same ID will be overwritten. |
| 80 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 81 | void addChange(CompatChange change) { |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 82 | synchronized (mChanges) { |
| 83 | mChanges.put(change.getId(), change); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Retrieves the set of disabled changes for a given app. Any change ID not in the returned |
| 89 | * array is by default enabled for the app. |
| 90 | * |
| 91 | * @param app The app in question |
| 92 | * @return A sorted long array of change IDs. We use a primitive array to minimize memory |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 93 | * footprint: Every app process will store this array statically so we aim to reduce |
| 94 | * overhead as much as possible. |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 95 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 96 | long[] getDisabledChanges(ApplicationInfo app) { |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 97 | LongArray disabled = new LongArray(); |
| 98 | synchronized (mChanges) { |
| 99 | for (int i = 0; i < mChanges.size(); ++i) { |
| 100 | CompatChange c = mChanges.valueAt(i); |
| 101 | if (!c.isEnabled(app)) { |
| 102 | disabled.add(c.getId()); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | // Note: we don't need to explicitly sort the array, as the behaviour of LongSparseArray |
| 107 | // (mChanges) ensures it's already sorted. |
| 108 | return disabled.toArray(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Look up a change ID by name. |
| 113 | * |
| 114 | * @param name Name of the change to look up |
| 115 | * @return The change ID, or {@code -1} if no change with that name exists. |
| 116 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 117 | long lookupChangeId(String name) { |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 118 | synchronized (mChanges) { |
| 119 | for (int i = 0; i < mChanges.size(); ++i) { |
| 120 | if (TextUtils.equals(mChanges.valueAt(i).getName(), name)) { |
| 121 | return mChanges.keyAt(i); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Find if a given change is enabled for a given application. |
| 130 | * |
| 131 | * @param changeId The ID of the change in question |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 132 | * @param app App to check for |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 133 | * @return {@code true} if the change is enabled for this app. Also returns {@code true} if the |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 134 | * change ID is not known, as unknown changes are enabled by default. |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 135 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 136 | boolean isChangeEnabled(long changeId, ApplicationInfo app) { |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 137 | synchronized (mChanges) { |
| 138 | CompatChange c = mChanges.get(changeId); |
| 139 | if (c == null) { |
| 140 | // we know nothing about this change: default behaviour is enabled. |
| 141 | return true; |
| 142 | } |
| 143 | return c.isEnabled(app); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Overrides the enabled state for a given change and app. This method is intended to be used |
| 149 | * *only* for debugging purposes, ultimately invoked either by an adb command, or from some |
| 150 | * developer settings UI. |
| 151 | * |
| 152 | * <p>Note, package overrides are not persistent and will be lost on system or runtime restart. |
| 153 | * |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 154 | * @param changeId The ID of the change to be overridden. Note, this call will succeed even |
| 155 | * if |
| 156 | * this change is not known; it will only have any effect if any code in the |
| 157 | * platform is gated on the ID given. |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 158 | * @param packageName The app package name to override the change for. |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 159 | * @param enabled If the change should be enabled or disabled. |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 160 | * @return {@code true} if the change existed before adding the override. |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 161 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 162 | boolean addOverride(long changeId, String packageName, boolean enabled) { |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 163 | boolean alreadyKnown = true; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 164 | synchronized (mChanges) { |
| 165 | CompatChange c = mChanges.get(changeId); |
| 166 | if (c == null) { |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 167 | alreadyKnown = false; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 168 | c = new CompatChange(changeId); |
| 169 | addChange(c); |
| 170 | } |
| 171 | c.addPackageOverride(packageName, enabled); |
| 172 | } |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 173 | return alreadyKnown; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 177 | * Check whether the change is known to the compat config. |
| 178 | * |
| 179 | * @return {@code true} if the change is known. |
| 180 | */ |
| 181 | boolean isKnownChangeId(long changeId) { |
| 182 | synchronized (mChanges) { |
| 183 | CompatChange c = mChanges.get(changeId); |
| 184 | return c != null; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 189 | * Removes an override previously added via {@link #addOverride(long, String, boolean)}. This |
| 190 | * restores the default behaviour for the given change and app, once any app processes have been |
| 191 | * restarted. |
| 192 | * |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 193 | * @param changeId The ID of the change that was overridden. |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 194 | * @param packageName The app package name that was overridden. |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 195 | * @return {@code true} if an override existed; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 196 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 197 | boolean removeOverride(long changeId, String packageName) { |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 198 | boolean overrideExists = false; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 199 | synchronized (mChanges) { |
| 200 | CompatChange c = mChanges.get(changeId); |
| 201 | if (c != null) { |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 202 | overrideExists = true; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 203 | c.removePackageOverride(packageName); |
| 204 | } |
| 205 | } |
Andrei Onea | c1ba019 | 2019-06-28 18:42:22 +0100 | [diff] [blame] | 206 | return overrideExists; |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 207 | } |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 208 | |
Andrei Onea | ea997f2 | 2019-09-12 18:08:59 +0100 | [diff] [blame] | 209 | /** |
| 210 | * Overrides the enabled state for a given change and app. This method is intended to be used |
| 211 | * *only* for debugging purposes. |
| 212 | * |
| 213 | * <p>Note, package overrides are not persistent and will be lost on system or runtime restart. |
| 214 | * |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 215 | * @param overrides list of overrides to default changes config. |
Andrei Onea | ea997f2 | 2019-09-12 18:08:59 +0100 | [diff] [blame] | 216 | * @param packageName app for which the overrides will be applied. |
| 217 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 218 | void addOverrides(CompatibilityChangeConfig overrides, String packageName) { |
Andrei Onea | ea997f2 | 2019-09-12 18:08:59 +0100 | [diff] [blame] | 219 | synchronized (mChanges) { |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 220 | for (Long changeId : overrides.enabledChanges()) { |
Andrei Onea | ea997f2 | 2019-09-12 18:08:59 +0100 | [diff] [blame] | 221 | addOverride(changeId, packageName, true); |
| 222 | } |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 223 | for (Long changeId : overrides.disabledChanges()) { |
Andrei Onea | ea997f2 | 2019-09-12 18:08:59 +0100 | [diff] [blame] | 224 | addOverride(changeId, packageName, false); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Removes all overrides previously added via {@link #addOverride(long, String, boolean)} or |
| 231 | * {@link #addAppOverrides(CompatibilityChangeConfig, String)} for a certain package. |
| 232 | * |
| 233 | * <p>This restores the default behaviour for the given change and app, once any app |
| 234 | * processes have been restarted. |
| 235 | * |
| 236 | * @param packageName The package for which the overrides should be purged. |
| 237 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 238 | void removePackageOverrides(String packageName) { |
Andrei Onea | ea997f2 | 2019-09-12 18:08:59 +0100 | [diff] [blame] | 239 | synchronized (mChanges) { |
| 240 | for (int i = 0; i < mChanges.size(); ++i) { |
| 241 | mChanges.valueAt(i).removePackageOverride(packageName); |
| 242 | } |
| 243 | } |
| 244 | } |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 245 | |
Andrei Onea | c61bf0c | 2019-07-10 16:43:09 +0100 | [diff] [blame] | 246 | /** |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 247 | * Dumps the current list of compatibility config information. |
| 248 | * |
| 249 | * @param pw The {@link PrintWriter} instance to which the information will be dumped. |
| 250 | */ |
| 251 | void dumpConfig(PrintWriter pw) { |
Andrei Onea | c61bf0c | 2019-07-10 16:43:09 +0100 | [diff] [blame] | 252 | synchronized (mChanges) { |
| 253 | if (mChanges.size() == 0) { |
| 254 | pw.println("No compat overrides."); |
| 255 | return; |
| 256 | } |
| 257 | for (int i = 0; i < mChanges.size(); ++i) { |
| 258 | CompatChange c = mChanges.valueAt(i); |
| 259 | pw.println(c.toString()); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 264 | /** |
| 265 | * Get the config for a given app. |
| 266 | * |
| 267 | * @param applicationInfo the {@link ApplicationInfo} for which the info should be dumped. |
| 268 | * @return A {@link CompatibilityChangeConfig} which contains the compat config info for the |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 269 | * given app. |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 270 | */ |
| 271 | |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 272 | CompatibilityChangeConfig getAppConfig(ApplicationInfo applicationInfo) { |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 273 | Set<Long> enabled = new HashSet<>(); |
| 274 | Set<Long> disabled = new HashSet<>(); |
| 275 | synchronized (mChanges) { |
| 276 | for (int i = 0; i < mChanges.size(); ++i) { |
| 277 | CompatChange c = mChanges.valueAt(i); |
| 278 | if (c.isEnabled(applicationInfo)) { |
| 279 | enabled.add(c.getId()); |
| 280 | } else { |
| 281 | disabled.add(c.getId()); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | return new CompatibilityChangeConfig(new ChangeConfig(enabled, disabled)); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Dumps all the compatibility change information. |
| 290 | * |
| 291 | * @return An array of {@link CompatibilityChangeInfo} with the current changes. |
| 292 | */ |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 293 | CompatibilityChangeInfo[] dumpChanges() { |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 294 | synchronized (mChanges) { |
| 295 | CompatibilityChangeInfo[] changeInfos = new CompatibilityChangeInfo[mChanges.size()]; |
| 296 | for (int i = 0; i < mChanges.size(); ++i) { |
| 297 | CompatChange change = mChanges.valueAt(i); |
| 298 | changeInfos[i] = new CompatibilityChangeInfo(change.getId(), |
atrost | f69bbe1 | 2019-11-06 16:00:38 +0000 | [diff] [blame] | 299 | change.getName(), |
| 300 | change.getEnableAfterTargetSdk(), |
| 301 | change.getDisabled()); |
Andrei Onea | 18041f78 | 2019-10-01 14:34:56 +0100 | [diff] [blame] | 302 | } |
| 303 | return changeInfos; |
| 304 | } |
| 305 | } |
| 306 | |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 307 | CompatConfig initConfigFromLib(File libraryDir) { |
| 308 | if (!libraryDir.exists() || !libraryDir.isDirectory()) { |
| 309 | Slog.e(TAG, "No directory " + libraryDir + ", skipping"); |
| 310 | return this; |
| 311 | } |
| 312 | for (File f : libraryDir.listFiles()) { |
atrost | 7aa64a7 | 2019-08-29 14:22:13 +0100 | [diff] [blame] | 313 | Slog.d(TAG, "Found a config file: " + f.getPath()); |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 314 | //TODO(b/138222363): Handle duplicate ids across config files. |
atrost | 7aa64a7 | 2019-08-29 14:22:13 +0100 | [diff] [blame] | 315 | readConfig(f); |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 316 | } |
| 317 | return this; |
| 318 | } |
| 319 | |
| 320 | private void readConfig(File configFile) { |
| 321 | try (InputStream in = new BufferedInputStream(new FileInputStream(configFile))) { |
| 322 | for (Change change : XmlParser.read(in).getCompatChange()) { |
atrost | 7aa64a7 | 2019-08-29 14:22:13 +0100 | [diff] [blame] | 323 | Slog.d(TAG, "Adding: " + change.toString()); |
atrost | 7e6060f | 2019-07-24 14:12:48 +0100 | [diff] [blame] | 324 | addChange(new CompatChange(change)); |
| 325 | } |
| 326 | } catch (IOException | DatatypeConfigurationException | XmlPullParserException e) { |
| 327 | Slog.e(TAG, "Encountered an error while reading/parsing compat config file", e); |
| 328 | } |
| 329 | } |
| 330 | |
Mathew Inwood | e188acc | 2019-06-20 15:13:33 +0100 | [diff] [blame] | 331 | } |