diff --git a/api/module-lib-current.txt b/api/module-lib-current.txt index 45917e7b07b7c..032e30ce83127 100644 --- a/api/module-lib-current.txt +++ b/api/module-lib-current.txt @@ -19,6 +19,10 @@ package android.content.rollback { package android.graphics { + public final class Compatibility { + method public static void setTargetSdkVersion(int); + } + public final class ImageDecoder implements java.lang.AutoCloseable { method @AnyThread @NonNull public static android.graphics.ImageDecoder.Source createSource(@NonNull android.content.ContentResolver, @NonNull android.net.Uri, @Nullable android.content.res.Resources); } diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 21b56d3e337f0..ab94e2d173e6b 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -82,7 +82,6 @@ import android.database.sqlite.SQLiteDebug.DbStats; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.HardwareRenderer; -import android.graphics.ImageDecoder; import android.hardware.display.DisplayManagerGlobal; import android.inputmethodservice.InputMethodService; import android.net.ConnectivityManager; @@ -6280,12 +6279,9 @@ public final class ActivityThread extends ClientTransactionHandler { Message.updateCheckRecycle(data.appInfo.targetSdkVersion); - // Prior to P, internal calls to decode Bitmaps used BitmapFactory, - // which may scale up to account for density. In P, we switched to - // ImageDecoder, which skips the upscale to save memory. ImageDecoder - // needs to still scale up in older apps, in case they rely on the - // size of the Bitmap without considering its density. - ImageDecoder.sApiLevel = data.appInfo.targetSdkVersion; + // Supply the targetSdkVersion to the UI rendering module, which may + // need it in cases where it does not have access to the appInfo. + android.graphics.Compatibility.setTargetSdkVersion(data.appInfo.targetSdkVersion); /* * Before spawning a new process, reset the time zone to be the system time zone. diff --git a/graphics/java/android/graphics/Compatibility.java b/graphics/java/android/graphics/Compatibility.java new file mode 100644 index 0000000000000..113cf9faab177 --- /dev/null +++ b/graphics/java/android/graphics/Compatibility.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.graphics; + +import android.annotation.SystemApi; + +/** + * Helper class for graphics classes to retrieve the targetSdkVersion, as + * specified by the app. + * @hide + */ +@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) +public final class Compatibility { + private Compatibility() {} + + private static int sTargetSdkVersion = 0; + + /** + * Exposed so that ActivityThread can set it correctly once when binding the + * application. No other code should call this. + * @hide + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static void setTargetSdkVersion(int targetSdkVersion) { + sTargetSdkVersion = targetSdkVersion; + } + + /** + * Public for access by other packages in the module (like android.graphics.drawable), + * but should not be accessed outside the module. + * @hide + */ + public static int getTargetSdkVersion() { + return sTargetSdkVersion; + } +} diff --git a/graphics/java/android/graphics/ImageDecoder.java b/graphics/java/android/graphics/ImageDecoder.java index 3622f5146d3d2..aec9453e27e47 100644 --- a/graphics/java/android/graphics/ImageDecoder.java +++ b/graphics/java/android/graphics/ImageDecoder.java @@ -171,9 +171,6 @@ import java.util.concurrent.atomic.AtomicBoolean; * */ public final class ImageDecoder implements AutoCloseable { - /** @hide **/ - public static int sApiLevel; - /** * Source of encoded image data. * @@ -1933,7 +1930,8 @@ public final class ImageDecoder implements AutoCloseable { // For P and above, only resize if it would be a downscale. Scale up prior // to P in case the app relies on the Bitmap's size without considering density. - if (srcDensity < dstDensity && sApiLevel >= Build.VERSION_CODES.P) { + if (srcDensity < dstDensity + && Compatibility.getTargetSdkVersion() >= Build.VERSION_CODES.P) { return srcDensity; } diff --git a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java index 9fb72cf08b519..73dbe65bd25bc 100644 --- a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java +++ b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java @@ -25,8 +25,6 @@ import android.animation.TimeInterpolator; import android.animation.ValueAnimator; import android.annotation.NonNull; import android.annotation.Nullable; -import android.app.ActivityThread; -import android.app.Application; import android.compat.annotation.UnsupportedAppUsage; import android.content.pm.ActivityInfo.Config; import android.content.res.ColorStateList; @@ -368,14 +366,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { * @return whether invalid animations for vector drawable should be ignored. */ private static boolean shouldIgnoreInvalidAnimation() { - Application app = ActivityThread.currentApplication(); - if (app == null || app.getApplicationInfo() == null) { - return true; - } - if (app.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) { - return true; - } - return false; + return android.graphics.Compatibility.getTargetSdkVersion() < Build.VERSION_CODES.N; } @Override