summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenuItem.java
blob: 21df4208d6f6295ca4e2f33de13ca7cf601fc764 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* 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 org.mozilla.gecko.R;
import org.mozilla.gecko.util.ResourceDrawableUtils;
import org.mozilla.gecko.widget.GeckoActionProvider;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.ActionProvider;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;

public class GeckoMenuItem implements MenuItem {
    private static final int SHARE_BAR_HISTORY_SIZE = 2;

    // These values mirror MenuItem values that are only available on API >= 11.
    public static final int SHOW_AS_ACTION_NEVER = 0;
    public static final int SHOW_AS_ACTION_IF_ROOM = 1;
    public static final int SHOW_AS_ACTION_ALWAYS = 2;
    public static final int SHOW_AS_ACTION_WITH_TEXT = 4;
    public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8;

    // A View that can show a MenuItem should be able to initialize from
    // the properties of the MenuItem.
    public static interface Layout {
        public void initialize(GeckoMenuItem item);
        public void setShowIcon(boolean show);
    }

    public static interface OnShowAsActionChangedListener {
        public boolean hasActionItemBar();
        public void onShowAsActionChanged(GeckoMenuItem item);
    }

    private final int mId;
    private final int mOrder;
    private View mActionView;
    private int mActionEnum;
    private CharSequence mTitle;
    private CharSequence mTitleCondensed;
    private boolean mCheckable;
    private boolean mChecked;
    private boolean mVisible = true;
    private boolean mEnabled = true;
    private Drawable mIcon;
    private int mIconRes;
    private GeckoActionProvider mActionProvider;
    private GeckoSubMenu mSubMenu;
    private MenuItem.OnMenuItemClickListener mMenuItemClickListener;
    final GeckoMenu mMenu;
    OnShowAsActionChangedListener mShowAsActionChangedListener;

    private volatile boolean mShouldDispatchChanges = true;
    private volatile boolean mDidChange;

    public GeckoMenuItem(GeckoMenu menu, int id, int order, int titleRes) {
        mMenu = menu;
        mId = id;
        mOrder = order;
        mTitle = mMenu.getResources().getString(titleRes);
    }

    public GeckoMenuItem(GeckoMenu menu, int id, int order, CharSequence title) {
        mMenu = menu;
        mId = id;
        mOrder = order;
        mTitle = title;
    }

    /**
     * Stop dispatching item changed events to presenters until
     * [start|resume]DispatchingItemsChanged() is called. Useful when
     * many menu operations are going to be performed as a batch.
     */
    public void stopDispatchingChanges() {
        mDidChange = false;
        mShouldDispatchChanges = false;
    }

    /**
     * Resume dispatching item changed events to presenters. This method
     * will NOT call onItemChanged if any menu operations were queued.
     * Only future menu operations will call onItemChanged. Useful for
     * sequelching presenter updates.
     */
    public void resumeDispatchingChanges() {
        mShouldDispatchChanges = true;
    }

    /**
     * Start dispatching item changed events to presenters. This method
     * will call onItemChanged if any menu operations were queued.
     */
    public void startDispatchingChanges() {
        if (mDidChange) {
            mMenu.onItemChanged(this);
        }
        mShouldDispatchChanges = true;
    }

    @Override
    public boolean collapseActionView() {
        return false;
    }

    @Override
    public boolean expandActionView() {
        return false;
    }

    public boolean hasActionProvider() {
        return (mActionProvider != null);
    }

    public int getActionEnum() {
        return mActionEnum;
    }

    public GeckoActionProvider getGeckoActionProvider() {
        return mActionProvider;
    }

    @Override
    public ActionProvider getActionProvider() {
        return null;
    }

    @Override
    public View getActionView() {
        if (mActionProvider != null) {
            return mActionProvider.onCreateActionView(SHARE_BAR_HISTORY_SIZE,
                    GeckoActionProvider.ActionViewType.DEFAULT);
        }

        return mActionView;
    }

    @Override
    public char getAlphabeticShortcut() {
        return 0;
    }

    @Override
    public int getGroupId() {
        return 0;
    }

    @Override
    public Drawable getIcon() {
        if (mIcon == null) {
            if (mIconRes != 0)
                return ResourceDrawableUtils.getDrawable(mMenu.getContext(), mIconRes);
            else
                return null;
        } else {
            return mIcon;
        }
    }

    @Override
    public Intent getIntent() {
        return null;
    }

    @Override
    public int getItemId() {
        return mId;
    }

    @Override
    public ContextMenu.ContextMenuInfo getMenuInfo() {
        return null;
    }

    @Override
    public char getNumericShortcut() {
        return 0;
    }

    @Override
    public int getOrder() {
        return mOrder;
    }

    @Override
    public SubMenu getSubMenu() {
        return mSubMenu;
    }

    @Override
    public CharSequence getTitle() {
        return mTitle;
    }

    @Override
    public CharSequence getTitleCondensed() {
        return mTitleCondensed;
    }

    @Override
    public boolean hasSubMenu() {
        if (mActionProvider != null)
            return mActionProvider.hasSubMenu();

        return (mSubMenu != null);
    }

    public boolean isActionItem() {
        return (mActionEnum > 0);
    }

    @Override
    public boolean isActionViewExpanded() {
        return false;
    }

