Draw text selection in Java
Bug: 5208328
Bug: 5224394
Also use framework assets
Change-Id: If704fd7df5aa95648d78e615571d4a2658d29534
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index 4748522..5200b12 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -34,7 +34,6 @@
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
-import android.graphics.CornerPathEffect;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
@@ -58,8 +57,6 @@
import android.os.StrictMode;
import android.provider.Settings;
import android.speech.tts.TextToSpeech;
-import android.text.Selection;
-import android.text.Spannable;
import android.util.AttributeSet;
import android.util.EventLog;
import android.util.Log;
@@ -606,9 +603,15 @@
// know to handle Shift and arrows natively first
private boolean mAccessibilityScriptInjected;
+ static final boolean USE_JAVA_TEXT_SELECTION = true;
+ private Region mTextSelectionRegion = new Region();
+ private Paint mTextSelectionPaint;
+ private Drawable mSelectHandleLeft;
+ private Drawable mSelectHandleRight;
+
static final boolean USE_WEBKIT_RINGS = true;
// the color used to highlight the touch rectangles
- private static final int mHightlightColor = 0x6633b5e5;
+ private static final int HIGHLIGHT_COLOR = 0x6633b5e5;
// the round corner for the highlight path
private static final float TOUCH_HIGHLIGHT_ARC = 5.0f;
// the region indicating where the user touched on the screen
@@ -4050,8 +4053,10 @@
@Override
protected void onDraw(Canvas canvas) {
- // if mNativeClass is 0, the WebView has been destroyed. Do nothing.
+ // if mNativeClass is 0, the WebView is either destroyed or not
+ // initialized. In either case, just draw the background color and return
if (mNativeClass == 0) {
+ canvas.drawColor(mBackgroundColor);
return;
}
@@ -4105,7 +4110,7 @@
} else {
if (mTouchHightlightPaint == null) {
mTouchHightlightPaint = new Paint();
- mTouchHightlightPaint.setColor(mHightlightColor);
+ mTouchHightlightPaint.setColor(HIGHLIGHT_COLOR);
}
RegionIterator iter = new RegionIterator(mTouchHighlightRegion);
Rect r = new Rect();
@@ -4400,7 +4405,7 @@
int extras = DRAW_EXTRAS_NONE;
if (mFindIsUp) {
extras = DRAW_EXTRAS_FIND;
- } else if (mSelectingText) {
+ } else if (mSelectingText && !USE_JAVA_TEXT_SELECTION) {
extras = DRAW_EXTRAS_SELECTION;
nativeSetSelectionPointer(mDrawSelectionPointer,
mZoomManager.getInvScale(),
@@ -4427,6 +4432,10 @@
nativeUseHardwareAccelSkia(mHardwareAccelSkia);
}
+ if (mSelectingText && USE_JAVA_TEXT_SELECTION) {
+ drawTextSelectionHandles(canvas);
+ }
+
} else {
DrawFilter df = null;
if (mZoomManager.isZoomAnimating() || UIAnimationsRunning) {
@@ -4461,6 +4470,56 @@
}
}
+ private void drawTextSelectionHandles(Canvas canvas) {
+ if (mTextSelectionPaint == null) {
+ mTextSelectionPaint = new Paint();
+ mTextSelectionPaint.setColor(HIGHLIGHT_COLOR);
+ }
+ mTextSelectionRegion.setEmpty();
+ nativeGetTextSelectionRegion(mTextSelectionRegion);
+ Rect r = new Rect();
+ RegionIterator iter = new RegionIterator(mTextSelectionRegion);
+ int start_x = -1;
+ int start_y = -1;
+ int end_x = -1;
+ int end_y = -1;
+ while (iter.next(r)) {
+ r = new Rect(
+ contentToViewDimension(r.left),
+ contentToViewDimension(r.top),
+ contentToViewDimension(r.right),
+ contentToViewDimension(r.bottom));
+ // Regions are in order. First one is where selection starts,
+ // last one is where it ends
+ if (start_x < 0 || start_y < 0) {
+ start_x = r.left;
+ start_y = r.bottom;
+ }
+ end_x = r.right;
+ end_y = r.bottom;
+ canvas.drawRect(r, mTextSelectionPaint);
+ }
+ if (mSelectHandleLeft == null) {
+ mSelectHandleLeft = mContext.getResources().getDrawable(
+ com.android.internal.R.drawable.text_select_handle_left);
+ }
+ // Magic formula copied from TextView
+ start_x -= (mSelectHandleLeft.getIntrinsicWidth() * 3) / 4;
+ mSelectHandleLeft.setBounds(start_x, start_y,
+ start_x + mSelectHandleLeft.getIntrinsicWidth(),
+ start_y + mSelectHandleLeft.getIntrinsicHeight());
+ if (mSelectHandleRight == null) {
+ mSelectHandleRight = mContext.getResources().getDrawable(
+ com.android.internal.R.drawable.text_select_handle_right);
+ }
+ end_x -= mSelectHandleRight.getIntrinsicWidth() / 4;
+ mSelectHandleRight.setBounds(end_x, end_y,
+ end_x + mSelectHandleRight.getIntrinsicWidth(),
+ end_y + mSelectHandleRight.getIntrinsicHeight());
+ mSelectHandleLeft.draw(canvas);
+ mSelectHandleRight.draw(canvas);
+ }
+
// draw history
private boolean mDrawHistory = false;
private Picture mHistoryPicture = null;
@@ -9329,4 +9388,5 @@
private native int nativeGetBackgroundColor();
native boolean nativeSetProperty(String key, String value);
native String nativeGetProperty(String key);
+ private native void nativeGetTextSelectionRegion(Region region);
}