Merge changes from topic "vt_enable" into main

* changes:
  Implement disableVmTethering
  Deprecate old implementation around providing network to ferrochrome
  Turn network feature on in VmLauncherApp
  Implement enableVmTethering
diff --git a/ferrochrome_app/Android.bp b/ferrochrome_app/Android.bp
new file mode 100644
index 0000000..182d289
--- /dev/null
+++ b/ferrochrome_app/Android.bp
@@ -0,0 +1,23 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_app {
+    name: "FerrochromeApp",
+    srcs: ["java/**/*.java"],
+    resource_dirs: ["res"],
+    platform_apis: true,
+    // TODO(b/348113995): move this app to product partition
+    system_ext_specific: true,
+    privileged: true,
+    init_rc: ["custom_vm_setup.rc"],
+    required: ["custom_vm_setup"],
+    certificate: "platform",
+}
+
+sh_binary {
+    name: "custom_vm_setup",
+    src: "custom_vm_setup.sh",
+    system_ext_specific: true,
+    host_supported: false,
+}
diff --git a/ferrochrome_app/AndroidManifest.xml b/ferrochrome_app/AndroidManifest.xml
new file mode 100644
index 0000000..d783bbc
--- /dev/null
+++ b/ferrochrome_app/AndroidManifest.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.virtualization.ferrochrome"
+    android:sharedUserId="android.uid.system" >
+
+    <uses-permission android:name="android.permission.INTERNET" />
+    <application
+        android:label="Ferrochrome">
+        <activity android:name=".FerrochromeActivity"
+                  android:configChanges="orientation|screenSize|keyboard|keyboardHidden|navigation|uiMode"
+                  android:theme="@style/MyTheme"
+                  android:screenOrientation="landscape"
+                  android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>
diff --git a/ferrochrome_app/custom_vm_setup.rc b/ferrochrome_app/custom_vm_setup.rc
new file mode 100644
index 0000000..f4244b7
--- /dev/null
+++ b/ferrochrome_app/custom_vm_setup.rc
@@ -0,0 +1,23 @@
+# Copyright (C) 2024 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+service custom_vm_setup /system_ext/bin/custom_vm_setup
+    user shell
+    group shell
+    disabled
+    oneshot
+    seclabel u:r:shell:s0
+
+on property:debug.custom_vm_setup.start=true
+    start custom_vm_setup
\ No newline at end of file
diff --git a/ferrochrome_app/custom_vm_setup.sh b/ferrochrome_app/custom_vm_setup.sh
new file mode 100644
index 0000000..d57ed7f
--- /dev/null
+++ b/ferrochrome_app/custom_vm_setup.sh
@@ -0,0 +1,15 @@
+#!/system/bin/sh
+
+function copy_files() {
+  cp -u /sdcard/vm_config.json /data/local/tmp
+  cp -u /sdcard/vmlinuz /data/local/tmp
+  cp -u /sdcard/chromiumos_test_image.bin /data/local/tmp
+  chmod 666 /data/local/tmp/vm_config.json
+  chmod 666 /data/local/tmp/chromiumos_test_image.bin
+  chmod 666 /data/local/tmp/vmlinuz
+}
+setprop debug.custom_vm_setup.done false
+copy_files
+pm grant com.google.android.virtualization.vmlauncher android.permission.USE_CUSTOM_VIRTUAL_MACHINE
+setprop debug.custom_vm_setup.start false
+setprop debug.custom_vm_setup.done true
\ No newline at end of file
diff --git a/ferrochrome_app/java/com/android/virtualization/ferrochrome/FerrochromeActivity.java b/ferrochrome_app/java/com/android/virtualization/ferrochrome/FerrochromeActivity.java
new file mode 100644
index 0000000..3629c85
--- /dev/null
+++ b/ferrochrome_app/java/com/android/virtualization/ferrochrome/FerrochromeActivity.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.virtualization.ferrochrome;
+
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
+import android.os.Environment;
+import android.os.SystemProperties;
+import android.util.Log;
+import android.view.WindowManager;
+import android.widget.TextView;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class FerrochromeActivity extends Activity {
+    ExecutorService executorService = Executors.newSingleThreadExecutor();
+    private static final String TAG = "FerrochromeActivity";
+    private static final String FERROCHROME_VERSION = "R127-15916.0.0";
+    private static final String EXTERNAL_STORAGE_DIR =
+            Environment.getExternalStorageDirectory().getPath() + File.separator;
+    private static final Path IMAGE_PATH =
+            Path.of(EXTERNAL_STORAGE_DIR + "chromiumos_test_image.bin");
+    private static final Path KERNEL_PATH = Path.of(EXTERNAL_STORAGE_DIR + "vmlinuz.bin");
+    private static final Path IMAGE_VERSION_INFO =
+            Path.of(EXTERNAL_STORAGE_DIR + "ferrochrome_image_version");
+    private static final Path VM_CONFIG_PATH = Path.of(EXTERNAL_STORAGE_DIR + "vm_config.json");
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_ferrochrome);
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+
+        // Clean up the existing vm launcher process if there is
+        ActivityManager am = getSystemService(ActivityManager.class);
+        am.killBackgroundProcesses(getVmLauncherAppPackageName());
+
+        executorService.execute(
+                () -> {
+                    if (Files.notExists(IMAGE_PATH)
+                            || Files.notExists(KERNEL_PATH)
+                            || !FERROCHROME_VERSION.equals(getVersionInfo())) {
+
+                        updateStatus("image doesn't exist");
+                        updateStatus("download image");
+                        if (download(FERROCHROME_VERSION)) {
+                            updateStatus("download done");
+                        } else {
+                            updateStatus("download failed, check internet connection and retry");
+                            return;
+                        }
+                    } else {
+                        updateStatus("there are already downloaded images");
+                    }
+                    updateStatus("write down vm config");
+                    copyVmConfigJson();
+                    updateStatus("custom_vm_setup: copy files to /data/local/tmp");
+                    SystemProperties.set("debug.custom_vm_setup.start", "true");
+                    while (!SystemProperties.getBoolean("debug.custom_vm_setup.done", false)) {
+                        // Wait for custom_vm_setup
+                        try {
+                            Thread.sleep(1000);
+                        } catch (Exception e) {
+                            Log.d(TAG, e.toString());
+                        }
+                        updateStatus("wait for custom_vm_setup");
+                    }
+                    updateStatus("enable vmlauncher");
+                    updateStatus("ready for ferrochrome");
+                    runOnUiThread(
+                            () ->
+                                    startActivity(
+                                            new Intent()
+                                                    .setClassName(
+                                                            getVmLauncherAppPackageName(),
+                                                            "com.android.virtualization.vmlauncher.MainActivity")));
+                });
+    }
+
+    private String getVmLauncherAppPackageName() {
+        PackageManager pm = getPackageManager();
+        for (String packageName :
+                new String[] {
+                    "com.android.virtualization.vmlauncher",
+                    "com.google.android.virtualization.vmlauncher"
+                }) {
+            try {
+                pm.getPackageInfo(packageName, 0);
+                return packageName;
+            } catch (PackageManager.NameNotFoundException e) {
+                continue;
+            }
+        }
+        return null;
+    }
+
+    private void updateStatus(String line) {
+        Log.d(TAG, line);
+        runOnUiThread(
+                () -> {
+                    TextView statusView = findViewById(R.id.status_txt_view);
+                    statusView.append(line + "\n");
+                });
+    }
+
+    private void copyVmConfigJson() {
+        if (Files.exists(VM_CONFIG_PATH)) {
+            return;
+        }
+        try (InputStream is = getResources().openRawResource(R.raw.vm_config)) {
+            Files.copy(is, VM_CONFIG_PATH, StandardCopyOption.REPLACE_EXISTING);
+        } catch (IOException e) {
+            updateStatus(e.toString());
+        }
+    }
+
+    private String getVersionInfo() {
+        try {
+            return new String(Files.readAllBytes(IMAGE_VERSION_INFO), StandardCharsets.UTF_8);
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
+    private boolean updateVersionInfo(String version) {
+        try {
+            Files.write(IMAGE_VERSION_INFO, version.getBytes(StandardCharsets.UTF_8));
+        } catch (IOException e) {
+            Log.d(TAG, e.toString());
+        }
+        return true;
+    }
+
+    private boolean download(String version) {
+        String urlString =
+                "https://storage.googleapis.com/chromiumos-image-archive/ferrochrome-public/"
+                        + version
+                        + "/image.zip";
+        boolean hasKernel = false;
+        boolean hasImage = false;
+        try (InputStream is = (new URL(urlString)).openStream();
+                ZipInputStream zis = new ZipInputStream(is)) {
+            ZipEntry entry;
+            while ((entry = zis.getNextEntry()) != null) {
+                Path dest;
+                if (entry.getName().contains("chromiumos_test_image.bin")) {
+                    dest = IMAGE_PATH;
+                    hasImage = true;
+                } else if (entry.getName().contains("boot_images/vmlinuz-")) {
+                    dest = KERNEL_PATH;
+                    hasKernel = true;
+                } else {
+                    continue;
+                }
+                updateStatus("copy " + entry.getName() + " start");
+                Files.copy(zis, dest, StandardCopyOption.REPLACE_EXISTING);
+                updateStatus("copy " + entry.getName() + " done");
+                if (hasImage && hasKernel) {
+                    updateVersionInfo(version);
+                    break;
+                }
+            }
+        } catch (Exception e) {
+            updateStatus(e.toString());
+            return false;
+        }
+        return true;
+    }
+}
diff --git a/ferrochrome_app/res/layout/activity_ferrochrome.xml b/ferrochrome_app/res/layout/activity_ferrochrome.xml
new file mode 100644
index 0000000..7d5e8aa
--- /dev/null
+++ b/ferrochrome_app/res/layout/activity_ferrochrome.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".FerrochromeActivity">
+  <TextView
+      android:layout_width="match_parent"
+      android:layout_height="match_parent"
+      android:id="@+id/status_txt_view"/>
+
+</RelativeLayout>
\ No newline at end of file
diff --git a/ferrochrome_app/res/raw/vm_config.json b/ferrochrome_app/res/raw/vm_config.json
new file mode 100644
index 0000000..91071bb
--- /dev/null
+++ b/ferrochrome_app/res/raw/vm_config.json
@@ -0,0 +1,21 @@
+{
+    "name": "cros",
+    "kernel": "/data/local/tmp/vmlinuz",
+    "disks": [
+        {
+            "image": "/data/local/tmp/chromiumos_test_image.bin",
+            "partitions": [],
+            "writable": true
+        }
+    ],
+    "params": "root=/dev/vda3 rootwait noinitrd ro enforcing=0 cros_debug cros_secure",
+    "protected": false,
+    "cpu_topology": "match_host",
+    "platform_version": "~1.0",
+    "memory_mib": 8096,
+    "gpu": {
+        "backend": "virglrenderer",
+        "context_types": ["virgl2"]
+    },
+    "console_input_device": "ttyS0"
+}
\ No newline at end of file
diff --git a/ferrochrome_app/res/values/themes.xml b/ferrochrome_app/res/values/themes.xml
new file mode 100644
index 0000000..c9a9ed2
--- /dev/null
+++ b/ferrochrome_app/res/values/themes.xml
@@ -0,0 +1,4 @@
+<resources xmlns:tools="http://schemas.android.com/tools">
+    <style name="MyTheme" parent="@android:style/Theme.DeviceDefault.NoActionBar">
+    </style>
+</resources>
diff --git a/vmlauncher_app/AndroidManifest.xml b/vmlauncher_app/AndroidManifest.xml
index f4f13af..ecfef86 100644
--- a/vmlauncher_app/AndroidManifest.xml
+++ b/vmlauncher_app/AndroidManifest.xml
@@ -9,16 +9,20 @@
     <application
         android:label="VmLauncherApp">
         <activity android:name=".MainActivity"
-                  android:enabled="false"
                   android:screenOrientation="landscape"
                   android:configChanges="orientation|screenSize|keyboard|keyboardHidden|navigation|uiMode"
                   android:theme="@style/MyTheme"
                   android:exported="true">
+        </activity>
+        <activity-alias android:name=".MainActivityAlias"
+                android:targetActivity=".MainActivity"
+                android:exported="true"
+                android:enabled="false">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
-        </activity>
+        </activity-alias>
     </application>
 
 </manifest>