1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/menu/MenuItemDefault.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.menu; 1.9 + 1.10 +import org.mozilla.gecko.R; 1.11 + 1.12 +import android.content.Context; 1.13 +import android.content.res.Resources; 1.14 +import android.graphics.Rect; 1.15 +import android.graphics.drawable.Drawable; 1.16 +import android.util.AttributeSet; 1.17 +import android.widget.TextView; 1.18 + 1.19 +public class MenuItemDefault extends TextView 1.20 + implements GeckoMenuItem.Layout { 1.21 + private static final int[] STATE_MORE = new int[] { R.attr.state_more }; 1.22 + private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checkable, android.R.attr.state_checked }; 1.23 + private static final int[] STATE_UNCHECKED = new int[] { android.R.attr.state_checkable }; 1.24 + 1.25 + private Drawable mIcon; 1.26 + private Drawable mState; 1.27 + private static Rect sIconBounds; 1.28 + 1.29 + private boolean mCheckable = false; 1.30 + private boolean mChecked = false; 1.31 + private boolean mHasSubMenu = false; 1.32 + private boolean mShowIcon = false; 1.33 + 1.34 + public MenuItemDefault(Context context) { 1.35 + this(context, null); 1.36 + } 1.37 + 1.38 + public MenuItemDefault(Context context, AttributeSet attrs) { 1.39 + this(context, attrs, R.attr.menuItemDefaultStyle); 1.40 + } 1.41 + 1.42 + public MenuItemDefault(Context context, AttributeSet attrs, int defStyle) { 1.43 + super(context, attrs, defStyle); 1.44 + 1.45 + Resources res = getResources(); 1.46 + int width = res.getDimensionPixelSize(R.dimen.menu_item_row_width); 1.47 + int height = res.getDimensionPixelSize(R.dimen.menu_item_row_height); 1.48 + setMinimumWidth(width); 1.49 + setMinimumHeight(height); 1.50 + 1.51 + int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon); 1.52 + Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize); 1.53 + 1.54 + mState = res.getDrawable(R.drawable.menu_item_state).mutate(); 1.55 + mState.setBounds(stateIconBounds); 1.56 + 1.57 + if (sIconBounds == null) { 1.58 + int iconSize = res.getDimensionPixelSize(R.dimen.menu_item_icon); 1.59 + sIconBounds = new Rect(0, 0, iconSize, iconSize); 1.60 + } 1.61 + 1.62 + setCompoundDrawables(mIcon, null, mState, null); 1.63 + } 1.64 + 1.65 + @Override 1.66 + public int[] onCreateDrawableState(int extraSpace) { 1.67 + final int[] drawableState = super.onCreateDrawableState(extraSpace + 2); 1.68 + 1.69 + if (mHasSubMenu) 1.70 + mergeDrawableStates(drawableState, STATE_MORE); 1.71 + else if (mCheckable && mChecked) 1.72 + mergeDrawableStates(drawableState, STATE_CHECKED); 1.73 + else if (mCheckable && !mChecked) 1.74 + mergeDrawableStates(drawableState, STATE_UNCHECKED); 1.75 + 1.76 + return drawableState; 1.77 + } 1.78 + 1.79 + @Override 1.80 + public void initialize(GeckoMenuItem item) { 1.81 + if (item == null) 1.82 + return; 1.83 + 1.84 + setTitle(item.getTitle()); 1.85 + setIcon(item.getIcon()); 1.86 + setEnabled(item.isEnabled()); 1.87 + setCheckable(item.isCheckable()); 1.88 + setChecked(item.isChecked()); 1.89 + setSubMenuIndicator(item.hasSubMenu()); 1.90 + } 1.91 + 1.92 + private void refreshIcon() { 1.93 + setCompoundDrawables(mShowIcon ? mIcon : null, null, mState, null); 1.94 + } 1.95 + 1.96 + void setIcon(Drawable icon) { 1.97 + mIcon = icon; 1.98 + 1.99 + if (mIcon != null) { 1.100 + mIcon.setBounds(sIconBounds); 1.101 + mIcon.setAlpha(isEnabled() ? 255 : 99); 1.102 + } 1.103 + 1.104 + refreshIcon(); 1.105 + } 1.106 + 1.107 + void setIcon(int icon) { 1.108 + setIcon((icon == 0) ? null : getResources().getDrawable(icon)); 1.109 + } 1.110 + 1.111 + void setTitle(CharSequence title) { 1.112 + setText(title); 1.113 + } 1.114 + 1.115 + @Override 1.116 + public void setEnabled(boolean enabled) { 1.117 + super.setEnabled(enabled); 1.118 + 1.119 + if (mIcon != null) 1.120 + mIcon.setAlpha(enabled ? 255 : 99); 1.121 + 1.122 + if (mState != null) 1.123 + mState.setAlpha(enabled ? 255 : 99); 1.124 + } 1.125 + 1.126 + private void setCheckable(boolean checkable) { 1.127 + if (mCheckable != checkable) { 1.128 + mCheckable = checkable; 1.129 + refreshDrawableState(); 1.130 + } 1.131 + } 1.132 + 1.133 + private void setChecked(boolean checked) { 1.134 + if (mChecked != checked) { 1.135 + mChecked = checked; 1.136 + refreshDrawableState(); 1.137 + } 1.138 + } 1.139 + 1.140 + @Override 1.141 + public void setShowIcon(boolean show) { 1.142 + if (mShowIcon != show) { 1.143 + mShowIcon = show; 1.144 + refreshIcon(); 1.145 + } 1.146 + } 1.147 + 1.148 + void setSubMenuIndicator(boolean hasSubMenu) { 1.149 + if (mHasSubMenu != hasSubMenu) { 1.150 + mHasSubMenu = hasSubMenu; 1.151 + refreshDrawableState(); 1.152 + } 1.153 + } 1.154 +}