From 03e2b04b992309977d2d200aa069475ca586c8fc Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 25 Aug 2022 10:39:50 -0700 Subject: [PATCH 01/12] Implement OnBackInvoked___ --- .../embedding/android/FlutterActivity.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index 13137ebf7dd7b..abc589c90e406 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -35,6 +35,9 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; +import android.window.OnBackInvokedCallback; +import android.window.OnBackInvokedDispatcher; +import androidx.activity.ComponentActivity; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; @@ -208,7 +211,7 @@ // A number of methods in this class have the same implementation as FlutterFragmentActivity. These // methods are duplicated for readability purposes. Be sure to replicate any change in this class in // FlutterFragmentActivity, too. -public class FlutterActivity extends Activity +public class FlutterActivity extends ComponentActivity implements FlutterActivityAndFragmentDelegate.Host, LifecycleOwner { private static final String TAG = "FlutterActivity"; @@ -453,9 +456,7 @@ public Intent build(@NonNull Context context) { @NonNull private LifecycleRegistry lifecycle; - public FlutterActivity() { - lifecycle = new LifecycleRegistry(this); - } + public FlutterActivity() {} /** * This method exists so that JVM tests can ensure that a delegate exists without putting this @@ -495,6 +496,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) { + getOnBackInvokedDispatcher() + .registerOnBackInvokedCallback( + OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback); + } + configureWindowForTransparency(); setContentView(createFlutterView()); @@ -502,6 +509,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { configureStatusBarForFullscreenFlutterExperience(); } + private final OnBackInvokedCallback onBackInvokedCallback = + new OnBackInvokedCallback() { + @Override + public void onBackInvoked() { + onBackPressed(); + } + }; + /** * Switches themes for this {@code Activity} from the theme used to launch this {@code Activity} * to a "normal theme" that is intended for regular {@code Activity} operation. @@ -656,6 +671,9 @@ protected void onPause() { @Override protected void onStop() { super.onStop(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) { + getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback); + } if (stillAttachedForEvent("onStop")) { delegate.onStop(); } @@ -789,6 +807,9 @@ public Activity getActivity() { @Override @NonNull public Lifecycle getLifecycle() { + if (lifecycle == null) { + lifecycle = new LifecycleRegistry(this); + } return lifecycle; } From 08971f981f1b87a6d2c0c49e9c908d425d5e7199 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Fri, 26 Aug 2022 13:12:13 -0700 Subject: [PATCH 02/12] Tests --- .../embedding/android/FlutterActivity.java | 26 +++++++++++++------ .../android/FlutterActivityTest.java | 23 ++++++++++++++++ .../platform/android/test_runner/build.gradle | 9 ++++--- .../src/main/resources/robolectric.properties | 2 +- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index abc589c90e406..2a0ade5f16eb0 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -496,11 +496,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) { - getOnBackInvokedDispatcher() - .registerOnBackInvokedCallback( - OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback); - } + registerOnBackInvokedCallback(); configureWindowForTransparency(); @@ -509,6 +505,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { configureStatusBarForFullscreenFlutterExperience(); } + @VisibleForTesting + public void registerOnBackInvokedCallback() { + if (Build.VERSION.SDK_INT >= 33) { + getOnBackInvokedDispatcher() + .registerOnBackInvokedCallback( + OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback); + } + } + + @VisibleForTesting + public void unregisterOnBackInvokedCallback() { + if (Build.VERSION.SDK_INT >= 33) { + getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback); + } + } + private final OnBackInvokedCallback onBackInvokedCallback = new OnBackInvokedCallback() { @Override @@ -671,9 +683,6 @@ protected void onPause() { @Override protected void onStop() { super.onStop(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) { - getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback); - } if (stillAttachedForEvent("onStop")) { delegate.onStop(); } @@ -699,6 +708,7 @@ protected void onSaveInstanceState(Bundle outState) { *

