Fix splash screen with theme references crash on Android API 21 (flutter/engine#26083)

This commit is contained in:
Arjunsinh Jadeja
2021-06-25 01:51:02 +05:30
committed by GitHub
parent 1359ea97ff
commit 3cbfde36c2
6 changed files with 192 additions and 8 deletions

View File

@@ -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",

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<FlutterActivity> 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<FlutterActivity> 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<FlutterActivity> 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) {

View File

@@ -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<FlutterFragmentActivity> 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<FlutterActivity> 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<FlutterActivity> 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;

View File

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