2012-05-31 16:01: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/. */
|
|
|
|
|
2013-05-15 10:41:25 -07:00
|
|
|
package org.mozilla.gecko.menu;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.R;
|
2012-05-31 16:01:50 -07:00
|
|
|
|
|
|
|
import android.content.Context;
|
2012-07-27 23:13:34 -07:00
|
|
|
import android.content.res.Resources;
|
2013-02-20 23:07:05 -08:00
|
|
|
import android.graphics.Rect;
|
2012-05-31 16:01:50 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2013-02-20 23:07:06 -08:00
|
|
|
public class MenuItemDefault extends TextView
|
2012-07-17 17:54:54 -07:00
|
|
|
implements GeckoMenuItem.Layout {
|
2013-02-20 23:07:07 -08:00
|
|
|
private static final int[] STATE_MORE = new int[] { R.attr.state_more };
|
|
|
|
private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checkable, android.R.attr.state_checked };
|
|
|
|
private static final int[] STATE_UNCHECKED = new int[] { android.R.attr.state_checkable };
|
2012-05-31 16:01:50 -07:00
|
|
|
|
2013-02-20 23:07:05 -08:00
|
|
|
private Drawable mIcon;
|
2014-10-10 16:17:01 -07:00
|
|
|
private final Drawable mState;
|
2013-02-20 23:07:07 -08:00
|
|
|
private static Rect sIconBounds;
|
2013-02-20 23:07:06 -08:00
|
|
|
|
2014-07-25 20:14:47 -07:00
|
|
|
private boolean mCheckable;
|
|
|
|
private boolean mChecked;
|
|
|
|
private boolean mHasSubMenu;
|
|
|
|
private boolean mShowIcon;
|
2012-05-31 16:01:50 -07:00
|
|
|
|
2013-05-15 10:38:17 -07:00
|
|
|
public MenuItemDefault(Context context) {
|
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
2012-05-31 16:01:50 -07:00
|
|
|
public MenuItemDefault(Context context, AttributeSet attrs) {
|
2013-06-05 15:28:02 -07:00
|
|
|
this(context, attrs, R.attr.menuItemDefaultStyle);
|
2013-05-15 10:38:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public MenuItemDefault(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
2012-05-31 16:01:50 -07:00
|
|
|
|
2013-06-05 15:28:02 -07:00
|
|
|
Resources res = getResources();
|
2013-05-15 10:38:17 -07:00
|
|
|
int width = res.getDimensionPixelSize(R.dimen.menu_item_row_width);
|
|
|
|
int height = res.getDimensionPixelSize(R.dimen.menu_item_row_height);
|
|
|
|
setMinimumWidth(width);
|
|
|
|
setMinimumHeight(height);
|
|
|
|
|
2013-02-20 23:07:07 -08:00
|
|
|
int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon);
|
|
|
|
Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize);
|
|
|
|
|
2014-02-24 21:05:49 -08:00
|
|
|
mState = res.getDrawable(R.drawable.menu_item_state).mutate();
|
2013-02-20 23:07:07 -08:00
|
|
|
mState.setBounds(stateIconBounds);
|
|
|
|
|
2013-02-20 23:07:05 -08:00
|
|
|
if (sIconBounds == null) {
|
|
|
|
int iconSize = res.getDimensionPixelSize(R.dimen.menu_item_icon);
|
|
|
|
sIconBounds = new Rect(0, 0, iconSize, iconSize);
|
2013-02-20 23:07:07 -08:00
|
|
|
}
|
2013-02-20 23:07:06 -08:00
|
|
|
|
2013-02-20 23:07:07 -08:00
|
|
|
setCompoundDrawables(mIcon, null, mState, null);
|
|
|
|
}
|
2013-02-20 23:07:06 -08:00
|
|
|
|
2013-02-20 23:07:07 -08:00
|
|
|
@Override
|
|
|
|
public int[] onCreateDrawableState(int extraSpace) {
|
|
|
|
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
|
2013-02-20 23:07:06 -08:00
|
|
|
|
2013-02-20 23:07:07 -08:00
|
|
|
if (mHasSubMenu)
|
|
|
|
mergeDrawableStates(drawableState, STATE_MORE);
|
|
|
|
else if (mCheckable && mChecked)
|
|
|
|
mergeDrawableStates(drawableState, STATE_CHECKED);
|
|
|
|
else if (mCheckable && !mChecked)
|
|
|
|
mergeDrawableStates(drawableState, STATE_UNCHECKED);
|
2013-02-20 23:07:06 -08:00
|
|
|
|
2013-02-20 23:07:07 -08:00
|
|
|
return drawableState;
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
2013-02-21 15:02:22 -08:00
|
|
|
@Override
|
2013-06-05 15:28:02 -07:00
|
|
|
public void initialize(GeckoMenuItem item) {
|
|
|
|
if (item == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
setTitle(item.getTitle());
|
|
|
|
setIcon(item.getIcon());
|
|
|
|
setEnabled(item.isEnabled());
|
|
|
|
setCheckable(item.isCheckable());
|
|
|
|
setChecked(item.isChecked());
|
|
|
|
setSubMenuIndicator(item.hasSubMenu());
|
2013-02-21 15:02:22 -08:00
|
|
|
}
|
|
|
|
|
2013-12-18 18:49:53 -08:00
|
|
|
private void refreshIcon() {
|
|
|
|
setCompoundDrawables(mShowIcon ? mIcon : null, null, mState, null);
|
|
|
|
}
|
|
|
|
|
2013-06-05 15:28:02 -07:00
|
|
|
void setIcon(Drawable icon) {
|
2013-02-20 23:07:05 -08:00
|
|
|
mIcon = icon;
|
|
|
|
|
2013-06-05 15:28:02 -07:00
|
|
|
if (mIcon != null) {
|
2013-02-20 23:07:05 -08:00
|
|
|
mIcon.setBounds(sIconBounds);
|
2013-06-05 15:28:02 -07:00
|
|
|
mIcon.setAlpha(isEnabled() ? 255 : 99);
|
|
|
|
}
|
2013-02-20 23:07:05 -08:00
|
|
|
|
2013-12-18 18:49:53 -08:00
|
|
|
refreshIcon();
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 15:28:02 -07:00
|
|
|
void setIcon(int icon) {
|
2013-12-20 14:51:53 -08:00
|
|
|
setIcon((icon == 0) ? null : getResources().getDrawable(icon));
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 15:28:02 -07:00
|
|
|
void setTitle(CharSequence title) {
|
2013-02-20 23:07:06 -08:00
|
|
|
setText(title);
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
super.setEnabled(enabled);
|
2013-02-20 23:07:05 -08:00
|
|
|
|
|
|
|
if (mIcon != null)
|
|
|
|
mIcon.setAlpha(enabled ? 255 : 99);
|
|
|
|
|
2013-02-20 23:07:06 -08:00
|
|
|
if (mState != null)
|
|
|
|
mState.setAlpha(enabled ? 255 : 99);
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 15:28:02 -07:00
|
|
|
private void setCheckable(boolean checkable) {
|
2013-02-20 23:07:07 -08:00
|
|
|
if (mCheckable != checkable) {
|
|
|
|
mCheckable = checkable;
|
|
|
|
refreshDrawableState();
|
|
|
|
}
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 15:28:02 -07:00
|
|
|
private void setChecked(boolean checked) {
|
2013-02-20 23:07:07 -08:00
|
|
|
if (mChecked != checked) {
|
|
|
|
mChecked = checked;
|
|
|
|
refreshDrawableState();
|
|
|
|
}
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
2012-09-27 11:04:02 -07:00
|
|
|
|
2013-12-18 18:49:53 -08:00
|
|
|
@Override
|
|
|
|
public void setShowIcon(boolean show) {
|
|
|
|
if (mShowIcon != show) {
|
|
|
|
mShowIcon = show;
|
|
|
|
refreshIcon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:41:10 -07:00
|
|
|
void setSubMenuIndicator(boolean hasSubMenu) {
|
2013-02-20 23:07:07 -08:00
|
|
|
if (mHasSubMenu != hasSubMenu) {
|
|
|
|
mHasSubMenu = hasSubMenu;
|
|
|
|
refreshDrawableState();
|
|
|
|
}
|
2012-09-27 11:04:02 -07:00
|
|
|
}
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|