Bug 1084529 - Move hardcoded colors to resources on Android. r=mcomella
authorErik Edrosa <erik.edrosa@gmail.com>
Thu, 23 Oct 2014 14:24:38 -0700
changeset 212122 c77cb1e0d919d2f8b798b802d8b83ebb02b877a6
parent 212121 52e8c44b21f0775b2d77fb4e4a0bd1e92400a2c0
child 212123 54fab44be5f327fdb342eebc8860ce595b3c7c10
push id27698
push usercbook@mozilla.com
push dateFri, 24 Oct 2014 13:53:50 +0000
treeherdermozilla-central@6e35802ae3e2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs1084529
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1084529 - Move hardcoded colors to resources on Android. r=mcomella
mobile/android/base/home/TopSitesThumbnailView.java
mobile/android/base/resources/values/colors.xml
mobile/android/base/resources/values/search_colors.xml
mobile/android/base/toolbar/CanvasDelegate.java
mobile/android/base/toolbar/ShapedButton.java
mobile/android/search/java/org/mozilla/search/autocomplete/SuggestionsFragment.java
--- a/mobile/android/base/home/TopSitesThumbnailView.java
+++ b/mobile/android/base/home/TopSitesThumbnailView.java
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.ThumbnailHelper;
 
 import android.content.Context;
+import android.content.res.Resources;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.PorterDuff.Mode;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.widget.ImageView;
 
 /**
@@ -21,44 +22,43 @@ import android.widget.ImageView;
  */
 public class TopSitesThumbnailView extends ImageView {
     private static final String LOGTAG = "GeckoTopSitesThumbnailView";
 
     // 27.34% opacity filter for the dominant color.
     private static final int COLOR_FILTER = 0x46FFFFFF;
 
     // Default filter color for "Add a bookmark" views.
-    private static final int DEFAULT_COLOR = 0xFFECF0F3;
+    private final int mDefaultColor = getResources().getColor(R.color.top_site_default);
 
     // Stroke width for the border.
     private final float mStrokeWidth = getResources().getDisplayMetrics().density * 2;
 
     // Paint for drawing the border.
-    private static final Paint sBorderPaint;
-
-    // Initializing the static border paint.
-    static {
-        sBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
-        sBorderPaint.setColor(0xFFCFD9E1);
-        sBorderPaint.setStyle(Paint.Style.STROKE);
-    }
+    private final Paint mBorderPaint;
 
     public TopSitesThumbnailView(Context context) {
         this(context, null);
 
         // A border will be drawn if needed.
         setWillNotDraw(false);
     }
 
     public TopSitesThumbnailView(Context context, AttributeSet attrs) {
         this(context, attrs, R.attr.topSitesThumbnailViewStyle);
     }
 
     public TopSitesThumbnailView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
+
+        // Initialize the border paint.
+        final Resources res = getResources();
+        mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+        mBorderPaint.setColor(res.getColor(R.color.top_site_border));
+        mBorderPaint.setStyle(Paint.Style.STROKE);
     }
 
     /**
      * Measure the view to determine the measured width and height.
      * The height is constrained by the measured width.
      *
      * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
      * @param heightMeasureSpec vertical space requirements as imposed by the parent, but ignored.
@@ -77,18 +77,18 @@ public class TopSitesThumbnailView exten
     /**
      * {@inheritDoc}
      */
     @Override
     public void onDraw(Canvas canvas) {
         super.onDraw(canvas);
 
         if (getBackground() == null) {
-            sBorderPaint.setStrokeWidth(mStrokeWidth);
-            canvas.drawRect(0, 0, getWidth(), getHeight(), sBorderPaint);
+            mBorderPaint.setStrokeWidth(mStrokeWidth);
+            canvas.drawRect(0, 0, getWidth(), getHeight(), mBorderPaint);
         }
     }
 
     /**
      * Sets the background color with a filter to reduce the color opacity.
      *
      * @param color the color filter to apply over the drawable.
      */
@@ -99,16 +99,16 @@ public class TopSitesThumbnailView exten
     /**
      * Sets the background to a Drawable by applying the specified color as a filter.
      *
      * @param color the color filter to apply over the drawable.
      */
     @Override
     public void setBackgroundColor(int color) {
         if (color == 0) {
-            color = DEFAULT_COLOR;
+            color = mDefaultColor;
         }
 
         Drawable drawable = getResources().getDrawable(R.drawable.top_sites_thumbnail_bg);
         drawable.setColorFilter(color, Mode.SRC_ATOP);
         setBackgroundDrawable(drawable);
     }
 }
