Bug 771915 - Show origin of the page if it differs from the webapps. r=mfinkle
authorWes Johnston <wjohnston@mozilla.com>
Mon, 27 Aug 2012 17:11:44 -0700
changeset 105654 8264b7d01a5abec721e01bdc8b85c52458e0a3b8
parent 105653 38ca4f43a4fd6a3f33bf055895ab1965bb3f528c
child 105655 3f301dd62fb244821a2a04fe9f9280d675ad5cd6
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersmfinkle
bugs771915
milestone18.0a1
Bug 771915 - Show origin of the page if it differs from the webapps. r=mfinkle
mobile/android/base/Makefile.in
mobile/android/base/MarketplaceApp.java.in
mobile/android/base/WebApp.java.in
mobile/android/base/WebAppsFragment.java.frag
mobile/android/base/resources/drawable/webapp_titlebar_bg.xml
mobile/android/base/resources/layout/web_app.xml
mobile/android/base/resources/values/styles.xml
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -1001,16 +1001,17 @@ MOZ_ANDROID_DRAWABLES += \
   mobile/android/base/resources/drawable/tabs_tray_bg_repeat.xml                \
   mobile/android/base/resources/drawable/tabs_tray_selected_bg_repeat.xml       \
   mobile/android/base/resources/drawable/tabs_tray_active_selector.xml          \
   mobile/android/base/resources/drawable/tabs_tray_default_selector.xml         \
   mobile/android/base/resources/drawable/tabs_tray_list_divider.xml             \
   mobile/android/base/resources/drawable/tabs_shadow.xml                        \
   mobile/android/base/resources/drawable/shadow.png                             \
   mobile/android/base/resources/drawable/marketplace.png                        \
+  mobile/android/base/resources/drawable/webapp_titlebar_bg.xml                 \
   $(NULL)
 
 MOZ_ANDROID_DRAWABLES += $(shell if test -e $(topsrcdir)/$(MOZ_BRANDING_DIRECTORY)/android-resources.mn; then cat $(topsrcdir)/$(MOZ_BRANDING_DIRECTORY)/android-resources.mn | tr '\n' ' ';  fi)
 
 RESOURCES=$(RES_LAYOUT) $(RES_LAYOUT_LAND_V14) $(RES_LAYOUT_LARGE_V11) $(RES_LAYOUT_XLARGE_V11) $(RES_VALUES) $(RES_VALUES_LAND) $(RES_VALUES_V11) $(RES_VALUES_LARGE_V11) $(RES_VALUES_XLARGE_V11) $(RES_VALUES_LAND_V14) $(RES_XML) $(RES_ANIM) $(RES_DRAWABLE_NODPI) $(RES_DRAWABLE_BASE) $(RES_DRAWABLE_LDPI) $(RES_DRAWABLE_HDPI) $(RES_DRAWABLE_XHDPI) $(RES_DRAWABLE_MDPI_V11) $(RES_DRAWABLE_HDPI_V11) $(RES_DRAWABLE_XHDPI_V11) $(RES_DRAWABLE_LAND_V14) $(RES_DRAWABLE_LAND_MDPI_V14) $(RES_DRAWABLE_LAND_HDPI_V14) $(RES_DRAWABLE_LAND_XHDPI_V14) $(RES_DRAWABLE_LARGE_MDPI_V11) $(RES_DRAWABLE_LARGE_HDPI_V11) $(RES_DRAWABLE_LARGE_XHDPI_V11) $(RES_DRAWABLE_XLARGE_MDPI_V11) $(RES_DRAWABLE_XLARGE_HDPI_V11) $(RES_DRAWABLE_XLARGE_XHDPI_V11) $(RES_COLOR) $(RES_MENU)
 
 RES_DIRS= \
   res/layout                    \
