Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7edef699 authored by Shobhit Goel's avatar Shobhit Goel Committed by Android Build Coastguard Worker
Browse files

Don't let apps enable compat changes past the SDK

Currently only app target sdk is considered for enabling combat changes.
Modifying the condition to check both platform version and app target sdk.

Bug: 312266666
Test: atest PlatformCompatTest
Test: atest CompatConfigTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:ae6bf24f198d821d7d2a9e74c62126130196c931)
Merged-In: I444f47f04d2ecaf02a0a23c6a9de8283ecfb9cf1
Change-Id: I444f47f04d2ecaf02a0a23c6a9de8283ecfb9cf1
parent a153f291
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -252,9 +252,6 @@ public final class CompatChange extends CompatibilityChangeInfo {
            // 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;
+5 −24
Original line number Diff line number Diff line
@@ -868,6 +868,7 @@ public class CompatConfigTest {
                + "<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();
@@ -885,32 +886,12 @@ public class CompatConfigTest {
            ApplicationInfoBuilder.create().withTargetSdk(5).build())).isFalse();
        assertThat(compatConfig.isChangeEnabled(1236L,
            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);
        assertThat(compatConfig.isChangeEnabled(1237L,
            ApplicationInfoBuilder.create().withTargetSdk(31).build())).isFalse();

        compatConfig.initConfigFromLib(dir);
        // Force the platform sdk version to be same as enabled target sdk
        when(mBuildClassifier.platformTargetSdk()).thenReturn(31);

        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();
    }