gecko/mobile/android/base/ShapedButton.java
Mark Finkle 63386424e3 Backout 11fcd37bfb7d, cdf30a34dec9, d5f4626f5b73, e0a0a5b98b95, 944db176184e, 984a10bc4125, 585a7f8fc448, abfc930bc7cd for test failures
--HG--
rename : mobile/android/base/resources/drawable-xlarge-mdpi-v11/tabs_button.xml.in => mobile/android/base/resources/drawable-xlarge-mdpi-v11/tabs_button.xml
rename : mobile/android/base/resources/drawable/address_bar_url.xml.in => mobile/android/base/resources/drawable/address_bar_url.xml
rename : mobile/android/base/resources/drawable/menu_level.xml.in => mobile/android/base/resources/drawable/menu_level.xml
2012-11-28 21:55:05 -05:00

103 lines
3.2 KiB
Java

/* 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;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageButton;
public abstract class ShapedButton extends ImageButton
implements CanvasDelegate.DrawManager,
LightweightTheme.OnChangeListener {
protected GeckoActivity mActivity;
protected Path mPath;
protected CurveTowards mSide;
protected CanvasDelegate mCanvasDelegate;
protected enum CurveTowards { NONE, LEFT, RIGHT };
abstract public void onLightweightThemeChanged();
abstract public void onLightweightThemeReset();
public ShapedButton(Context context, AttributeSet attrs) {
super(context, attrs);
mActivity = (GeckoActivity) context;
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;
setWillNotDraw(false);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mActivity.getLightweightTheme().addListener(this);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mActivity.getLightweightTheme().removeListener(this);
}
@Override
public void draw(Canvas canvas) {
mCanvasDelegate.draw(canvas, mPath, getWidth(), getHeight());
}
@Override
public void defaultDraw(Canvas canvas) {
super.draw(canvas);
}
@Override
public void setBackgroundDrawable(Drawable drawable) {
if (getBackground() == null || drawable == null) {
super.setBackgroundDrawable(drawable);
return;
}
int[] padding = new int[] { getPaddingLeft(),
getPaddingTop(),
getPaddingRight(),
getPaddingBottom()
};
drawable.setLevel(getBackground().getLevel());
super.setBackgroundDrawable(drawable);
setPadding(padding[0], padding[1], padding[2], padding[3]);
}
@Override
public void setBackgroundResource(int resId) {
if (getBackground() == null || resId == 0) {
super.setBackgroundResource(resId);
return;
}
setBackgroundDrawable(getContext().getResources().getDrawable(resId));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
onLightweightThemeChanged();
}
}