package jasenmoloy.wirelesscontrol.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import jasenmoloy.wirelesscontrol.data.Constants;
import jasenmoloy.wirelesscontrol.data.GeofenceData;
import jasenmoloy.wirelesscontrol.debug.Debug;

/**
 * Created by jasenmoloy on 4/26/16.
 */
public class GeofenceCardView extends CardView {
    /// ----------------------
    /// Class Fields
    /// ----------------------

    /// ----------------------
    /// Object Fields
    /// ----------------------

    int mPosition;
    GeofenceData mData;
    int mLastEvent;

    /// ----------------------
    /// Getters / Setters
    /// ----------------------

    /// ----------------------
    /// Public Methods
    /// ----------------------

    public GeofenceCardView(Context context) {
        super(context);
    }

    public GeofenceCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GeofenceCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setData(int position, GeofenceData data) {
        mPosition = position;
        mData = data;
    }

    /// ----------------------
    /// Callback Methods
    /// ----------------------

    /**
     * Handle an user's touch response to open an edit geofence activity.
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Debug.logDebug("GeofenceCardView", "position: " + mPosition + " ev.getAction(): " + ev.getAction());
        int action = ev.getAction();

        switch(action) {
            case MotionEvent.ACTION_UP:
                //If the user's last action was pressing down, then we're attempting to "tap"
                // the button rather than scroll.
                if(mLastEvent == MotionEvent.ACTION_DOWN) {
                    Context context = getContext();
                    Intent intent = new Intent(context, EditGeofenceActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); //Prevents reinstantiation if the activity already exists
                    intent.putExtra(Constants.BROADCAST_EXTRA_KEY_GEOFENCE_ID, mPosition);
                    intent.putExtra(Constants.BROADCAST_EXTRA_KEY_GEODATA, mData);
                    context.startActivity(intent);
                }
                break;
        }

        mLastEvent = action;
        return true;
    }

    /**
     * Intecept all touch events heading to the children to be handled here.
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true; //JAM Intercept all touch events within this view group
    }

    /// ----------------------
    /// Protected Methods
    /// ----------------------

    /// ----------------------
    /// Private Methods
    /// ----------------------
}

@@ -173,16 +173,53 @@
 uteSet;%0A
+import android.view.GestureDetector;%0A
 import a
@@ -350,55 +350,8 @@
 ata;
-%0Aimport jasenmoloy.wirelesscontrol.debug.Debug;
 %0A%0A/*
@@ -440,16 +440,61 @@
 ardView 
+implements GestureDetector.OnGestureListener 
 %7B%0A    //
@@ -652,24 +652,64 @@
 ----------%0A%0A
+    GestureDetector mGestureDectector;%0A%0A
     int mPos
@@ -742,28 +742,8 @@
 ata;
-%0A    int mLastEvent;
 %0A%0A  
@@ -982,24 +982,40 @@
 r(context);%0A
+        init();%0A
     %7D%0A%0A    p
@@ -1103,24 +1103,40 @@
 xt, attrs);%0A
+        init();%0A
     %7D%0A%0A    p
@@ -1252,16 +1252,32 @@
 Style);%0A
+        init();%0A
     %7D%0A%0A 
@@ -1679,991 +1679,42 @@
     
-Debug.logDebug(%22GeofenceCardView%22, %22position: %22 + mPosition + %22 ev.getAction(): %22 + ev.getAction());%0A        int action = ev.getAction();%0A%0A        switch(action) %7B%0A            case MotionEvent.ACTION_UP:%0A                //If the user's last action was pressing down, then we're attempting to %22tap%22%0A                // the button rather than scroll.%0A                if(mLastEvent == MotionEvent.ACTION_DOWN) %7B%0A                    Context context = getContext();%0A                    Intent intent = new Intent(context, EditGeofenceActivity.class);%0A                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); //Prevents reinstantiation if the activity already exists%0A                    intent.putExtra(Constants.BROADCAST_EXTRA_KEY_GEOFENCE_ID, mPosition);%0A                    intent.putExtra(Constants.BROADCAST_EXTRA_KEY_GEODATA, mData);%0A                    context.startActivity(intent);%0A                %7D%0A                break;%0A        %7D%0A%0A        mLastEvent = action
+mGestureDectector.onTouchEvent(ev)
 ;%0A  
@@ -1735,24 +1735,26 @@
 rue;%0A    %7D%0A%0A
+%0A%0A
     /**%0A    
@@ -2015,32 +2015,1045 @@
 ew group%0A    %7D%0A%0A
+    @Override%0A    public boolean onDown(MotionEvent e) %7B%0A        return false;%0A    %7D%0A%0A    @Override%0A    public void onShowPress(MotionEvent e) %7B%0A%0A    %7D%0A%0A    @Override%0A    public boolean onSingleTapUp(MotionEvent e) %7B%0A        Context context = getContext();%0A        Intent intent = new Intent(context, EditGeofenceActivity.class);%0A        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); //Prevents reinstantiation if the activity already exists%0A        intent.putExtra(Constants.BROADCAST_EXTRA_KEY_GEOFENCE_ID, mPosition);%0A        intent.putExtra(Constants.BROADCAST_EXTRA_KEY_GEODATA, mData);%0A        context.startActivity(intent);%0A        return true;%0A    %7D%0A%0A    @Override%0A    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) %7B%0A        return false;%0A    %7D%0A%0A    @Override%0A    public void onLongPress(MotionEvent e) %7B%0A%0A    %7D%0A%0A    @Override%0A    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) %7B%0A        return false;%0A    %7D%0A%0A
     /// --------
@@ -3211,10 +3211,167 @@
 -------%0A
+    private void init() %7B%0A        mGestureDectector = new GestureDetector(getContext(), this);%0A        mGestureDectector.setIsLongpressEnabled(false);%0A    %7D%0A
 %7D%0A

Improve method for determining tap vs. scroll on GeofenceCardView using GestureDetector