--- a/mobile/android/base/resources/values/colors.xml
+++ b/mobile/android/base/resources/values/colors.xml
@@ -139,9 +139,16 @@
   <color name="toast_background">#DD363B40</color>
   <color name="toast_button_background">#00000000</color>
   <color name="toast_button_pressed">#DD2C3136</color>
   <color name="toast_button_text">#FFFFFFFF</color>
 
   <!-- Colour used for Find-In-Page dialog -->
   <color name="find_status_default">#AFB1B3</color>
 
+  <!-- Canvas delegate paint color -->
+  <color name="canvas_delegate_paint">#FFFF0000</color>
+
+  <!-- Top sites thumbnail colors -->
+  <color name="top_site_default">#FFECF0F3</color>
+  <color name="top_site_border">#FFCFD9E1</color>
+
 </resources>
--- a/mobile/android/base/resources/values/search_colors.xml
+++ b/mobile/android/base/resources/values/search_colors.xml
@@ -21,9 +21,12 @@
     <!--Facet button colors-->
     <color name="facet_button_background_color_default">@android:color/white</color>
     <color name="facet_button_background_color_pressed">#FAFAFA</color>
 
     <color name="facet_button_text_color_default">#ADB0B1</color>
     <color name="facet_button_text_color_selected">#383E42</color>
 
     <color name="network_error_link">#0092DB</color>
+
+    <!-- Suggestion highlight color -->
+    <color name="suggestion_highlight">#FF999999</color>
 </resources>
