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;
|
|
|
|
|
2012-07-28 00:53:54 +00:00
|
|
|
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
|
2012-08-20 19:43:53 +00:00
|
|
|
import org.mozilla.gecko.gfx.LayerView;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2012-07-19 20:16:44 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.PointF;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageView;
|
2012-07-28 00:53:54 +00:00
|
|
|
import android.widget.RelativeLayout;
|
2012-07-19 20:16:44 +00:00
|
|
|
|
|
|
|
class TextSelectionHandle extends ImageView implements View.OnTouchListener {
|
|
|
|
private static final String LOGTAG = "GeckoTextSelectionHandle";
|
|
|
|
|
2012-09-21 17:56:41 +00:00
|
|
|
private enum HandleType { START, MIDDLE, END };
|
2012-07-19 20:16:44 +00:00
|
|
|
|
|
|
|
private final HandleType mHandleType;
|
|
|
|
private final int mWidth;
|
|
|
|
private final int mHeight;
|
2012-07-23 19:50:43 +00:00
|
|
|
private final int mShadow;
|
2012-07-19 20:16:44 +00:00
|
|
|
|
2012-12-19 06:58:01 +00:00
|
|
|
private float mLeft;
|
|
|
|
private float mTop;
|
2012-11-01 04:49:15 +00:00
|
|
|
private boolean mIsRTL;
|
2012-07-26 14:13:48 +00:00
|
|
|
private PointF mGeckoPoint;
|
2012-12-19 06:58:01 +00:00
|
|
|
private float mTouchStartX;
|
|
|
|
private float mTouchStartY;
|
2012-12-19 06:58:10 +00:00
|
|
|
private int mLayerViewX;
|
|
|
|
private int mLayerViewY;
|
2012-07-19 20:16:44 +00:00
|
|
|
|
2012-07-19 22:41:21 +00:00
|
|
|
private RelativeLayout.LayoutParams mLayoutParams;
|
|
|
|
|
2012-11-01 04:49:15 +00:00
|
|
|
private static final int IMAGE_LEVEL_LTR = 0;
|
|
|
|
private static final int IMAGE_LEVEL_RTL = 1;
|
|
|
|
|
2013-02-08 04:35:55 +00:00
|
|
|
public TextSelectionHandle(Context context, AttributeSet attrs) {
|
2012-07-19 20:16:44 +00:00
|
|
|
super(context, attrs);
|
|
|
|
setOnTouchListener(this);
|
|
|
|
|
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextSelectionHandle);
|
2012-09-21 17:56:41 +00:00
|
|
|
int handleType = a.getInt(R.styleable.TextSelectionHandle_handleType, 0x01);
|
|
|
|
|
|
|
|
if (handleType == 0x01)
|
|
|
|
mHandleType = HandleType.START;
|
|
|
|
else if (handleType == 0x02)
|
|
|
|
mHandleType = HandleType.MIDDLE;
|
|
|
|
else
|
|
|
|
mHandleType = HandleType.END;
|
|
|
|
|
|
|
|
mGeckoPoint = new PointF(0.0f, 0.0f);
|
2012-07-19 20:16:44 +00:00
|
|
|
|
|
|
|
mWidth = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_width);
|
|
|
|
mHeight = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_height);
|
2012-07-23 19:50:43 +00:00
|
|
|
mShadow = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_shadow);
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2012-07-19 20:16:44 +00:00
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
switch (event.getActionMasked()) {
|
|
|
|
case MotionEvent.ACTION_DOWN: {
|
2012-12-19 06:58:01 +00:00
|
|
|
mTouchStartX = event.getX();
|
|
|
|
mTouchStartY = event.getY();
|
2012-12-19 06:58:10 +00:00
|
|
|
|
|
|
|
int[] rect = new int[2];
|
2013-06-06 18:05:06 +00:00
|
|
|
GeckoAppShell.getLayerView().getLocationOnScreen(rect);
|
2012-12-19 06:58:10 +00:00
|
|
|
mLayerViewX = rect[0];
|
|
|
|
mLayerViewY = rect[1];
|
2012-07-19 20:16:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MotionEvent.ACTION_UP: {
|
|
|
|
mTouchStartX = 0;
|
|
|
|
mTouchStartY = 0;
|
|
|
|
|
|
|
|
// Reposition handles to line up with ends of selection
|
|
|
|
JSONObject args = new JSONObject();
|
|
|
|
try {
|
|
|
|
args.put("handleType", mHandleType.toString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Position");
|
|
|
|
}
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Position", args.toString()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MotionEvent.ACTION_MOVE: {
|
2012-12-19 06:58:10 +00:00
|
|
|
move(event.getRawX(), event.getRawY());
|
2012-07-19 20:16:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-19 06:58:01 +00:00
|
|
|
private void move(float newX, float newY) {
|
2012-12-19 06:58:10 +00:00
|
|
|
// newX and newY are absolute coordinates, so we need to adjust them to
|
|
|
|
// account for other views on the screen (such as the URL bar). We also
|
|
|
|
// need to include the offset amount of the touch location relative to
|
|
|
|
// the top left of the handle (mTouchStartX and mTouchStartY).
|
|
|
|
mLeft = newX - mLayerViewX - mTouchStartX;
|
|
|
|
mTop = newY - mLayerViewY - mTouchStartY;
|
2012-07-19 20:16:44 +00:00
|
|
|
|
2013-06-06 18:05:06 +00:00
|
|
|
LayerView layerView = GeckoAppShell.getLayerView();
|
2012-08-20 19:43:53 +00:00
|
|
|
if (layerView == null) {
|
|
|
|
Log.e(LOGTAG, "Can't move selection because layerView is null");
|
2012-07-19 20:16:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Send x coordinate on the right side of the start handle, left side of the end handle.
|
2012-12-19 06:58:01 +00:00
|
|
|
float left = mLeft + adjustLeftForHandle();
|
2012-09-21 17:56:41 +00:00
|
|
|
|
2012-12-19 06:58:01 +00:00
|
|
|
PointF geckoPoint = new PointF(left, mTop);
|
2012-08-20 19:43:53 +00:00
|
|
|
geckoPoint = layerView.convertViewPointToLayerPoint(geckoPoint);
|
2012-07-19 20:16:44 +00:00
|
|
|
|
|
|
|
JSONObject args = new JSONObject();
|
|
|
|
try {
|
|
|
|
args.put("handleType", mHandleType.toString());
|
2012-12-19 06:58:01 +00:00
|
|
|
args.put("x", (int) geckoPoint.x);
|
|
|
|
args.put("y", (int) geckoPoint.y);
|
2012-07-19 20:16:44 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Move");
|
|
|
|
}
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Move", args.toString()));
|
|
|
|
|
2012-12-19 06:58:10 +00:00
|
|
|
// If we're positioning a cursor, don't move the handle here. Gecko
|
|
|
|
// will tell us the position of the caret, so we set the handle
|
|
|
|
// position then. This allows us to lock the handle to wherever the
|
|
|
|
// caret appears.
|
|
|
|
if (!mHandleType.equals(HandleType.MIDDLE)) {
|
|
|
|
setLayoutPosition();
|
|
|
|
}
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 04:49:15 +00:00
|
|
|
void positionFromGecko(int left, int top, boolean rtl) {
|
2013-06-06 18:05:06 +00:00
|
|
|
LayerView layerView = GeckoAppShell.getLayerView();
|
2012-08-20 19:43:53 +00:00
|
|
|
if (layerView == null) {
|
|
|
|
Log.e(LOGTAG, "Can't position handle because layerView is null");
|
2012-07-19 20:16:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-19 06:58:01 +00:00
|
|
|
mGeckoPoint = new PointF(left, top);
|
2012-11-01 04:49:15 +00:00
|
|
|
if (mIsRTL != rtl) {
|
|
|
|
mIsRTL = rtl;
|
|
|
|
setImageLevel(mIsRTL ? IMAGE_LEVEL_RTL : IMAGE_LEVEL_LTR);
|
|
|
|
}
|
|
|
|
|
2012-08-20 19:43:53 +00:00
|
|
|
ImmutableViewportMetrics metrics = layerView.getViewportMetrics();
|
2013-04-25 17:47:08 +00:00
|
|
|
PointF offset = metrics.getMarginOffset();
|
|
|
|
repositionWithViewport(metrics.viewportRectLeft - offset.x, metrics.viewportRectTop - offset.y, metrics.zoomFactor);
|
2012-07-26 14:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void repositionWithViewport(float x, float y, float zoom) {
|
|
|
|
PointF viewPoint = new PointF((mGeckoPoint.x * zoom) - x,
|
|
|
|
(mGeckoPoint.y * zoom) - y);
|
|
|
|
|
2012-12-19 06:58:01 +00:00
|
|
|
mLeft = viewPoint.x - adjustLeftForHandle();
|
|
|
|
mTop = viewPoint.y;
|
2012-07-19 20:16:44 +00:00
|
|
|
|
2012-07-19 22:41:21 +00:00
|
|
|
setLayoutPosition();
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:49:15 +00:00
|
|
|
private float adjustLeftForHandle() {
|
|
|
|
if (mHandleType.equals(HandleType.START))
|
|
|
|
return mIsRTL ? mShadow : mWidth - mShadow;
|
|
|
|
else if (mHandleType.equals(HandleType.MIDDLE))
|
2013-04-25 22:27:22 +00:00
|
|
|
return mWidth / 2;
|
2012-11-01 04:49:15 +00:00
|
|
|
else
|
|
|
|
return mIsRTL ? mWidth - mShadow : mShadow;
|
|
|
|
}
|
|
|
|
|
2012-07-19 22:41:21 +00:00
|
|
|
private void setLayoutPosition() {
|
|
|
|
if (mLayoutParams == null) {
|
|
|
|
mLayoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
|
|
|
|
// Set negative right/bottom margins so that the handles can be dragged outside of
|
|
|
|
// the content area (if they are dragged to the left/top, the dyanmic margins set
|
|
|
|
// below will take care of that).
|
|
|
|
mLayoutParams.rightMargin = 0 - mWidth;
|
|
|
|
mLayoutParams.bottomMargin = 0 - mHeight;
|
|
|
|
}
|
|
|
|
|
2012-12-19 06:58:01 +00:00
|
|
|
mLayoutParams.leftMargin = (int) mLeft;
|
|
|
|
mLayoutParams.topMargin = (int) mTop;
|
2012-07-19 22:41:21 +00:00
|
|
|
setLayoutParams(mLayoutParams);
|
2012-07-19 20:16:44 +00:00
|
|
|
}
|
|
|
|
}
|