--- a/mobile/android/base/MarketplaceApp.java.in
+++ b/mobile/android/base/MarketplaceApp.java.in
@@ -20,17 +20,16 @@ import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.WebAppAllocator;
 
 /*
 * This is a stub activity, meant to just install the marketplace WebApp
 * and then launch it
 */
 public class MarketplaceApp extends WebApp {
-    protected int mWebAppIndex;
     private static final String LOGTAG = "GeckoMarketplaceApp";
     public static final String MARKETPLACE_HOST = "marketplace.mozilla.org";
     public static final String MARKETPLACE_URI = "https://marketplace.mozilla.org";
 
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
--- a/mobile/android/base/WebApp.java.in
+++ b/mobile/android/base/WebApp.java.in
@@ -4,41 +4,76 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #filter substitution
 package @ANDROID_PACKAGE_NAME@;
 
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.View;
 import android.view.MenuItem;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+import android.widget.RelativeLayout;
+import android.content.Context;
+
+import java.net.URL;
 
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.GeckoAppShell;
+import org.mozilla.gecko.WebAppAllocator;
+import org.mozilla.gecko.Tab;
+import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.R;
 
 public class WebApp extends GeckoApp {
-    protected int mWebAppIndex;
+    private URL mOrigin;
+    private TextView mTitlebarText = null;
+    private View mTitlebar = null;
     private static final String LOGTAG = "WebApp";
 
+    protected int getIndex() { return 0; }
+
     @Override
     public int getLayout() { return R.layout.web_app; }
 
     @Override
     public boolean hasTabsSideBar() { return false; }
 
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
-        Log.i("DEBUG", "xxx WebApp");
         super.onCreate(savedInstanceState);
 
+        String action = getIntent().getAction();
         Bundle extras = getIntent().getExtras();
         String title = extras != null ? extras.getString(Intent.EXTRA_SHORTCUT_NAME) : null;
         setTitle(title != null ? title : "Web App");
+
+        mTitlebarText = (TextView)findViewById(R.id.webapp_title);
+        mTitlebar = findViewById(R.id.webapp_titlebar);
+        if (!action.startsWith(ACTION_WEBAPP_PREFIX)) {
+            Log.e(LOGTAG, "WebApp launch, but intent action is " + action + "!");
+            return;
+        }
+
+        // Try to use the origin stored in the WebAppAllocator first
+        String origin = WebAppAllocator.getInstance(this).getAppForIndex(getIndex());
+        try {
+            mOrigin = new URL(origin);
+        } catch (java.net.MalformedURLException ex) {
+            // If that failed fall back to the origin stored in the shortcut
+            Log.i(LOGTAG, "Webapp is not registered with allocator");
+            try {
+                mOrigin = new URL(getIntent().getData().toString());
+            } catch (java.net.MalformedURLException ex2) {
+                Log.e(LOGTAG, "Unable to parse intent url: ", ex);
+            }
+        }
     }
 
     public String getPackageName() {
         return "@ANDROID_PACKAGE_NAME@";
     }
 
     public String getContentProcessName() {
         return "@MOZ_CHILD_PROCESS_NAME@";
@@ -83,10 +118,32 @@ public class WebApp extends GeckoApp {
     }
 
 #ifdef MOZ_LINKER_EXTRACT
     @Override
     public boolean linkerExtract() {
         return true;
     }
 #endif
+
+    @Override
+    public void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) {
+        switch(msg) {
+            case LOCATION_CHANGE:
+                if (Tabs.getInstance().isSelectedTab(tab)) {
+                    try {
+                        String title = tab.getURL();
+                        URL page = new URL(title);
+                        mTitlebarText.setText(page.getProtocol() + "://" + page.getHost());
+
+                        if (mOrigin != null && mOrigin.getHost().equals(page.getHost()))
+                            mTitlebar.setVisibility(View.GONE);
+                        else
+                            mTitlebar.setVisibility(View.VISIBLE);
+                    } catch (java.net.MalformedURLException ex) {
+                        Log.e(LOGTAG, "Unable to parse url: ", ex);
+                    }
+                }
+        }
+        super.onTabChanged(tab, msg, data);
+    }
 };
 
--- a/mobile/android/base/WebAppsFragment.java.frag
+++ b/mobile/android/base/WebAppsFragment.java.frag
@@ -1,2 +1,5 @@
-public static class WebApp@APPNUM@ extends WebApp { }
+public static class WebApp@APPNUM@ extends WebApp {
+    @Override
+    protected int getIndex() { return @APPNUM@; }
+}
 
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/drawable/webapp_titlebar_bg.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+   - License, v. 2.0. If a copy of the MPL was not distributed with this
+   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
+    <item>
+        <shape android:shape="rectangle">
+               <gradient android:angle="270"
+                         android:startColor="#E0E6EB"
+                         android:centerColor="#CAD3DD"
+                         android:endColor="#BDC5CE"
+                         android:type="linear"/>
+        </shape>
+    </item>
+</layer-list>
--- a/mobile/android/base/resources/layout/web_app.xml
+++ b/mobile/android/base/resources/layout/web_app.xml
@@ -1,15 +1,31 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/main_layout"
+              android:orientation="vertical"
               style="@style/Screen">
 
+    <LinearLayout android:id="@+id/webapp_titlebar"
+                  android:visibility="gone"
+                  style="@style/WebView.Titlebar"
+                  android:layout_width="fill_parent"
+                  android:layout_height="wrap_content"
+                  android:orientation="horizontal">
+    
+        <TextView android:id="@+id/webapp_title"
+                  style="@style/WebView.Titlebar.Title"
+                  android:layout_width="fill_parent"
+                  android:layout_height="wrap_content"
+                  android:singleLine="true"/>
+
+    </LinearLayout>
+
     <RelativeLayout android:id="@+id/gecko_layout"
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     android:layout_weight="1">
 
         <include layout="@layout/shared_ui_components"/>
 
-   </RelativeLayout>
+    </RelativeLayout>
 
 </LinearLayout>
--- a/mobile/android/base/resources/values/styles.xml
+++ b/mobile/android/base/resources/values/styles.xml
@@ -149,9 +149,20 @@
         <item name="android:layout_height">wrap_content</item>
         <item name="android:layout_marginLeft">5dip</item>
         <item name="android:layout_marginRight">5dip</item>
         <item name="android:scaleType">fitCenter</item>
         <item name="android:layout_centerVertical">true</item>
         <item name="android:background">@android:color/transparent</item>
     </style>
 
+    <style name="WebView">
+    </style>
+
+    <style name="WebView.Titlebar">
+        <item name="android:background">@drawable/webapp_titlebar_bg</item>
+        <item name="android:padding">2dp</item>
+    </style>
+
+    <style name="WebView.Titlebar.Title">
+        <item name="android:textColor">#ff222222</item>
+    </style>
 </resources>