package com.mikekorcha.mediabuttonoverlay.listeners;

import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;


public abstract class OnMediaButtonTouchListener implements View.OnTouchListener {

    private boolean isMoving;

    private int initialX;
    private int initialY;
    private float initialTouchX;
    private float initialTouchY;

    private WindowManager.LayoutParams layoutParams;

    protected OnMediaButtonTouchListener(WindowManager.LayoutParams params) {
        this.layoutParams = params;
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialX = layoutParams.x;
                initialY = layoutParams.y;
                initialTouchX = motionEvent.getRawX();
                initialTouchY = motionEvent.getRawY();
                return true;

            case MotionEvent.ACTION_MOVE:
                if(Math.abs(motionEvent.getX()) < view.getWidth() ||
                        Math.abs(motionEvent.getY()) < view.getHeight()) {
                    isMoving = true;

                    int[] coords = getCoords(motionEvent);
                    onDrag(view, motionEvent, coords[0], coords[1]);
                }
                return true;

            case MotionEvent.ACTION_UP:
                if(isMoving) {
                    isMoving = false;

                    int[] coords = getCoords(motionEvent);
                    onDrop(view, motionEvent, coords[0], coords[1]);
                }
                else {
                    onClick();
                }
                return true;
        }
        return false;
    }

    private int[] getCoords(MotionEvent motionEvent) {
        return new int[] {initialX + (int) (motionEvent.getRawX() - initialTouchX),
                          initialY + (int) (motionEvent.getRawY() - initialTouchY)};
    }

    public abstract void onClick();
    public abstract void onDrag(View view, MotionEvent motionEvent, int screenX, int screenY);
    public abstract void onDrop(View view, MotionEvent motionEvent, int screenX, int screenY);
}

@@ -47,16 +47,41 @@
 eners;%0A%0A
+import android.util.Log;%0A
 import a
@@ -634,32 +634,79 @@
  motionEvent) %7B%0A
+        int%5B%5D coords = getCoords(motionEvent);%0A
         switch(m
@@ -1064,38 +1064,54 @@
  if(
-Math.abs(motionEvent.getX()) %3C
+coords%5B0%5D %3C initialX %7C%7C coords%5B0%5D %3E initialX +
  vie
@@ -1122,19 +1122,16 @@
 tWidth()
- %7C%7C
 %0A       
@@ -1151,38 +1151,57 @@
     
-Math.abs(motionEvent.getY()) %3C
+%7C%7C coords%5B1%5D %3C initialY %7C%7C coords%5B1%5D %3E initialY +
  vie
@@ -1259,67 +1259,8 @@
 e;%0A%0A
-                    int%5B%5D coords = getCoords(motionEvent);%0A
     
@@ -1363,32 +1363,36 @@
    return true;%0A
+    
 %0A            cas
@@ -1490,67 +1490,8 @@
 e;%0A%0A
-                    int%5B%5D coords = getCoords(motionEvent);%0A
     

Improve move vs click accuracy (#1)