package fr._46cl.focus;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

import android.os.SystemClock;
import android.view.MotionEvent;
import android.util.DisplayMetrics;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.KeyEvent;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.inputmethod.InputMethodManager;

public class Focus extends CordovaPlugin {

    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
            super.initialize(cordova, webView);
    }

    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if ("focus".equals(action)) {

            // Get device pixel density
            DisplayMetrics metrics = this.cordova.getActivity().getApplicationContext().getResources().getDisplayMetrics();
            float density = metrics.density;

            // Get bounding positions of target element
            JSONObject rect = args.getJSONObject(0);
            float left = (density *  rect.getInt("left"));
            float top = (density * rect.getInt("top"));
            float right = (density * rect.getInt("right"));
            float bottom = (density * rect.getInt("bottom"));

            // Compute its center
            final Integer centerLeft = (int) (left + ((right - left) / 2));
            final Integer centerTop = (int) (top + ((bottom - top) / 2));

            // Emulate click
            cordova.getActivity().runOnUiThread(new Runnable() {
              public void run() {
                webView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, centerLeft, centerTop, 0));
                webView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, centerLeft, centerTop, 0));
              }
            });
            return true;
        }
        return false;
    }
}


@@ -1764,25 +1764,24 @@
 top) / 2));%0A
-%0A
             
@@ -1892,24 +1892,87 @@
 oid run() %7B%0A
+                final long uMillis=SystemClock.uptimeMillis();%0A
             
@@ -2025,62 +2025,24 @@
 ain(
-SystemClock.uptimeMillis(), SystemClock.uptime
+uMillis, u
 Millis
-()
 , Mo
@@ -2158,62 +2158,24 @@
 ain(
-SystemClock.uptimeMillis(), SystemClock.uptime
+uMillis, u
 Millis
-()
 , Mo

fix issue #1 on master