After calling, this activity should be disposed immediately and not be re-used. */ private void release() { + unregisterOnBackInvokedCallback(); if (delegate != null) { delegate.release(); delegate = null; diff --git a/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java b/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java index d6d1d30d442f0..ec17a12212d99 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java @@ -88,6 +88,21 @@ public void flutterViewHasId() { assertTrue(activity.findViewById(FlutterActivity.FLUTTER_VIEW_ID) instanceof FlutterView); } + // TODO(garyq): Robolectric does not yet support android api 33 yet. Switch to a robolectric + // test that directly exercises the OnBackInvoked APIs when API 33 is supported. + @Test + @TargetApi(33) + public void itRegistersOnBackInvokedCallbackOnCreate() { + Intent intent = FlutterActivityWithReportFullyDrawn.createDefaultIntent(ctx); + ActivityController activityController = + Robolectric.buildActivity(FlutterActivityWithReportFullyDrawn.class, intent); + FlutterActivityWithReportFullyDrawn activity = spy(activityController.get()); + + activity.onCreate(null); + + verify(activity, times(1)).registerOnBackInvokedCallback(); + } + @Test public void itCreatesDefaultIntentWithExpectedDefaults() { Intent intent = FlutterActivity.createDefaultIntent(ctx); @@ -596,6 +611,14 @@ public void resetFullyDrawn() { } } + private class FlutterActivityWithMockBackInvokedHandling extends FlutterActivity { + @Override + public void registerOnBackInvokedCallback() {} + + @Override + public void unregisterOnBackInvokedCallback() {} + } + private static final class FakeFlutterPlugin implements FlutterPlugin, ActivityAware, diff --git a/shell/platform/android/test_runner/build.gradle b/shell/platform/android/test_runner/build.gradle index dfdf361aeeae0..a76b546c7fccd 100644 --- a/shell/platform/android/test_runner/build.gradle +++ b/shell/platform/android/test_runner/build.gradle @@ -36,6 +36,7 @@ android { compileSdkVersion 33 defaultConfig { + targetSdkVersion 33 minSdkVersion 16 } @@ -70,11 +71,11 @@ android { testImplementation "androidx.test:core:1.4.0" testImplementation "com.google.android.play:core:1.8.0" testImplementation "com.ibm.icu:icu4j:69.1" - testImplementation "org.robolectric:robolectric:4.7.3" - testImplementation "junit:junit:4.13" - testImplementation "androidx.test.ext:junit:1.1.3" + testImplementation "org.robolectric:robolectric:4.8.2" + testImplementation "junit:junit:4.13.2" + testImplementation "androidx.test.ext:junit:1.1.4-alpha07" - def mockitoVersion = "4.1.0" + def mockitoVersion = "4.7.0" testImplementation "org.mockito:mockito-core:$mockitoVersion" testImplementation "org.mockito:mockito-inline:$mockitoVersion" testImplementation "org.mockito:mockito-android:$mockitoVersion" diff --git a/shell/platform/android/test_runner/src/main/resources/robolectric.properties b/shell/platform/android/test_runner/src/main/resources/robolectric.properties index ba72a7e57eb78..ac3b918cdd7d5 100644 --- a/shell/platform/android/test_runner/src/main/resources/robolectric.properties +++ b/shell/platform/android/test_runner/src/main/resources/robolectric.properties @@ -1,2 +1,2 @@ -sdk=31 +sdk=32 shadows=io.flutter.CustomShadowContextImpl From d6e915f10da66106d13674a274bfe5e510998675 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Fri, 26 Aug 2022 13:19:02 -0700 Subject: [PATCH 03/12] Docs --- .../embedding/android/FlutterActivity.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index 2a0ade5f16eb0..2cccc202734ab 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -505,6 +505,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { configureStatusBarForFullscreenFlutterExperience(); } + /** + * Registers the callback with OnBackInvokedDispatcher to capture back navigation + * gestures and pass them to the framework. + * + * This replaces the deprecated onBackPressed method override in order to support + * API 33's predictive back navigation feature. + * + * The callback must be unregistered in order to prevent unpredictable behavior + * once outside the Flutter app. + */ @VisibleForTesting public void registerOnBackInvokedCallback() { if (Build.VERSION.SDK_INT >= 33) { @@ -514,6 +524,12 @@ public void registerOnBackInvokedCallback() { } } + /** + * Unregisters the callback from OnBackInvokedDispatcher. + * + * This should be called when the activity is no longer in use to prevent unpredictable + * behavior such as being stuck and unable to press back. + */ @VisibleForTesting public void unregisterOnBackInvokedCallback() { if (Build.VERSION.SDK_INT >= 33) { From 9cdeaaafea8533e7555be0f901cbc479a65f5634 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Fri, 26 Aug 2022 21:44:16 -0700 Subject: [PATCH 04/12] Formatting --- .../flutter/embedding/android/FlutterActivity.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index 2cccc202734ab..db01b4aedb94b 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -506,14 +506,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } /** - * Registers the callback with OnBackInvokedDispatcher to capture back navigation - * gestures and pass them to the framework. + * Registers the callback with OnBackInvokedDispatcher to capture back navigation gestures and + * pass them to the framework. * - * This replaces the deprecated onBackPressed method override in order to support - * API 33's predictive back navigation feature. + *

This replaces the deprecated onBackPressed method override in order to support API 33's + * predictive back navigation feature. * - * The callback must be unregistered in order to prevent unpredictable behavior - * once outside the Flutter app. + * The callback must be unregistered in order to prevent unpredictable behavior once outside + * the Flutter app. */ @VisibleForTesting public void registerOnBackInvokedCallback() { @@ -527,7 +527,7 @@ public void registerOnBackInvokedCallback() { /** * Unregisters the callback from OnBackInvokedDispatcher. * - * This should be called when the activity is no longer in use to prevent unpredictable + *

This should be called when the activity is no longer in use to prevent unpredictable * behavior such as being stuck and unable to press back. */ @VisibleForTesting From 87d57d3702650b3c17aa3fa52d15047ed3f96493 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Fri, 26 Aug 2022 21:45:08 -0700 Subject: [PATCH 05/12] Formatting --- .../android/io/flutter/embedding/android/FlutterActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index db01b4aedb94b..09efad2ca14ed 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -512,7 +512,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { *

This replaces the deprecated onBackPressed method override in order to support API 33's * predictive back navigation feature. * - * The callback must be unregistered in order to prevent unpredictable behavior once outside + *

The callback must be unregistered in order to prevent unpredictable behavior once outside * the Flutter app. */ @VisibleForTesting From 7b06ba05bbc15e1d53bfea78b221dda9f13c65a9 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 1 Sep 2022 09:17:47 -0700 Subject: [PATCH 06/12] Test unregister --- .../embedding/android/FlutterActivity.java | 3 ++- .../embedding/android/FlutterActivityTest.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index 09efad2ca14ed..5ea2b8783ee01 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -723,7 +723,8 @@ protected void onSaveInstanceState(Bundle outState) { * *

After calling, this activity should be disposed immediately and not be re-used. */ - private void release() { + @VisibleForTesting + public void release() { unregisterOnBackInvokedCallback(); if (delegate != null) { delegate.release(); diff --git a/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java b/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java index ec17a12212d99..75d7abfcb4f03 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java @@ -103,6 +103,21 @@ public void itRegistersOnBackInvokedCallbackOnCreate() { verify(activity, times(1)).registerOnBackInvokedCallback(); } + // TODO(garyq): Robolectric does not yet support android api 33 yet. Switch to a robolectric + // test that directly exercises the OnBackInvoked APIs when API 33 is supported. + @Test + @TargetApi(33) + public void itUnregistersOnBackInvokedCallbackOnRelease() { + Intent intent = FlutterActivityWithReportFullyDrawn.createDefaultIntent(ctx); + ActivityController activityController = + Robolectric.buildActivity(FlutterActivityWithReportFullyDrawn.class, intent); + FlutterActivityWithReportFullyDrawn activity = spy(activityController.get()); + + activity.release(); + + verify(activity, times(1)).unregisterOnBackInvokedCallback(); + } + @Test public void itCreatesDefaultIntentWithExpectedDefaults() { Intent intent = FlutterActivity.createDefaultIntent(ctx); From f1fe98bf84714f44876e9ba271e3dacbb10c9188 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 1 Sep 2022 09:19:30 -0700 Subject: [PATCH 07/12] Use getLifecycle() --- .../flutter/embedding/android/FlutterActivity.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index 5ea2b8783ee01..94c2814999792 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -494,7 +494,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { delegate.onAttach(this); delegate.onRestoreInstanceState(savedInstanceState); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_CREATE); registerOnBackInvokedCallback(); @@ -664,7 +664,7 @@ private void configureStatusBarForFullscreenFlutterExperience() { @Override protected void onStart() { super.onStart(); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_START); if (stillAttachedForEvent("onStart")) { delegate.onStart(); } @@ -673,7 +673,7 @@ protected void onStart() { @Override protected void onResume() { super.onResume(); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_RESUME); if (stillAttachedForEvent("onResume")) { delegate.onResume(); } @@ -693,7 +693,7 @@ protected void onPause() { if (stillAttachedForEvent("onPause")) { delegate.onPause(); } - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); } @Override @@ -702,7 +702,7 @@ protected void onStop() { if (stillAttachedForEvent("onStop")) { delegate.onStop(); } - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); + getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_STOP); } @Override @@ -755,7 +755,7 @@ protected void onDestroy() { delegate.onDetach(); } release(); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); } @Override From 26a55beecc6e39584d30a31bb3f66957bf13c215 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 1 Sep 2022 09:50:39 -0700 Subject: [PATCH 08/12] Lifecycle use getter --- .../embedding/android/FlutterActivity.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index 94c2814999792..cf030e6457751 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -494,7 +494,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { delegate.onAttach(this); delegate.onRestoreInstanceState(savedInstanceState); - getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_CREATE); registerOnBackInvokedCallback(); @@ -664,7 +664,7 @@ private void configureStatusBarForFullscreenFlutterExperience() { @Override protected void onStart() { super.onStart(); - getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_START); + getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_START); if (stillAttachedForEvent("onStart")) { delegate.onStart(); } @@ -673,7 +673,7 @@ protected void onStart() { @Override protected void onResume() { super.onResume(); - getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_RESUME); if (stillAttachedForEvent("onResume")) { delegate.onResume(); } @@ -693,7 +693,7 @@ protected void onPause() { if (stillAttachedForEvent("onPause")) { delegate.onPause(); } - getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); } @Override @@ -702,7 +702,7 @@ protected void onStop() { if (stillAttachedForEvent("onStop")) { delegate.onStop(); } - getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_STOP); + getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_STOP); } @Override @@ -755,7 +755,7 @@ protected void onDestroy() { delegate.onDetach(); } release(); - getLifecycle().handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); } @Override @@ -840,6 +840,10 @@ public Lifecycle getLifecycle() { return lifecycle; } + private LifecycleRegistry getLifecycleRegistry() { + return (LifecycleRegistry) lifecycle; + } + /** * {@link FlutterActivityAndFragmentDelegate.Host} method that is used by {@link * FlutterActivityAndFragmentDelegate} to obtain Flutter shell arguments when initializing From 49ba91cc90e0a4ef5d1225a99eb4e519937a2744 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 1 Sep 2022 10:28:39 -0700 Subject: [PATCH 09/12] Undo lifecycle changes: --- .../embedding/android/FlutterActivity.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index cf030e6457751..c74a7a34418b3 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -37,7 +37,6 @@ import android.view.WindowManager; import android.window.OnBackInvokedCallback; import android.window.OnBackInvokedDispatcher; -import androidx.activity.ComponentActivity; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; @@ -211,7 +210,7 @@ // A number of methods in this class have the same implementation as FlutterFragmentActivity. These // methods are duplicated for readability purposes. Be sure to replicate any change in this class in // FlutterFragmentActivity, too. -public class FlutterActivity extends ComponentActivity +public class FlutterActivity extends Activity implements FlutterActivityAndFragmentDelegate.Host, LifecycleOwner { private static final String TAG = "FlutterActivity"; @@ -456,7 +455,9 @@ public Intent build(@NonNull Context context) { @NonNull private LifecycleRegistry lifecycle; - public FlutterActivity() {} + public FlutterActivity() { + lifecycle = new LifecycleRegistry(this); + } /** * This method exists so that JVM tests can ensure that a delegate exists without putting this @@ -494,7 +495,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { delegate.onAttach(this); delegate.onRestoreInstanceState(savedInstanceState); - getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); registerOnBackInvokedCallback(); @@ -664,7 +665,7 @@ private void configureStatusBarForFullscreenFlutterExperience() { @Override protected void onStart() { super.onStart(); - getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); if (stillAttachedForEvent("onStart")) { delegate.onStart(); } @@ -673,7 +674,7 @@ protected void onStart() { @Override protected void onResume() { super.onResume(); - getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); if (stillAttachedForEvent("onResume")) { delegate.onResume(); } @@ -693,7 +694,7 @@ protected void onPause() { if (stillAttachedForEvent("onPause")) { delegate.onPause(); } - getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); } @Override @@ -702,7 +703,7 @@ protected void onStop() { if (stillAttachedForEvent("onStop")) { delegate.onStop(); } - getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); } @Override @@ -755,7 +756,7 @@ protected void onDestroy() { delegate.onDetach(); } release(); - getLifecycleRegistry().handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); } @Override @@ -834,16 +835,9 @@ public Activity getActivity() { @Override @NonNull public Lifecycle getLifecycle() { - if (lifecycle == null) { - lifecycle = new LifecycleRegistry(this); - } return lifecycle; } - private LifecycleRegistry getLifecycleRegistry() { - return (LifecycleRegistry) lifecycle; - } - /** * {@link FlutterActivityAndFragmentDelegate.Host} method that is used by {@link * FlutterActivityAndFragmentDelegate} to obtain Flutter shell arguments when initializing From b01082fa8dc70a36a21a467934e489a99adac55d Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 1 Sep 2022 11:38:11 -0700 Subject: [PATCH 10/12] API gate callback --- .../android/io/flutter/embedding/android/FlutterActivity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index c74a7a34418b3..d94ab09fe5b96 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -538,13 +538,13 @@ public void unregisterOnBackInvokedCallback() { } } - private final OnBackInvokedCallback onBackInvokedCallback = + private final OnBackInvokedCallback onBackInvokedCallback = Build.VERSION.SDK_INT >= 33 ? new OnBackInvokedCallback() { @Override public void onBackInvoked() { onBackPressed(); } - }; + } : null; /** * Switches themes for this {@code Activity} from the theme used to launch this {@code Activity} From 2e9a5eaaa32ee5fd5d95277de2b14b277338f3ae Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 1 Sep 2022 11:39:09 -0700 Subject: [PATCH 11/12] Formatting --- .../embedding/android/FlutterActivity.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java index d94ab09fe5b96..dd502a880c800 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java @@ -538,13 +538,15 @@ public void unregisterOnBackInvokedCallback() { } } - private final OnBackInvokedCallback onBackInvokedCallback = Build.VERSION.SDK_INT >= 33 ? - new OnBackInvokedCallback() { - @Override - public void onBackInvoked() { - onBackPressed(); - } - } : null; + private final OnBackInvokedCallback onBackInvokedCallback = + Build.VERSION.SDK_INT >= 33 + ? new OnBackInvokedCallback() { + @Override + public void onBackInvoked() { + onBackPressed(); + } + } + : null; /** * Switches themes for this {@code Activity} from the theme used to launch this {@code Activity} From 46ee4c16c01734ad25d82cdc281ecb764dfeac9b Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 1 Sep 2022 17:45:23 -0700 Subject: [PATCH 12/12] REvert some test dependency bumps --- shell/platform/android/test_runner/build.gradle | 3 +-- .../test_runner/src/main/resources/robolectric.properties | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/shell/platform/android/test_runner/build.gradle b/shell/platform/android/test_runner/build.gradle index a76b546c7fccd..d1dfa4cc8d37b 100644 --- a/shell/platform/android/test_runner/build.gradle +++ b/shell/platform/android/test_runner/build.gradle @@ -36,7 +36,6 @@ android { compileSdkVersion 33 defaultConfig { - targetSdkVersion 33 minSdkVersion 16 } @@ -71,7 +70,7 @@ android { testImplementation "androidx.test:core:1.4.0" testImplementation "com.google.android.play:core:1.8.0" testImplementation "com.ibm.icu:icu4j:69.1" - testImplementation "org.robolectric:robolectric:4.8.2" + testImplementation "org.robolectric:robolectric:4.7.3" testImplementation "junit:junit:4.13.2" testImplementation "androidx.test.ext:junit:1.1.4-alpha07" diff --git a/shell/platform/android/test_runner/src/main/resources/robolectric.properties b/shell/platform/android/test_runner/src/main/resources/robolectric.properties index ac3b918cdd7d5..ba72a7e57eb78 100644 --- a/shell/platform/android/test_runner/src/main/resources/robolectric.properties +++ b/shell/platform/android/test_runner/src/main/resources/robolectric.properties @@ -1,2 +1,2 @@ -sdk=32 +sdk=31 shadows=io.flutter.CustomShadowContextImpl