2012-07-19 20:16:44 +00:00
|
|
|
/* 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;
|
|
|
|
|
2013-11-19 19:57:37 +00:00
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils;
|
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils.BitmapLoader;
|
2012-07-26 14:13:48 +00:00
|
|
|
import org.mozilla.gecko.gfx.Layer;
|
2012-08-20 19:43:53 +00:00
|
|
|
import org.mozilla.gecko.gfx.LayerView;
|
2013-11-19 19:57:37 +00:00
|
|
|
import org.mozilla.gecko.menu.GeckoMenu;
|
|
|
|
import org.mozilla.gecko.menu.GeckoMenuItem;
|
2012-08-03 01:38:45 +00:00
|
|
|
import org.mozilla.gecko.util.EventDispatcher;
|
2012-07-28 04:57:47 +00:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2012-08-03 00:13:40 +00:00
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2013-06-06 18:05:06 +00:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2013-11-19 19:57:37 +00:00
|
|
|
import org.mozilla.gecko.ActionModeCompat.Callback;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
2012-09-21 17:56:41 +00:00
|
|
|
import org.json.JSONArray;
|
2012-12-19 06:57:54 +00:00
|
|
|
import org.json.JSONException;
|
2012-07-19 20:16:44 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2013-11-19 19:57:37 +00:00
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
2012-07-28 00:53:54 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
|
2012-07-26 14:13:48 +00:00
|
|
|
class TextSelection extends Layer implements GeckoEventListener {
|
2012-07-19 20:16:44 +00:00
|
|
|
private static final String LOGTAG = "GeckoTextSelection";
|
|
|
|
|
|
|
|
private final TextSelectionHandle mStartHandle;
|
2012-09-21 17:56:41 +00:00
|
|
|
private final TextSelectionHandle mMiddleHandle;
|
2012-07-19 20:16:44 +00:00
|
|
|
private final TextSelectionHandle mEndHandle;
|
2012-08-03 01:38:45 +00:00
|
|
|
private final EventDispatcher mEventDispatcher;
|
2012-07-19 20:16:44 +00:00
|
|
|
|
2012-07-26 14:13:48 +00:00
|
|
|
private float mViewLeft;
|
|
|
|
private float mViewTop;
|
|
|
|
private float mViewZoom;
|
|
|
|
|
2013-11-19 19:57:37 +00:00
|
|
|
private TextSelectionActionModeCallback mCallback;
|
|
|
|
|
|
|
|
// These timers are used to avoid flicker caused by selection handles showing/hiding quickly. For isntance
|
|
|
|
// when moving between single handle caret mode and two handle selection mode.
|
|
|
|
private 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;
|
|
|
|
|
2012-09-21 17:56:41 +00:00
|
|
|
TextSelection(TextSelectionHandle startHandle,
|
|
|
|
TextSelectionHandle middleHandle,
|
|
|
|
TextSelectionHandle endHandle,
|
2012-12-19 06:57:54 +00:00
|
|
|
EventDispatcher eventDispatcher,
|
|
|
|
GeckoApp activity) {
|
2012-07-19 20:16:44 +00:00
|
|
|
mStartHandle = startHandle;
|
2012-09-21 17:56:41 +00:00
|
|
|
mMiddleHandle = middleHandle;
|
2012-07-19 20:16:44 +00:00
|
|
|
mEndHandle = endHandle;
|
2012-08-03 01:38:45 +00:00
|
|
|
mEventDispatcher = eventDispatcher;
|
2012-07-19 20:16:44 +00:00
|
|
|
|
2012-09-21 17:56:41 +00:00
|
|
|
// Only register listeners if we have valid start/middle/end handles
|
|
|
|
if (mStartHandle == null || mMiddleHandle == null || mEndHandle == null) {
|
2012-07-23 22:41:55 +00:00
|
|
|
Log.e(LOGTAG, "Failed to initialize text selection because at least one handle is null");
|
|
|
|
} else {
|
2012-08-03 01:38:45 +00:00
|
|
|
registerEventListener("TextSelection:ShowHandles");
|
|
|
|
registerEventListener("TextSelection:HideHandles");
|
|
|
|
registerEventListener("TextSelection:PositionHandles");
|
2013-11-19 19:57:37 +00:00
|
|
|
registerEventListener("TextSelection:Update");
|
2012-07-23 22:41:55 +00:00
|
|
|
}
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy() {
|
2012-08-03 01:38:45 +00:00
|
|
|
unregisterEventListener("TextSelection:ShowHandles");
|
|
|
|
unregisterEventListener("TextSelection:HideHandles");
|
|
|
|
unregisterEventListener("TextSelection:PositionHandles");
|
2013-11-19 19:57:37 +00:00
|
|
|
unregisterEventListener("TextSelection:Update");
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
|
|
|
|
2012-10-18 17:41:06 +00:00
|
|
|
private TextSelectionHandle getHandle(String name) {
|
|
|
|
if (name.equals("START")) {
|
|
|
|
return mStartHandle;
|
|
|
|
} else if (name.equals("MIDDLE")) {
|
|
|
|
return mMiddleHandle;
|
|
|
|
} else {
|
|
|
|
return mEndHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2012-12-19 06:57:54 +00:00
|
|
|
public void handleMessage(final String event, final JSONObject message) {
|
2013-06-06 18:05:06 +00:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2012-12-19 06:57:54 +00:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
if (event.equals("TextSelection:ShowHandles")) {
|
|
|
|
final JSONArray handles = message.getJSONArray("handles");
|
|
|
|
for (int i=0; i < handles.length(); i++) {
|
|
|
|
String handle = handles.getString(i);
|
|
|
|
getHandle(handle).setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
mViewLeft = 0.0f;
|
|
|
|
mViewTop = 0.0f;
|
|
|
|
mViewZoom = 0.0f;
|
2013-06-06 18:05:06 +00:00
|
|
|
LayerView layerView = GeckoAppShell.getLayerView();
|
2012-12-19 06:57:54 +00:00
|
|
|
if (layerView != null) {
|
|
|
|
layerView.addLayer(TextSelection.this);
|
|
|
|
}
|
2013-11-19 19:57:37 +00:00
|
|
|
|
|
|
|
if (mActionModeTimerTask != null)
|
|
|
|
mActionModeTimerTask.cancel();
|
|
|
|
showActionMode(message.getJSONArray("actions"));
|
|
|
|
} else if (event.equals("TextSelection:Update")) {
|
|
|
|
if (mActionModeTimerTask != null)
|
|
|
|
mActionModeTimerTask.cancel();
|
|
|
|
showActionMode(message.getJSONArray("actions"));
|
2012-12-19 06:57:54 +00:00
|
|
|
} else if (event.equals("TextSelection:HideHandles")) {
|
2013-06-06 18:05:06 +00:00
|
|
|
LayerView layerView = GeckoAppShell.getLayerView();
|
2012-12-19 06:57:54 +00:00
|
|
|
if (layerView != null) {
|
|
|
|
layerView.removeLayer(TextSelection.this);
|
|
|
|
}
|
|
|
|
|
2013-11-19 19:57:37 +00:00
|
|
|
mActionModeTimerTask = new ActionModeTimerTask();
|
|
|
|
mActionModeTimer.schedule(mActionModeTimerTask, 250);
|
|
|
|
|
2013-03-25 22:25:19 +00:00
|
|
|
mStartHandle.setVisibility(View.GONE);
|
|
|
|
mMiddleHandle.setVisibility(View.GONE);
|
|
|
|
mEndHandle.setVisibility(View.GONE);
|
2012-12-19 06:57:54 +00:00
|
|
|
} else if (event.equals("TextSelection:PositionHandles")) {
|
|
|
|
final boolean rtl = message.getBoolean("rtl");
|
|
|
|
final JSONArray positions = message.getJSONArray("positions");
|
|
|
|
for (int i=0; i < positions.length(); i++) {
|
|
|
|
JSONObject position = positions.getJSONObject(i);
|
|
|
|
int left = position.getInt("left");
|
|
|
|
int top = position.getInt("top");
|
|
|
|
|
|
|
|
TextSelectionHandle handle = getHandle(position.getString("handle"));
|
|
|
|
handle.setVisibility(position.getBoolean("hidden") ? View.GONE : View.VISIBLE);
|
|
|
|
handle.positionFromGecko(left, top, rtl);
|
|
|
|
}
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
2012-12-19 06:57:54 +00:00
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "JSON exception", e);
|
|
|
|
}
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
2012-12-19 06:57:54 +00:00
|
|
|
});
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
2012-07-26 14:13:48 +00:00
|
|
|
|
2013-11-19 19:57:37 +00:00
|
|
|
private void showActionMode(final JSONArray items) {
|
|
|
|
if (mCallback != null) {
|
|
|
|
mCallback.updateItems(items);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Context context = mStartHandle.getContext();
|
|
|
|
if (context instanceof ActionModeCompat.Presenter) {
|
|
|
|
final ActionModeCompat.Presenter presenter = (ActionModeCompat.Presenter) context;
|
|
|
|
mCallback = new TextSelectionActionModeCallback(items);
|
|
|
|
presenter.startActionModeCompat(mCallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void endActionMode() {
|
|
|
|
Context context = mStartHandle.getContext();
|
|
|
|
if (context instanceof ActionModeCompat.Presenter) {
|
|
|
|
final ActionModeCompat.Presenter presenter = (ActionModeCompat.Presenter) context;
|
|
|
|
presenter.endActionModeCompat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 14:13:48 +00:00
|
|
|
@Override
|
|
|
|
public void draw(final RenderContext context) {
|
|
|
|
// cache the relevant values from the context and bail out if they are the same. we do this
|
|
|
|
// because this draw function gets called a lot (once per compositor frame) and we want to
|
|
|
|
// avoid doing a lot of extra work in cases where it's not needed.
|
2013-04-25 17:47:08 +00:00
|
|
|
final float viewLeft = context.viewport.left - context.offset.x;
|
|
|
|
final float viewTop = context.viewport.top - context.offset.y;
|
|
|
|
final float viewZoom = context.zoomFactor;
|
|
|
|
|
|
|
|
if (FloatUtils.fuzzyEquals(mViewLeft, viewLeft)
|
|
|
|
&& FloatUtils.fuzzyEquals(mViewTop, viewTop)
|
|
|
|
&& FloatUtils.fuzzyEquals(mViewZoom, viewZoom)) {
|
2012-07-26 14:13:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-25 17:47:08 +00:00
|
|
|
mViewLeft = viewLeft;
|
|
|
|
mViewTop = viewTop;
|
|
|
|
mViewZoom = viewZoom;
|
2012-07-26 14:13:48 +00:00
|
|
|
|
2013-06-06 18:05:06 +00:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2012-07-26 14:13:48 +00:00
|
|
|
public void run() {
|
2013-04-25 17:47:08 +00:00
|
|
|
mStartHandle.repositionWithViewport(viewLeft, viewTop, viewZoom);
|
|
|
|
mMiddleHandle.repositionWithViewport(viewLeft, viewTop, viewZoom);
|
|
|
|
mEndHandle.repositionWithViewport(viewLeft, viewTop, viewZoom);
|
2012-07-26 14:13:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-08-03 01:38:45 +00:00
|
|
|
|
|
|
|
private void registerEventListener(String event) {
|
|
|
|
mEventDispatcher.registerEventListener(event, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void unregisterEventListener(String event) {
|
|
|
|
mEventDispatcher.unregisterEventListener(event, this);
|
|
|
|
}
|
2013-11-19 19:57:37 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareActionMode(final ActionModeCompat mode, final Menu 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"));
|
|
|
|
menuitem.setShowAsAction(obj.optBoolean("showAsAction") ? 1 : 0);
|
|
|
|
|
|
|
|
BitmapUtils.getDrawable(mStartHandle.getContext(), obj.optString("icon"), new BitmapLoader() {
|
|
|
|
public void onBitmapFound(Drawable d) {
|
|
|
|
if (d != null) {
|
|
|
|
menuitem.setIcon(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch(Exception ex) {
|
|
|
|
Log.i(LOGTAG, "Exception building menu", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onCreateActionMode(ActionModeCompat mode, Menu menu) {
|
|
|
|
mActionMode = mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onActionItemClicked(ActionModeCompat mode, MenuItem item) {
|
|
|
|
try {
|
|
|
|
final JSONObject obj = mItems.getJSONObject(item.getItemId());
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("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
|
|
|
|
public void onDestroyActionMode(ActionModeCompat mode) {
|
|
|
|
mActionMode = null;
|
|
|
|
mCallback = null;
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:End", null));
|
|
|
|
}
|
|
|
|
}
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|