|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko; |
|
6 |
|
7 import org.mozilla.gecko.widget.GeckoPopupMenu; |
|
8 |
|
9 import android.view.Menu; |
|
10 import android.view.MenuItem; |
|
11 import android.view.View; |
|
12 |
|
13 class ActionModeCompat implements GeckoPopupMenu.OnMenuItemClickListener, |
|
14 View.OnClickListener { |
|
15 private final String LOGTAG = "GeckoActionModeCompat"; |
|
16 |
|
17 private Callback mCallback; |
|
18 private ActionModeCompatView mView; |
|
19 private Presenter mPresenter; |
|
20 |
|
21 /* A set of callbacks to be called during this ActionMode's lifecycle. These will control the |
|
22 * creation, interaction with, and destruction of menuitems for the view */ |
|
23 public static interface Callback { |
|
24 /* Called when action mode is first created. Implementors should use this to inflate menu resources. */ |
|
25 public boolean onCreateActionMode(ActionModeCompat mode, Menu menu); |
|
26 |
|
27 /* Called to refresh an action mode's action menu. Called whenever the mode is invalidated. Implementors |
|
28 * should use this to enable/disable/show/hide menu items. */ |
|
29 public boolean onPrepareActionMode(ActionModeCompat mode, Menu menu); |
|
30 |
|
31 /* Called to report a user click on an action button. */ |
|
32 public boolean onActionItemClicked(ActionModeCompat mode, MenuItem item); |
|
33 |
|
34 /* Called when an action mode is about to be exited and destroyed. */ |
|
35 public void onDestroyActionMode(ActionModeCompat mode); |
|
36 } |
|
37 |
|
38 /* Presenters handle the actual showing/hiding of the action mode UI in the app. Its their responsibility |
|
39 * to create an action mode, and assign it Callbacks and ActionModeCompatView's. */ |
|
40 public static interface Presenter { |
|
41 /* Called when an action mode should be shown */ |
|
42 public void startActionModeCompat(final Callback callback); |
|
43 |
|
44 /* Called when whatever action mode is showing should be hidden */ |
|
45 public void endActionModeCompat(); |
|
46 } |
|
47 |
|
48 public ActionModeCompat(Presenter presenter, Callback callback, ActionModeCompatView view) { |
|
49 mPresenter = presenter; |
|
50 mCallback = callback; |
|
51 |
|
52 mView = view; |
|
53 mView.initForMode(this); |
|
54 } |
|
55 |
|
56 public void finish() { |
|
57 // Clearing the menu will also clear the ActionItemBar |
|
58 mView.getMenu().clear(); |
|
59 if (mCallback != null) { |
|
60 mCallback.onDestroyActionMode(this); |
|
61 } |
|
62 } |
|
63 |
|
64 public CharSequence getTitle() { |
|
65 return mView.getTitle(); |
|
66 } |
|
67 |
|
68 public void setTitle(CharSequence title) { |
|
69 mView.setTitle(title); |
|
70 } |
|
71 |
|
72 public void setTitle(int resId) { |
|
73 mView.setTitle(resId); |
|
74 } |
|
75 |
|
76 public Menu getMenu() { |
|
77 return mView.getMenu(); |
|
78 } |
|
79 |
|
80 public void invalidate() { |
|
81 if (mCallback != null) { |
|
82 mCallback.onPrepareActionMode(this, mView.getMenu()); |
|
83 } |
|
84 mView.invalidate(); |
|
85 } |
|
86 |
|
87 /* GeckoPopupMenu.OnMenuItemClickListener */ |
|
88 @Override |
|
89 public boolean onMenuItemClick(MenuItem item) { |
|
90 if (mCallback != null) { |
|
91 return mCallback.onActionItemClicked(this, item); |
|
92 } |
|
93 return false; |
|
94 } |
|
95 |
|
96 /* View.OnClickListener*/ |
|
97 @Override |
|
98 public void onClick(View v) { |
|
99 mPresenter.endActionModeCompat(); |
|
100 } |
|
101 } |