Bug 1063703 - Use dominant color algorithm to get search engine color. r=mfinkle,bnicholson
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Thu, 25 Sep 2014 11:28:45 -0700
changeset 222685 4f5b01ea7e7c6d9f9d6d897c4c34a6afd2e4e1c6
parent 222684 d64aba881c45476d110b1b5a285265ba4955b8e7
child 222686 05645d479d896500a553c29a011f1921525722ae
push id7107
push userraliiev@mozilla.com
push dateMon, 13 Oct 2014 17:43:31 +0000
treeherdermozilla-aurora@b4b34e0acc75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle, bnicholson
bugs1063703
milestone35.0a1
Bug 1063703 - Use dominant color algorithm to get search engine color. r=mfinkle,bnicholson
mobile/android/search/java/org/mozilla/search/autocomplete/SearchBar.java
mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
--- a/mobile/android/search/java/org/mozilla/search/autocomplete/SearchBar.java
+++ b/mobile/android/search/java/org/mozilla/search/autocomplete/SearchBar.java
@@ -1,15 +1,16 @@
 /* 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/. */
 
 package org.mozilla.search.autocomplete;
 
 import android.content.Context;
+import android.graphics.Bitmap;
 import android.graphics.Color;
 import android.graphics.PorterDuff;
 import android.graphics.PorterDuffColorFilter;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.util.AttributeSet;
@@ -122,29 +123,33 @@ public class SearchBar extends FrameLayo
         editText.setSelection(text.length());
     }
 
     public String getText() {
         return editText.getText().toString();
     }
 
     public void setEngine(SearchEngine engine) {
-        int color = engine.getColor();
-        if (color == Color.TRANSPARENT) {
-            // Fall back to default orange if the search engine doesn't specify a color.
-            color = getResources().getColor(R.color.highlight_orange);
-        }
-        // Update the focused background color.
-        focusedBackground.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
-
         final String iconURL = engine.getIconURL();
-        final BitmapDrawable d = new BitmapDrawable(getResources(), BitmapUtils.getBitmapFromDataURI(iconURL));
+        final Bitmap bitmap = BitmapUtils.getBitmapFromDataURI(iconURL);
+        final BitmapDrawable d = new BitmapDrawable(getResources(), bitmap);
         engineIcon.setImageDrawable(d);
         engineIcon.setContentDescription(engine.getName());
 
+        // Update the focused background color.
+        int color = BitmapUtils.getDominantColor(bitmap);
+
+        // BitmapUtils#getDominantColor ignores black and white pixels, but it will
+        // return white if no dominant color was found. We don't want to create a
+        // white underline for the search bar, so we default to black instead.
+        if (color == Color.WHITE) {
+            color = Color.BLACK;
+        }
+        focusedBackground.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
+
         editText.setHint(getResources().getString(R.string.search_bar_hint, engine.getName()));
     }
 
     @SuppressWarnings("deprecation")
     public void setActive(boolean active) {
         if (this.active == active) {
             return;
         }
--- a/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
+++ b/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
@@ -1,15 +1,14 @@
 /* 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/. */
 
 package org.mozilla.search.providers;
 
-import android.graphics.Color;
 import android.net.Uri;
 import android.util.Log;
 import android.util.Xml;
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.IOException;
@@ -190,24 +189,16 @@ public class SearchEngine {
     public String getName() {
         return shortName;
     }
 
     public String getIconURL() {
         return iconURL;
     }
 
-    public int getColor() {
-        // TOOD: Add brand colors to search plugin XML.
-        if (identifier.equals("yahoo")) {
-            return 0xFF500095;
-        }
-        return Color.TRANSPARENT;
-    }
-
     /**
      * Determine whether a particular url belongs to this search engine. If not,
      * the url will be sent to Fennec.
      */
     public boolean isSearchResultsPage(String url) {
         return resultsUri.getAuthority().equalsIgnoreCase(Uri.parse(url).getAuthority());
     }