Commit 0ae49c6c authored by Thomas Krex's avatar Thomas Krex

Warnungen im sherlock unterdrückt

parent a696b275
......@@ -16,8 +16,6 @@
package com.actionbarsherlock.internal.view.menu;
import com.actionbarsherlock.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
......@@ -32,247 +30,251 @@ import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.actionbarsherlock.R;
/**
* The item view for each item in the ListView-based MenuViews.
*/
public class ListMenuItemView extends LinearLayout implements MenuView.ItemView {
private MenuItemImpl mItemData;
private ImageView mIconView;
private RadioButton mRadioButton;
private TextView mTitleView;
private CheckBox mCheckBox;
private TextView mShortcutView;
private Drawable mBackground;
private int mTextAppearance;
private Context mTextAppearanceContext;
private boolean mPreserveIconSpacing;
//UNUSED private int mMenuType;
private LayoutInflater mInflater;
private boolean mForceShowIcon;
private MenuItemImpl mItemData;
final Context mContext;
public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
mContext = context;
TypedArray a =
context.obtainStyledAttributes(
attrs, R.styleable.SherlockMenuView, defStyle, 0);
mBackground = a.getDrawable(R.styleable.SherlockMenuView_itemBackground);
mTextAppearance = a.getResourceId(R.styleable.
SherlockMenuView_itemTextAppearance, -1);
mPreserveIconSpacing = a.getBoolean(
R.styleable.SherlockMenuView_preserveIconSpacing, false);
mTextAppearanceContext = context;
a.recycle();
}
public ListMenuItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
setBackgroundDrawable(mBackground);
mTitleView = (TextView) findViewById(R.id.abs__title);
if (mTextAppearance != -1) {
mTitleView.setTextAppearance(mTextAppearanceContext,
mTextAppearance);
}
mShortcutView = (TextView) findViewById(R.id.abs__shortcut);
}
public void initialize(MenuItemImpl itemData, int menuType) {
mItemData = itemData;
//UNUSED mMenuType = menuType;
setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
setTitle(itemData.getTitleForItemView(this));
setCheckable(itemData.isCheckable());
setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
setIcon(itemData.getIcon());
setEnabled(itemData.isEnabled());
}
public void setForceShowIcon(boolean forceShow) {
mPreserveIconSpacing = mForceShowIcon = forceShow;
}
public void setTitle(CharSequence title) {
if (title != null) {
mTitleView.setText(title);
if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE);
} else {
if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE);
}
}
public MenuItemImpl getItemData() {
return mItemData;
}
public void setCheckable(boolean checkable) {
if (!checkable && mRadioButton == null && mCheckBox == null) {
return;
}
if (mRadioButton == null) {
insertRadioButton();
}
if (mCheckBox == null) {
insertCheckBox();
}
// Depending on whether its exclusive check or not, the checkbox or
// radio button will be the one in use (and the other will be otherCompoundButton)
final CompoundButton compoundButton;
final CompoundButton otherCompoundButton;
if (mItemData.isExclusiveCheckable()) {
compoundButton = mRadioButton;
otherCompoundButton = mCheckBox;
} else {
compoundButton = mCheckBox;
otherCompoundButton = mRadioButton;
}
if (checkable) {
compoundButton.setChecked(mItemData.isChecked());
final int newVisibility = checkable ? VISIBLE : GONE;
if (compoundButton.getVisibility() != newVisibility) {
compoundButton.setVisibility(newVisibility);
}
// Make sure the other compound button isn't visible
if (otherCompoundButton.getVisibility() != GONE) {
otherCompoundButton.setVisibility(GONE);
}
} else {
mCheckBox.setVisibility(GONE);
mRadioButton.setVisibility(GONE);
}
}
public void setChecked(boolean checked) {
CompoundButton compoundButton;
if (mItemData.isExclusiveCheckable()) {
if (mRadioButton == null) {
insertRadioButton();
}
compoundButton = mRadioButton;
} else {
if (mCheckBox == null) {
insertCheckBox();
}
compoundButton = mCheckBox;
}
compoundButton.setChecked(checked);
}
public void setShortcut(boolean showShortcut, char shortcutKey) {
final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
? VISIBLE : GONE;
if (newVisibility == VISIBLE) {
mShortcutView.setText(mItemData.getShortcutLabel());
}
if (mShortcutView.getVisibility() != newVisibility) {
mShortcutView.setVisibility(newVisibility);
}
}
public void setIcon(Drawable icon) {
final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
if (!showIcon && !mPreserveIconSpacing) {
return;
}
if (mIconView == null && icon == null && !mPreserveIconSpacing) {
return;
}
if (mIconView == null) {
insertIconView();
}
if (icon != null || mPreserveIconSpacing) {
mIconView.setImageDrawable(showIcon ? icon : null);
if (mIconView.getVisibility() != VISIBLE) {
mIconView.setVisibility(VISIBLE);
}
} else {
mIconView.setVisibility(GONE);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mIconView != null && mPreserveIconSpacing) {
// Enforce minimum icon spacing
ViewGroup.LayoutParams lp = getLayoutParams();
LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
if (lp.height > 0 && iconLp.width <= 0) {
iconLp.width = lp.height;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void insertIconView() {
LayoutInflater inflater = getInflater();
mIconView = (ImageView) inflater.inflate(R.layout.abs__list_menu_item_icon,
this, false);
addView(mIconView, 0);
}
private void insertRadioButton() {
LayoutInflater inflater = getInflater();
mRadioButton =
(RadioButton) inflater.inflate(R.layout.abs__list_menu_item_radio,
this, false);
addView(mRadioButton);
}
private void insertCheckBox() {
LayoutInflater inflater = getInflater();
mCheckBox =
(CheckBox) inflater.inflate(R.layout.abs__list_menu_item_checkbox,
this, false);
addView(mCheckBox);
}
public boolean prefersCondensedTitle() {
return false;
}
public boolean showsIcon() {
return mForceShowIcon;
}
private LayoutInflater getInflater() {
if (mInflater == null) {
mInflater = LayoutInflater.from(mContext);
}
return mInflater;
}
private ImageView mIconView;
private RadioButton mRadioButton;
private TextView mTitleView;
private CheckBox mCheckBox;
private TextView mShortcutView;
private Drawable mBackground;
private int mTextAppearance;
private Context mTextAppearanceContext;
private boolean mPreserveIconSpacing;
// UNUSED private int mMenuType;
private LayoutInflater mInflater;
private boolean mForceShowIcon;
final Context mContext;
public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
mContext = context;
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SherlockMenuView, defStyle, 0);
mBackground = a
.getDrawable(R.styleable.SherlockMenuView_itemBackground);
mTextAppearance = a.getResourceId(
R.styleable.SherlockMenuView_itemTextAppearance, -1);
mPreserveIconSpacing = a.getBoolean(
R.styleable.SherlockMenuView_preserveIconSpacing, false);
mTextAppearanceContext = context;
a.recycle();
}
public ListMenuItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@SuppressWarnings("deprecation")
@Override
protected void onFinishInflate() {
super.onFinishInflate();
setBackgroundDrawable(mBackground);
mTitleView = (TextView) findViewById(R.id.abs__title);
if (mTextAppearance != -1) {
mTitleView.setTextAppearance(mTextAppearanceContext,
mTextAppearance);
}
mShortcutView = (TextView) findViewById(R.id.abs__shortcut);
}
public void initialize(MenuItemImpl itemData, int menuType) {
mItemData = itemData;
// UNUSED mMenuType = menuType;
setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
setTitle(itemData.getTitleForItemView(this));
setCheckable(itemData.isCheckable());
setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
setIcon(itemData.getIcon());
setEnabled(itemData.isEnabled());
}
public void setForceShowIcon(boolean forceShow) {
mPreserveIconSpacing = mForceShowIcon = forceShow;
}
public void setTitle(CharSequence title) {
if (title != null) {
mTitleView.setText(title);
if (mTitleView.getVisibility() != VISIBLE)
mTitleView.setVisibility(VISIBLE);
} else {
if (mTitleView.getVisibility() != GONE)
mTitleView.setVisibility(GONE);
}
}
public MenuItemImpl getItemData() {
return mItemData;
}
public void setCheckable(boolean checkable) {
if (!checkable && mRadioButton == null && mCheckBox == null) {
return;
}
if (mRadioButton == null) {
insertRadioButton();
}
if (mCheckBox == null) {
insertCheckBox();
}
// Depending on whether its exclusive check or not, the checkbox or
// radio button will be the one in use (and the other will be
// otherCompoundButton)
final CompoundButton compoundButton;
final CompoundButton otherCompoundButton;
if (mItemData.isExclusiveCheckable()) {
compoundButton = mRadioButton;
otherCompoundButton = mCheckBox;
} else {
compoundButton = mCheckBox;
otherCompoundButton = mRadioButton;
}
if (checkable) {
compoundButton.setChecked(mItemData.isChecked());
final int newVisibility = checkable ? VISIBLE : GONE;
if (compoundButton.getVisibility() != newVisibility) {
compoundButton.setVisibility(newVisibility);
}
// Make sure the other compound button isn't visible
if (otherCompoundButton.getVisibility() != GONE) {
otherCompoundButton.setVisibility(GONE);
}
} else {
mCheckBox.setVisibility(GONE);
mRadioButton.setVisibility(GONE);
}
}
public void setChecked(boolean checked) {
CompoundButton compoundButton;
if (mItemData.isExclusiveCheckable()) {
if (mRadioButton == null) {
insertRadioButton();
}
compoundButton = mRadioButton;
} else {
if (mCheckBox == null) {
insertCheckBox();
}
compoundButton = mCheckBox;
}
compoundButton.setChecked(checked);
}
public void setShortcut(boolean showShortcut, char shortcutKey) {
final int newVisibility = (showShortcut && mItemData
.shouldShowShortcut()) ? VISIBLE : GONE;
if (newVisibility == VISIBLE) {
mShortcutView.setText(mItemData.getShortcutLabel());
}
if (mShortcutView.getVisibility() != newVisibility) {
mShortcutView.setVisibility(newVisibility);
}
}
public void setIcon(Drawable icon) {
final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
if (!showIcon && !mPreserveIconSpacing) {
return;
}
if (mIconView == null && icon == null && !mPreserveIconSpacing) {
return;
}
if (mIconView == null) {
insertIconView();
}
if (icon != null || mPreserveIconSpacing) {
mIconView.setImageDrawable(showIcon ? icon : null);
if (mIconView.getVisibility() != VISIBLE) {
mIconView.setVisibility(VISIBLE);
}
} else {
mIconView.setVisibility(GONE);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mIconView != null && mPreserveIconSpacing) {
// Enforce minimum icon spacing
ViewGroup.LayoutParams lp = getLayoutParams();
LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
if (lp.height > 0 && iconLp.width <= 0) {
iconLp.width = lp.height;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void insertIconView() {
LayoutInflater inflater = getInflater();
mIconView = (ImageView) inflater.inflate(
R.layout.abs__list_menu_item_icon, this, false);
addView(mIconView, 0);
}
private void insertRadioButton() {
LayoutInflater inflater = getInflater();
mRadioButton = (RadioButton) inflater.inflate(
R.layout.abs__list_menu_item_radio, this, false);
addView(mRadioButton);
}
private void insertCheckBox() {
LayoutInflater inflater = getInflater();
mCheckBox = (CheckBox) inflater.inflate(
R.layout.abs__list_menu_item_checkbox, this, false);
addView(mCheckBox);
}
public boolean prefersCondensedTitle() {
return false;
}
public boolean showsIcon() {
return mForceShowIcon;
}
private LayoutInflater getInflater() {
if (mInflater == null) {
mInflater = LayoutInflater.from(mContext);
}
return mInflater;
}
}
......@@ -17,6 +17,7 @@
package com.actionbarsherlock.internal.view.menu;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.database.DataSetObserver;
......@@ -32,6 +33,7 @@ import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import com.actionbarsherlock.R;
import com.actionbarsherlock.internal.view.View_HasStateListenerSupport;
import com.actionbarsherlock.internal.view.View_OnAttachStateChangeListener;
......@@ -40,348 +42,368 @@ import com.actionbarsherlock.view.MenuItem;
/**
* Presents a menu as a small, simple popup anchored to another view.
*
* @hide
*/
public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener,
ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener,
View_OnAttachStateChangeListener, MenuPresenter {
//UNUSED private static final String TAG = "MenuPopupHelper";
static final int ITEM_LAYOUT = R.layout.abs__popup_menu_item_layout;
private Context mContext;
private LayoutInflater mInflater;
private IcsListPopupWindow mPopup;
private MenuBuilder mMenu;
private int mPopupMaxWidth;
private View mAnchorView;
private boolean mOverflowOnly;
private ViewTreeObserver mTreeObserver;
private MenuAdapter mAdapter;
private Callback mPresenterCallback;
boolean mForceShowIcon;
private ViewGroup mMeasureParent;
public MenuPopupHelper(Context context, MenuBuilder menu) {
this(context, menu, null, false);
}
public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
this(context, menu, anchorView, false);
}
public MenuPopupHelper(Context context, MenuBuilder menu,
View anchorView, boolean overflowOnly) {
mContext = context;
mInflater = LayoutInflater.from(context);
mMenu = menu;
mOverflowOnly = overflowOnly;
final Resources res = context.getResources();
mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
res.getDimensionPixelSize(R.dimen.abs__config_prefDialogWidth));
mAnchorView = anchorView;
menu.addMenuPresenter(this);
}
public void setAnchorView(View anchor) {
mAnchorView = anchor;
}
public void setForceShowIcon(boolean forceShow) {
mForceShowIcon = forceShow;
}
public void show() {
if (!tryShow()) {
throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
}
}
public boolean tryShow() {
mPopup = new IcsListPopupWindow(mContext, null, R.attr.popupMenuStyle);
mPopup.setOnDismissListener(this);
mPopup.setOnItemClickListener(this);
mAdapter = new MenuAdapter(mMenu);
mPopup.setAdapter(mAdapter);
mPopup.setModal(true);
View anchor = mAnchorView;
if (anchor != null) {
// Don't attach to the VTO unless the anchor itself is attached to avoid VTO-related leaks.
if (anchor.getWindowToken() != null) {
ViewTreeObserver vto = anchor.getViewTreeObserver();
if (vto != mTreeObserver) {
if (mTreeObserver != null && mTreeObserver.isAlive()) {
mTreeObserver.removeGlobalOnLayoutListener(this);
}
if ((mTreeObserver = vto) != null) {
vto.addOnGlobalLayoutListener(this);
}
}
} else if (anchor instanceof View_HasStateListenerSupport) {
((View_HasStateListenerSupport) anchor).addOnAttachStateChangeListener(this);
}
mPopup.setAnchorView(anchor);
} else {
return false;
}
mPopup.setContentWidth(Math.min(measureContentWidth(mAdapter), mPopupMaxWidth));
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopup.show();
mPopup.getListView().setOnKeyListener(this);
return true;
}
public void dismiss() {
if (isShowing()) {
mPopup.dismiss();
}
}
public void onDismiss() {
mPopup = null;
mMenu.close();
if (mTreeObserver != null) {
if (mTreeObserver.isAlive()) mTreeObserver.removeGlobalOnLayoutListener(this);
mTreeObserver = null;
} else if (mAnchorView instanceof View_HasStateListenerSupport) {
((View_HasStateListenerSupport) mAnchorView).removeOnAttachStateChangeListener(this);
}
}
public boolean isShowing() {
return mPopup != null && mPopup.isShowing();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MenuAdapter adapter = mAdapter;
adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
dismiss();
return true;
}
return false;
}
private int measureContentWidth(ListAdapter adapter) {
// Menus don't tend to be long, so this is more sane than it looks.
int width = 0;
View itemView = null;
int itemType = 0;
final int widthMeasureSpec =
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec =
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int count = adapter.getCount();
for (int i = 0; i < count; i++) {
final int positionType = adapter.getItemViewType(i);
if (positionType != itemType) {
itemType = positionType;
itemView = null;
}
if (mMeasureParent == null) {
mMeasureParent = new FrameLayout(mContext);
}
itemView = adapter.getView(i, itemView, mMeasureParent);
itemView.measure(widthMeasureSpec, heightMeasureSpec);