2012-05-31 23:01:50 +00: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-07-28 06:13:34 +00:00
|
|
|
import android.content.res.Resources;
|
2013-02-21 07:07:05 +00:00
|
|
|
import android.graphics.Rect;
|
2012-05-31 23:01:50 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
2012-09-20 18:04:50 +00:00
|
|
|
import android.widget.AbsListView;
|
|
|
|
import android.widget.CheckBox;
|
2012-07-28 00:53:54 +00:00
|
|
|
import android.widget.ImageView;
|
2012-05-31 23:01:50 +00:00
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2012-07-18 00:54:54 +00:00
|
|
|
public class MenuItemDefault extends LinearLayout
|
|
|
|
implements GeckoMenuItem.Layout {
|
2013-02-21 07:07:05 +00:00
|
|
|
private static Rect sIconBounds;
|
2013-02-21 07:07:06 +00:00
|
|
|
private static Drawable sChecked;
|
|
|
|
private static Drawable sUnChecked;
|
|
|
|
private static Drawable sMore;
|
2012-05-31 23:01:50 +00:00
|
|
|
|
|
|
|
private TextView mTitle;
|
2012-09-20 18:04:50 +00:00
|
|
|
|
2013-02-21 07:07:05 +00:00
|
|
|
private Drawable mIcon;
|
2013-02-21 07:07:06 +00:00
|
|
|
private Drawable mState;
|
|
|
|
|
2012-09-20 18:04:50 +00:00
|
|
|
private boolean mCheckable;
|
|
|
|
private boolean mChecked;
|
2012-09-27 18:04:02 +00:00
|
|
|
private boolean mHasSubMenu;
|
2012-05-31 23:01:50 +00:00
|
|
|
|
|
|
|
public MenuItemDefault(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
2012-07-28 06:13:34 +00:00
|
|
|
Resources res = context.getResources();
|
2012-09-19 17:32:01 +00:00
|
|
|
setLayoutParams(new AbsListView.LayoutParams((int) (res.getDimension(R.dimen.menu_item_row_width)),
|
|
|
|
(int) (res.getDimension(R.dimen.menu_item_row_height))));
|
2012-05-31 23:01:50 +00:00
|
|
|
|
|
|
|
inflate(context, R.layout.menu_item, this);
|
|
|
|
mTitle = (TextView) findViewById(R.id.title);
|
2012-09-20 18:04:50 +00:00
|
|
|
|
|
|
|
mCheckable = false;
|
|
|
|
mChecked = false;
|
2012-09-27 18:04:02 +00:00
|
|
|
mHasSubMenu = false;
|
2013-02-21 07:07:05 +00:00
|
|
|
|
|
|
|
if (sIconBounds == null) {
|
|
|
|
int iconSize = res.getDimensionPixelSize(R.dimen.menu_item_icon);
|
|
|
|
sIconBounds = new Rect(0, 0, iconSize, iconSize);
|
2013-02-21 07:07:06 +00:00
|
|
|
|
|
|
|
int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon);
|
|
|
|
Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize);
|
|
|
|
|
|
|
|
sChecked = res.getDrawable(R.drawable.menu_item_check);
|
|
|
|
sChecked.setBounds(stateIconBounds);
|
|
|
|
|
|
|
|
sUnChecked = res.getDrawable(R.drawable.menu_item_uncheck);
|
|
|
|
sUnChecked.setBounds(stateIconBounds);
|
|
|
|
|
|
|
|
sMore = res.getDrawable(R.drawable.menu_item_more);
|
|
|
|
sMore.setBounds(stateIconBounds);
|
2013-02-21 07:07:05 +00:00
|
|
|
}
|
2012-05-31 23:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getLayout() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setIcon(Drawable icon) {
|
2013-02-21 07:07:05 +00:00
|
|
|
mIcon = icon;
|
|
|
|
|
|
|
|
if (mIcon != null)
|
|
|
|
mIcon.setBounds(sIconBounds);
|
|
|
|
|
2013-02-21 07:07:06 +00:00
|
|
|
mTitle.setCompoundDrawables(mIcon, null, mState, null);
|
2012-05-31 23:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setIcon(int icon) {
|
2013-02-21 07:07:05 +00:00
|
|
|
Drawable drawable = null;
|
|
|
|
|
|
|
|
if (icon != 0)
|
|
|
|
drawable = getContext().getResources().getDrawable(icon);
|
|
|
|
|
|
|
|
setIcon(drawable);
|
2012-05-31 23:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTitle(CharSequence title) {
|
|
|
|
mTitle.setText(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
super.setEnabled(enabled);
|
|
|
|
mTitle.setEnabled(enabled);
|
2013-02-21 07:07:05 +00:00
|
|
|
|
|
|
|
if (mIcon != null)
|
|
|
|
mIcon.setAlpha(enabled ? 255 : 99);
|
|
|
|
|
2013-02-21 07:07:06 +00:00
|
|
|
if (mState != null)
|
|
|
|
mState.setAlpha(enabled ? 255 : 99);
|
2012-05-31 23:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setCheckable(boolean checkable) {
|
2012-09-20 18:04:50 +00:00
|
|
|
mCheckable = checkable;
|
2013-02-21 07:07:06 +00:00
|
|
|
refreshState();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void refreshState() {
|
|
|
|
if (mHasSubMenu)
|
|
|
|
mState = sMore;
|
|
|
|
else if (mCheckable && mChecked)
|
|
|
|
mState = sChecked;
|
|
|
|
else if (mCheckable && !mChecked)
|
|
|
|
mState = sUnChecked;
|
|
|
|
else
|
|
|
|
mState = null;
|
|
|
|
|
|
|
|
mTitle.setCompoundDrawables(mIcon, null, mState, null);
|
2012-05-31 23:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setChecked(boolean checked) {
|
2012-09-20 18:04:50 +00:00
|
|
|
mChecked = checked;
|
2013-02-21 07:07:06 +00:00
|
|
|
refreshState();
|
2012-05-31 23:01:50 +00:00
|
|
|
}
|
2012-09-27 18:04:02 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSubMenuIndicator(boolean hasSubMenu) {
|
|
|
|
mHasSubMenu = hasSubMenu;
|
2013-02-21 07:07:06 +00:00
|
|
|
mState = sMore;
|
|
|
|
refreshState();
|
2012-09-27 18:04:02 +00:00
|
|
|
}
|
2012-05-31 23:01:50 +00:00
|
|
|
}
|