Permalink
Browse files

Merge pull request #20 from fastrde/timer

TimerClass to frequently update touches (Issue #3)

'enableTimerClass' switch the TimerClass on and off for testing porposes.
false -> every single touch is send directly
true  -> touches are queued and send 'updateRate' times per second.
  • Loading branch information...
fastrde committed Aug 28, 2012
2 parents 12d57c4 + dbf9a46 commit 0ea9cdb3b14f24f944421f101463c825748b80cd
Showing with 95 additions and 12 deletions.
  1. +54 −12 src/com/changeit/wmpolyfill/WebClient.java
  2. +41 −0 src/com/changeit/wmpolyfill/WebClientTouchUpdater.java
@@ -4,6 +4,10 @@
*/
package com.changeit.wmpolyfill;
+
+import java.util.ArrayList;
+import java.util.Timer;
+
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;
@@ -50,6 +54,14 @@
/** Maintains a reference to the webview object for coding convenience reasons */
private WebView view;
+ /** Parameters for TouchUpdater */
+ private boolean enableTimerClass = false; // true = use TimerClass to queue and send events, false = send events directly
+ private int updateRate = 60; //Framerate for updates (Default: 60 Frames per second) play with it to see the difference ( =1 =5 etc.)
+ private ArrayList<String> updateTouches = new ArrayList<String>(); //holds touches since the last update
+ private Timer updateTimer = new Timer();
+ /** Reduce move events (higher value = less moveevents but jumpy */
+ private int pixelTolerance = 5; //0 = no tolerance
+
/**
* Constructor
* Enables Javascript2Java an vice versa.
@@ -87,10 +99,14 @@ public void onLoadResource(WebView view, String url) {
@Override
public void onPageFinished(WebView view, String url)
{
-// android.util.Log.v("console", "pagefinished_" + url);
+ //android.util.Log.v("console", "pagefinished_" + url);
if (Build.VERSION.SDK_INT <= 10) {
//this.view = view;
injectWMPJs();
+ if (enableTimerClass){
+ updateTimer.schedule(new WebClientTouchUpdater(this, view), 0, (1000/updateRate));
+ android.util.Log.d("debug-console", "TimerClass enabled");
+ }
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
WebView view = (WebView) arg0;
@@ -101,13 +117,18 @@ public boolean onTouch(View arg0, MotionEvent arg1) {
*/
if (moveBuffer.length() > 0 || arg1.getAction() != MotionEvent.ACTION_MOVE) {
String EventJSON = getEvent(arg1);
- view.loadUrl("javascript: WMP.polyfill(" + EventJSON + ");");
-
-// android.util.Log.d("debug-console", EventJSON);
+ if (enableTimerClass){
+ // add touches to buffer for TouchUpdater
+ updateTouches.add(EventJSON);
+
+ }else{
+ //send Touches directly
+ view.loadUrl("javascript: WMP.polyfill(" + EventJSON + ");");
+ }
+ android.util.Log.d("debug-console", EventJSON);
}
return true;
}
-
/**
* FALSE : let other handlers do their work (good if we want to test for already working touchevents)
* TRUE : stop propagating / bubbling event to other handlers (good if we don't want selection or zoom handlers to happen in webview)
@@ -117,7 +138,27 @@ public boolean onTouch(View arg0, MotionEvent arg1) {
});
}
}
-
+ /**
+ * Returns the collected touches and clears the TouchBuffer
+ * (needed for TouchUpdater)
+ * @author fastrde
+ * @return
+ */
+ public ArrayList<String> getTouches(){
+ ArrayList<String> tmpTouches = updateTouches;
+ updateTouches = new ArrayList<String>();
+ return tmpTouches;
+ }
+ /**
+ * set the updateRate for the TouchUpdater
+ * @author fastrde
+ * @param rate UpdateRate in updates per second
+ */
+ public void setUpdateRate(int rate){
+ if (rate > 0 && rate < 160){ //check for bounding TODO: proper bounding?
+ this.updateRate = rate;
+ }
+ }
/**
* Update this.moveBuffer with any new touches of concern
*
@@ -135,12 +176,13 @@ private boolean updateMoveBuffer(WebView view, MotionEvent event) {
for (int i = 0; i < event.getPointerCount(); i++)
{
- if ( (int)lastMotionEvent.getX(i) == (int)event.getX(i)
- && (int)lastMotionEvent.getY(i) == (int)event.getY(i)
- // Ignore Events outside of viewport
- || (int)event.getX(i) > view.getWidth()
- || (int)event.getY(i) > view.getHeight())
- continue;
+ if (
+ Math.abs((int)lastMotionEvent.getX(i)-(int)event.getX(i)) <= pixelTolerance
+ && Math.abs((int)lastMotionEvent.getY(i)-(int)event.getY(i)) <= pixelTolerance
+ // Ignore Events outside of viewport
+ || (int)event.getX(i) > view.getWidth()
+ || (int)event.getY(i) > view.getHeight())
+ continue;
addMoveToBuffer(event, i);
}
@@ -0,0 +1,41 @@
+
+package com.changeit.wmpolyfill;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TimerTask;
+import android.util.Log;
+import android.webkit.WebView;
+
+/**
+ * Class implements a TimerTask that sends the touches collected by WebClient to the WebApp
+ * @author fastr
+ *
+ */
+public class WebClientTouchUpdater extends TimerTask{
+ private WebView view;
+ private WebClient wmp;
+ @Override
+ /**
+ * Firing the touches collected between the last time and now
+ */
+ public void run() {
+ ArrayList<String> touches = wmp.getTouches();
+ Iterator<String> itr = touches.iterator();
+ while (itr.hasNext()) {
+ Log.d("wmp.console","Sending Polyfills");
+ //TODO: hand over the complete array in _one_ javascript call...
+ view.loadUrl("javascript: WMP.polyfill(" + itr.next() + ");");
+ }
+ }
+ /**
+ * Constructor
+ * @param wmp
+ * @param view
+ */
+ public WebClientTouchUpdater(WebClient wmp, WebView view){
+ super();
+ this.wmp = wmp;
+ this.view = view;
+ }
+}

0 comments on commit 0ea9cdb

Please sign in to comment.