Bug 932967: Cleanup LWT to not use Activity, but use Application. [r=mfinkle]

--HG--
extra : rebase_source : aceef27c2fd841965f614f4c0157cdcbccf152e7
This commit is contained in:
Sriram Ramasubramanian 2013-10-30 14:44:14 -07:00
parent cc780d71aa
commit abb2cad8d6
7 changed files with 71 additions and 89 deletions

View File

@ -5,14 +5,11 @@
package org.mozilla.gecko;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
@ -37,7 +34,6 @@ public class BackButton extends ShapedButton {
// Path is masked.
mPath = new Path();
mBorderPath = new Path();
mCanvasDelegate = new CanvasDelegate(this, Mode.DST_IN);
}
@Override
@ -76,19 +72,17 @@ public class BackButton extends ShapedButton {
// The drawable is constructed as per @drawable/url_bar_nav_button.
@Override
public void onLightweightThemeChanged() {
Drawable drawable = mActivity.getLightweightTheme().getDrawable(this);
final Drawable drawable = mTheme.getDrawable(this);
if (drawable == null)
return;
Resources resources = getContext().getResources();
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] { R.attr.state_private, android.R.attr.state_pressed }, new ColorDrawable(resources.getColor(R.color.highlight_nav_pb)));
stateList.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(resources.getColor(R.color.highlight_nav)));
stateList.addState(new int[] { R.attr.state_private, android.R.attr.state_focused }, new ColorDrawable(resources.getColor(R.color.highlight_nav_focused_pb)));
stateList.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(resources.getColor(R.color.highlight_nav_focused)));
stateList.addState(new int[] { R.attr.state_private }, new ColorDrawable(resources.getColor(R.color.background_private)));
stateList.addState(new int[] {}, drawable);
final StateListDrawable stateList = new StateListDrawable();
stateList.addState(PRIVATE_PRESSED_STATE_SET, getColorDrawable(R.color.highlight_nav_pb));
stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_nav));
stateList.addState(PRIVATE_FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused_pb));
stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused));
stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_private));
stateList.addState(EMPTY_STATE_SET, drawable);
setBackgroundDrawable(stateList);
}

View File

@ -7,32 +7,27 @@ package org.mozilla.gecko;
import org.mozilla.gecko.widget.GeckoLinearLayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
public class BrowserToolbarBackground extends GeckoLinearLayout {
private GeckoActivity mActivity;
private final LightweightTheme mTheme;
public BrowserToolbarBackground(Context context, AttributeSet attrs) {
super(context, attrs);
mActivity = (GeckoActivity) context;
mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
}
@Override
public void onLightweightThemeChanged() {
Drawable drawable = mActivity.getLightweightTheme().getDrawable(this);
final Drawable drawable = mTheme.getDrawable(this);
if (drawable == null)
return;
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] { R.attr.state_private }, new ColorDrawable(mActivity.getResources().getColor(R.color.background_private)));
stateList.addState(new int[] {}, drawable);
final StateListDrawable stateList = new StateListDrawable();
stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_private));
stateList.addState(EMPTY_STATE_SET, drawable);
setBackgroundDrawable(stateList);
}

View File

@ -5,13 +5,11 @@
package org.mozilla.gecko;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
@ -69,20 +67,18 @@ public class ForwardButton extends ShapedButton {
// The drawable is constructed as per @drawable/url_bar_nav_button.
@Override
public void onLightweightThemeChanged() {
Drawable drawable = mActivity.getLightweightTheme().getDrawable(this);
final Drawable drawable = mTheme.getDrawable(this);
if (drawable == null)
return;
Resources resources = getContext().getResources();
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] { R.attr.state_private, android.R.attr.state_pressed }, new ColorDrawable(resources.getColor(R.color.highlight_nav_pb)));
stateList.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(resources.getColor(R.color.highlight_nav)));
stateList.addState(new int[] { R.attr.state_private, android.R.attr.state_focused }, new ColorDrawable(resources.getColor(R.color.highlight_nav_focused_pb)));
stateList.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(resources.getColor(R.color.highlight_nav_focused)));
stateList.addState(new int[] { R.attr.state_private }, new ColorDrawable(resources.getColor(R.color.background_private)));
stateList.addState(new int[] {}, drawable);
final StateListDrawable stateList = new StateListDrawable();
stateList.addState(PRIVATE_PRESSED_STATE_SET, getColorDrawable(R.color.highlight_nav_pb));
stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_nav));
stateList.addState(PRIVATE_FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused_pb));
stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused));
stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_private));
stateList.addState(EMPTY_STATE_SET, drawable);
setBackgroundDrawable(stateList);
}

