Skip to content

Commit

Permalink
Initial Project Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
btate committed Jul 6, 2012
0 parents commit c6fee3b
Show file tree
Hide file tree
Showing 65 changed files with 13,372 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.metadata
tmp/**
.DS_Store
*.tmp
*.bak
tmp/**/*
*.swp
*~.nib
Thumbs.db
Desktop.ini
*~
*.apk
bin
gen
local.properties
*.jar
.classpath
33 changes: 33 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>BT Android WebView Selection</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
28 changes: 28 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.brandontate.androidwebviewselection"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".BTAndroidWebViewSelectionActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<provider android:name="com.brandontate.androidwebviewselection.BTInternalContentProvider"
android:authorities="com.brandontate.androidwebviewselection.internal" />
</application>

</manifest>
Empty file added README.md
Empty file.
294 changes: 294 additions & 0 deletions assets/android.selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
// Namespace
var android = {};
android.selection = {};

android.selection.selectionStartRange = null;
android.selection.selectionEndRange = null;


/** The last point touched by the user. { 'x': xPoint, 'y': yPoint } */
android.selection.lastTouchPoint = null;


/**
* Starts the touch and saves the given x and y coordinates as last touch point
*/
android.selection.startTouch = function(x, y){

android.selection.lastTouchPoint = {'x': x, 'y': y};

};



/**
* Checks to see if there is a selection.
*
* @return boolean
*/
android.selection.hasSelection = function(){
return window.getSelection().toString().length > 0;
};


/**
* Clears the current selection.
*/
android.selection.clearSelection = function(){

try{
// if current selection clear it.
var sel = window.getSelection();
sel.removeAllRanges();
}catch(err){
window.TextSelection.jsError(err);
}
};


/**
* Handles the long touch action by selecting the last touched element.
*/
android.selection.longTouch = function() {

try{

android.selection.clearSelection();

// if current selection clear it.
var sel = window.getSelection();

var oneWordCaret = document.caretRangeFromPoint(android.selection.lastTouchPoint.x, android.selection.lastTouchPoint.y);
oneWordCaret.expand("word");


sel.addRange(oneWordCaret);

var temp = sel.getRangeAt(0);

android.selection.saveSelectionStart();
android.selection.saveSelectionEnd();


// Show the context menu in app
android.selection.selectionChanged();
//android.selection.selectBetweenHandles();

}
catch(err){
window.TextSelection.jsError(err);
}

};

/**
* Tells the app to show the context menu.
*/
android.selection.selectionChanged = function(){

try{

var sel = window.getSelection();
if(!sel){
return;
}

var range = sel.getRangeAt(0);


// Add spans to the selection to get page offsets
var selectionEnd = $("<span id=\"selectionEnd\"/>");
var selectionStart = $("<span id=\"selectionStart\"/>");


var startRange = document.createRange();
startRange.setStart(range.startContainer, range.startOffset);
startRange.insertNode(selectionStart[0]);

var endRange = document.createRange();
endRange.setStart(range.endContainer, range.endOffset);
endRange.insertNode(selectionEnd[0]);


// Create the bounds json object for the selection
var handleBounds = "{'left': " + selectionStart.offset().left + ", ";
handleBounds += "'top': " + selectionStart.offset().top + ", ";
handleBounds += "'right': " + selectionEnd.offset().left + ", ";
handleBounds += "'bottom': " + selectionEnd.offset().top + "}";


// Pull the spans
selectionStart.remove();
selectionEnd.remove();

// Reset range
sel.removeAllRanges();
sel.addRange(range);

// Menu bounds
var rect = range.getBoundingClientRect();

var menuBounds = "{'left': " + rect.left + ", ";
menuBounds += "'top': " + rect.top + ", ";
menuBounds += "'right': " + rect.right + ", ";
menuBounds += "'bottom': " + rect.bottom + "}";

// Rangy
var rangyRange = android.selection.getRange();

// Text to send to the selection
var text = window.getSelection().toString();

// Set the content width
window.TextSelection.setContentWidth(document.body.clientWidth);

// Tell the interface that the selection changed
window.TextSelection.selectionChanged(rangyRange, text, handleBounds, menuBounds);
}
catch(err){
window.TextSelection.jsError(err);
}
};



android.selection.getRange = function() {
var serializedRangeSelected = rangy.serializeSelection();
var serializerModule = rangy.modules.Serializer;
if (serializedRangeSelected != '') {
if (rangy.supported && serializerModule && serializerModule.supported) {
var beginingCurly = serializedRangeSelected.indexOf("{");
serializedRangeSelected = serializedRangeSelected.substring(0, beginingCurly);
return serializedRangeSelected;
}
}
}



/**
* Returns the last touch point as a readable string.
*/
android.selection.lastTouchPointString = function(){
if(android.selection.lastTouchPoint == null)
return "undefined";

return "{" + android.selection.lastTouchPoint.x + "," + android.selection.lastTouchPoint.y + "}";
};



android.selection.saveSelectionStart = function(){
try{

// Save the starting point of the selection
var sel = window.getSelection();
var range = sel.getRangeAt(0);

var saveRange = document.createRange();

saveRange.setStart(range.startContainer, range.startOffset);

android.selection.selectionStartRange = saveRange;
}catch(err){
window.TextSelection.jsError(err);
}

};

android.selection.saveSelectionEnd = function(){

try{

// Save the end point of the selection
var sel = window.getSelection();
var range = sel.getRangeAt(0);

var saveRange = document.createRange();
saveRange.setStart(range.endContainer, range.endOffset);

android.selection.selectionEndRange = saveRange;
}catch(err){
window.TextSelection.jsError(err);
}

};



/**
* Sets the last caret position for the start handle.
*/
android.selection.setStartPos = function(x, y){

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

android.selection.selectBetweenHandles();
}catch(err){
window.TextSelection.jsError(err);
}

};

/**
* Sets the last caret position for the end handle.
*/
android.selection.setEndPos = function(x, y){

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

android.selection.selectBetweenHandles();

}catch(err){
window.TextSelection.jsError(err);
}

};

/**
* Selects all content between the two handles
*/
android.selection.selectBetweenHandles = function(){

try{
var startCaret = android.selection.selectionStartRange;
var endCaret = android.selection.selectionEndRange;

// If we have two carets, update the selection
if (startCaret && endCaret) {

// If end caret comes before start caret, need to flip
if(startCaret.compareBoundaryPoints (Range.START_TO_END, endCaret) > 0){
var temp = startCaret;
startCaret = endCaret;
endCaret = temp;

android.selection.selectionStartRange = startCaret;
android.selection.selectionEndRange = endCaret;
}

var range = document.createRange();
range.setStart(startCaret.startContainer, startCaret.startOffset);
range.setEnd(endCaret.startContainer, endCaret.startOffset);


android.selection.clearSelection();

var selection = window.getSelection();
selection.addRange(range);



}

android.selection.selectionChanged();
}
catch(err){
window.TextSelection.jsError(err);
}
};



Loading

0 comments on commit c6fee3b

Please sign in to comment.