WIP
authorMichael Comella <michael.l.comella@gmail.com>
Fri, 22 May 2015 11:32:09 -0700
changeset 266272 bcd15b22f748ee862279a52a7f91260ba3795d6d
parent 266271 02d343f01fa55053e21e36191c279f19ebbdb486
child 551100 a598ff230c880b9455015c0903901a325b33bd77
push id2231
push usermichael.l.comella@gmail.com
push dateFri, 22 May 2015 20:04:59 +0000
milestone41.0a1
WIP
mobile/android/base/resources/layout/search_engine_bar_item.xml
mobile/android/base/resources/layout/search_engine_row.xml
mobile/android/base/resources/values/attrs.xml
mobile/android/base/widget/FaviconView.java
--- a/mobile/android/base/resources/layout/search_engine_bar_item.xml
+++ b/mobile/android/base/resources/layout/search_engine_bar_item.xml
@@ -6,22 +6,25 @@
 <!-- TwoWayView doesn't let us set the margin around items (except as
      gecko:itemMargin, but that doesn't increase the hit area) so we
      have to surround the main View by a ViewGroup to create a pressable margin.
 
      Note: the layout_height values are shared with the parent
      View (browser_search at the time of this writing). -->
 <FrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:gecko="http://schemas.android.com/apk/res-auto"
     android:id="@+id/search_engine_icon_container"
     android:layout_height="match_parent"
     android:layout_width="72dp"
     android:background="@color/pressed_about_page_header_grey">
 
     <!-- Width & height are set to make the Favicons as sharp as possible
          based on asset size. -->
     <org.mozilla.gecko.widget.FaviconView
         android:id="@+id/search_engine_icon"
         android:layout_width="16dp"
         android:layout_height="16dp"
-        android:layout_gravity="center"/>
+        android:layout_gravity="center"
+        gecko:defaultFaviconDrawable="@drawable/search_icon_default_favicon"
+        gecko:defaultFaviconKey="DefaultFaviconSearch"/>
 
 </FrameLayout>
--- a/mobile/android/base/resources/layout/search_engine_row.xml
+++ b/mobile/android/base/resources/layout/search_engine_row.xml
@@ -1,23 +1,26 @@
 <?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/. -->
 
-<merge xmlns:android="http://schemas.android.com/apk/res/android">
+<merge xmlns:android="http://schemas.android.com/apk/res/android"
+       xmlns:gecko="http://schemas.android.com/apk/res-auto">
 
     <org.mozilla.gecko.widget.FaviconView android:id="@+id/suggestion_icon"
                                           android:layout_width="@dimen/favicon_bg"
                                           android:layout_height="@dimen/favicon_bg"
                                           android:layout_marginLeft="10dip"
                                           android:layout_marginRight="10dip"
                                           android:layout_centerVertical="true"
                                           android:minWidth="@dimen/favicon_bg"
-                                          android:minHeight="@dimen/favicon_bg"/>
+                                          android:minHeight="@dimen/favicon_bg"
+                                          gecko:defaultFaviconDrawable="@drawable/search_icon_default_favicon"
+                                          gecko:defaultFaviconKey="DefaultFaviconSearch"/>
 
     <org.mozilla.gecko.widget.FlowLayout android:id="@+id/suggestion_layout"
                                          android:layout_toRightOf="@id/suggestion_icon"
                                          android:layout_centerVertical="true"
                                          android:layout_width="wrap_content"
                                          android:layout_height="wrap_content"
                                          android:layout_marginRight="10dip"
                                          android:duplicateParentState="true">
--- a/mobile/android/base/resources/values/attrs.xml
+++ b/mobile/android/base/resources/values/attrs.xml
@@ -182,16 +182,18 @@
 
     <declare-styleable name="FloatingHintEditText">
         <attr name="floatingHintEditTextStyle" format="reference" />
     </declare-styleable>
 
     <declare-styleable name="FaviconView">
         <attr name="dominantBorderEnabled" format="boolean" />
         <attr name="overrideScaleType" format="boolean" />
+        <attr name="defaultFaviconDrawable" format="reference" />
+        <attr name="defaultFaviconKey" format="string" />
     </declare-styleable>
 
     <declare-styleable name="OverlayDialogButton">
         <attr name="drawable" format="reference" />
         <attr name="enabledText" format="string" />
         <attr name="disabledText" format="string" />
     </declare-styleable>
 
