/*
 * Copyright (c) 2013 Menny Even-Danan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.anysoftkeyboard.devicespecific;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import com.anysoftkeyboard.utils.Log;

@TargetApi(8)
public class AskV8GestureDetector extends GestureDetector {
    private static final String TAG = "AskV8GestureDetector";

    private final ScaleGestureDetector mScaleGestureDetector;
    private final AskOnGestureListener mListener;

    public AskV8GestureDetector(Context context, AskOnGestureListener listener,
                                Handler handler, boolean ignoreMultitouch) {
        super(context, listener, handler, ignoreMultitouch);

        mListener = listener;

        mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                final float factor = detector.getScaleFactor();
                Log.d(TAG, "onScale factor " + factor);

                if (factor > 1.1)
                    return mListener.onSeparate(factor);
                else if (factor < 0.9)
                    return mListener.onPinch(factor);

                return false;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            //https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/26
            mScaleGestureDetector.onTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            //I have nothing I can do here.
        } catch (ArrayIndexOutOfBoundsException e) {
            //I have nothing I can do here.
        }
        return super.onTouchEvent(ev);
    }

}

@@ -735,16 +735,66 @@
 andler;%0A
+import android.support.v4.view.MotionEventCompat;%0A
 import a
@@ -1074,16 +1074,69 @@
 ctor%22;%0A%0A
+    private static final int NOT_A_POINTER_ID = -1;%0A%0A
     priv
@@ -1236,24 +1236,89 @@
 mListener;%0A%0A
+    private int mSingleFingerEventPointerId = NOT_A_POINTER_ID;%0A%0A
     public A
@@ -2214,376 +2214,1265 @@
     
-try %7B%0A            //https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/26%0A            mScaleGestureDetector.onTouchEvent(ev);%0A        %7D catch (IllegalArgumentException e) %7B%0A            //I have nothing I can do here.%0A        %7D catch (ArrayIndexOutOfBoundsException e) %7B%0A            //I have nothing I can do here.%0A        %7D%0A        return super.onTouchEvent(ev)
+//I want to keep track on the first finger (https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/300)%0A        switch (MotionEventCompat.getActionMasked(ev))%7B%0A            case MotionEvent.ACTION_DOWN:%0A                if (ev.getPointerCount() == 1)%0A                    mSingleFingerEventPointerId = ev.getPointerId(0);%0A                break;%0A            case MotionEvent.ACTION_CANCEL:%0A            case MotionEvent.ACTION_UP:%0A                if (ev.getPointerCount() == 1)%0A                    mSingleFingerEventPointerId = NOT_A_POINTER_ID;%0A        %7D%0A        try %7B%0A            //https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/26%0A            mScaleGestureDetector.onTouchEvent(ev);%0A        %7D catch (IllegalArgumentException e) %7B%0A            //I have nothing I can do here.%0A        %7D catch (ArrayIndexOutOfBoundsException e) %7B%0A            //I have nothing I can do here.%0A        %7D%0A        //I'm going to pass the event to the super, only if it is a single touch, and the event is for the first finger%0A        //https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/300%0A        if (ev.getPointerCount() == 1 && ev.getPointerId(0) == mSingleFingerEventPointerId)%0A            return super.onTouchEvent(ev);%0A        else%0A            return false
 ;%0A  

fix for issue #300. The gesture detector was not handling multiple touches very well. All good now.