Bug 1041632 - Part 6: eliminate enclosing instance member access in LightweightTheme. r=ckitching
authorRichard Newman <rnewman@mozilla.com>
Mon, 21 Jul 2014 10:21:54 -0700
changeset 195369 e014881a4d8d32e6c6a45a368519d08401a764c7
parent 195368 0f90d365211db06383e134042e8daa38fba5cce2
child 195370 827990281932854dbb5fdb350a9903a5a64bf7b1
push id46575
push userkwierso@gmail.com
push dateTue, 22 Jul 2014 00:35:21 +0000
treeherdermozilla-inbound@fee5c4bdd713 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckitching
bugs1041632
milestone33.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 1041632 - Part 6: eliminate enclosing instance member access in LightweightTheme. r=ckitching
mobile/android/base/LightweightTheme.java
--- a/mobile/android/base/LightweightTheme.java
+++ b/mobile/android/base/LightweightTheme.java
@@ -3,17 +3,17 @@
  * 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.gecko;
 
 import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.ThreadUtils;
-
+import org.mozilla.gecko.util.ThreadUtils.AssertBehavior;
 import org.json.JSONObject;
 
 import android.app.Application;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.graphics.Shader;
@@ -29,33 +29,33 @@ import android.view.View;
 import android.view.ViewParent;
 
 import java.util.ArrayList;
 import java.util.List;
 
 public class LightweightTheme implements GeckoEventListener {
     private static final String LOGTAG = "GeckoLightweightTheme";
 
-    private Application mApplication;
-    private Handler mHandler;
+    private final Application mApplication;
+    /* inner-access */ final Handler mHandler;
 
     private Bitmap mBitmap;
     private int mColor;
     private boolean mIsLight;
 
     public static interface OnChangeListener {
         // The View should change its background/text color. 
         public void onLightweightThemeChanged();
 
         // The View should reset to its default background/text color.
         public void onLightweightThemeReset();
     }
 
-    private List<OnChangeListener> mListeners;
-    
+    private final List<OnChangeListener> mListeners;
+
     public LightweightTheme(Application application) {
         mApplication = application;
         mHandler = new Handler(Looper.getMainLooper());
         mListeners = new ArrayList<OnChangeListener>();
 
         // unregister isn't needed as the lifetime is same as the application.
         EventDispatcher.getInstance().registerGeckoThreadListener(this,
             "LightweightTheme:Update",
@@ -142,17 +142,17 @@ public class LightweightTheme implements
 
         // Dominant color based on the cropped bitmap.
         mColor = BitmapUtils.getDominantColor(cropped, false);
 
         // Calculate the luminance to determine if it's a light or a dark theme.
         double luminance = (0.2125 * ((mColor & 0x00FF0000) >> 16)) + 
                            (0.7154 * ((mColor & 0x0000FF00) >> 8)) + 
                            (0.0721 * (mColor &0x000000FF));
-        mIsLight = (luminance > 110) ? true : false;
+        mIsLight = luminance > 110;
 
         // The bitmap image might be smaller than the device's width.
         // If it's smaller, fill the extra space on the left with the dominant color.
         if (bitmap.getWidth() >= maxWidth) {
             mBitmap = bitmap;
         } else {
             Paint paint = new Paint();
             paint.setAntiAlias(true);
@@ -183,22 +183,26 @@ public class LightweightTheme implements
     }
 
     /**
      * Reset the lightweight theme.
      * Note: This should be called on the UI thread to restrict accessing the
      * bitmap to a single thread.
      */
     private void resetLightweightTheme() {
-        if (mBitmap != null) {
-            // Reset the bitmap.
-            mBitmap = null;
+        ThreadUtils.assertOnUiThread(AssertBehavior.NONE);
+        if (mBitmap == null) {
+            return;
+        }
 
-            for (OnChangeListener listener : mListeners)
-                listener.onLightweightThemeReset();
+        // Reset the bitmap.
+        mBitmap = null;
+
+        for (OnChangeListener listener : mListeners) {
+            listener.onLightweightThemeReset();
         }
     }
 
     /**
      * A lightweight theme is enabled only if there is an active bitmap.
      *
      * @return True if the theme is enabled.
      */
@@ -218,18 +222,19 @@ public class LightweightTheme implements
     /**
      * Crop the image based on the position of the view on the window.
      * Either the View or one of its ancestors might have scrolled or translated.
      * This value should be taken into account while mapping the View to the Bitmap.
      *
      * @param view The view requesting a cropped bitmap.
      */
     private Bitmap getCroppedBitmap(View view) {
-        if (mBitmap == null || view == null)
+        if (mBitmap == null || view == null) {
             return null;
+        }
 
         // Get the global position of the view on the entire screen.
         Rect rect = new Rect();
         view.getGlobalVisibleRect(rect);
 
         // Get the activity's window position. This does an IPC call, may be expensive.
         Rect window = new Rect();
         view.getWindowVisibleDisplayFrame(window);
@@ -253,18 +258,19 @@ public class LightweightTheme implements
                 offsetY += (int) curView.getTranslationY() - curView.getScrollY();
             } else {
                 offsetX -= curView.getScrollX();
                 offsetY -= curView.getScrollY();
             }
 
             parent = curView.getParent();
 
-            if (parent instanceof View)
+            if (parent instanceof View) {
                 curView = (View) parent;
+            }
 
         } while(parent instanceof View && parent != null);
 
         // Adjust the coordinates for the offset.
         left -= offsetX;
         right -= offsetX;
         top -= offsetY;
         bottom -= offsetY;
@@ -288,21 +294,22 @@ public class LightweightTheme implements
     /**
      * Converts the cropped bitmap to a BitmapDrawable and returns the same.
      *
      * @param view The view for which a background drawable is required.
      * @return Either the cropped bitmap as a Drawable or null.
      */
     public Drawable getDrawable(View view) {
         Bitmap bitmap = getCroppedBitmap(view);
-        if (bitmap == null)
+        if (bitmap == null) {
             return null;
+        }
 
         BitmapDrawable drawable = new BitmapDrawable(view.getContext().getResources(), bitmap);
-        drawable.setGravity(Gravity.TOP|Gravity.RIGHT);
+        drawable.setGravity(Gravity.TOP | Gravity.RIGHT);
         drawable.setTileModeXY(Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
         return drawable;
     }
 
     /**
      * Converts the cropped bitmap to a LightweightThemeDrawable, placing it over the dominant color.
      *
      * @param view The view for which a background drawable is required.
@@ -328,20 +335,22 @@ public class LightweightTheme implements
      *
      * @param view The view for which a background drawable is required.
      * @param color The color over which the drawable should be drawn.
      * @param needsDominantColor A layer of dominant color is needed or not.
      * @return Either the cropped bitmap as a Drawable or null.
      */
     public LightweightThemeDrawable getColorDrawable(View view, int color, boolean needsDominantColor) {
         Bitmap bitmap = getCroppedBitmap(view);
-        if (bitmap == null)
+        if (bitmap == null) {
             return null;
+        }
 
         LightweightThemeDrawable drawable = new LightweightThemeDrawable(view.getContext().getResources(), bitmap);
-        if (needsDominantColor)
+        if (needsDominantColor) {
             drawable.setColorWithFilter(color, (mColor & 0x22FFFFFF));
-        else
+        } else {
             drawable.setColor(color);
+        }
 
         return drawable;
     }
 }