View File

@ -93,8 +93,4 @@ public class GeckoActivity extends FragmentActivity implements GeckoActivityStat
MemoryMonitor.getInstance().onTrimMemory(level);
super.onTrimMemory(level);
}
public LightweightTheme getLightweightTheme() {
return ((GeckoApplication) getApplication()).getLightweightTheme();
}
}

View File

@ -7,30 +7,28 @@ package org.mozilla.gecko;
import org.mozilla.gecko.widget.GeckoImageButton;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
public class ShapedButton extends GeckoImageButton
implements CanvasDelegate.DrawManager {
protected GeckoActivity mActivity;
protected final LightweightTheme mTheme;
private Path mPath;
private CurveTowards mSide;
private final Path mPath;
private final CurveTowards mSide;
protected CanvasDelegate mCanvasDelegate;
protected final CanvasDelegate mCanvasDelegate;
private enum CurveTowards { NONE, LEFT, RIGHT };
public ShapedButton(Context context, AttributeSet attrs) {
super(context, attrs);
mActivity = (GeckoActivity) context;
mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BrowserToolbarCurve);
int curveTowards = a.getInt(R.styleable.BrowserToolbarCurve_curveTowards, 0x00);
@ -57,9 +55,9 @@ public class ShapedButton extends GeckoImageButton
if (mSide == CurveTowards.NONE)
return;
int width = getMeasuredWidth();
int height = getMeasuredHeight();
int curve = (int) (height * 1.125f);
final int width = getMeasuredWidth();
final int height = getMeasuredHeight();
final int curve = (int) (height * 1.125f);
mPath.reset();
@ -97,20 +95,19 @@ public class ShapedButton extends GeckoImageButton
// The drawable is constructed as per @drawable/shaped_button.
@Override
public void onLightweightThemeChanged() {
int background = mActivity.getResources().getColor(R.color.background_tabs);
LightweightThemeDrawable lightWeight = mActivity.getLightweightTheme().getColorDrawable(this, background);
final int background = getResources().getColor(R.color.background_tabs);
final LightweightThemeDrawable lightWeight = mTheme.getColorDrawable(this, background);
if (lightWeight == null)
return;
lightWeight.setAlpha(34, 34);
Resources resources = this.getContext().getResources();
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(resources.getColor(R.color.highlight_shaped)));
stateList.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(resources.getColor(R.color.highlight_shaped_focused)));
stateList.addState(new int[] { R.attr.state_private }, new ColorDrawable(resources.getColor(R.color.background_tabs)));
stateList.addState(new int[] {}, lightWeight);
final StateListDrawable stateList = new StateListDrawable();
stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_shaped));
stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_shaped_focused));
stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_tabs));
stateList.addState(EMPTY_STATE_SET, lightWeight);
setBackgroundDrawable(stateList);
}
@ -140,11 +137,6 @@ public class ShapedButton extends GeckoImageButton
@Override
public void setBackgroundResource(int resId) {
if (getBackground() == null || resId == 0) {
super.setBackgroundResource(resId);
return;
}
setBackgroundDrawable(getContext().getResources().getDrawable(resId));
setBackgroundDrawable(getResources().getDrawable(resId));
}
}

View File

