Fix native_viewport_service to always create viewports on the current
activity.

This allows several native viewport clients to be launched and have each
one draw on a separate surface, on different tasks. This is done by
storing the current activity context and making it globally available.

Note that apps embedded in a window manager still cannot be displayed on
separate Android tasks.

Tracking feature bug: #292

R=qsr@chromium.org

Review URL: https://codereview.chromium.org/1221253003.
diff --git a/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java b/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java
index 96a3170..a833bfb 100644
--- a/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java
+++ b/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java
@@ -5,7 +5,6 @@
 package org.chromium.mojo.authentication;
 
 import android.accounts.AccountManager;
-import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Parcel;
@@ -79,14 +78,14 @@
         }
     }
 
-    private final Activity mContext;
+    private final Context mContext;
     private final String mConsumerURL;
     private final IntentReceiverManager mIntentReceiverManager;
     private Db mDb = null;
     private File mDbFile = null;
 
     public AuthenticationServiceImpl(Context context, Core core, String consumerURL, Shell shell) {
-        mContext = (Activity) context;
+        mContext = context;
         mConsumerURL = consumerURL;
         mIntentReceiverManager = ShellHelper.connectToService(
                 core, shell, "mojo:intent_receiver", IntentReceiverManager.MANAGER);
diff --git a/services/native_viewport/android/src/org/chromium/mojo/PlatformViewportAndroid.java b/services/native_viewport/android/src/org/chromium/mojo/PlatformViewportAndroid.java
index 0175ff0..fabcb08 100644
--- a/services/native_viewport/android/src/org/chromium/mojo/PlatformViewportAndroid.java
+++ b/services/native_viewport/android/src/org/chromium/mojo/PlatformViewportAndroid.java
@@ -14,6 +14,7 @@
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputConnection;
 
+import org.chromium.base.ApplicationStatus;
 import org.chromium.base.CalledByNative;
 import org.chromium.base.JNINamespace;
 import org.chromium.mojo.keyboard.KeyboardServiceImpl;
@@ -28,8 +29,8 @@
     private final SurfaceHolder.Callback mSurfaceCallback;
 
     @CalledByNative
-    public static PlatformViewportAndroid createForActivity(
-            Activity activity, long nativeViewport) {
+    public static PlatformViewportAndroid create(long nativeViewport) {
+        Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
         PlatformViewportAndroid rv = new PlatformViewportAndroid(activity, nativeViewport);
         KeyboardServiceImpl.setActiveView(rv);
         activity.setContentView(rv);
diff --git a/services/native_viewport/platform_viewport_android.cc b/services/native_viewport/platform_viewport_android.cc
index 352ec69..a19d5c0 100644
--- a/services/native_viewport/platform_viewport_android.cc
+++ b/services/native_viewport/platform_viewport_android.cc
@@ -149,9 +149,9 @@
 void PlatformViewportAndroid::Init(const gfx::Rect& bounds) {
   JNIEnv* env = base::android::AttachCurrentThread();
   java_platform_viewport_android_ = JavaObjectWeakGlobalRef(
-      env, Java_PlatformViewportAndroid_createForActivity(
-               env, base::android::GetApplicationContext(),
-               reinterpret_cast<jlong>(this)).obj());
+      env,
+      Java_PlatformViewportAndroid_create(env, reinterpret_cast<jlong>(this))
+          .obj());
 }
 
 void PlatformViewportAndroid::Show() {
diff --git a/shell/android/apk/AndroidManifest.xml.jinja2 b/shell/android/apk/AndroidManifest.xml.jinja2
index 36e42dc..2ba3e72 100644
--- a/shell/android/apk/AndroidManifest.xml.jinja2
+++ b/shell/android/apk/AndroidManifest.xml.jinja2
@@ -24,7 +24,8 @@
         <meta-data android:name="com.google.android.gms.version"
                    android:value="@integer/google_play_services_version" />
         <activity android:name="org.chromium.mojo.shell.MojoShellActivity"
-                  android:launchMode="singleTask"
+                  android:launchMode="standard"
+                  android:documentLaunchMode="intoExisting"
                   android:theme="@android:style/Theme.Holo.Light.NoActionBar"
                   android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
                   android:windowSoftInputMode="adjustPan"
diff --git a/shell/android/apk/src/org/chromium/mojo/shell/MojoShellActivity.java b/shell/android/apk/src/org/chromium/mojo/shell/MojoShellActivity.java
index 707c976..bad2a3b 100644
--- a/shell/android/apk/src/org/chromium/mojo/shell/MojoShellActivity.java
+++ b/shell/android/apk/src/org/chromium/mojo/shell/MojoShellActivity.java
@@ -33,13 +33,8 @@
         // has no obligation to kill the application process between destroying and restarting the
         // activity. If the application process is kept alive, initialization parameters sent with
         // the intent will be stale.
-        // TODO(qsr): We should be passing application context here as required by
-        // InitApplicationContext on the native side. Currently we can't, as PlatformViewportAndroid
-        // relies on this being the activity context.
-        ShellMain.ensureInitialized(this, getParametersFromIntent(getIntent()));
-
         // TODO(eseidel): ShellMain can fail, but we're ignoring the return.
-        ShellMain.start();
+        ShellMain.ensureInitialized(getApplicationContext(), getParametersFromIntent(getIntent()));
 
         onNewIntent(getIntent());
 
diff --git a/shell/android/apk/src/org/chromium/mojo/shell/ShellMain.java b/shell/android/apk/src/org/chromium/mojo/shell/ShellMain.java
index a752581..f633759 100644
--- a/shell/android/apk/src/org/chromium/mojo/shell/ShellMain.java
+++ b/shell/android/apk/src/org/chromium/mojo/shell/ShellMain.java
@@ -135,6 +135,7 @@
             Log.e(TAG, "ShellMain initialization failed.", e);
             throw new RuntimeException(e);
         }
+        start();
     }
 
     /**