From 3cbfde36c29f9b29e6aa3c5c2dac15f7a2ba3e81 Mon Sep 17 00:00:00 2001 From: Arjunsinh Jadeja Date: Fri, 25 Jun 2021 01:51:02 +0530 Subject: [PATCH] Fix splash screen with theme references crash on Android API 21 (flutter/engine#26083) --- .../flutter/shell/platform/android/BUILD.gn | 1 + .../embedding/android/FlutterActivity.java | 10 +-- .../android/FlutterFragmentActivity.java | 10 +-- .../android/FlutterActivityTest.java | 70 ++++++++++++++++++ .../android/FlutterFragmentActivityTest.java | 72 +++++++++++++++++++ .../android/SplashShadowResources.java | 37 ++++++++++ 6 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/SplashShadowResources.java diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index 926686b42d..c4e3a8f907 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -468,6 +468,7 @@ action("robolectric_tests") { "test/io/flutter/embedding/android/KeyChannelResponderTest.java", "test/io/flutter/embedding/android/KeyboardManagerTest.java", "test/io/flutter/embedding/android/RobolectricFlutterActivity.java", + "test/io/flutter/embedding/android/SplashShadowResources.java", "test/io/flutter/embedding/engine/FlutterEngineCacheTest.java", "test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java", "test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java", diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index af7c6070b8..26bfbf7e46 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -24,6 +24,7 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; @@ -35,6 +36,7 @@ import android.view.WindowManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; +import androidx.core.content.res.ResourcesCompat; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleRegistry; @@ -492,16 +494,16 @@ public class FlutterActivity extends Activity * to be used in a manifest file. */ @Nullable - @SuppressWarnings("deprecation") private Drawable getSplashScreenFromManifest() { try { Bundle metaData = getMetaData(); int splashScreenId = metaData != null ? metaData.getInt(SPLASH_SCREEN_META_DATA_KEY) : 0; return splashScreenId != 0 - ? Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP - ? getResources().getDrawable(splashScreenId, getTheme()) - : getResources().getDrawable(splashScreenId) + ? ResourcesCompat.getDrawable(getResources(), splashScreenId, getTheme()) : null; + } catch (Resources.NotFoundException e) { + Log.e(TAG, "Splash screen not found. Ensure the drawable exists and that it's valid."); + throw e; } catch (PackageManager.NameNotFoundException e) { // This is never expected to happen. return null; diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java index 85abc679f5..7dd4f73ca5 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java @@ -22,6 +22,7 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; @@ -35,6 +36,7 @@ import android.widget.FrameLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; +import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import io.flutter.Log; @@ -321,17 +323,17 @@ public class FlutterFragmentActivity extends FragmentActivity * to be used in a manifest file. */ @Nullable - @SuppressWarnings("deprecation") private Drawable getSplashScreenFromManifest() { try { Bundle metaData = getMetaData(); Integer splashScreenId = metaData != null ? metaData.getInt(SPLASH_SCREEN_META_DATA_KEY) : null; return splashScreenId != null - ? Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP - ? getResources().getDrawable(splashScreenId, getTheme()) - : getResources().getDrawable(splashScreenId) + ? ResourcesCompat.getDrawable(getResources(), splashScreenId, getTheme()) : null; + } catch (Resources.NotFoundException e) { + Log.e(TAG, "Splash screen not found. Ensure the drawable exists and that it's valid."); + throw e; } catch (PackageManager.NameNotFoundException e) { // This is never expected to happen. return null; diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java index 423f3a0f93..303800decc 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java @@ -4,6 +4,7 @@ import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.HANDLE_D import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -12,9 +13,12 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.robolectric.Shadows.shadowOf; +import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; +import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.os.Bundle; import androidx.annotation.NonNull; @@ -22,6 +26,7 @@ import androidx.annotation.Nullable; import androidx.lifecycle.DefaultLifecycleObserver; import androidx.lifecycle.LifecycleOwner; import io.flutter.FlutterInjector; +import io.flutter.TestUtils; import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterEngineCache; @@ -309,6 +314,71 @@ public class FlutterActivityTest { assertEquals(1, registeredEngines.size()); } + @Test + public void itDoesNotCrashWhenSplashScreenMetadataIsNotDefined() { + Intent intent = FlutterActivity.createDefaultIntent(RuntimeEnvironment.application); + ActivityController activityController = + Robolectric.buildActivity(FlutterActivity.class, intent); + FlutterActivity flutterActivity = activityController.get(); + + // We never supplied the metadata to the robolectric activity info so it doesn't exist. + SplashScreen splashScreen = flutterActivity.provideSplashScreen(); + // It should quietly return a null and not crash. + assertNull(splashScreen); + } + + @Test + @Config(shadows = {SplashShadowResources.class}) + public void itLoadsSplashScreenDrawable() throws PackageManager.NameNotFoundException { + TestUtils.setApiVersion(19); + Intent intent = FlutterActivity.createDefaultIntent(RuntimeEnvironment.application); + ActivityController activityController = + Robolectric.buildActivity(FlutterActivity.class, intent); + FlutterActivity flutterActivity = activityController.get(); + + // Inject splash screen drawable resource id in the metadata. + PackageManager pm = RuntimeEnvironment.application.getPackageManager(); + ActivityInfo activityInfo = + pm.getActivityInfo(flutterActivity.getComponentName(), PackageManager.GET_META_DATA); + activityInfo.metaData = new Bundle(); + activityInfo.metaData.putInt( + FlutterActivityLaunchConfigs.SPLASH_SCREEN_META_DATA_KEY, + SplashShadowResources.SPLASH_DRAWABLE_ID); + shadowOf(RuntimeEnvironment.application.getPackageManager()).addOrUpdateActivity(activityInfo); + + // It should load the drawable. + SplashScreen splashScreen = flutterActivity.provideSplashScreen(); + assertNotNull(splashScreen); + } + + @Test + @Config(shadows = {SplashShadowResources.class}) + @TargetApi(21) // Theme references in drawables requires API 21+ + public void itLoadsThemedSplashScreenDrawable() throws PackageManager.NameNotFoundException { + // A drawable with theme references can be parsed only if the app theme is supplied + // in getDrawable methods. This test verifies it by fetching a (fake) themed drawable. + // On failure, a Resource.NotFoundException will ocurr. + TestUtils.setApiVersion(21); + Intent intent = FlutterActivity.createDefaultIntent(RuntimeEnvironment.application); + ActivityController activityController = + Robolectric.buildActivity(FlutterActivity.class, intent); + FlutterActivity flutterActivity = activityController.get(); + + // Inject themed splash screen drawable resource id in the metadata. + PackageManager pm = RuntimeEnvironment.application.getPackageManager(); + ActivityInfo activityInfo = + pm.getActivityInfo(flutterActivity.getComponentName(), PackageManager.GET_META_DATA); + activityInfo.metaData = new Bundle(); + activityInfo.metaData.putInt( + FlutterActivityLaunchConfigs.SPLASH_SCREEN_META_DATA_KEY, + SplashShadowResources.THEMED_SPLASH_DRAWABLE_ID); + shadowOf(RuntimeEnvironment.application.getPackageManager()).addOrUpdateActivity(activityInfo); + + // It should load the drawable. + SplashScreen splashScreen = flutterActivity.provideSplashScreen(); + assertNotNull(splashScreen); + } + static class FlutterActivityWithProvidedEngine extends FlutterActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentActivityTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentActivityTest.java index fe63994e56..0f222a278d 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentActivityTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentActivityTest.java @@ -4,13 +4,17 @@ import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.HANDLE_D import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; +import static org.robolectric.Shadows.shadowOf; +import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; +import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.ViewGroup; @@ -18,6 +22,7 @@ import android.widget.FrameLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import io.flutter.FlutterInjector; +import io.flutter.TestUtils; import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterJNI; @@ -30,6 +35,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.android.controller.ActivityController; import org.robolectric.annotation.Config; @Config(manifest = Config.NONE) @@ -219,6 +226,71 @@ public class FlutterFragmentActivityTest { assertEquals(0, activity.numberOfEnginesCreated); } + @Test + public void itDoesNotCrashWhenSplashScreenMetadataIsNotDefined() { + Intent intent = FlutterFragmentActivity.createDefaultIntent(RuntimeEnvironment.application); + ActivityController activityController = + Robolectric.buildActivity(FlutterFragmentActivity.class, intent); + FlutterFragmentActivity fragmentActivity = activityController.get(); + + // We never supplied the resource key to robolectric so it doesn't exist. + SplashScreen splashScreen = fragmentActivity.provideSplashScreen(); + // It should quietly return a null and not crash. + assertNull(splashScreen); + } + + @Test + @Config(shadows = {SplashShadowResources.class}) + public void itLoadsSplashScreenDrawable() throws PackageManager.NameNotFoundException { + TestUtils.setApiVersion(19); + Intent intent = FlutterActivity.createDefaultIntent(RuntimeEnvironment.application); + ActivityController activityController = + Robolectric.buildActivity(FlutterActivity.class, intent); + FlutterActivity flutterActivity = activityController.get(); + + // Inject splash screen drawable resource id in the metadata + PackageManager pm = RuntimeEnvironment.application.getPackageManager(); + ActivityInfo activityInfo = + pm.getActivityInfo(flutterActivity.getComponentName(), PackageManager.GET_META_DATA); + activityInfo.metaData = new Bundle(); + activityInfo.metaData.putInt( + FlutterActivityLaunchConfigs.SPLASH_SCREEN_META_DATA_KEY, + SplashShadowResources.SPLASH_DRAWABLE_ID); + shadowOf(RuntimeEnvironment.application.getPackageManager()).addOrUpdateActivity(activityInfo); + + // It should load the drawable. + SplashScreen splashScreen = flutterActivity.provideSplashScreen(); + assertNotNull(splashScreen); + } + + @Test + @Config(shadows = {SplashShadowResources.class}) + @TargetApi(21) // Theme references in drawables requires API 21+ + public void itLoadsThemedSplashScreenDrawable() throws PackageManager.NameNotFoundException { + // A drawable with theme references can be parsed only if the app theme is supplied + // in getDrawable methods. This test verifies it by fetching a (fake) themed drawable. + // On failure, a Resource.NotFoundException will ocurr. + TestUtils.setApiVersion(21); + Intent intent = FlutterActivity.createDefaultIntent(RuntimeEnvironment.application); + ActivityController activityController = + Robolectric.buildActivity(FlutterActivity.class, intent); + FlutterActivity flutterActivity = activityController.get(); + + // Inject themed splash screen drawable resource id in the metadata. + PackageManager pm = RuntimeEnvironment.application.getPackageManager(); + ActivityInfo activityInfo = + pm.getActivityInfo(flutterActivity.getComponentName(), PackageManager.GET_META_DATA); + activityInfo.metaData = new Bundle(); + activityInfo.metaData.putInt( + FlutterActivityLaunchConfigs.SPLASH_SCREEN_META_DATA_KEY, + SplashShadowResources.THEMED_SPLASH_DRAWABLE_ID); + shadowOf(RuntimeEnvironment.application.getPackageManager()).addOrUpdateActivity(activityInfo); + + // It should load the drawable. + SplashScreen splashScreen = flutterActivity.provideSplashScreen(); + assertNotNull(splashScreen); + } + static class FlutterFragmentActivityWithProvidedEngine extends FlutterFragmentActivity { int numberOfEnginesCreated = 0; diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/SplashShadowResources.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/SplashShadowResources.java new file mode 100644 index 0000000000..2747092c83 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/SplashShadowResources.java @@ -0,0 +1,37 @@ +package io.flutter.embedding.android; + +import android.content.res.Resources; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import org.robolectric.annotation.Implements; +import org.robolectric.annotation.RealObject; +import org.robolectric.shadow.api.Shadow; + +@Implements(Resources.class) +public class SplashShadowResources { + @RealObject private Resources resources; + + public static final int SPLASH_DRAWABLE_ID = 191919; + public static final int THEMED_SPLASH_DRAWABLE_ID = 212121; + + // All other getDrawable() calls, call this method internally. + public Drawable getDrawableForDensity(int id, int density, Resources.Theme theme) + throws Exception { + if (id == SPLASH_DRAWABLE_ID) { + return new ColorDrawable(Color.BLUE); + } + + if (id == THEMED_SPLASH_DRAWABLE_ID) { + // We pretend the drawable contains theme references. It can't be parsed without the app + // theme. + if (theme == null) { + throw new Resources.NotFoundException( + "Cannot parse drawable due to missing theme references."); + } + return new ColorDrawable(Color.GRAY); + } + + return Shadow.directlyOn(resources, Resources.class).getDrawableForDensity(id, density, theme); + } +}