Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.gfx.ImmutableViewportMetrics; |
michael@0 | 8 | import org.mozilla.gecko.gfx.LayerView; |
michael@0 | 9 | |
michael@0 | 10 | import org.json.JSONObject; |
michael@0 | 11 | |
michael@0 | 12 | import android.content.Context; |
michael@0 | 13 | import android.content.res.TypedArray; |
michael@0 | 14 | import android.graphics.PointF; |
michael@0 | 15 | import android.util.AttributeSet; |
michael@0 | 16 | import android.util.Log; |
michael@0 | 17 | import android.view.MotionEvent; |
michael@0 | 18 | import android.view.View; |
michael@0 | 19 | import android.widget.ImageView; |
michael@0 | 20 | import android.widget.RelativeLayout; |
michael@0 | 21 | |
michael@0 | 22 | class TextSelectionHandle extends ImageView implements View.OnTouchListener { |
michael@0 | 23 | private static final String LOGTAG = "GeckoTextSelectionHandle"; |
michael@0 | 24 | |
michael@0 | 25 | private enum HandleType { START, MIDDLE, END }; |
michael@0 | 26 | |
michael@0 | 27 | private final HandleType mHandleType; |
michael@0 | 28 | private final int mWidth; |
michael@0 | 29 | private final int mHeight; |
michael@0 | 30 | private final int mShadow; |
michael@0 | 31 | |
michael@0 | 32 | private float mLeft; |
michael@0 | 33 | private float mTop; |
michael@0 | 34 | private boolean mIsRTL; |
michael@0 | 35 | private PointF mGeckoPoint; |
michael@0 | 36 | private float mTouchStartX; |
michael@0 | 37 | private float mTouchStartY; |
michael@0 | 38 | private int mLayerViewX; |
michael@0 | 39 | private int mLayerViewY; |
michael@0 | 40 | |
michael@0 | 41 | private RelativeLayout.LayoutParams mLayoutParams; |
michael@0 | 42 | |
michael@0 | 43 | private static final int IMAGE_LEVEL_LTR = 0; |
michael@0 | 44 | private static final int IMAGE_LEVEL_RTL = 1; |
michael@0 | 45 | |
michael@0 | 46 | public TextSelectionHandle(Context context, AttributeSet attrs) { |
michael@0 | 47 | super(context, attrs); |
michael@0 | 48 | setOnTouchListener(this); |
michael@0 | 49 | |
michael@0 | 50 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextSelectionHandle); |
michael@0 | 51 | int handleType = a.getInt(R.styleable.TextSelectionHandle_handleType, 0x01); |
michael@0 | 52 | |
michael@0 | 53 | if (handleType == 0x01) |
michael@0 | 54 | mHandleType = HandleType.START; |
michael@0 | 55 | else if (handleType == 0x02) |
michael@0 | 56 | mHandleType = HandleType.MIDDLE; |
michael@0 | 57 | else |
michael@0 | 58 | mHandleType = HandleType.END; |
michael@0 | 59 | |
michael@0 | 60 | mIsRTL = false; |
michael@0 | 61 | mGeckoPoint = new PointF(0.0f, 0.0f); |
michael@0 | 62 | |
michael@0 | 63 | mWidth = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_width); |
michael@0 | 64 | mHeight = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_height); |
michael@0 | 65 | mShadow = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_shadow); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | @Override |
michael@0 | 69 | public boolean onTouch(View v, MotionEvent event) { |
michael@0 | 70 | switch (event.getActionMasked()) { |
michael@0 | 71 | case MotionEvent.ACTION_DOWN: { |
michael@0 | 72 | mTouchStartX = event.getX(); |
michael@0 | 73 | mTouchStartY = event.getY(); |
michael@0 | 74 | |
michael@0 | 75 | int[] rect = new int[2]; |
michael@0 | 76 | GeckoAppShell.getLayerView().getLocationOnScreen(rect); |
michael@0 | 77 | mLayerViewX = rect[0]; |
michael@0 | 78 | mLayerViewY = rect[1]; |
michael@0 | 79 | break; |
michael@0 | 80 | } |
michael@0 | 81 | case MotionEvent.ACTION_UP: { |
michael@0 | 82 | mTouchStartX = 0; |
michael@0 | 83 | mTouchStartY = 0; |
michael@0 | 84 | |
michael@0 | 85 | // Reposition handles to line up with ends of selection |
michael@0 | 86 | JSONObject args = new JSONObject(); |
michael@0 | 87 | try { |
michael@0 | 88 | args.put("handleType", mHandleType.toString()); |
michael@0 | 89 | } catch (Exception e) { |
michael@0 | 90 | Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Position"); |
michael@0 | 91 | } |
michael@0 | 92 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Position", args.toString())); |
michael@0 | 93 | break; |
michael@0 | 94 | } |
michael@0 | 95 | case MotionEvent.ACTION_MOVE: { |
michael@0 | 96 | move(event.getRawX(), event.getRawY()); |
michael@0 | 97 | break; |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | return true; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | private void move(float newX, float newY) { |
michael@0 | 104 | // newX and newY are absolute coordinates, so we need to adjust them to |
michael@0 | 105 | // account for other views on the screen (such as the URL bar). We also |
michael@0 | 106 | // need to include the offset amount of the touch location relative to |
michael@0 | 107 | // the top left of the handle (mTouchStartX and mTouchStartY). |
michael@0 | 108 | mLeft = newX - mLayerViewX - mTouchStartX; |
michael@0 | 109 | mTop = newY - mLayerViewY - mTouchStartY; |
michael@0 | 110 | |
michael@0 | 111 | LayerView layerView = GeckoAppShell.getLayerView(); |
michael@0 | 112 | if (layerView == null) { |
michael@0 | 113 | Log.e(LOGTAG, "Can't move selection because layerView is null"); |
michael@0 | 114 | return; |
michael@0 | 115 | } |
michael@0 | 116 | // Send x coordinate on the right side of the start handle, left side of the end handle. |
michael@0 | 117 | float left = mLeft + adjustLeftForHandle(); |
michael@0 | 118 | |
michael@0 | 119 | PointF geckoPoint = new PointF(left, mTop); |
michael@0 | 120 | geckoPoint = layerView.convertViewPointToLayerPoint(geckoPoint); |
michael@0 | 121 | |
michael@0 | 122 | JSONObject args = new JSONObject(); |
michael@0 | 123 | try { |
michael@0 | 124 | args.put("handleType", mHandleType.toString()); |
michael@0 | 125 | args.put("x", (int) geckoPoint.x); |
michael@0 | 126 | args.put("y", (int) geckoPoint.y); |
michael@0 | 127 | } catch (Exception e) { |
michael@0 | 128 | Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Move"); |
michael@0 | 129 | } |
michael@0 | 130 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Move", args.toString())); |
michael@0 | 131 | |
michael@0 | 132 | // If we're positioning a cursor, don't move the handle here. Gecko |
michael@0 | 133 | // will tell us the position of the caret, so we set the handle |
michael@0 | 134 | // position then. This allows us to lock the handle to wherever the |
michael@0 | 135 | // caret appears. |
michael@0 | 136 | if (!mHandleType.equals(HandleType.MIDDLE)) { |
michael@0 | 137 | setLayoutPosition(); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | void positionFromGecko(int left, int top, boolean rtl) { |
michael@0 | 142 | LayerView layerView = GeckoAppShell.getLayerView(); |
michael@0 | 143 | if (layerView == null) { |
michael@0 | 144 | Log.e(LOGTAG, "Can't position handle because layerView is null"); |
michael@0 | 145 | return; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | mGeckoPoint = new PointF(left, top); |
michael@0 | 149 | if (mIsRTL != rtl) { |
michael@0 | 150 | mIsRTL = rtl; |
michael@0 | 151 | setImageLevel(mIsRTL ? IMAGE_LEVEL_RTL : IMAGE_LEVEL_LTR); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | ImmutableViewportMetrics metrics = layerView.getViewportMetrics(); |
michael@0 | 155 | PointF offset = metrics.getMarginOffset(); |
michael@0 | 156 | repositionWithViewport(metrics.viewportRectLeft - offset.x, metrics.viewportRectTop - offset.y, metrics.zoomFactor); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | void repositionWithViewport(float x, float y, float zoom) { |
michael@0 | 160 | PointF viewPoint = new PointF((mGeckoPoint.x * zoom) - x, |
michael@0 | 161 | (mGeckoPoint.y * zoom) - y); |
michael@0 | 162 | |
michael@0 | 163 | mLeft = viewPoint.x - adjustLeftForHandle(); |
michael@0 | 164 | mTop = viewPoint.y; |
michael@0 | 165 | |
michael@0 | 166 | setLayoutPosition(); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | private float adjustLeftForHandle() { |
michael@0 | 170 | if (mHandleType.equals(HandleType.START)) |
michael@0 | 171 | return mIsRTL ? mShadow : mWidth - mShadow; |
michael@0 | 172 | else if (mHandleType.equals(HandleType.MIDDLE)) |
michael@0 | 173 | return mWidth / 2; |
michael@0 | 174 | else |
michael@0 | 175 | return mIsRTL ? mWidth - mShadow : mShadow; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | private void setLayoutPosition() { |
michael@0 | 179 | if (mLayoutParams == null) { |
michael@0 | 180 | mLayoutParams = (RelativeLayout.LayoutParams) getLayoutParams(); |
michael@0 | 181 | // Set negative right/bottom margins so that the handles can be dragged outside of |
michael@0 | 182 | // the content area (if they are dragged to the left/top, the dyanmic margins set |
michael@0 | 183 | // below will take care of that). |
michael@0 | 184 | mLayoutParams.rightMargin = 0 - mWidth; |
michael@0 | 185 | mLayoutParams.bottomMargin = 0 - mHeight; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | mLayoutParams.leftMargin = (int) mLeft; |
michael@0 | 189 | mLayoutParams.topMargin = (int) mTop; |
michael@0 | 190 | setLayoutParams(mLayoutParams); |
michael@0 | 191 | } |
michael@0 | 192 | } |