Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6dd6e3c

Browse files
committed
refactor
1 parent 23a3dc0 commit 6dd6e3c

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.flutter.embedding.engine.FlutterShellArgs;
2626
import io.flutter.embedding.engine.dart.DartExecutor;
2727
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
28-
import io.flutter.embedding.engine.systemchannels.RestorationChannel;
2928
import io.flutter.plugin.platform.PlatformPlugin;
3029
import java.util.Arrays;
3130

@@ -307,9 +306,8 @@ void onActivityCreated(@Nullable Bundle bundle) {
307306
frameworkBundle = bundle.getByteArray(FRAMEWORK_RESTORATION_BUNDLE_KEY);
308307
}
309308

310-
final RestorationChannel restorationChannel = flutterEngine.getRestorationChannel();
311-
if (host.shouldRestoreAndSaveState() && !restorationChannel.hasRestorationDataBeenSet()) {
312-
restorationChannel.setRestorationData(frameworkBundle);
309+
if (host.shouldRestoreAndSaveState()) {
310+
flutterEngine.getRestorationChannel().setRestorationData(frameworkBundle);
313311
}
314312

315313
if (host.shouldAttachEngineToActivity()) {
@@ -465,7 +463,8 @@ void onSaveInstanceState(@Nullable Bundle bundle) {
465463

466464
if (host.shouldRestoreAndSaveState()) {
467465
bundle.putByteArray(
468-
FRAMEWORK_RESTORATION_BUNDLE_KEY, flutterEngine.getRestorationChannel().getRestorationData());
466+
FRAMEWORK_RESTORATION_BUNDLE_KEY,
467+
flutterEngine.getRestorationChannel().getRestorationData());
469468
}
470469

471470
if (host.shouldAttachEngineToActivity()) {
@@ -836,9 +835,6 @@ PlatformPlugin providePlatformPlugin(
836835
* onSaveInstanceState(Bundle)} the current framework instance state obtained from {@code
837836
* RestorationChannel} will be stored in the provided bundle.
838837
*
839-
* <p>This may only be set to true if the engine in used has the
840-
* {@code FlutterEngine.willProvideRestorationData} flag set to true.
841-
*
842838
* <p>This defaults to true, unless a cached engine is used.
843839
*/
844840
boolean shouldRestoreAndSaveState();

shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ public FlutterEngine(
165165
/**
166166
* Same as {@link #FlutterEngine(Context, String[], boolean)} with added support for configuring
167167
* whether the engine will receive restoration data.
168+
*
169+
* <p>When the engine is configured to receive restoration data {@code
170+
* RestorationChannel.setRestorationData(byte[] data)} must be called to provide the restoration
171+
* data. All requests to get restoration data from the framework will be blocked until that method
172+
* is called. If the engine is configured to wait for restoration data, but it turns out later
173+
* that no restoration data has been provided by the operating system, that method must still be
174+
* called with null as an argument to indicate "no data".
168175
*/
169176
public FlutterEngine(
170177
@NonNull Context context,

shell/platform/android/io/flutter/embedding/engine/systemchannels/RestorationChannel.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,23 @@
1818
* store it on disk and - when the app is relaunched - provide the stored data back to the framework
1919
* to recreate the original state of the app.
2020
*
21-
* <p>The channel only accepts restoration data for the engine when {@code waitForRestorationData}
22-
* is set to true. If it is not set, it will send null to the framework as initial restoration data.
23-
* When it is set to true, it will wait until {@code setRestorationData(byte[])} has been called
24-
* before responding to restoration data requests from the framework. In other words, if
25-
* {@code waitForRestorationData} is true, {@code setRestorationData(byte[])} must be called
26-
* (possibly with null as argument if no restoration data is available).
27-
*
28-
* <p>Restoration data for the framework can only be set once via
29-
* {@code setRestorationData(byte[])}. The current restoration data provided by the framework can be
30-
* read via {@code getRestorationData()}.
21+
* <p>The channel can be configured to delay responding to the framework's request for restoration
22+
* data via {@code waitForRestorationData} until the engine-side has provided the data. This is
23+
* useful for use cases where the engine is pre-warmed at a point in the application's life cycle
24+
* where the operating system has not been made available to the engine yet. For example, if the
25+
* engine is pre-warmed as part of the Application before an Activity is created, this flag should
26+
* be set to true because Android will only provide the restoration data to the Activity.
3127
*
28+
* <p>The current restoration data provided by the framework can be read via {@code
29+
* getRestorationData()}.
3230
*/
3331
public class RestorationChannel {
3432
private static final String TAG = "RestorationChannel";
3533

3634
public RestorationChannel(
3735
@NonNull DartExecutor dartExecutor, @NonNull boolean waitForRestorationData) {
3836
this.waitForRestorationData = waitForRestorationData;
39-
MethodChannel channel =
40-
new MethodChannel(dartExecutor, "flutter/restoration", StandardMethodCodec.INSTANCE);
37+
channel = new MethodChannel(dartExecutor, "flutter/restoration", StandardMethodCodec.INSTANCE);
4138
channel.setMethodCallHandler(handler);
4239
}
4340

@@ -50,39 +47,51 @@ public RestorationChannel(
5047
* the engine never calls {@code setRestorationData}. If it has been set to true, but it later
5148
* turns out that there is no restoration data, {@code setRestorationData} must be called with
5249
* null.
53-
*
54-
* <p>When constructing an engine set this to true if you want to provide restoration data to
55-
* the framework by calling {@code setRestorationData(byte[])}.
5650
*/
5751
public final boolean waitForRestorationData;
5852

53+
private MethodChannel channel;
5954
private byte[] restorationData;
6055
private MethodChannel.Result pendingResult;
6156
private boolean engineHasProvidedData = false;
6257
private boolean frameworkHasRequestedData = false;
6358

64-
/** Whether {@code setRestorationData(byte[])} has been called. */
65-
public boolean hasRestorationDataBeenSet() {
66-
return engineHasProvidedData;
67-
}
68-
6959
/** Obtain the most current restoration data that the framework has provided. */
7060
public byte[] getRestorationData() {
7161
return restorationData;
7262
}
7363

74-
/** Set the restoration data that will be sent to the framework when the framework requests it. */
64+
/** Set the restoration data from which the framework will restore its state. */
7565
public void setRestorationData(byte[] data) {
76-
if (!waitForRestorationData || engineHasProvidedData) {
77-
Log.e(
78-
TAG,
79-
"Channel has not been configured to wait for restoration data or data has already been provided.");
80-
return;
81-
}
8266
engineHasProvidedData = true;
8367
if (pendingResult != null) {
8468
pendingResult.success(data);
8569
pendingResult = null;
70+
} else if (frameworkHasRequestedData) {
71+
channel.invokeMethod(
72+
"push",
73+
data,
74+
new MethodChannel.Result() {
75+
@Override
76+
public void success(Object result) {
77+
restorationData = data;
78+
}
79+
80+
@Override
81+
public void error(String errorCode, String errorMessage, Object errorDetails) {
82+
Log.e(
83+
TAG,
84+
"Error "
85+
+ errorCode
86+
+ " while sending restoration data to framework: "
87+
+ errorMessage);
88+
}
89+
90+
@Override
91+
public void notImplemented() {
92+
// Nothing to do.
93+
}
94+
});
8695
} else {
8796
restorationData = data;
8897
}
@@ -111,6 +120,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
111120
result.success(null);
112121
break;
113122
case "get":
123+
frameworkHasRequestedData = true;
114124
if (engineHasProvidedData || !waitForRestorationData) {
115125
result.success(restorationData);
116126
} else {

0 commit comments

Comments
 (0)