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
|
/* 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;
import org.mozilla.gecko.menu.GeckoMenu;
import org.mozilla.gecko.menu.GeckoMenuItem;
import org.mozilla.gecko.util.ResourceDrawableUtils;
import org.mozilla.gecko.text.TextSelection;
import org.mozilla.gecko.util.GeckoEventListener;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.ActionModeCompat.Callback;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MenuItem;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
import android.util.Log;
class ActionBarTextSelection implements TextSelection, GeckoEventListener {
private static final String LOGTAG = "GeckoTextSelection";
private static final int SHUTDOWN_DELAY_MS = 250;
private final Context context;
private boolean mDraggingHandles;
private String selectionID; // Unique ID provided for each selection action.
private String mCurrentItems;
private TextSelectionActionModeCallback mCallback;
// These timers are used to avoid flicker caused by selection handles showing/hiding quickly.
// For instance when moving between single handle caret mode and two handle selection mode.
private final Timer mActionModeTimer = new Timer("actionMode");
private class ActionModeTimerTask extends TimerTask {
@Override
public void run() {
ThreadUtils.postToUiThread(new Runnable() {
@Override
public void run() {
endActionMode();
}
});
}
};
private ActionModeTimerTask mActionModeTimerTask;
ActionBarTextSelection(Context context) {
this.context = context;
}
@Override
public void create() {
// Only register listeners if we have valid start/middle/end handles
if (context == null) {
Log.e(LOGTAG, "Failed to initialize text selection because at least one context is null");
} else {
GeckoApp.getEventDispatcher().registerGeckoThreadListener(this,
"TextSelection:ActionbarInit",
"TextSelection:ActionbarStatus",
"TextSelection:ActionbarUninit",
"TextSelection:Update");
}
}
@Override
public boolean dismiss() {
// We do not call endActionMode() here because this is already handled by the activity.
return false;
}
@Override
public void destroy() {
if (context == null) {
Log.e(LOGTAG, "Do not unregister TextSelection:* listeners since context is null");
} else {
GeckoApp.getEventDispatcher().unregisterGeckoThreadListener(this,
"TextSelection:ActionbarInit",
"TextSelection:ActionbarStatus",
"TextSelection:ActionbarUninit",
"TextSelection:Update");
}
}
@Override
public void handleMessage(final String event, final JSONObject message) {
ThreadUtils.postToUiThread(new Runnable() {
@Override
public void run() {
try {
if (event.equals("TextSelection:Update")) {
if (mActionModeTimerTask != null)
mActionModeTimerTask.cancel();
showActionMode(message.getJSONArray("actions"));
} else if (event.equals("TextSelection:ActionbarInit")) {
// Init / Open the action bar. Note the current selectionID,
// cancel any pending actionBar close.
Telemetry.sendUIEvent(TelemetryContract.Event.SHOW,
TelemetryContract.Method.CONTENT, "text_selection");
selectionID = message.getString("selectionID");
mCurrentItems = null;
if (mActionModeTimerTask != null) {
mActionModeTimerTask.cancel();
}
} else if (event.equals("TextSelection:ActionbarStatus")) {
// Ensure async updates from SearchService for example are valid.
if (selectionID != message.optString("selectionID")) {
return;
}
// Update the actionBar actions as provided by Gecko.
showActionMode(message.getJSONArray("actions"));
} else if (event.equals("TextSelection:ActionbarUninit")) {
// Uninit the actionbar. Schedule a cancellable close
// action to avoid UI jank. (During SelectionAll for ex).
mCurrentItems = null;
mActionModeTimerTask = new ActionModeTimerTask();
mActionModeTimer.schedule(mActionModeTimerTask, SHUTDOWN_DELAY_MS);
}
} catch (JSONException e) {
Log.e(LOGTAG, "JSON exception", e);
}
}
});
}
private void showActionMode(final JSONArray items) {
String itemsString = items.toString();
if (itemsString.equals(mCurrentItems)) {
return;
}
mCurrentItems = itemsString;
if (mCallback != null) {
mCallback.updateItems(items);
return;
}
if (context instanceof ActionModeCompat.Presenter) {
final ActionModeCompat.Presenter presenter = (ActionModeCompat.Presenter) context;
mCallback = new TextSelectionActionModeCallback(items);
presenter.startActionModeCompat(mCallback);
mCallback.animateIn();
}
}
private void endActionMode() {
if (context instanceof ActionModeCompat.Presenter) {
final ActionModeCompat.Presenter presenter = (ActionModeCompat.Presenter) context;
presenter.endActionModeCompat();
}
mCurrentItems = null;
}
private class TextSelectionActionModeCallback implements Callback {
private JSONArray mItems;
private ActionModeCompat mActionMode;
public TextSelectionActionModeCallback(JSONArray items) {
mItems = items;
}
public void updateItems(JSONArray items) {
mItems = items;
if (mActionMode != null) {
mActionMode.invalidate();
}
}
public void animateIn() {
if (mActionMode != null) {
mActionMode.animateIn();
}
}
@Override
public boolean onPrepareActionMode(final ActionModeCompat mode, final GeckoMenu menu) {
// Android would normally expect us to only update the state of menu items here
// To make the js-java interaction a bit simpler, we just wipe out the menu here and recreate all
// the javascript menu items in onPrepare instead. This will be called any time invalidate() is called on the
// action mode.
menu.clear();
int length = mItems.length();
for (int i = 0; i < length; i++) {
try {
final JSONObject obj = mItems.getJSONObject(i);
final GeckoMenuItem menuitem = (GeckoMenuItem) menu.add(0, i, 0, obj.optString("label"));
final int actionEnum = obj.optBoolean("showAsAction") ? GeckoMenuItem.SHOW_AS_ACTION_ALWAYS : GeckoMenuItem.SHOW_AS_ACTION_NEVER;
menuitem.setShowAsAction(actionEnum, R.attr.menuItemActionModeStyle);
final String iconString = obj.optString("icon");
ResourceDrawableUtils.getDrawable(context, iconString, new ResourceDrawableUtils.BitmapLoader() {
@Override
public void onBitmapFound(Drawable d) {
if (d != null) {
menuitem.setIcon(d);
}
}
});
} catch (Exception ex) {
Log.i(LOGTAG, "Exception building menu", ex);
}
}
return true;
}
@Override
public boolean onCreateActionMode(ActionModeCompat mode, GeckoMenu unused) {
mActionMode = mode;
return true;
}
@Override
public boolean onActionItemClicked(ActionModeCompat mode, MenuItem item) {
try {
final JSONObject obj = mItems.getJSONObject(item.getItemId());
GeckoAppShell.notifyObservers("TextSelection:Action", obj.optString("id"));
return true;
} catch (Exception ex) {
Log.i(LOGTAG, "Exception calling action", ex);
}
return false;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionModeCompat mode) {
mActionMode = null;
mCallback = null;
final JSONObject args = new JSONObject();
try {
args.put("selectionID", selectionID);
} catch (JSONException e) {
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:End", e);
return;
}
GeckoAppShell.notifyObservers("TextSelection:End", args.toString());
}
}
}
|