Merge changes Idd7d86f9,I73569744 am: 420476e0cd

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1532338

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I0a5605574dd5126717f879b2dd367861cc35c215
This commit is contained in:
Andrei-Valentin Onea
2021-03-12 14:05:58 +00:00
committed by Automerger Merge Worker
10 changed files with 115 additions and 14 deletions

View File

@@ -31,4 +31,14 @@ public class AndroidBuildClassifier {
public boolean isFinalBuild() {
return "REL".equals(Build.VERSION.CODENAME);
}
/**
* The current platform SDK version.
*/
public int platformTargetSdk() {
if (isFinalBuild()) {
return Build.VERSION.SDK_INT;
}
return Build.VERSION_CODES.CUR_DEVELOPMENT;
}
}

View File

@@ -34,7 +34,8 @@ public final class OverrideAllowedState implements Parcelable {
DISABLED_NON_TARGET_SDK,
DISABLED_TARGET_SDK_TOO_HIGH,
DEFERRED_VERIFICATION,
LOGGING_ONLY_CHANGE
LOGGING_ONLY_CHANGE,
PLATFORM_TOO_OLD
})
@Retention(RetentionPolicy.SOURCE)
public @interface State {
@@ -65,6 +66,10 @@ public final class OverrideAllowedState implements Parcelable {
* Change is marked as logging only, and cannot be toggled.
*/
public static final int LOGGING_ONLY_CHANGE = 5;
/**
* Change is gated by a target sdk version newer than the current platform sdk version.
*/
public static final int PLATFORM_TOO_OLD = 6;
@State
public final int state;
@@ -123,6 +128,11 @@ public final class OverrideAllowedState implements Parcelable {
throw new SecurityException(String.format(
"Cannot override %1$d because it is marked as a logging-only change.",
changeId));
case PLATFORM_TOO_OLD:
throw new SecurityException(String.format(
"Cannot override %1$d for %2$s because the change's targetSdk threshold "
+ "(%3$d) is above the platform sdk.",
changeId, packageName, changeIdTargetSdk));
}
}
@@ -170,6 +180,8 @@ public final class OverrideAllowedState implements Parcelable {
return "DEFERRED_VERIFICATION";
case LOGGING_ONLY_CHANGE:
return "LOGGING_ONLY_CHANGE";
case PLATFORM_TOO_OLD:
return "PLATFORM_TOO_OLD";
}
return "UNKNOWN";
}

View File

