michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.menu; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.List; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.annotation.TargetApi; michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.util.AttributeSet; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.widget.ImageButton; michael@0: import android.widget.LinearLayout; michael@0: import android.os.Build; michael@0: michael@0: public class MenuItemActionView extends LinearLayout michael@0: implements GeckoMenuItem.Layout, michael@0: View.OnClickListener { michael@0: private static final String LOGTAG = "GeckoMenuItemActionView"; michael@0: michael@0: private MenuItemDefault mMenuItem; michael@0: private MenuItemActionBar mMenuButton; michael@0: private List mActionButtons; michael@0: private List mActionButtonListeners = new ArrayList(); michael@0: michael@0: public MenuItemActionView(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public MenuItemActionView(Context context, AttributeSet attrs) { michael@0: this(context, attrs, R.attr.menuItemActionViewStyle); michael@0: } michael@0: michael@0: @TargetApi(14) michael@0: public MenuItemActionView(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs); michael@0: michael@0: // Set these explicitly, since setting a style isn't supported for LinearLayouts until V11. michael@0: if (Build.VERSION.SDK_INT >= 11) { michael@0: setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE); michael@0: setDividerDrawable(getResources().getDrawable(R.drawable.divider_vertical)); michael@0: } michael@0: michael@0: if (Build.VERSION.SDK_INT >= 14) { michael@0: setDividerPadding(0); michael@0: } michael@0: michael@0: LayoutInflater.from(context).inflate(R.layout.menu_item_action_view, this); michael@0: mMenuItem = (MenuItemDefault) findViewById(R.id.menu_item); michael@0: mMenuButton = (MenuItemActionBar) findViewById(R.id.menu_item_button); michael@0: mActionButtons = new ArrayList(); michael@0: } michael@0: michael@0: @Override michael@0: protected void onLayout(boolean changed, int left, int top, int right, int bottom) { michael@0: View parent = (View) getParent(); michael@0: final int padding = getPaddingLeft() + getPaddingRight(); michael@0: final int parentPadding = parent.getPaddingLeft() + parent.getPaddingRight(); michael@0: if ((right - left - padding) < (parent.getMeasuredWidth() - parentPadding) || mActionButtons.size() != 0) { michael@0: // Use the icon. michael@0: mMenuItem.setVisibility(View.GONE); michael@0: mMenuButton.setVisibility(View.VISIBLE); michael@0: } else { michael@0: // Use the button. michael@0: mMenuItem.setVisibility(View.VISIBLE); michael@0: mMenuButton.setVisibility(View.GONE); michael@0: } michael@0: michael@0: super.onLayout(changed, left, top, right, bottom); michael@0: } michael@0: michael@0: @Override michael@0: public void initialize(GeckoMenuItem item) { michael@0: if (item == null) michael@0: return; michael@0: michael@0: mMenuItem.initialize(item); michael@0: mMenuButton.initialize(item); michael@0: setEnabled(item.isEnabled()); michael@0: } michael@0: michael@0: @Override michael@0: public void setEnabled(boolean enabled) { michael@0: super.setEnabled(enabled); michael@0: mMenuItem.setEnabled(enabled); michael@0: mMenuButton.setEnabled(enabled); michael@0: michael@0: for (ImageButton button : mActionButtons) { michael@0: button.setEnabled(enabled); michael@0: button.setAlpha(enabled ? 255 : 99); michael@0: } michael@0: } michael@0: michael@0: public void setMenuItemClickListener(View.OnClickListener listener) { michael@0: mMenuItem.setOnClickListener(listener); michael@0: mMenuButton.setOnClickListener(listener); michael@0: } michael@0: michael@0: public void addActionButtonClickListener(View.OnClickListener listener) { michael@0: mActionButtonListeners.add(listener); michael@0: } michael@0: michael@0: @Override michael@0: public void setShowIcon(boolean show) { michael@0: mMenuItem.setShowIcon(show); michael@0: } michael@0: michael@0: public void setIcon(Drawable icon) { michael@0: mMenuItem.setIcon(icon); michael@0: mMenuButton.setIcon(icon); michael@0: } michael@0: michael@0: public void setIcon(int icon) { michael@0: mMenuItem.setIcon(icon); michael@0: mMenuButton.setIcon(icon); michael@0: } michael@0: michael@0: public void setTitle(CharSequence title) { michael@0: mMenuItem.setTitle(title); michael@0: mMenuButton.setContentDescription(title); michael@0: } michael@0: michael@0: public void setSubMenuIndicator(boolean hasSubMenu) { michael@0: mMenuItem.setSubMenuIndicator(hasSubMenu); michael@0: } michael@0: michael@0: public void addActionButton(Drawable drawable) { michael@0: // If this is the first icon, retain the text. michael@0: // If not, make the menu item an icon. michael@0: final int count = mActionButtons.size(); michael@0: mMenuItem.setVisibility(View.GONE); michael@0: mMenuButton.setVisibility(View.VISIBLE); michael@0: michael@0: if (drawable != null) { michael@0: ImageButton button = new ImageButton(getContext(), null, R.attr.menuItemShareActionButtonStyle); michael@0: button.setImageDrawable(drawable); michael@0: button.setOnClickListener(this); michael@0: button.setTag(count); michael@0: michael@0: final int height = (int) (getResources().getDimension(R.dimen.menu_item_row_height)); michael@0: LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, height); michael@0: params.weight = 1.0f; michael@0: button.setLayoutParams(params); michael@0: michael@0: // Fill in the action-buttons to the left of the actual menu button. michael@0: mActionButtons.add(button); michael@0: addView(button, count); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void onClick(View view) { michael@0: for (View.OnClickListener listener : mActionButtonListeners) { michael@0: listener.onClick(view); michael@0: } michael@0: } michael@0: }