2020 *
2121 * <p>The channel can be configured to delay responding to the framework's request for restoration
2222 * 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.
23+ * useful when the engine is pre-warmed at a point in the application's life cycle where the
24+ * restoration data is not available yet. For example, if the engine is pre-warmed as part of the
25+ * Application before an Activity is created, this flag should be set to true because Android will
26+ * only provide the restoration data to the Activity during the onCreate callback .
2727 *
2828 * <p>The current restoration data provided by the framework can be read via {@code
2929 * getRestorationData()}.
@@ -46,20 +46,26 @@ public RestorationChannel(
4646 }
4747
4848 /**
49- * Whether {@code setRestorationData} will be called to provide restoration data for the
50- * framework .
49+ * Whether the channel delays responding to the framework's initial request for restoration data
50+ * until {@code setRestorationData} has been called .
5151 *
52- * <p>When this is set to true, the channel will delay answering any requests for restoration data
53- * by the framework until {@code setRestorationData} has been called. It must be set to false if
54- * the engine never calls {@code setRestorationData}. If it has been set to true, but it later
55- * turns out that there is no restoration data, {@code setRestorationData} must be called with
56- * null.
52+ * <p>If the engine never calls {@code setRestorationData} this flag must be set to false.
53+ * If set to true, the engine must call {@code setRestorationData} either with the actual
54+ * restoration data as argument or null if it turns out that there is no restoration data.
55+ *
56+ * <p>If the response to the framework's request for restoration data is not delayed until the
57+ * data has been set via {@code setRestorationData}, the framework may intermittently initialize
58+ * itself to default values until the restoration data has been made available. Setting this flag
59+ * to true avoids that extra work.
5760 */
5861 public final boolean waitForRestorationData ;
5962
60- private MethodChannel channel ;
63+ // Holds the the most current restoration data which may have been provided by the engine
64+ // via "setRestorationData" or by the framework via the method channel. This is the data the
65+ // framework should be restored to in case the app is terminated.
6166 private byte [] restorationData ;
62- private MethodChannel .Result pendingResult ;
67+ private MethodChannel channel ;
68+ private MethodChannel .Result pendingFrameworkRequest ;
6369 private boolean engineHasProvidedData = false ;
6470 private boolean frameworkHasRequestedData = false ;
6571
@@ -71,11 +77,14 @@ public byte[] getRestorationData() {
7177 /** Set the restoration data from which the framework will restore its state. */
7278 public void setRestorationData (byte [] data ) {
7379 engineHasProvidedData = true ;
74- if (pendingResult != null ) {
75- pendingResult .success (data );
76- pendingResult = null ;
80+ if (pendingFrameworkRequest != null ) {
81+ // If their is a pending request from the framework, answer it.
82+ pendingFrameworkRequest .success (data );
83+ pendingFrameworkRequest = null ;
7784 restorationData = data ;
7885 } else if (frameworkHasRequestedData ) {
86+ // If the framework has previously received the engine's restoration data, push the new data
87+ // directly to it.
7988 channel .invokeMethod (
8089 "push" ,
8190 data ,
@@ -101,6 +110,7 @@ public void notImplemented() {
101110 }
102111 });
103112 } else {
113+ // Otherwise, just cache the data until the framework asks for it.
104114 restorationData = data ;
105115 }
106116 }
@@ -121,7 +131,6 @@ public void clearData() {
121131 public void onMethodCall (@ NonNull MethodCall call , @ NonNull MethodChannel .Result result ) {
122132 final String method = call .method ;
123133 final Object args = call .arguments ;
124- Log .v (TAG , "Received '" + method + "' message." );
125134 switch (method ) {
126135 case "put" :
127136 restorationData = (byte []) args ;
@@ -132,7 +141,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
132141 if (engineHasProvidedData || !waitForRestorationData ) {
133142 result .success (restorationData );
134143 } else {
135- pendingResult = result ;
144+ pendingFrameworkRequest = result ;
136145 }
137146 break ;
138147 default :
0 commit comments