Skip to content

Commit

Permalink
A few tweaks.
Browse files Browse the repository at this point in the history
Changed the javascript to use parseFloat for localization.
  • Loading branch information
btate committed Aug 5, 2013
1 parent fe8c2ae commit 68278ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
6 changes: 3 additions & 3 deletions assets/android.selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android.selection.lastTouchPoint = null;
*/
android.selection.startTouch = function(x, y){

android.selection.lastTouchPoint = {'x': x, 'y': y};
android.selection.lastTouchPoint = {'x': parseFloat(x + ""), 'y': parseFloat(y + "")};

};

Expand Down Expand Up @@ -222,7 +222,7 @@ android.selection.saveSelectionEnd = function(){
android.selection.setStartPos = function(x, y){

try{
android.selection.selectionStartRange = document.caretRangeFromPoint(x, y);
android.selection.selectionStartRange = document.caretRangeFromPoint(parseFloat(x + ""), parseFloat(y + ""));

android.selection.selectBetweenHandles();
}catch(err){
Expand All @@ -237,7 +237,7 @@ android.selection.setStartPos = function(x, y){
android.selection.setEndPos = function(x, y){

try{
android.selection.selectionEndRange = document.caretRangeFromPoint(x, y);
android.selection.selectionEndRange = document.caretRangeFromPoint(parseFloat(x + ""), parseFloat(y + ""));

android.selection.selectBetweenHandles();

Expand Down
62 changes: 37 additions & 25 deletions src/com/brandontate/androidwebviewselection/BTWebView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.brandontate.androidwebviewselection;

import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -41,22 +43,22 @@ public class BTWebView extends WebView implements TextSelectionJavascriptInterfa
protected Context ctx;

/** The context menu. */
private QuickAction mContextMenu;
protected QuickAction mContextMenu;

/** The drag layer for selection. */
private DragLayer mSelectionDragLayer;
protected DragLayer mSelectionDragLayer;

/** The drag controller for selection. */
private DragController mDragController;
protected DragController mDragController;

/** The start selection handle. */
private ImageView mStartSelectionHandle;
protected ImageView mStartSelectionHandle;

/** the end selection handle. */
private ImageView mEndSelectionHandle;
protected ImageView mEndSelectionHandle;

/** The selection bounds. */
private Rect mSelectionBounds = null;
protected Rect mSelectionBounds = null;

/** The previously selected region. */
protected Region lastSelectedRegion = null;
Expand All @@ -81,15 +83,16 @@ public class BTWebView extends WebView implements TextSelectionJavascriptInterfa


/** Identifier for the selection start handle. */
private final int SELECTION_START_HANDLE = 0;
protected final int SELECTION_START_HANDLE = 0;

/** Identifier for the selection end handle. */
private final int SELECTION_END_HANDLE = 1;
protected final int SELECTION_END_HANDLE = 1;

/** Last touched selection handle. */
private int mLastTouchedSelectionHandle = -1;


protected int mLastTouchedSelectionHandle = -1;

/** The current scale of the web view. */
protected float mCurrentScale = 1.0f;

public BTWebView(Context context) {
super(context);
Expand Down Expand Up @@ -130,11 +133,9 @@ public BTWebView(Context context, AttributeSet attrs) {
@Override
public boolean onTouch(View v, MotionEvent event) {

float xPoint = getDensityIndependentValue(event.getX(), ctx) / getDensityIndependentValue(this.getScale(), ctx);
float yPoint = getDensityIndependentValue(event.getY(), ctx) / getDensityIndependentValue(this.getScale(), ctx);

// TODO: Need to update this to use this.getScale() as a factor.

float xPoint = getDensityIndependentValue(event.getX(), ctx) / getDensityIndependentValue(getScale(), ctx);
float yPoint = getDensityIndependentValue(event.getY(), ctx) / getDensityIndependentValue(getScale(), ctx);

if(event.getAction() == MotionEvent.ACTION_DOWN){

String startTouchUrl = String.format("javascript:android.selection.startTouch(%f, %f);",
Expand Down Expand Up @@ -224,7 +225,21 @@ protected void setup(Context context){
// Webview setup
this.getSettings().setJavaScriptEnabled(true);
this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.getSettings().setPluginsEnabled(true);
this.getSettings().setPluginState(WebSettings.PluginState.ON);
//this.getSettings().setBuiltInZoomControls(true);

// Webview client.
setWebViewClient(new WebViewClient(){
// This is how it is supposed to work, so I'll leave it in, but this doesn't get called on pinch
// So for now I have to use deprected getScale method.
@Override
public void onScaleChanged(WebView view, float oldScale, float newScale) {
super.onScaleChanged(view, oldScale, newScale);
mCurrentScale = newScale;
}
});



// Zoom out fully
//this.getSettings().setLoadWithOverviewMode(true);
Expand Down Expand Up @@ -340,8 +355,7 @@ public void handleMessage(Message m){

/**
* Starts selection mode.
*
* @param selectionBounds
*
*/
public void startSelectionMode(){

Expand Down Expand Up @@ -461,7 +475,7 @@ public void onDragEnd() {
MyAbsoluteLayout.LayoutParams startHandleParams = (MyAbsoluteLayout.LayoutParams) this.mStartSelectionHandle.getLayoutParams();
MyAbsoluteLayout.LayoutParams endHandleParams = (MyAbsoluteLayout.LayoutParams) this.mEndSelectionHandle.getLayoutParams();

float scale = getDensityIndependentValue(this.getScale(), ctx);
float scale = getDensityIndependentValue(getScale(), ctx);

float startX = startHandleParams.x - this.getScrollX();
float startY = startHandleParams.y - this.getScrollY();
Expand Down Expand Up @@ -495,9 +509,9 @@ public void onDragEnd() {

/**
* Shows the context menu using the given region as an anchor point.
* @param region
* @param displayRect
*/
private void showContextMenu(Rect displayRect){
protected void showContextMenu(Rect displayRect){

// Don't show this twice
if(this.contextMenuVisible){
Expand Down Expand Up @@ -632,14 +646,12 @@ public void tsjiEndSelectionMode(){
* @param text
* @param handleBounds
* @param menuBounds
* @param showHighlight
* @param showUnHighlight
*/
public void tsjiSelectionChanged(String range, String text, String handleBounds, String menuBounds){
try {
JSONObject selectionBoundsObject = new JSONObject(handleBounds);

float scale = getDensityIndependentValue(this.getScale(), ctx);
float scale = getDensityIndependentValue(getScale(), ctx);

Rect handleRect = new Rect();
handleRect.left = (int) (getDensityDependentValue(selectionBoundsObject.getInt("left"), getContext()) * scale);
Expand Down

0 comments on commit 68278ff

Please sign in to comment.