Web runtime
 - link following with simple SOP check
 - back button
 - fullscreen (title shown as toast for now)

Change-Id: I6c64dd87b2f216f02a1d0ee4e7e5d51067ca80ee
diff --git a/core/java/android/webruntime/WebRuntimeActivity.java b/core/java/android/webruntime/WebRuntimeActivity.java
index 2d3b62d..f4447fc 100644
--- a/core/java/android/webruntime/WebRuntimeActivity.java
+++ b/core/java/android/webruntime/WebRuntimeActivity.java
@@ -25,8 +25,17 @@
 import android.os.Bundle;
 import android.util.Log;
 import android.webkit.WebView;
+import android.webkit.WebViewClient;
+import android.widget.Toast;
+import android.view.Menu;
+import android.view.KeyEvent;
+import android.view.Window;
+
 import com.android.internal.R;
 
+import java.net.MalformedURLException;
+import java.net.URL;
+
 /**
  * The runtime used to display installed web applications.
  * @hide
@@ -35,7 +44,8 @@
 {
     private final static String LOGTAG = "WebRuntimeActivity";
 
-    WebView webView;
+    private WebView mWebView;
+    private URL mBaseUrl;
 
     /** Called when the activity is first created. */
     @Override
@@ -70,13 +80,62 @@
             return;
         }
 
+        try {
+            mBaseUrl = new URL(url);
+        } catch (MalformedURLException e) {
+            Log.d(LOGTAG, "Invalid URL");
+        }
+
+        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.web_runtime);
-        webView = (WebView) findViewById(R.id.webview);
-        webView.loadUrl(url);
+        mWebView = (WebView) findViewById(R.id.webview);
+        mWebView.getSettings().setJavaScriptEnabled(true);
+        mWebView.setWebViewClient(new WebViewClient() {
+            @Override
+            public boolean shouldOverrideUrlLoading(WebView view, String url) {
+                try {
+                    URL newOrigin = new URL(url);
+                    if (newOrigin.getHost().equals(mBaseUrl.getHost())) {
+                        // If simple same origin test passes, load in the webview.
+                        // FIXME: We should do a more robust SOP check.
+                        return false;
+                    }
+                } catch(MalformedURLException e) {
+                    // Don't load anything if this wasn't a proper URL.
+                    return true;
+                }
+
+                // Otherwise this is a URL that is not same origin so pass it to the
+                // Browser to load.
+                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
+                return true;
+            }
+        });
 
         String title = metaData.getString("android.webruntime.title");
+        // We turned off the title bar to go full screen so display the
+        // webapp's title as a toast.
         if (title != null) {
-            setTitle(title);
+            Toast.makeText(this, title, Toast.LENGTH_SHORT).show();
         }
+
+        // Load the webapp's base URL.
+        mWebView.loadUrl(url);
+    }
+
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event) {
+        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
+            mWebView.goBack();
+            return true;
+        }
+        return super.onKeyDown(keyCode, event);
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        menu.add(0, 0, 0, "Menu item 1");
+        menu.add(0, 1, 0, "Menu item 2");
+        return true;
     }
 }