18
18
* store it on disk and - when the app is relaunched - provide the stored data back to the framework
19
19
* to recreate the original state of the app.
20
20
*
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.
31
27
*
28
+ * <p>The current restoration data provided by the framework can be read via {@code
29
+ * getRestorationData()}.
32
30
*/
33
31
public class RestorationChannel {
34
32
private static final String TAG = "RestorationChannel" ;
35
33
36
34
public RestorationChannel (
37
35
@ NonNull DartExecutor dartExecutor , @ NonNull boolean waitForRestorationData ) {
38
36
this .waitForRestorationData = waitForRestorationData ;
39
- MethodChannel channel =
40
- new MethodChannel (dartExecutor , "flutter/restoration" , StandardMethodCodec .INSTANCE );
37
+ channel = new MethodChannel (dartExecutor , "flutter/restoration" , StandardMethodCodec .INSTANCE );
41
38
channel .setMethodCallHandler (handler );
42
39
}
43
40
@@ -50,39 +47,51 @@ public RestorationChannel(
50
47
* the engine never calls {@code setRestorationData}. If it has been set to true, but it later
51
48
* turns out that there is no restoration data, {@code setRestorationData} must be called with
52
49
* 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[])}.
56
50
*/
57
51
public final boolean waitForRestorationData ;
58
52
53
+ private MethodChannel channel ;
59
54
private byte [] restorationData ;
60
55
private MethodChannel .Result pendingResult ;
61
56
private boolean engineHasProvidedData = false ;
62
57
private boolean frameworkHasRequestedData = false ;
63
58
64
- /** Whether {@code setRestorationData(byte[])} has been called. */
65
- public boolean hasRestorationDataBeenSet () {
66
- return engineHasProvidedData ;
67
- }
68
-
69
59
/** Obtain the most current restoration data that the framework has provided. */
70
60
public byte [] getRestorationData () {
71
61
return restorationData ;
72
62
}
73
63
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 . */
75
65
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
- }
82
66
engineHasProvidedData = true ;
83
67
if (pendingResult != null ) {
84
68
pendingResult .success (data );
85
69
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
+ });
86
95
} else {
87
96
restorationData = data ;
88
97
}
@@ -111,6 +120,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
111
120
result .success (null );
112
121
break ;
113
122
case "get" :
123
+ frameworkHasRequestedData = true ;
114
124
if (engineHasProvidedData || !waitForRestorationData ) {
115
125
result .success (restorationData );
116
126
} else {
0 commit comments