@ -47,7 +47,8 @@ public class TabsPanel extends LinearLayout
}
private Context mContext;
private GeckoApp mActivity;
private final GeckoApp mActivity;
private final LightweightTheme mTheme;
private RelativeLayout mHeader;
private TabsListContainer mTabsContainer;
private PanelView mPanel;
@ -68,6 +69,7 @@ public class TabsPanel extends LinearLayout
super(context, attrs);
mContext = context;
mActivity = (GeckoApp) context;
mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
@ -161,19 +163,19 @@ public class TabsPanel extends LinearLayout
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mActivity.getLightweightTheme().addListener(this);
mTheme.addListener(this);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mActivity.getLightweightTheme().removeListener(this);
mTheme.removeListener(this);
}
@Override
public void onLightweightThemeChanged() {
int background = mActivity.getResources().getColor(R.color.background_tabs);
LightweightThemeDrawable drawable = mActivity.getLightweightTheme().getColorDrawable(this, background, true);
final int background = getResources().getColor(R.color.background_tabs);
final LightweightThemeDrawable drawable = mTheme.getColorDrawable(this, background, true);
if (drawable == null)
return;
@ -194,11 +196,8 @@ public class TabsPanel extends LinearLayout
// Tabs List Container holds the ListView
public static class TabsListContainer extends FrameLayout {
private Context mContext;
public TabsListContainer(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public PanelView getCurrentPanelView() {
@ -229,11 +228,11 @@ public class TabsPanel extends LinearLayout
// Tabs Panel Toolbar contains the Buttons
public static class TabsPanelToolbar extends LinearLayout
implements LightweightTheme.OnChangeListener {
private BrowserApp mActivity;
private final LightweightTheme mTheme;
public TabsPanelToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
mActivity = (BrowserApp) context;
mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
(int) context.getResources().getDimension(R.dimen.browser_toolbar_height)));
@ -244,19 +243,19 @@ public class TabsPanel extends LinearLayout
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mActivity.getLightweightTheme().addListener(this);
mTheme.addListener(this);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mActivity.getLightweightTheme().removeListener(this);
mTheme.removeListener(this);
}
@Override
public void onLightweightThemeChanged() {
int background = mActivity.getResources().getColor(R.color.background_tabs);
LightweightThemeDrawable drawable = mActivity.getLightweightTheme().getColorDrawable(this, background);
final int background = getResources().getColor(R.color.background_tabs);
final LightweightThemeDrawable drawable = mTheme.getColorDrawable(this, background);
if (drawable == null)
return;

View File

@ -4,22 +4,28 @@
package org.mozilla.gecko.widget;
import org.mozilla.gecko.GeckoActivity;
import org.mozilla.gecko.GeckoApplication;
import org.mozilla.gecko.LightweightTheme;
import org.mozilla.gecko.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.widget.@VIEWTYPE@;
public class Gecko@VIEWTYPE@ extends @VIEWTYPE@
implements LightweightTheme.OnChangeListener {
private GeckoActivity mActivity;
private final LightweightTheme mTheme;
private static final int[] STATE_PRIVATE_MODE = { R.attr.state_private };
private static final int[] STATE_LIGHT = { R.attr.state_light };
private static final int[] STATE_DARK = { R.attr.state_dark };
protected static final int[] PRIVATE_PRESSED_STATE_SET = { R.attr.state_private, android.R.attr.state_pressed };
protected static final int[] PRIVATE_FOCUSED_STATE_SET = { R.attr.state_private, android.R.attr.state_focused };
protected static final int[] PRIVATE_STATE_SET = { R.attr.state_private };
private boolean mIsPrivate = false;
private boolean mIsLight = false;
private boolean mIsDark = false;
@ -27,7 +33,7 @@ public class Gecko@VIEWTYPE@ extends @VIEWTYPE@
public Gecko@VIEWTYPE@(Context context, AttributeSet attrs) {
super(context, attrs);
mActivity = (GeckoActivity) context;
mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LightweightTheme);
mAutoUpdateTheme = a.getBoolean(R.styleable.LightweightTheme_autoUpdateTheme, true);
@ -39,7 +45,7 @@ public class Gecko@VIEWTYPE@ extends @VIEWTYPE@
super.onAttachedToWindow();
if (mAutoUpdateTheme)
mActivity.getLightweightTheme().addListener(this);
mTheme.addListener(this);
}
@Override
@ -47,7 +53,7 @@ public class Gecko@VIEWTYPE@ extends @VIEWTYPE@
super.onDetachedFromWindow();
if (mAutoUpdateTheme)
mActivity.getLightweightTheme().removeListener(this);
mTheme.removeListener(this);
}
@Override
@ -66,8 +72,8 @@ public class Gecko@VIEWTYPE@ extends @VIEWTYPE@
@Override
public void onLightweightThemeChanged() {
if (mAutoUpdateTheme && mActivity.getLightweightTheme().isEnabled())
setTheme(mActivity.getLightweightTheme().isLightTheme());
if (mAutoUpdateTheme && mTheme.isEnabled())
setTheme(mTheme.isLightTheme());
}
@Override
@ -122,9 +128,13 @@ public class Gecko@VIEWTYPE@ extends @VIEWTYPE@
mAutoUpdateTheme = autoUpdateTheme;
if (mAutoUpdateTheme)
mActivity.getLightweightTheme().addListener(this);
mTheme.addListener(this);
else
mActivity.getLightweightTheme().removeListener(this);
mTheme.removeListener(this);
}
}
public ColorDrawable getColorDrawable(int id) {
return new ColorDrawable(getResources().getColor(id));
}
}