Don't show the error page

Make sure that we don't show the error page. This is done by

1) having a boolean state tracking the success/fail of the page load
   attempt, and

2) using postVisualStateCallback so that we don't show the webview
   before we are told that DOM is ready to be rendered. (otherwise,
there's a risk that we are displaying previous DOM which is for the
error page).

Bug: 375514172
Test: run the Terminal app several times and check if the transient
error page is not shown.

Change-Id: Ia7e387281b8b3aef19d4d9425c45b17354c0bc97
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java b/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java
index 0a750e3..e0773f6 100644
--- a/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java
@@ -24,6 +24,7 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.res.Configuration;
+import android.graphics.Bitmap;
 import android.graphics.drawable.Icon;
 import android.graphics.fonts.FontStyle;
 import android.net.http.SslError;
@@ -176,6 +177,9 @@
         Log.i(TAG, "URL=" + getTerminalServiceUrl().toString());
         mWebView.setWebViewClient(
                 new WebViewClient() {
+                    private boolean mLoadFailed = false;
+                    private long mRequestId = 0;
+
                     @Override
                     public boolean shouldOverrideUrlLoading(
                             WebView view, WebResourceRequest request) {
@@ -183,8 +187,14 @@
                     }
 
                     @Override
+                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
+                        mLoadFailed = false;
+                    }
+
+                    @Override
                     public void onReceivedError(
                             WebView view, WebResourceRequest request, WebResourceError error) {
+                        mLoadFailed = true;
                         switch (error.getErrorCode()) {
                             case WebViewClient.ERROR_CONNECT:
                             case WebViewClient.ERROR_HOST_LOOKUP:
@@ -199,17 +209,22 @@
 
                     @Override
                     public void onPageFinished(WebView view, String url) {
-                        URL loadedUrl = null;
-                        try {
-                            loadedUrl = new URL(url);
-                        } catch (MalformedURLException e) {
-                            // cannot happen.
+                        if (mLoadFailed) {
+                            return;
                         }
-                        Log.i(TAG, "on page finished. URL=" + loadedUrl);
-                        if (getTerminalServiceUrl().toString().equals(url)) {
-                            android.os.Trace.endAsyncSection("executeTerminal", 0);
-                            view.setVisibility(View.VISIBLE);
-                        }
+
+                        mRequestId++;
+                        view.postVisualStateCallback(
+                                mRequestId,
+                                new WebView.VisualStateCallback() {
+                                    @Override
+                                    public void onComplete(long requestId) {
+                                        if (requestId == mRequestId) {
+                                            android.os.Trace.endAsyncSection("executeTerminal", 0);
+                                            view.setVisibility(View.VISIBLE);
+                                        }
+                                    }
+                                });
                     }
 
                     @Override