@@ -28,6 +28,7 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import com.android.internal.compat.AndroidBuildClassifier;
import com.android.internal.compat.CompatibilityChangeInfo;
import com.android.internal.compat.OverrideAllowedState;
import com.android.server.compat.config.Change;
@@ -55,7 +56,7 @@ public final class CompatChange extends CompatibilityChangeInfo {
* A change ID to be used only in the CTS test for this SystemApi
*/
@ChangeId
@EnabledSince(targetSdkVersion = 1235) // Needs to be > test APK targetSdkVersion.
@EnabledSince(targetSdkVersion = 31) // Needs to be > test APK targetSdkVersion.
static final long CTS_SYSTEM_API_CHANGEID = 149391281; // This is a bug id.
/**
@@ -233,7 +234,7 @@ public final class CompatChange extends CompatibilityChangeInfo {
* @param app Info about the app in question
* @return {@code true} if the change should be enabled for the package.
*/
boolean isEnabled(ApplicationInfo app) {
boolean isEnabled(ApplicationInfo app, AndroidBuildClassifier buildClassifier) {
if (app == null) {
return defaultValue();
}
@@ -244,7 +245,13 @@ public final class CompatChange extends CompatibilityChangeInfo {
return false;
}
if (getEnableSinceTargetSdk() != -1) {
return app.targetSdkVersion >= getEnableSinceTargetSdk();
// If the change is gated by a platform version newer than the one currently installed
// on the device, disregard the app's target sdk version.
int compareSdk = Math.min(app.targetSdkVersion, buildClassifier.platformTargetSdk());
if (compareSdk != app.targetSdkVersion) {
compareSdk = app.targetSdkVersion;
}
return compareSdk >= getEnableSinceTargetSdk();
}
return true;
}

View File

@@ -74,12 +74,14 @@ final class CompatConfig {
private final LongSparseArray<CompatChange> mChanges = new LongSparseArray<>();
private final OverrideValidatorImpl mOverrideValidator;
private final AndroidBuildClassifier mAndroidBuildClassifier;
private Context mContext;
private File mOverridesFile;
@VisibleForTesting
CompatConfig(AndroidBuildClassifier androidBuildClassifier, Context context) {
mOverrideValidator = new OverrideValidatorImpl(androidBuildClassifier, context, this);
mAndroidBuildClassifier = androidBuildClassifier;
mContext = context;
}
@@ -133,7 +135,7 @@ final class CompatConfig {
synchronized (mChanges) {
for (int i = 0; i < mChanges.size(); ++i) {
CompatChange c = mChanges.valueAt(i);
if (!c.isEnabled(app)) {
if (!c.isEnabled(app, mAndroidBuildClassifier)) {
disabled.add(c.getId());
}
}
@@ -175,7 +177,7 @@ final class CompatConfig {
// we know nothing about this change: default behaviour is enabled.
return true;
}
return c.isEnabled(app);
return c.isEnabled(app, mAndroidBuildClassifier);
}
}
@@ -475,7 +477,7 @@ final class CompatConfig {
synchronized (mChanges) {
for (int i = 0; i < mChanges.size(); ++i) {
CompatChange c = mChanges.valueAt(i);
if (c.isEnabled(applicationInfo)) {
if (c.isEnabled(applicationInfo, mAndroidBuildClassifier)) {
enabled.add(c.getId());
} else {
disabled.add(c.getId());

View File

@@ -22,6 +22,7 @@ import static com.android.internal.compat.OverrideAllowedState.DISABLED_NON_TARG
import static com.android.internal.compat.OverrideAllowedState.DISABLED_NOT_DEBUGGABLE;
import static com.android.internal.compat.OverrideAllowedState.DISABLED_TARGET_SDK_TOO_HIGH;
import static com.android.internal.compat.OverrideAllowedState.LOGGING_ONLY_CHANGE;
import static com.android.internal.compat.OverrideAllowedState.PLATFORM_TOO_OLD;
import android.content.Context;
import android.content.pm.ApplicationInfo;
@@ -85,6 +86,9 @@ public class OverrideValidatorImpl extends IOverrideValidator.Stub {
if (debuggableBuild) {
return new OverrideAllowedState(ALLOWED, -1, -1);
}
if (maxTargetSdk >= mAndroidBuildClassifier.platformTargetSdk()) {
return new OverrideAllowedState(PLATFORM_TOO_OLD, -1, maxTargetSdk);
}
PackageManager packageManager = mContext.getPackageManager();
if (packageManager == null) {
throw new IllegalStateException("No PackageManager!");

View File

@@ -66,18 +66,22 @@ public class PlatformCompat extends IPlatformCompat.Stub {
private final Context mContext;
private final ChangeReporter mChangeReporter;
private final CompatConfig mCompatConfig;
private final AndroidBuildClassifier mBuildClassifier;
public PlatformCompat(Context context) {
mContext = context;
mChangeReporter = new ChangeReporter(ChangeReporter.SOURCE_SYSTEM_SERVER);
mCompatConfig = CompatConfig.create(new AndroidBuildClassifier(), mContext);
mBuildClassifier = new AndroidBuildClassifier();
mCompatConfig = CompatConfig.create(mBuildClassifier, mContext);
}
@VisibleForTesting
PlatformCompat(Context context, CompatConfig compatConfig) {
PlatformCompat(Context context, CompatConfig compatConfig,
AndroidBuildClassifier buildClassifier) {
mContext = context;
mChangeReporter = new ChangeReporter(ChangeReporter.SOURCE_SYSTEM_SERVER);
mCompatConfig = compatConfig;
mBuildClassifier = buildClassifier;
registerPackageReceiver(context);
}
@@ -392,7 +396,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {
return false;
}
if (change.getEnableSinceTargetSdk() > 0) {
return change.getEnableSinceTargetSdk() >= Build.VERSION_CODES.Q;
return change.getEnableSinceTargetSdk() >= Build.VERSION_CODES.Q
&& change.getEnableSinceTargetSdk() <= mBuildClassifier.platformTargetSdk();
}
return true;
}

View File

@@ -120,6 +120,11 @@ class CompatConfigBuilder {
return this;
}
CompatConfigBuilder addEnabledSinceApexChangeWithId(int sdk, long id) {
mChanges.add(new CompatChange(id, "", -1, sdk, false, false, "", false));
return this;
}
CompatConfig build() {
CompatConfig config = new CompatConfig(mBuildClassifier, mContext);
config.forceNonDebuggableFinalForTest(false);

View File

@@ -86,6 +86,7 @@ public class CompatConfigTest {
// Assume userdebug/eng non-final build
when(mBuildClassifier.isDebuggableBuild()).thenReturn(true);
when(mBuildClassifier.isFinalBuild()).thenReturn(false);
when(mBuildClassifier.platformTargetSdk()).thenReturn(30);
ChangeIdStateCache.disable();
when(mPackageManager.getApplicationInfo(anyString(), anyInt()))
.thenThrow(new NameNotFoundException());
@@ -566,6 +567,34 @@ public class CompatConfigTest {
ApplicationInfoBuilder.create().withTargetSdk(1).build())).isTrue();
}
@Test
public void testReadApexConfig() throws IOException {
String configXml = "<config>"
+ "<compat-change id=\"1234\" name=\"MY_CHANGE1\" enableAfterTargetSdk=\"2\" />"
+ "<compat-change id=\"1235\" name=\"MY_CHANGE2\" disabled=\"true\" />"
+ "<compat-change id=\"1236\" name=\"MY_CHANGE3\" />"
+ "<compat-change id=\"1237\" name=\"MY_CHANGE4\" enableSinceTargetSdk=\"31\" />"
+ "</config>";
File dir = createTempDir();
writeToFile(dir, "platform_compat_config.xml", configXml);
CompatConfig compatConfig = new CompatConfig(mBuildClassifier, mContext);
compatConfig.forceNonDebuggableFinalForTest(false);
compatConfig.initConfigFromLib(dir);
assertThat(compatConfig.isChangeEnabled(1234L,
ApplicationInfoBuilder.create().withTargetSdk(1).build())).isFalse();
assertThat(compatConfig.isChangeEnabled(1234L,
ApplicationInfoBuilder.create().withTargetSdk(3).build())).isTrue();
assertThat(compatConfig.isChangeEnabled(1235L,
ApplicationInfoBuilder.create().withTargetSdk(5).build())).isFalse();
assertThat(compatConfig.isChangeEnabled(1236L,
ApplicationInfoBuilder.create().withTargetSdk(1).build())).isTrue();
assertThat(compatConfig.isChangeEnabled(1237L,
ApplicationInfoBuilder.create().withTargetSdk(31).build())).isTrue();
}
@Test
public void testReadConfigMultipleFiles() throws IOException {
String configXml1 = "<config>"

View File

@@ -22,6 +22,7 @@ import static com.android.internal.compat.OverrideAllowedState.DISABLED_NON_TARG
import static com.android.internal.compat.OverrideAllowedState.DISABLED_NOT_DEBUGGABLE;
import static com.android.internal.compat.OverrideAllowedState.DISABLED_TARGET_SDK_TOO_HIGH;
import static com.android.internal.compat.OverrideAllowedState.LOGGING_ONLY_CHANGE;
import static com.android.internal.compat.OverrideAllowedState.PLATFORM_TOO_OLD;
import static com.google.common.truth.Truth.assertThat;
@@ -52,6 +53,7 @@ public class OverrideValidatorImplTest {
private static final int TARGET_SDK = 10;
private static final int TARGET_SDK_BEFORE = 9;
private static final int TARGET_SDK_AFTER = 11;
private static final int PLATFORM_SDK_VERSION = 30;
@Mock
private PackageManager mPackageManager;
@@ -61,6 +63,7 @@ public class OverrideValidatorImplTest {
private AndroidBuildClassifier debuggableBuild() {
AndroidBuildClassifier buildClassifier = mock(AndroidBuildClassifier.class);
when(buildClassifier.isDebuggableBuild()).thenReturn(true);
when(buildClassifier.platformTargetSdk()).thenReturn(PLATFORM_SDK_VERSION);
return buildClassifier;
}
@@ -68,6 +71,7 @@ public class OverrideValidatorImplTest {
AndroidBuildClassifier buildClassifier = mock(AndroidBuildClassifier.class);
when(buildClassifier.isDebuggableBuild()).thenReturn(false);
when(buildClassifier.isFinalBuild()).thenReturn(false);
when(buildClassifier.platformTargetSdk()).thenReturn(PLATFORM_SDK_VERSION);
return buildClassifier;
}
@@ -75,6 +79,7 @@ public class OverrideValidatorImplTest {
AndroidBuildClassifier buildClassifier = mock(AndroidBuildClassifier.class);
when(buildClassifier.isDebuggableBuild()).thenReturn(false);
when(buildClassifier.isFinalBuild()).thenReturn(true);
when(buildClassifier.platformTargetSdk()).thenReturn(PLATFORM_SDK_VERSION);
return buildClassifier;
}
@@ -332,6 +337,26 @@ public class OverrideValidatorImplTest {
TARGET_SDK_BEFORE));
}
@Test
public void getOverrideAllowedState_targetSdkChangeGreaterThanOsVersion_rejectOverride()
throws Exception {
final AndroidBuildClassifier buildClassifier = finalBuild();
CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext)
.addEnabledSinceApexChangeWithId(PLATFORM_SDK_VERSION + 1, 1).build();
IOverrideValidator overrideValidator = config.getOverrideValidator();
when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt()))
.thenReturn(ApplicationInfoBuilder.create()
.withPackageName(PACKAGE_NAME)
.debuggable()
.build());
OverrideAllowedState stateTargetSdkLessChange =
overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME);
assertThat(stateTargetSdkLessChange).isEqualTo(
new OverrideAllowedState(PLATFORM_TOO_OLD, -1,
PLATFORM_SDK_VERSION));
}
@Test
public void getOverrideAllowedState_finalBuildEnabledChangeDebugApp_rejectOverride()
throws Exception {

View File

@@ -78,11 +78,12 @@ public class PlatformCompatTest {
when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt()))
.thenThrow(new PackageManager.NameNotFoundException());
mCompatConfig = new CompatConfig(mBuildClassifier, mContext);
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig);
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig, mBuildClassifier);
// Assume userdebug/eng non-final build
mCompatConfig.forceNonDebuggableFinalForTest(false);
when(mBuildClassifier.isDebuggableBuild()).thenReturn(true);
when(mBuildClassifier.isFinalBuild()).thenReturn(false);
when(mBuildClassifier.platformTargetSdk()).thenReturn(30);
LocalServices.removeServiceForTest(PackageManagerInternal.class);
LocalServices.addService(PackageManagerInternal.class, mPackageManagerInternal);
}
@@ -99,7 +100,7 @@ public class PlatformCompatTest {
.addLoggingOnlyChangeWithId(7L)
.addOverridableChangeWithId(8L)
.build();
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig);
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig, mBuildClassifier);
assertThat(mPlatformCompat.listAllChanges()).asList().containsExactly(
new CompatibilityChangeInfo(1L, "", -1, -1, false, false, "", false),
new CompatibilityChangeInfo(2L, "change2", -1, -1, true, false, "", false),
@@ -125,8 +126,9 @@ public class PlatformCompatTest {
.addEnableSinceSdkChangeWithId(Build.VERSION_CODES.Q, 5L)
.addEnableSinceSdkChangeWithId(Build.VERSION_CODES.R, 6L)
.addLoggingOnlyChangeWithId(7L)
.addEnableSinceSdkChangeWithId(31, 8L)
.build();
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig);
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig, mBuildClassifier);
assertThat(mPlatformCompat.listUIChanges()).asList().containsExactly(
new CompatibilityChangeInfo(1L, "", -1, -1, false, false, "", false),
new CompatibilityChangeInfo(2L, "change2", -1, -1, true, false, "", false),
@@ -144,7 +146,7 @@ public class PlatformCompatTest {
.addEnableAfterSdkChangeWithId(Build.VERSION_CODES.O, 3L)
.build();
mCompatConfig.forceNonDebuggableFinalForTest(true);
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig);
mPlatformCompat = new PlatformCompat(mContext, mCompatConfig, mBuildClassifier);
// Before adding overrides.
assertThat(mPlatformCompat.isChangeEnabledByPackageName(1, PACKAGE_NAME, 0)).isTrue();