mobile/android/base/menu/MenuItemDefault.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial