Close the shell when all activities are destroyed.

Fixes https://github.com/domokit/modular/issues/391

R=qsr@chromium.org, qsr

Review URL: https://codereview.chromium.org/1515543002 .
diff --git a/shell/android/apk/src/org/chromium/mojo/shell/ShellService.java b/shell/android/apk/src/org/chromium/mojo/shell/ShellService.java
index 2f489fc..c669068 100644
--- a/shell/android/apk/src/org/chromium/mojo/shell/ShellService.java
+++ b/shell/android/apk/src/org/chromium/mojo/shell/ShellService.java
@@ -154,6 +154,13 @@
         }
         String viewportId = rootIntent.getStringExtra("ViewportId");
         NativeViewportSupportApplicationDelegate.viewportClosed(viewportId);
+
+        if (ApplicationStatus.getRunningActivities().size() == 0) {
+            // There are only background apps in the shell, so we close ourselves so we can cleanly
+            // restart. We may want to investigate how to keep backround apps and restart UI
+            // activities cleanly.
+            nativeQuitShell();
+        }
     }
 
     /**
@@ -321,4 +328,6 @@
     private static native void nativeStartApplicationURL(String url);
 
     private static native void nativeBindShell(int shellHandle);
+
+    private static native void nativeQuitShell();
 }
diff --git a/shell/android/main.cc b/shell/android/main.cc
index f940684..eb31de5 100644
--- a/shell/android/main.cc
+++ b/shell/android/main.cc
@@ -341,6 +341,11 @@
                                      mojo::MessagePipeHandle(shell_handle)))));
 }
 
+static void QuitShell(JNIEnv* env, jclass jcaller) {
+  g_internal_data.Get().shell_task_runner->PostTask(
+      FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
+}
+
 bool RegisterShellService(JNIEnv* env) {
   return RegisterNativesImpl(env);
 }