mobile/android/base/menu/MenuItemDefault.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.menu;
     7 import org.mozilla.gecko.R;
     9 import android.content.Context;
    10 import android.content.res.Resources;
    11 import android.graphics.Rect;
    12 import android.graphics.drawable.Drawable;
    13 import android.util.AttributeSet;
    14 import android.widget.TextView;
    16 public class MenuItemDefault extends TextView
    17                              implements GeckoMenuItem.Layout {
    18     private static final int[] STATE_MORE = new int[] { R.attr.state_more };
    19     private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checkable, android.R.attr.state_checked };
    20     private static final int[] STATE_UNCHECKED = new int[] { android.R.attr.state_checkable };
    22     private Drawable mIcon;
    23     private Drawable mState;
    24     private static Rect sIconBounds;
    26     private boolean mCheckable = false;
    27     private boolean mChecked = false;
    28     private boolean mHasSubMenu = false;
    29     private boolean mShowIcon = false;
    31     public MenuItemDefault(Context context) {
    32         this(context, null);
    33     }
    35     public MenuItemDefault(Context context, AttributeSet attrs) {
    36         this(context, attrs, R.attr.menuItemDefaultStyle);
    37     }
    39     public MenuItemDefault(Context context, AttributeSet attrs, int defStyle) {
    40         super(context, attrs, defStyle);
    42         Resources res = getResources();
    43         int width = res.getDimensionPixelSize(R.dimen.menu_item_row_width);
    44         int height = res.getDimensionPixelSize(R.dimen.menu_item_row_height);
    45         setMinimumWidth(width);
    46         setMinimumHeight(height);
    48         int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon);
    49         Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize);
    51         mState = res.getDrawable(R.drawable.menu_item_state).mutate();
    52         mState.setBounds(stateIconBounds);
    54         if (sIconBounds == null) {
    55             int iconSize = res.getDimensionPixelSize(R.dimen.menu_item_icon);
    56             sIconBounds = new Rect(0, 0, iconSize, iconSize);
    57         }
    59         setCompoundDrawables(mIcon, null, mState, null);
    60     }
    62     @Override
    63     public int[] onCreateDrawableState(int extraSpace) {
    64         final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
    66         if (mHasSubMenu)
    67             mergeDrawableStates(drawableState, STATE_MORE);
    68         else if (mCheckable && mChecked)
    69             mergeDrawableStates(drawableState, STATE_CHECKED);
    70         else if (mCheckable && !mChecked)
    71             mergeDrawableStates(drawableState, STATE_UNCHECKED);
    73         return drawableState;
    74     }
    76     @Override
    77     public void initialize(GeckoMenuItem item) {
    78         if (item == null)
    79             return;
    81         setTitle(item.getTitle());        
    82         setIcon(item.getIcon());
    83         setEnabled(item.isEnabled());
    84         setCheckable(item.isCheckable());
    85         setChecked(item.isChecked());
    86         setSubMenuIndicator(item.hasSubMenu());
    87     }
    89     private void refreshIcon() {
    90         setCompoundDrawables(mShowIcon ? mIcon : null, null, mState, null);
    91     }
    93     void setIcon(Drawable icon) {
    94         mIcon = icon;
    96         if (mIcon != null) {
    97             mIcon.setBounds(sIconBounds);
    98             mIcon.setAlpha(isEnabled() ? 255 : 99);
    99         }
   101         refreshIcon();
   102     }
   104     void setIcon(int icon) {
   105         setIcon((icon == 0) ? null : getResources().getDrawable(icon));
   106     }
   108     void setTitle(CharSequence title) {
   109         setText(title);
   110     }
   112     @Override
   113     public void setEnabled(boolean enabled) {
   114         super.setEnabled(enabled);
   116         if (mIcon != null)
   117             mIcon.setAlpha(enabled ? 255 : 99);
   119         if (mState != null)
   120             mState.setAlpha(enabled ? 255 : 99);
   121     }
   123     private void setCheckable(boolean checkable) {
   124         if (mCheckable != checkable) {
   125             mCheckable = checkable;
   126             refreshDrawableState();
   127         }
   128     }
   130     private void setChecked(boolean checked) {
   131         if (mChecked != checked) {
   132             mChecked = checked;
   133             refreshDrawableState();
   134         }
   135     }
   137     @Override
   138     public void setShowIcon(boolean show) {
   139         if (mShowIcon != show) {
   140             mShowIcon = show;
   141             refreshIcon();
   142         }
   143     }
   145     void setSubMenuIndicator(boolean hasSubMenu) {
   146         if (mHasSubMenu != hasSubMenu) {
   147             mHasSubMenu = hasSubMenu;
   148             refreshDrawableState();
   149         }
   150     }
   151 }

mercurial