summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemSwitcherLayout.java
blob: d01f52687684db7c9cfc00d0950a9014c9cd7b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.menu;

import java.util.ArrayList;
import java.util.List;

import org.mozilla.gecko.R;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

/**
 * This class is a container view for menu items that:
 *   * Shows text if there is enough space and there are
 *     no action buttons ({@link #mActionButtons}).
 *   * Shows an icon if there is not enough space for text,
 *     or there are action buttons.
 */
public class MenuItemSwitcherLayout extends LinearLayout
                                    implements GeckoMenuItem.Layout,
                                               View.OnClickListener {
    private final MenuItemDefault mMenuItem;
    private final MenuItemActionBar mMenuButton;
    private final List<ImageButton> mActionButtons;
    private final List<View.OnClickListener> mActionButtonListeners = new ArrayList<View.OnClickListener>();

    public MenuItemSwitcherLayout(Context context) {
        this(context, null);
    }

    public MenuItemSwitcherLayout(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.menuItemSwitcherLayoutStyle);
    }

    @TargetApi(14)
    public MenuItemSwitcherLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.menu_item_switcher_layout, this);
        mMenuItem = (MenuItemDefault) findViewById(R.id.menu_item);
        mMenuButton = (MenuItemActionBar) findViewById(R.id.menu_item_button);
        mActionButtons = new ArrayList<ImageButton>();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int width = right - left;

        final View parent = (View) getParent();
        final int parentPadding = parent.getPaddingLeft() + parent.getPaddingRight();
        final int horizontalSpaceAvailableInParent = parent.getMeasuredWidth() - parentPadding;

        // Check if there is another View sharing horizontal
        // space with this View in the parent.
        if (width < horizontalSpaceAvailableInParent || mActionButtons.size() != 0) {
            // Use the icon.
            mMenuItem.setVisibility(View.GONE);
            mMenuButton.setVisibility(View.VISIBLE);
        } else {
            // Use the button.
            mMenuItem.setVisibility(View.VISIBLE);
            mMenuButton.setVisibility(View.GONE);
        }

        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public void initialize(GeckoMenuItem item) {
        if (item == null) {
            return;
        }

        mMenuItem.initialize(item);
        mMenuButton.initialize(item);
        setEnabled(item.isEnabled());
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        mMenuItem.setEnabled(enabled);
        mMenuButton.setEnabled(enabled);

        for (ImageButton button : mActionButtons) {
             button.setEnabled(enabled);
             button.setAlpha(enabled ? 255 : 99);
        }
    }

    public void setMenuItemClickListener(View.OnClickListener listener) {
        mMenuItem.setOnClickListener(listener);
        mMenuButton.setOnClickListener(listener);
    }

    public void setMenuItemLongClickListener(View.OnLongClickListener listener) {
        mMenuItem.setOnLongClickListener(listener);
        mMenuButton.setOnLongClickListener(listener);
    }

    public void addActionButtonClickListener(View.OnClickListener listener) {
        mActionButtonListeners.add(listener);
    }

    @Override
    public void setShowIcon(boolean show) {
        mMenuItem.setShowIcon(show);
    }

    public void setIcon(Drawable icon) {
        mMenuItem.setIcon(icon);
        mMenuButton.setIcon(icon);
    }

    public void setIcon(int icon) {
        mMenuItem.setIcon(icon);
        mMenuButton.setIcon(icon);
    }

    public void setTitle(CharSequence title) {
        mMenuItem.setTitle(title);
        mMenuButton.setContentDescription(title);
    }

    public void setSubMenuIndicator(boolean hasSubMenu) {
        mMenuItem.setSubMenuIndicator(hasSubMenu);
    }

    public void addActionButton(Drawable drawable, CharSequence label) {
        // If this is the first icon, retain the text.
        // If not, make the menu item an icon.
        final int count = mActionButtons.size();
        mMenuItem.setVisibility(View.GONE);
        mMenuButton.setVisibility(View.VISIBLE);

        if (drawable != null) {
            ImageButton button = new ImageButton(getContext(), null, R.attr.menuItemSecondaryActionBarStyle);
            button.setImageDrawable(drawable);
            button.setContentDescription(label);
            button.setOnClickListener(this);
            button.setTag(count);

            final int height = (int) (getResources().getDimension(R.dimen.menu_item_row_height));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, height);
            params.weight = 1.0f;
            button.setLayoutParams(params);

            // Place action buttons to the right of the actual menu item
            mActionButtons.add(button);
            addView(button, count + 1);
        }
    }

    protected int getActionButtonCount() {
        return mActionButtons.size();
    }

    @Override
    public void onClick(View view) {
        for (View.OnClickListener listener : mActionButtonListeners) {
            listener.onClick(view);
        }
    }

    /**
     * Update the styles if this view is being used in the context menus.
     *
     * Ideally, we just use different layout files and styles to set this, but
     * MenuItemSwitcherLayout is too integrated into GeckoActionProvider to provide
     * an easy separation so instead I provide this hack. I'm sorry.
     */
    public void initContextMenuStyles() {
        final int defaultContextMenuPadding = getContext().getResources().getDimensionPixelOffset(
                R.dimen.context_menu_item_horizontal_padding);
        mMenuItem.setPadding(defaultContextMenuPadding, getPaddingTop(),
                defaultContextMenuPadding, getPaddingBottom());
    }
}