2012-10-31 22:10:59 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* 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.gecko;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils;
|
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2013-07-11 21:07:55 -07:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2012-10-31 22:10:59 -07:00
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
import android.graphics.Bitmap;
|
2013-03-12 14:45:01 -07:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
2012-10-31 22:10:59 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.Shader;
|
2013-04-08 00:51:56 -07:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2012-10-31 22:10:59 -07:00
|
|
|
import android.os.Build;
|
2013-02-26 11:42:24 -08:00
|
|
|
import android.os.Handler;
|
2013-04-08 00:51:56 -07:00
|
|
|
import android.os.Looper;
|
2012-10-31 22:10:59 -07:00
|
|
|
import android.util.DisplayMetrics;
|
2013-04-08 00:51:56 -07:00
|
|
|
import android.util.Log;
|
2012-10-31 22:10:59 -07:00
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewParent;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2013-04-08 00:51:56 -07:00
|
|
|
import java.util.List;
|
2012-10-31 22:10:59 -07:00
|
|
|
|
|
|
|
public class LightweightTheme implements GeckoEventListener {
|
|
|
|
private static final String LOGTAG = "GeckoLightweightTheme";
|
|
|
|
|
|
|
|
private Application mApplication;
|
2013-02-26 11:42:24 -08:00
|
|
|
private Handler mHandler;
|
|
|
|
|
2012-10-31 22:10:59 -07:00
|
|
|
private Bitmap mBitmap;
|
|
|
|
private int mColor;
|
2012-12-17 12:31:52 -08:00
|
|
|
private boolean mIsLight;
|
2012-10-31 22:10:59 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public LightweightTheme(Application application) {
|
|
|
|
mApplication = application;
|
2013-02-26 11:42:24 -08:00
|
|
|
mHandler = new Handler(Looper.getMainLooper());
|
2012-10-31 22:10:59 -07:00
|
|
|
mListeners = new ArrayList<OnChangeListener>();
|
|
|
|
|
2013-02-26 11:42:24 -08:00
|
|
|
// unregister isn't needed as the lifetime is same as the application.
|
2014-05-02 11:34:40 -07:00
|
|
|
EventDispatcher.getInstance().registerGeckoThreadListener(this,
|
|
|
|
"LightweightTheme:Update",
|
|
|
|
"LightweightTheme:Disable");
|
2012-10-31 22:10:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addListener(final OnChangeListener listener) {
|
|
|
|
// Don't inform the listeners that attached late.
|
|
|
|
// Their onLayout() will take care of them before their onDraw();
|
|
|
|
mListeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeListener(OnChangeListener listener) {
|
|
|
|
mListeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleMessage(String event, JSONObject message) {
|
|
|
|
try {
|
|
|
|
if (event.equals("LightweightTheme:Update")) {
|
|
|
|
JSONObject lightweightTheme = message.getJSONObject("data");
|
2013-07-11 21:07:55 -07:00
|
|
|
final String headerURL = lightweightTheme.getString("headerURL");
|
2013-05-07 18:29:48 -07:00
|
|
|
|
2013-07-11 21:07:55 -07:00
|
|
|
// Move any heavy lifting off the Gecko thread
|
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
2013-05-07 18:29:48 -07:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-07-11 21:07:55 -07:00
|
|
|
String croppedURL = headerURL;
|
|
|
|
int mark = croppedURL.indexOf('?');
|
|
|
|
if (mark != -1)
|
|
|
|
croppedURL = croppedURL.substring(0, mark);
|
|
|
|
|
|
|
|
// Get the image and convert it to a bitmap.
|
|
|
|
final Bitmap bitmap = BitmapUtils.decodeUrl(croppedURL);
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
setLightweightTheme(bitmap);
|
|
|
|
}
|
|
|
|
});
|
2013-05-07 18:29:48 -07:00
|
|
|
}
|
|
|
|
});
|
2012-10-31 22:10:59 -07:00
|
|
|
} else if (event.equals("LightweightTheme:Disable")) {
|
2013-02-26 11:42:24 -08:00
|
|
|
mHandler.post(new Runnable() {
|
2013-03-13 13:20:57 -07:00
|
|
|
@Override
|
2013-02-26 11:42:24 -08:00
|
|
|
public void run() {
|
|
|
|
resetLightweightTheme();
|
|
|
|
}
|
|
|
|
});
|
2012-10-31 22:10:59 -07:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 11:42:24 -08:00
|
|
|
/**
|
|
|
|
* Set a new lightweight theme with the given bitmap.
|
|
|
|
* Note: This should be called on the UI thread to restrict accessing the
|
|
|
|
* bitmap to a single thread.
|
|
|
|
*
|
|
|
|
* @param bitmap The bitmap used for the lightweight theme.
|
|
|
|
*/
|
|
|
|
private void setLightweightTheme(Bitmap bitmap) {
|
2013-03-12 14:45:01 -07:00
|
|
|
if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
|
2013-02-26 11:42:24 -08:00
|
|
|
mBitmap = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To find the dominant color only once, take the bottom 25% of pixels.
|
|
|
|
DisplayMetrics dm = mApplication.getResources().getDisplayMetrics();
|
|
|
|
int maxWidth = Math.max(dm.widthPixels, dm.heightPixels);
|
2013-03-12 14:45:01 -07:00
|
|
|
int height = (int) (bitmap.getHeight() * 0.25);
|
|
|
|
|
|
|
|
// The lightweight theme image's width and height.
|
|
|
|
int bitmapWidth = bitmap.getWidth();
|
|
|
|
int bitmapHeight = bitmap.getHeight();
|
|
|
|
|
|
|
|
// A cropped bitmap of the bottom 25% of pixels.
|
|
|
|
Bitmap cropped = Bitmap.createBitmap(bitmap,
|
|
|
|
bitmapWidth > maxWidth ? bitmapWidth - maxWidth : 0,
|
|
|
|
bitmapHeight - height,
|
|
|
|
bitmapWidth > maxWidth ? maxWidth : bitmapWidth,
|
|
|
|
height);
|
|
|
|
|
|
|
|
// Dominant color based on the cropped bitmap.
|
2013-02-26 11:42:24 -08:00
|
|
|
mColor = BitmapUtils.getDominantColor(cropped, false);
|
|
|
|
|
2013-03-12 14:45:01 -07:00
|
|
|
// Calculate the luminance to determine if it's a light or a dark theme.
|
2013-02-26 11:42:24 -08:00
|
|
|
double luminance = (0.2125 * ((mColor & 0x00FF0000) >> 16)) +
|
|
|
|
(0.7154 * ((mColor & 0x0000FF00) >> 8)) +
|
|
|
|
(0.0721 * (mColor &0x000000FF));
|
|
|
|
mIsLight = (luminance > 110) ? true : false;
|
|
|
|
|
2013-03-12 14:45:01 -07:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Create a bigger image that can fill the device width.
|
|
|
|
// By creating a canvas for the bitmap, anything drawn on the canvas
|
|
|
|
// will be drawn on the bitmap.
|
|
|
|
mBitmap = Bitmap.createBitmap(maxWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas canvas = new Canvas(mBitmap);
|
|
|
|
|
|
|
|
// Fill the canvas with dominant color.
|
|
|
|
canvas.drawColor(mColor);
|
|
|
|
|
|
|
|
// The image should be top-right aligned.
|
|
|
|
Rect rect = new Rect();
|
|
|
|
Gravity.apply(Gravity.TOP | Gravity.RIGHT,
|
|
|
|
bitmapWidth,
|
|
|
|
bitmapHeight,
|
|
|
|
new Rect(0, 0, maxWidth, bitmapHeight),
|
|
|
|
rect);
|
|
|
|
|
|
|
|
// Draw the bitmap.
|
|
|
|
canvas.drawBitmap(bitmap, null, rect, paint);
|
|
|
|
}
|
|
|
|
|
2013-02-26 11:42:24 -08:00
|
|
|
for (OnChangeListener listener : mListeners)
|
|
|
|
listener.onLightweightThemeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
for (OnChangeListener listener : mListeners)
|
|
|
|
listener.onLightweightThemeReset();
|
|
|
|
}
|
|
|
|
}
|
2012-12-26 11:10:14 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lightweight theme is enabled only if there is an active bitmap.
|
|
|
|
*
|
|
|
|
* @return True if the theme is enabled.
|
|
|
|
*/
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return (mBitmap != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on the luminance of the domanint color, a theme is classified as light or dark.
|
|
|
|
*
|
|
|
|
* @return True if the theme is light.
|
|
|
|
*/
|
2012-12-17 12:31:52 -08:00
|
|
|
public boolean isLightTheme() {
|
|
|
|
return mIsLight;
|
|
|
|
}
|
|
|
|
|
2012-10-31 22:10:59 -07:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Calculate the coordinates for the cropped bitmap.
|
|
|
|
int screenWidth = view.getContext().getResources().getDisplayMetrics().widthPixels;
|
|
|
|
int left = mBitmap.getWidth() - screenWidth + rect.left;
|
|
|
|
int right = mBitmap.getWidth() - screenWidth + rect.right;
|
|
|
|
int top = rect.top - window.top;
|
|
|
|
int bottom = rect.bottom - window.top;
|
|
|
|
|
|
|
|
int offsetX = 0;
|
|
|
|
int offsetY = 0;
|
|
|
|
|
|
|
|
// Find if this view or any of its ancestors has been translated or scrolled.
|
|
|
|
ViewParent parent;
|
|
|
|
View curView = view;
|
|
|
|
do {
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
|
|
|
offsetX += (int) curView.getTranslationX() - curView.getScrollX();
|
|
|
|
offsetY += (int) curView.getTranslationY() - curView.getScrollY();
|
|
|
|
} else {
|
|
|
|
offsetX -= curView.getScrollX();
|
|
|
|
offsetY -= curView.getScrollY();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = curView.getParent();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// The either the required height may be less than the available image height or more than it.
|
|
|
|
// If the height required is more, crop only the available portion on the image.
|
|
|
|
int width = right - left;
|
|
|
|
int height = (bottom > mBitmap.getHeight() ? mBitmap.getHeight() - top : bottom - top);
|
|
|
|
|
|
|
|
// There is a chance that the view is not visible or doesn't fall within the phone's size.
|
|
|
|
// In this case, 'rect' will have all values as '0'. Hence 'top' and 'bottom' may be negative,
|
|
|
|
// and createBitmap() will fail.
|
|
|
|
// The view will get a background in its next layout pass.
|
|
|
|
try {
|
|
|
|
return Bitmap.createBitmap(mBitmap, left, top, width, height);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
BitmapDrawable drawable = new BitmapDrawable(view.getContext().getResources(), bitmap);
|
|
|
|
drawable.setGravity(Gravity.TOP|Gravity.RIGHT);
|
|
|
|
drawable.setTileModeXY(Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
|
|
|
|
return drawable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-13 08:50:57 -08:00
|
|
|
* Converts the cropped bitmap to a LightweightThemeDrawable, placing it over the dominant color.
|
2012-10-31 22:10:59 -07:00
|
|
|
*
|
|
|
|
* @param view The view for which a background drawable is required.
|
|
|
|
* @return Either the cropped bitmap as a Drawable or null.
|
|
|
|
*/
|
2013-02-13 08:50:57 -08:00
|
|
|
public LightweightThemeDrawable getColorDrawable(View view) {
|
|
|
|
return getColorDrawable(view, mColor, false);
|
|
|
|
}
|
2012-10-31 22:10:59 -07:00
|
|
|
|
|
|
|
/**
|
2013-02-13 08:50:57 -08:00
|
|
|
* Converts the cropped bitmap to a LightweightThemeDrawable, placing it over the required color.
|
2012-10-31 22:10:59 -07:00
|
|
|
*
|
|
|
|
* @param view The view for which a background drawable is required.
|
2013-02-13 08:50:57 -08:00
|
|
|
* @param color The color over which the drawable should be drawn.
|
2012-10-31 22:10:59 -07:00
|
|
|
* @return Either the cropped bitmap as a Drawable or null.
|
|
|
|
*/
|
2013-02-13 08:50:57 -08:00
|
|
|
public LightweightThemeDrawable getColorDrawable(View view, int color) {
|
|
|
|
return getColorDrawable(view, color, false);
|
2012-12-13 13:13:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the cropped bitmap to a LightweightThemeDrawable, placing it over the required color.
|
|
|
|
*
|
|
|
|
* @param view The view for which a background drawable is required.
|
2013-02-13 08:50:57 -08:00
|
|
|
* @param color The color over which the drawable should be drawn.
|
|
|
|
* @param needsDominantColor A layer of dominant color is needed or not.
|
2012-12-13 13:13:36 -08:00
|
|
|
* @return Either the cropped bitmap as a Drawable or null.
|
|
|
|
*/
|
2013-02-13 08:50:57 -08:00
|
|
|
public LightweightThemeDrawable getColorDrawable(View view, int color, boolean needsDominantColor) {
|
2012-12-13 13:13:36 -08:00
|
|
|
Bitmap bitmap = getCroppedBitmap(view);
|
|
|
|
if (bitmap == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
LightweightThemeDrawable drawable = new LightweightThemeDrawable(view.getContext().getResources(), bitmap);
|
2013-02-13 08:50:57 -08:00
|
|
|
if (needsDominantColor)
|
|
|
|
drawable.setColorWithFilter(color, (mColor & 0x22FFFFFF));
|
|
|
|
else
|
|
|
|
drawable.setColor(color);
|
|
|
|
|
2012-12-13 13:13:36 -08:00
|
|
|
return drawable;
|
2012-10-31 22:10:59 -07:00
|
|
|
}
|
|
|
|
}
|