From be8e9b2fc7405a343091d73c6e2ce771995dc77a Mon Sep 17 00:00:00 2001 From: atrost Date: Tue, 17 Mar 2020 20:53:33 +0000 Subject: [PATCH] Introduce listUIChanges API for developer UI Filter out changes that are: logging only, older then enabled after P, newer then enabled after Q. Added missing @Test that broke builds. Test: atest com.android.server.compat.PlatformCompatTest Bug: 151299145 Change-Id: Ic4c12ceec3f4018a1f206b8498e8216466ecf960 --- .../compat/CompatibilityChangeInfo.java | 37 ++++++++++++++++ .../internal/compat/IPlatformCompat.aidl | 8 ++++ .../android/server/compat/PlatformCompat.java | 24 ++++++++++ .../server/compat/PlatformCompatTest.java | 44 +++++++++++++++++++ 4 files changed, 113 insertions(+) diff --git a/core/java/com/android/internal/compat/CompatibilityChangeInfo.java b/core/java/com/android/internal/compat/CompatibilityChangeInfo.java index ab890d277f43e..9ba025988126a 100644 --- a/core/java/com/android/internal/compat/CompatibilityChangeInfo.java +++ b/core/java/com/android/internal/compat/CompatibilityChangeInfo.java @@ -93,6 +93,43 @@ public class CompatibilityChangeInfo implements Parcelable { dest.writeString(mDescription); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder("CompatibilityChangeInfo(") + .append(getId()); + if (getName() != null) { + sb.append("; name=").append(getName()); + } + if (getEnableAfterTargetSdk() != -1) { + sb.append("; enableAfterTargetSdk=").append(getEnableAfterTargetSdk()); + } + if (getDisabled()) { + sb.append("; disabled"); + } + if (getLoggingOnly()) { + sb.append("; loggingOnly"); + } + return sb.append(")").toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || !(o instanceof CompatibilityChangeInfo)) { + return false; + } + CompatibilityChangeInfo that = (CompatibilityChangeInfo) o; + return this.mChangeId == that.mChangeId + && this.mName.equals(that.mName) + && this.mEnableAfterTargetSdk == that.mEnableAfterTargetSdk + && this.mDisabled == that.mDisabled + && this.mLoggingOnly == that.mLoggingOnly + && this.mDescription.equals(that.mDescription); + + } + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { diff --git a/core/java/com/android/internal/compat/IPlatformCompat.aidl b/core/java/com/android/internal/compat/IPlatformCompat.aidl index 523ed6fa9a4f0..6408def7eeac2 100644 --- a/core/java/com/android/internal/compat/IPlatformCompat.aidl +++ b/core/java/com/android/internal/compat/IPlatformCompat.aidl @@ -221,6 +221,14 @@ interface IPlatformCompat */ CompatibilityChangeInfo[] listAllChanges(); + /** + * List the compatibility changes that should be present in the UI. + * Filters out certain changes like e.g. logging only. + * + * @return An array of {@link CompatChangeInfo}. + */ + CompatibilityChangeInfo[] listUIChanges(); + /** * Get an instance that can determine whether a changeid can be overridden for a package name. */ diff --git a/services/core/java/com/android/server/compat/PlatformCompat.java b/services/core/java/com/android/server/compat/PlatformCompat.java index 8519b00237aa4..8ed864c71625a 100644 --- a/services/core/java/com/android/server/compat/PlatformCompat.java +++ b/services/core/java/com/android/server/compat/PlatformCompat.java @@ -28,6 +28,7 @@ import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManagerInternal; import android.os.Binder; +import android.os.Build; import android.os.RemoteException; import android.os.UserHandle; import android.util.Slog; @@ -44,6 +45,7 @@ import com.android.server.LocalServices; import java.io.FileDescriptor; import java.io.PrintWriter; +import java.util.Arrays; /** * System server internal API for gating and reporting compatibility changes. @@ -56,6 +58,9 @@ public class PlatformCompat extends IPlatformCompat.Stub { private final ChangeReporter mChangeReporter; private final CompatConfig mCompatConfig; + private static int sMinTargetSdk = Build.VERSION_CODES.P; + private static int sMaxTargetSdk = Build.VERSION_CODES.Q; + public PlatformCompat(Context context) { mContext = context; mChangeReporter = new ChangeReporter( @@ -220,6 +225,12 @@ public class PlatformCompat extends IPlatformCompat.Stub { return mCompatConfig.dumpChanges(); } + @Override + public CompatibilityChangeInfo[] listUIChanges() { + return Arrays.stream(listAllChanges()).filter( + x -> isShownInUI(x)).toArray(CompatibilityChangeInfo[]::new); + } + /** * Check whether the change is known to the compat config. * @@ -339,4 +350,17 @@ public class PlatformCompat extends IPlatformCompat.Stub { checkCompatChangeReadPermission(); checkCompatChangeLogPermission(); } + + private boolean isShownInUI(CompatibilityChangeInfo change) { + if (change.getLoggingOnly()) { + return false; + } + if (change.getEnableAfterTargetSdk() > 0) { + if (change.getEnableAfterTargetSdk() < sMinTargetSdk + || change.getEnableAfterTargetSdk() > sMaxTargetSdk) { + return false; + } + } + return true; + } } diff --git a/services/tests/servicestests/src/com/android/server/compat/PlatformCompatTest.java b/services/tests/servicestests/src/com/android/server/compat/PlatformCompatTest.java index 53b90f2b01db6..cef02ffc38319 100644 --- a/services/tests/servicestests/src/com/android/server/compat/PlatformCompatTest.java +++ b/services/tests/servicestests/src/com/android/server/compat/PlatformCompatTest.java @@ -30,10 +30,12 @@ import static org.testng.Assert.assertThrows; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.PackageManagerInternal; +import android.os.Build; import androidx.test.runner.AndroidJUnit4; import com.android.internal.compat.AndroidBuildClassifier; +import com.android.internal.compat.CompatibilityChangeInfo; import com.android.server.LocalServices; import org.junit.Before; @@ -77,6 +79,48 @@ public class PlatformCompatTest { LocalServices.addService(PackageManagerInternal.class, mPackageManagerInternal); } + @Test + public void testListAllChanges() { + mCompatConfig = CompatConfigBuilder.create(mBuildClassifier, mContext) + .addEnabledChangeWithId(1L) + .addDisabledChangeWithIdAndName(2L, "change2") + .addTargetSdkChangeWithIdAndDescription(Build.VERSION_CODES.O, 3L, "description") + .addTargetSdkChangeWithId(Build.VERSION_CODES.P, 4L) + .addTargetSdkChangeWithId(Build.VERSION_CODES.Q, 5L) + .addTargetSdkChangeWithId(Build.VERSION_CODES.R, 6L) + .addLoggingOnlyChangeWithId(7L) + .build(); + mPlatformCompat = new PlatformCompat(mContext, mCompatConfig); + assertThat(mPlatformCompat.listAllChanges()).asList().containsExactly( + new CompatibilityChangeInfo(1L, "", -1, false, false, ""), + new CompatibilityChangeInfo(2L, "change2", -1, true, false, ""), + new CompatibilityChangeInfo(3L, "", Build.VERSION_CODES.O, false, false, + "description"), + new CompatibilityChangeInfo(4L, "", Build.VERSION_CODES.P, false, false, ""), + new CompatibilityChangeInfo(5L, "", Build.VERSION_CODES.Q, false, false, ""), + new CompatibilityChangeInfo(6L, "", Build.VERSION_CODES.R, false, false, ""), + new CompatibilityChangeInfo(7L, "", -1, false, true, "")); + } + + @Test + public void testListUIChanges() { + mCompatConfig = CompatConfigBuilder.create(mBuildClassifier, mContext) + .addEnabledChangeWithId(1L) + .addDisabledChangeWithIdAndName(2L, "change2") + .addTargetSdkChangeWithIdAndDescription(Build.VERSION_CODES.O, 3L, "description") + .addTargetSdkChangeWithId(Build.VERSION_CODES.P, 4L) + .addTargetSdkChangeWithId(Build.VERSION_CODES.Q, 5L) + .addTargetSdkChangeWithId(Build.VERSION_CODES.R, 6L) + .addLoggingOnlyChangeWithId(7L) + .build(); + mPlatformCompat = new PlatformCompat(mContext, mCompatConfig); + assertThat(mPlatformCompat.listUIChanges()).asList().containsExactly( + new CompatibilityChangeInfo(1L, "", -1, false, false, ""), + new CompatibilityChangeInfo(2L, "change2", -1, true, false, ""), + new CompatibilityChangeInfo(4L, "", Build.VERSION_CODES.P, false, false, ""), + new CompatibilityChangeInfo(5L, "", Build.VERSION_CODES.Q, false, false, "")); + } + @Test public void testRegisterListenerToSameIdThrows() throws Exception { // Registering a listener to change 1 is successful.