mobile/android/base/menu/MenuItemActionView.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.menu;
michael@0 7
michael@0 8 import java.util.ArrayList;
michael@0 9 import java.util.List;
michael@0 10
michael@0 11 import org.mozilla.gecko.R;
michael@0 12
michael@0 13 import android.annotation.TargetApi;
michael@0 14 import android.content.Context;
michael@0 15 import android.content.res.TypedArray;
michael@0 16 import android.graphics.drawable.Drawable;
michael@0 17 import android.util.AttributeSet;
michael@0 18 import android.view.LayoutInflater;
michael@0 19 import android.view.View;
michael@0 20 import android.widget.ImageButton;
michael@0 21 import android.widget.LinearLayout;
michael@0 22 import android.os.Build;
michael@0 23
michael@0 24 public class MenuItemActionView extends LinearLayout
michael@0 25 implements GeckoMenuItem.Layout,
michael@0 26 View.OnClickListener {
michael@0 27 private static final String LOGTAG = "GeckoMenuItemActionView";
michael@0 28
michael@0 29 private MenuItemDefault mMenuItem;
michael@0 30 private MenuItemActionBar mMenuButton;
michael@0 31 private List<ImageButton> mActionButtons;
michael@0 32 private List<View.OnClickListener> mActionButtonListeners = new ArrayList<View.OnClickListener>();
michael@0 33
michael@0 34 public MenuItemActionView(Context context) {
michael@0 35 this(context, null);
michael@0 36 }
michael@0 37
michael@0 38 public MenuItemActionView(Context context, AttributeSet attrs) {
michael@0 39 this(context, attrs, R.attr.menuItemActionViewStyle);
michael@0 40 }
michael@0 41
michael@0 42 @TargetApi(14)
michael@0 43 public MenuItemActionView(Context context, AttributeSet attrs, int defStyle) {
michael@0 44 super(context, attrs);
michael@0 45
michael@0 46 // Set these explicitly, since setting a style isn't supported for LinearLayouts until V11.
michael@0 47 if (Build.VERSION.SDK_INT >= 11) {
michael@0 48 setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
michael@0 49 setDividerDrawable(getResources().getDrawable(R.drawable.divider_vertical));
michael@0 50 }
michael@0 51
michael@0 52 if (Build.VERSION.SDK_INT >= 14) {
michael@0 53 setDividerPadding(0);
michael@0 54 }
michael@0 55
michael@0 56 LayoutInflater.from(context).inflate(R.layout.menu_item_action_view, this);
michael@0 57 mMenuItem = (MenuItemDefault) findViewById(R.id.menu_item);
michael@0 58 mMenuButton = (MenuItemActionBar) findViewById(R.id.menu_item_button);
michael@0 59 mActionButtons = new ArrayList<ImageButton>();
michael@0 60 }
michael@0 61
michael@0 62 @Override
michael@0 63 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
michael@0 64 View parent = (View) getParent();
michael@0 65 final int padding = getPaddingLeft() + getPaddingRight();
michael@0 66 final int parentPadding = parent.getPaddingLeft() + parent.getPaddingRight();
michael@0 67 if ((right - left - padding) < (parent.getMeasuredWidth() - parentPadding) || mActionButtons.size() != 0) {
michael@0 68 // Use the icon.
michael@0 69 mMenuItem.setVisibility(View.GONE);
michael@0 70 mMenuButton.setVisibility(View.VISIBLE);
michael@0 71 } else {
michael@0 72 // Use the button.
michael@0 73 mMenuItem.setVisibility(View.VISIBLE);
michael@0 74 mMenuButton.setVisibility(View.GONE);
michael@0 75 }
michael@0 76
michael@0 77 super.onLayout(changed, left, top, right, bottom);
michael@0 78 }
michael@0 79
michael@0 80 @Override
michael@0 81 public void initialize(GeckoMenuItem item) {
michael@0 82 if (item == null)
michael@0 83 return;
michael@0 84
michael@0 85 mMenuItem.initialize(item);
michael@0 86 mMenuButton.initialize(item);
michael@0 87 setEnabled(item.isEnabled());
michael@0 88 }
michael@0 89
michael@0 90 @Override
michael@0 91 public void setEnabled(boolean enabled) {
michael@0 92 super.setEnabled(enabled);
michael@0 93 mMenuItem.setEnabled(enabled);
michael@0 94 mMenuButton.setEnabled(enabled);
michael@0 95
michael@0 96 for (ImageButton button : mActionButtons) {
michael@0 97 button.setEnabled(enabled);
michael@0 98 button.setAlpha(enabled ? 255 : 99);
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 public void setMenuItemClickListener(View.OnClickListener listener) {
michael@0 103 mMenuItem.setOnClickListener(listener);
michael@0 104 mMenuButton.setOnClickListener(listener);
michael@0 105 }
michael@0 106
michael@0 107 public void addActionButtonClickListener(View.OnClickListener listener) {
michael@0 108 mActionButtonListeners.add(listener);
michael@0 109 }
michael@0 110
michael@0 111 @Override
michael@0 112 public void setShowIcon(boolean show) {
michael@0 113 mMenuItem.setShowIcon(show);
michael@0 114 }
michael@0 115
michael@0 116 public void setIcon(Drawable icon) {
michael@0 117 mMenuItem.setIcon(icon);
michael@0 118 mMenuButton.setIcon(icon);
michael@0 119 }
michael@0 120
michael@0 121 public void setIcon(int icon) {
michael@0 122 mMenuItem.setIcon(icon);
michael@0 123 mMenuButton.setIcon(icon);
michael@0 124 }
michael@0 125
michael@0 126 public void setTitle(CharSequence title) {
michael@0 127 mMenuItem.setTitle(title);
michael@0 128 mMenuButton.setContentDescription(title);
michael@0 129 }
michael@0 130
michael@0 131 public void setSubMenuIndicator(boolean hasSubMenu) {
michael@0 132 mMenuItem.setSubMenuIndicator(hasSubMenu);
michael@0 133 }
michael@0 134
michael@0 135 public void addActionButton(Drawable drawable) {
michael@0 136 // If this is the first icon, retain the text.
michael@0 137 // If not, make the menu item an icon.
michael@0 138 final int count = mActionButtons.size();
michael@0 139 mMenuItem.setVisibility(View.GONE);
michael@0 140 mMenuButton.setVisibility(View.VISIBLE);
michael@0 141
michael@0 142 if (drawable != null) {
michael@0 143 ImageButton button = new ImageButton(getContext(), null, R.attr.menuItemShareActionButtonStyle);
michael@0 144 button.setImageDrawable(drawable);
michael@0 145 button.setOnClickListener(this);
michael@0 146 button.setTag(count);
michael@0 147
michael@0 148 final int height = (int) (getResources().getDimension(R.dimen.menu_item_row_height));
michael@0 149 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, height);
michael@0 150 params.weight = 1.0f;
michael@0 151 button.setLayoutParams(params);
michael@0 152
michael@0 153 // Fill in the action-buttons to the left of the actual menu button.
michael@0 154 mActionButtons.add(button);
michael@0 155 addView(button, count);
michael@0 156 }
michael@0 157 }
michael@0 158
michael@0 159 @Override
michael@0 160 public void onClick(View view) {
michael@0 161 for (View.OnClickListener listener : mActionButtonListeners) {
michael@0 162 listener.onClick(view);
michael@0 163 }
michael@0 164 }
michael@0 165 }

mercurial