Measure image size and show it in the description

Bug: N/A
Test: N/A
Change-Id: I4923bf0cebc5bd12983e1add0e8f66d4b59b1780
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/ImageArchive.java b/android/TerminalApp/java/com/android/virtualization/terminal/ImageArchive.java
index 12f485a..2f8c533 100644
--- a/android/TerminalApp/java/com/android/virtualization/terminal/ImageArchive.java
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/ImageArchive.java
@@ -27,6 +27,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.file.Files;
@@ -77,6 +78,51 @@
         }
     }
 
+    /**
+     * Creates ImageArchive from either SdCard or Internet. SdCard is used only when the build is
+     * debuggable and the file actually exists.
+     */
+    public static ImageArchive getDefault() {
+        ImageArchive archive = fromSdCard();
+        if (Build.isDebuggable() && archive.exists()) {
+            return archive;
+        } else {
+            return fromInternet();
+        }
+    }
+
+    /** Tests if ImageArchive exists on the medium. */
+    public boolean exists() {
+        if (mPath != null) {
+            return Files.exists(mPath);
+        } else {
+            // TODO
+            return true;
+        }
+    }
+
+    /** Returns size of the archive in bytes */
+    public long getSize() throws IOException {
+        if (!exists()) {
+            throw new IllegalStateException("Cannot get size of non existing archive");
+        }
+        if (mPath != null) {
+            return Files.size(mPath);
+        } else {
+            HttpURLConnection conn = null;
+            try {
+                conn = (HttpURLConnection) mUrl.openConnection();
+                conn.setRequestMethod("HEAD");
+                conn.getInputStream();
+                return conn.getContentLength();
+            } finally {
+                if (conn != null) {
+                    conn.disconnect();
+                }
+            }
+        }
+    }
+
     private InputStream getInputStream(Function<InputStream, InputStream> filter)
             throws IOException {
         InputStream is = mPath != null ? new FileInputStream(mPath.toFile()) : mUrl.openStream();
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/InstallerActivity.java b/android/TerminalApp/java/com/android/virtualization/terminal/InstallerActivity.java
index 52ef3d4..69b5ee7 100644
--- a/android/TerminalApp/java/com/android/virtualization/terminal/InstallerActivity.java
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/InstallerActivity.java
@@ -41,6 +41,7 @@
 import com.google.android.material.progressindicator.LinearProgressIndicator;
 import com.google.android.material.snackbar.Snackbar;
 
+import java.io.IOException;
 import java.lang.ref.WeakReference;
 
 public class InstallerActivity extends BaseActivity {
@@ -63,12 +64,8 @@
         mInstallProgressListener = new InstallProgressListener(this);
 
         setContentView(R.layout.activity_installer);
-
-        TextView desc = (TextView) findViewById(R.id.installer_desc);
-        desc.setText(
-                getString(
-                        R.string.installer_desc_text_format,
-                        Formatter.formatShortFileSize(this, ESTIMATED_IMG_SIZE_BYTES)));
+        updateSizeEstimation(ESTIMATED_IMG_SIZE_BYTES);
+        measureImageSizeAndUpdateDescription();
 
         mWaitForWifiCheckbox = (CheckBox) findViewById(R.id.installer_wait_for_wifi_checkbox);
         mInstallButton = (TextView) findViewById(R.id.installer_install_button);
@@ -85,6 +82,33 @@
         }
     }
 
+    private void updateSizeEstimation(long est) {
+        String desc =
+                getString(
+                        R.string.installer_desc_text_format,
+                        Formatter.formatShortFileSize(this, est));
+        runOnUiThread(
+                () -> {
+                    TextView view = (TextView) findViewById(R.id.installer_desc);
+                    view.setText(desc);
+                });
+    }
+
+    private void measureImageSizeAndUpdateDescription() {
+        new Thread(
+                        () -> {
+                            long est;
+                            try {
+                                est = ImageArchive.getDefault().getSize();
+                            } catch (IOException e) {
+                                Log.w(TAG, "Failed to measure image size.", e);
+                                return;
+                            }
+                            updateSizeEstimation(est);
+                        })
+                .start();
+    }
+
     @Override
     public void onResume() {
         super.onResume();