2012-08-31 11:59:50 -07:00
|
|
|
/* 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 android.content.Context;
|
2012-10-31 22:12:02 -07:00
|
|
|
import android.content.res.TypedArray;
|
2012-08-31 11:59:50 -07:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Path;
|
|
|
|
import android.graphics.PorterDuff.Mode;
|
2012-10-31 22:12:02 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2012-08-31 11:59:50 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
|
|
|
public class BrowserToolbarBackground extends LinearLayout
|
2012-10-31 22:12:02 -07:00
|
|
|
implements CanvasDelegate.DrawManager,
|
|
|
|
LightweightTheme.OnChangeListener {
|
|
|
|
private GeckoActivity mActivity;
|
|
|
|
private Path mPath;
|
|
|
|
private CurveTowards mSide;
|
|
|
|
private CanvasDelegate mCanvasDelegate;
|
|
|
|
|
|
|
|
private enum CurveTowards { NONE, LEFT, RIGHT };
|
2012-08-31 11:59:50 -07:00
|
|
|
|
|
|
|
public BrowserToolbarBackground(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
2012-10-31 22:12:02 -07:00
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BrowserToolbarCurve);
|
|
|
|
int curveTowards = a.getInt(R.styleable.BrowserToolbarCurve_curveTowards, 0x02);
|
|
|
|
a.recycle();
|
|
|
|
|
|
|
|
if (curveTowards == 0x00)
|
|
|
|
mSide = CurveTowards.NONE;
|
|
|
|
else if (curveTowards == 0x01)
|
|
|
|
mSide = CurveTowards.LEFT;
|
|
|
|
else
|
|
|
|
mSide = CurveTowards.RIGHT;
|
|
|
|
|
2012-08-31 11:59:50 -07:00
|
|
|
// Path is clipped.
|
|
|
|
mPath = new Path();
|
|
|
|
mCanvasDelegate = new CanvasDelegate(this, Mode.DST_OUT);
|
2012-10-31 22:12:02 -07:00
|
|
|
mActivity = (GeckoActivity) context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
mActivity.getLightweightTheme().addListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
mActivity.getLightweightTheme().removeListener(this);
|
2012-08-31 11:59:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
|
|
|
|
int width = getMeasuredWidth();
|
|
|
|
int height = getMeasuredHeight();
|
|
|
|
float curve = height * 1.125f;
|
|
|
|
|
|
|
|
mPath.reset();
|
2012-10-31 22:12:02 -07:00
|
|
|
|
|
|
|
if (mSide == CurveTowards.LEFT) {
|
|
|
|
mPath.moveTo(0, height);
|
|
|
|
mPath.cubicTo(curve * 0.75f, height,
|
|
|
|
curve * 0.25f, 0,
|
|
|
|
curve, 0);
|
|
|
|
mPath.lineTo(0, 0);
|
|
|
|
mPath.lineTo(0, height);
|
|
|
|
} else if (mSide == CurveTowards.RIGHT) {
|
|
|
|
mPath.moveTo(width, height);
|
|
|
|
mPath.cubicTo((width - (curve * 0.75f)), height,
|
|
|
|
(width - (curve * 0.25f)), 0,
|
|
|
|
(width - curve), 0);
|
|
|
|
mPath.lineTo(width, 0);
|
|
|
|
mPath.lineTo(width, height);
|
|
|
|
}
|
2012-08-31 11:59:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(Canvas canvas) {
|
|
|
|
mCanvasDelegate.draw(canvas, mPath, getWidth(), getHeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void defaultDraw(Canvas canvas) {
|
|
|
|
super.draw(canvas);
|
|
|
|
}
|
2012-10-31 22:12:02 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLightweightThemeChanged() {
|
|
|
|
Drawable drawable = mActivity.getLightweightTheme().getDrawable(this);
|
|
|
|
if (drawable == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int[] padding = new int[] { getPaddingLeft(),
|
|
|
|
getPaddingTop(),
|
|
|
|
getPaddingRight(),
|
|
|
|
getPaddingBottom()
|
|
|
|
};
|
|
|
|
setBackgroundDrawable(drawable);
|
|
|
|
setPadding(padding[0], padding[1], padding[2], padding[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLightweightThemeReset() {
|
|
|
|
int[] padding = new int[] { getPaddingLeft(),
|
|
|
|
getPaddingTop(),
|
|
|
|
getPaddingRight(),
|
|
|
|
getPaddingBottom()
|
|
|
|
};
|
|
|
|
setBackgroundResource(R.drawable.address_bar_bg);
|
|
|
|
setPadding(padding[0], padding[1], padding[2], padding[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
super.onLayout(changed, left, top, right, bottom);
|
|
|
|
onLightweightThemeChanged();
|
|
|
|
}
|
2012-08-31 11:59:50 -07:00
|
|
|
}
|