--- a/mobile/android/base/toolbar/CanvasDelegate.java
+++ b/mobile/android/base/toolbar/CanvasDelegate.java
@@ -19,26 +19,23 @@ class CanvasDelegate {
     PorterDuffXfermode mMode;
     DrawManager mDrawManager;
 
     // DrawManager would do a default draw of the background.
     static interface DrawManager {
         public void defaultDraw(Canvas canvas);
     }
 
-    CanvasDelegate(DrawManager drawManager, Mode mode) {
+    CanvasDelegate(DrawManager drawManager, Mode mode, Paint paint) {
         mDrawManager = drawManager;
 
         // DST_IN masks, DST_OUT clips.
         mMode = new PorterDuffXfermode(mode);
 
-        mPaint = new Paint();
-        mPaint.setAntiAlias(true);
-        mPaint.setColor(0xFFFF0000);
-        mPaint.setStrokeWidth(0.0f);
+        mPaint = paint;
     }
 
     void draw(Canvas canvas, Path path, int width, int height) {
         // Save the canvas. All PorterDuff operations should be done in a offscreen bitmap.
         int count = canvas.saveLayer(0, 0, width, height, null,
                                      Canvas.MATRIX_SAVE_FLAG |
                                      Canvas.CLIP_SAVE_FLAG |
                                      Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
--- a/mobile/android/base/toolbar/ShapedButton.java
+++ b/mobile/android/base/toolbar/ShapedButton.java
@@ -9,16 +9,17 @@ import org.mozilla.gecko.LightweightThem
 import org.mozilla.gecko.LightweightThemeDrawable;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.tabs.TabCurve;
 import org.mozilla.gecko.widget.ThemedImageButton;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
+import android.graphics.Paint;
 import android.graphics.Path;
 import android.graphics.PorterDuff.Mode;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.StateListDrawable;
 import android.util.AttributeSet;
 
 public class ShapedButton extends ThemedImageButton
                           implements CanvasDelegate.DrawManager {
@@ -28,17 +29,22 @@ public class ShapedButton extends Themed
     protected final CanvasDelegate mCanvasDelegate;
 
     public ShapedButton(Context context, AttributeSet attrs) {
         super(context, attrs);
         mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
 
         // Path is clipped.
         mPath = new Path();
-        mCanvasDelegate = new CanvasDelegate(this, Mode.DST_IN);
+
+        final Paint paint = new Paint();
+        paint.setAntiAlias(true);
+        paint.setColor(getResources().getColor(R.color.canvas_delegate_paint));
+        paint.setStrokeWidth(0.0f);
+        mCanvasDelegate = new CanvasDelegate(this, Mode.DST_IN, paint);
 
         setWillNotDraw(false);
     }
 
     @Override
     public void draw(Canvas canvas) {
         if (mCanvasDelegate != null)
             mCanvasDelegate.draw(canvas, mPath, getWidth(), getHeight());
--- a/mobile/android/search/java/org/mozilla/search/autocomplete/SuggestionsFragment.java
+++ b/mobile/android/search/java/org/mozilla/search/autocomplete/SuggestionsFragment.java
@@ -43,19 +43,16 @@ public class SuggestionsFragment extends
     private static final String KEY_SEARCH_TERM = "search_term";
 
     // Timeout for the suggestion client to respond
     private static final int SUGGESTION_TIMEOUT = 3000;
 
     // Number of search suggestions to show.
     private static final int SUGGESTION_MAX = 5;
 
-    // Color of search term match in search suggestion
-    private static final int SUGGESTION_HIGHLIGHT_COLOR = 0xFF999999;
-
     public static final String GECKO_SEARCH_TERMS_URL_PARAM = "__searchTerms__";
 
     private AcceptsSearchQuery searchListener;
 
     // Suggest client gets setup outside of the normal fragment lifecycle, therefore
     // clients should ensure that this isn't null before using it.
     private SuggestClient suggestClient;
     private SuggestionLoaderCallbacks suggestionLoaderCallbacks;
@@ -151,31 +148,31 @@ public class SuggestionsFragment extends
             loaderManager.initLoader(LOADER_ID_SUGGESTION, args, suggestionLoaderCallbacks);
         } else {
             loaderManager.restartLoader(LOADER_ID_SUGGESTION, args, suggestionLoaderCallbacks);
         }
     }
 
     public static class Suggestion {
 
-        private static final ForegroundColorSpan COLOR_SPAN =
-                new ForegroundColorSpan(SUGGESTION_HIGHLIGHT_COLOR);
-
         public final String value;
         public final SpannableString display;
+        public final ForegroundColorSpan colorSpan;
 
-        public Suggestion(String value, String searchTerm) {
+        public Suggestion(String value, String searchTerm, int suggestionHighlightColor) {
             this.value = value;
 
             display = new SpannableString(value);
 
+            colorSpan = new ForegroundColorSpan(suggestionHighlightColor);
+
             // Highlight mixed-case matches.
             final int start = value.toLowerCase().indexOf(searchTerm.toLowerCase());
             if (start >= 0) {
-                display.setSpan(COLOR_SPAN, start, start + searchTerm.length(), 0);
+                display.setSpan(colorSpan, start, start + searchTerm.length(), 0);
             }
         }
     }
 
     private class SuggestionLoaderCallbacks implements LoaderManager.LoaderCallbacks<List<Suggestion>> {
         @Override
         public Loader<List<Suggestion>> onCreateLoader(int id, Bundle args) {
             // We drop the user's search if suggestclient isn't ready. This happens if the
@@ -201,31 +198,35 @@ public class SuggestionsFragment extends
         @Override
         public void onLoaderReset(Loader<List<Suggestion>> loader) { }
     }
 
     private static class SuggestionAsyncLoader extends AsyncTaskLoader<List<Suggestion>> {
         private final SuggestClient suggestClient;
         private final String searchTerm;
         private List<Suggestion> suggestions;
+        private final int suggestionHighlightColor;
 
         public SuggestionAsyncLoader(Context context, SuggestClient suggestClient, String searchTerm) {
             super(context);
             this.suggestClient = suggestClient;
             this.searchTerm = searchTerm;
             this.suggestions = null;
+
+            // Color of search term match in search suggestion
+            suggestionHighlightColor = context.getResources().getColor(R.color.suggestion_highlight);
         }
 
         @Override
         public List<Suggestion> loadInBackground() {
             final List<String> values = suggestClient.query(searchTerm);
 
             final List<Suggestion> result = new ArrayList<Suggestion>(values.size());
             for (String value : values) {
-                result.add(new Suggestion(value, searchTerm));
+                result.add(new Suggestion(value, searchTerm, suggestionHighlightColor));
             }
 
             return result;
         }
 
         @Override
         public void deliverResult(List<Suggestion> suggestions) {
             this.suggestions = suggestions;