--- a/mobile/android/base/widget/FaviconView.java
+++ b/mobile/android/base/widget/FaviconView.java
@@ -18,17 +18,21 @@ import android.graphics.RectF;
 import android.util.AttributeSet;
 import android.widget.ImageView;
 /**
  * Special version of ImageView for favicons.
  * Displays solid colour background around Favicon to fill space not occupied by the icon. Colour
  * selected is the dominant colour of the provided Favicon.
  */
 public class FaviconView extends ImageView {
-    private static String DEFAULT_FAVICON_KEY = FaviconView.class.getSimpleName() + "DefaultFavicon";
+    private static String DEFAULT_FAVICON_KEY = "DefaultFavicon";
+    private static int DEFAULT_FAVICON_RES_ID = R.drawable.favicon_globe;
+
+    private int defaultFaviconDrawableId;
+    private String defaultFaviconKey;
 
     private Bitmap mIconBitmap;
 
     // Reference to the unscaled bitmap, if any, to prevent repeated assignments of the same bitmap
     // to the view from causing repeated rescalings (Some of the callers do this)
     private Bitmap mUnscaledBitmap;
 
     // Key into the Favicon dominant colour cache. Should be the Favicon URL if the image displayed
@@ -77,36 +81,61 @@ public class FaviconView extends ImageVi
 
     public FaviconView(Context context, AttributeSet attrs) {
         super(context, attrs);
         TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FaviconView, 0, 0);
 
         try {
             isDominantBorderEnabled = a.getBoolean(R.styleable.FaviconView_dominantBorderEnabled, true);
             isOverrideScaleTypeEnabled = a.getBoolean(R.styleable.FaviconView_overrideScaleType, true);
+
+            defaultFaviconDrawableId = a.getResourceId(R.styleable.FaviconView_defaultFaviconDrawable, -1);
+            defaultFaviconKey = a.getString(R.styleable.FaviconView_defaultFaviconKey);
         } finally {
             a.recycle();
         }
 
+        validateAndAdjustDefaultFavicon();
+
         if (isOverrideScaleTypeEnabled) {
             setScaleType(ImageView.ScaleType.CENTER);
         }
 
         mStrokeRect = new RectF();
         mBackgroundRect = new RectF();
 
         if (sStrokeWidth == 0) {
             sStrokeWidth = getResources().getDisplayMetrics().density;
             sStrokePaint.setStrokeWidth(sStrokeWidth);
         }
 
         mStrokeRect.left = mStrokeRect.top = sStrokeWidth;
         mBackgroundRect.left = mBackgroundRect.top = sStrokeWidth * 2.0f;
     }
 
+    private void validateAndAdjustDefaultFavicon() {
+        if ((defaultFaviconDrawableId < 0 && defaultFaviconKey != null) ||
+                (defaultFaviconDrawableId >= 0 && defaultFaviconKey == null)) {
+            throw new IllegalStateException("defaultFaviconDrawable and defaultFaviconKey both " +
+                    "either need to be specified or omitted");
+        }
+
+        if (DEFAULT_FAVICON_KEY.equals(defaultFaviconKey)) {
+            throw new IllegalStateException("defaultFaviconKey cannot be " + DEFAULT_FAVICON_KEY);
+        }
+
+        if (defaultFaviconDrawableId < 0) { // key == null too
+            defaultFaviconDrawableId = DEFAULT_FAVICON_RES_ID;
+            defaultFaviconKey = DEFAULT_FAVICON_KEY;
+        }
+
+        // Avoid potential collisions in the favicon cache keys.
+        defaultFaviconKey = FaviconView.class.getSimpleName() + defaultFaviconKey;
+    }
+
     @Override
     protected void onSizeChanged(int w, int h, int oldw, int oldh){
         super.onSizeChanged(w, h, oldw, oldh);
 
         // No point rechecking the image if there hasn't really been any change.
         if (w == mActualWidth && h == mActualHeight) {
             return;
         }
@@ -216,18 +245,18 @@ public class FaviconView extends ImageVi
         formatImage();
     }
 
     public void showDefaultFavicon() {
         // We handle the default favicon as any other favicon to avoid the complications of special
         // casing it. This means that the icon can be scaled both up and down, and the dominant
         // color box can used if it is enabled in XML attrs.
         final Bitmap defaultFaviconBitmap = BitmapFactory.decodeResource(getResources(),
-                R.drawable.favicon_globe);
-        updateAndScaleImage(defaultFaviconBitmap, DEFAULT_FAVICON_KEY);
+                defaultFaviconDrawableId);
+        updateAndScaleImage(defaultFaviconBitmap, defaultFaviconKey);
     }
 
     private void showNoImage() {
         setImageDrawable(null);
         mDominantColor = 0;
     }
 
     /**