    @Override
    public boolean isCheckable() {
        return mCheckable;
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public boolean isEnabled() {
        return mEnabled;
    }

    @Override
    public boolean isVisible() {
        return mVisible;
    }

    @Override
    public MenuItem setActionProvider(ActionProvider actionProvider) {
        return this;
    }

    public MenuItem setActionProvider(GeckoActionProvider actionProvider) {
        mActionProvider = actionProvider;
        if (mActionProvider != null) {
            actionProvider.setOnTargetSelectedListener(new GeckoActionProvider.OnTargetSelectedListener() {
                @Override
                public void onTargetSelected() {
                    mMenu.close();

                    // Refresh the menu item to show the high frequency apps.
                    mShowAsActionChangedListener.onShowAsActionChanged(GeckoMenuItem.this);
                }
            });
        }

        mShowAsActionChangedListener.onShowAsActionChanged(this);
        return this;
    }

    @Override
    public MenuItem setActionView(int resId) {
        return this;
    }

    @Override
    public MenuItem setActionView(View view) {
        return this;
    }

    @Override
    public MenuItem setAlphabeticShortcut(char alphaChar) {
        return this;
    }

    @Override
    public MenuItem setCheckable(boolean checkable) {
        if (mCheckable != checkable) {
            mCheckable = checkable;
            if (mShouldDispatchChanges) {
                mMenu.onItemChanged(this);
            } else {
                mDidChange = true;
            }
        }
        return this;
    }

    @Override
    public MenuItem setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            if (mShouldDispatchChanges) {
                mMenu.onItemChanged(this);
            } else {
                mDidChange = true;
            }
        }
        return this;
    }

    @Override
    public MenuItem setEnabled(boolean enabled) {
        if (mEnabled != enabled) {
            mEnabled = enabled;
            if (mShouldDispatchChanges) {
                mMenu.onItemChanged(this);
            } else {
                mDidChange = true;
            }
        }
        return this;
    }

    @Override
    public MenuItem setIcon(Drawable icon) {
        if (mIcon != icon) {
            mIcon = icon;
            if (mShouldDispatchChanges) {
                mMenu.onItemChanged(this);
            } else {
                mDidChange = true;
            }
        }
        return this;
    }

    @Override
    public MenuItem setIcon(int iconRes) {
        if (mIconRes != iconRes) {
            mIconRes = iconRes;
            if (mShouldDispatchChanges) {
                mMenu.onItemChanged(this);
            } else {
                mDidChange = true;
            }
        }
        return this;
    }

    @Override
    public MenuItem setIntent(Intent intent) {
        return this;
    }

    @Override
    public MenuItem setNumericShortcut(char numericChar) {
        return this;
    }

    @Override
    public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener) {
        return this;
    }

    @Override
    public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener) {
        mMenuItemClickListener = menuItemClickListener;
        return this;
    }

    @Override
    public MenuItem setShortcut(char numericChar, char alphaChar) {
        return this;
    }

    @Override
    public void setShowAsAction(int actionEnum) {
        setShowAsAction(actionEnum, 0);
    }

    public void setShowAsAction(int actionEnum, int style) {
        if (mShowAsActionChangedListener == null)
            return;

        if (mActionEnum == actionEnum)
            return;

        if (actionEnum > 0) {
            if (!mShowAsActionChangedListener.hasActionItemBar())
                return;

            if (!hasActionProvider()) {
                // Change the type to just an icon
                MenuItemActionBar actionView;
                if (style != 0) {
                    actionView = new MenuItemActionBar(mMenu.getContext(), null, style);
                } else {
                    if (actionEnum == SHOW_AS_ACTION_ALWAYS) {
                        actionView = new MenuItemActionBar(mMenu.getContext());
                    } else {
                        actionView = new MenuItemActionBar(mMenu.getContext(), null, R.attr.menuItemSecondaryActionBarStyle);
                    }
                }

                actionView.initialize(this);
                mActionView = actionView;
            }

            mActionEnum = actionEnum;
        }

        mShowAsActionChangedListener.onShowAsActionChanged(this);
    }

    @Override
    public MenuItem setShowAsActionFlags(int actionEnum) {
        return this;
    }

    public MenuItem setSubMenu(GeckoSubMenu subMenu) {
        mSubMenu = subMenu;
        return this;
    }

    @Override
    public MenuItem setTitle(CharSequence title) {
        if (!TextUtils.equals(mTitle, title)) {
            mTitle = title;
            if (mShouldDispatchChanges) {
                mMenu.onItemChanged(this);
            } else {
                mDidChange = true;
            }
        }
        return this;
    }

    @Override
    public MenuItem setTitle(int title) {
        CharSequence newTitle = mMenu.getResources().getString(title);
        return setTitle(newTitle);
    }

    @Override
    public MenuItem setTitleCondensed(CharSequence title) {
        mTitleCondensed = title;
        return this;
    }

    @Override
    public MenuItem setVisible(boolean visible) {
        // Action views are not normal menu items and visibility can get out
        // of sync unless we dispatch whenever required.
        if (isActionItem() || mVisible != visible) {
            mVisible = visible;
            if (mShouldDispatchChanges) {
                mMenu.onItemChanged(this);
            } else {
                mDidChange = true;
            }
        }
        return this;
    }

    public boolean invoke() {
        if (mMenuItemClickListener != null)
            return mMenuItemClickListener.onMenuItemClick(this);
        else
            return false;
    }

    public void setOnShowAsActionChangedListener(OnShowAsActionChangedListener listener) {
        mShowAsActionChangedListener = listener;
    }
}