<commit_before>/*
 * Copyright 2011, 2012 <NAME>.
 *
 * 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.
 *
 * ********************************************************************************
 *
 * Copyright 2014-2016 <NAME>
 */

package com.mishiranu.dashchan.widget;

import java.lang.reflect.Method;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Scroller;

import com.mishiranu.dashchan.graphics.TransparentTileDrawable;
import com.mishiranu.dashchan.util.AnimationUtils;

public class PhotoView extends View implements ScaleGestureDetector.OnScaleGestureListener {
	private enum ScrollEdge {NONE, START, END, BOTH}
	private enum TouchMode {UNDEFINED, COMMON, CLOSING_START, CLOSING_END, CLOSING_BOTH}

	private static final float CLOSE_SWIPE_FACTOR = 0.25f;

	private static final int INVALID_POINTER_ID = -1;

	private final TransparentTileDrawable tile;
	private Drawable drawable;
	private boolean hasAlpha;
	private boolean fitScreen;
	private boolean drawDim;
	private final Point previousDimensions = new Point();
	private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

	private int[] initialScalingData;
	private ValueAnimator initialScalingAnimator;
	private Rect initialScaleClipRect;

	private float minimumScale, maximumScale, doubleTapScale, initialScale;
	private final Matrix baseMatrix = new Matrix();
	private final Matrix transformMatrix = new Matrix();
	private final Matrix displayMatrix = new Matrix();
	private final RectF workRect = new RectF();
	private final float[] matrixValues = new float[9];

	private Listener listener;

	private final GestureDetector gestureDetector;
	private final ScaleGestureDetector scaleGestureDetector;

	private FlingRunnable flingRunnable;
	private ScrollEdge scrollEdgeX = ScrollEdge.BOTH;
	private ScrollEdge scrollEdgeY = ScrollEdge.BOTH;

	private TouchMode touchMode = TouchMode.UNDEFINED;
	private AnimatedRestoreSwipeRunnable animatedRestoreSwipeRunnable;

	private int activePointerId = INVALID_POINTER_ID;
	private int activePointerIndex = 0;

	private float lastTouchX;
	private float lastTouchY;

	private final float touchSlop;
	private final float minimumVelocity;

	private VelocityTracker velocityTracker;
	private boolean isDragging;

	private static final Method METHOD_IN_DOUBLE_TAP_MODE;

	static {
		Method inDoubleTapMode;
		try {
			inDoubleTapMode = ScaleGestureDetector.class.getDeclaredMethod("inDoubleTapMode");
			inDoubleTapMode.setAccessible(true);
		} catch (Exception e) {
			inDoubleTapMode = null;
		}
		METHOD_IN_DOUBLE_TAP_MODE = inDoubleTapMode;
	}

	public PhotoView(Context context, AttributeSet attr) {
		super(context, attr);
		ViewConfiguration configuration = ViewConfiguration.get(context);
		minimumVelocity = configuration.getScaledMinimumFlingVelocity();
		touchSlop = configuration.getScaledTouchSlop();
		gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
			@Override
			public boolean onDoubleTapEvent(MotionEvent e) {
				return PhotoView.this.onDoubleTapEvent(e);
			}

			@Override
			public boolean onSingleTapConfirmed(MotionEvent e) {
				return PhotoView.this.onSingleTapConfirmed(e);
			}

			@Override
			public void onLongPress(MotionEvent e) {
				PhotoView.this.onLongPress(e);
			}
		});
		scaleGestureDetector = new ScaleGestureDetector(getContext(), this);
		tile = new TransparentTileDrawable(context, true);
		initBaseMatrix(false);
	}

	private final Rect lastLayout = new Rect();

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		if (top != lastLayout.top || bottom != lastLayout.bottom
				|| left != lastLayout.left || right != lastLayout.right) {
			lastLayout.set(left, top, right, bottom);
			resetScale();
		}
		super.onLayout(changed, left, top, right, bottom);
	}

	public interface Listener {
		public void onClick(PhotoView photoView, boolean image, float x, float y);
		public void onLongClick(PhotoView photoView, float x, float y);
		public void onVerticalSwipe(PhotoView photoView, float value);
		public boolean onClose(PhotoView photoView, boolean down);<commit_msg>Bug fix: don't notify about long press on dragging in PhotoView<commit_after>/*
 * Copyright 2011, 2012 <NAME>.
 *
 * 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.
 *
 * ********************************************************************************
 *
 * Copyright 2014-2016 <NAME>
 */

package com.mishiranu.dashchan.widget;

import java.lang.reflect.Method;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Scroller;

import com.mishiranu.dashchan.graphics.TransparentTileDrawable;
import com.mishiranu.dashchan.util.AnimationUtils;

public class PhotoView extends View implements ScaleGestureDetector.OnScaleGestureListener {
	private enum ScrollEdge {NONE, START, END, BOTH}
	private enum TouchMode {UNDEFINED, COMMON, CLOSING_START, CLOSING_END, CLOSING_BOTH}

	private static final float CLOSE_SWIPE_FACTOR = 0.25f;

	private static final int INVALID_POINTER_ID = -1;

	private final TransparentTileDrawable tile;
	private Drawable drawable;
	private boolean hasAlpha;
	private boolean fitScreen;
	private boolean drawDim;
	private final Point previousDimensions = new Point();
	private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

	private int[] initialScalingData;
	private ValueAnimator initialScalingAnimator;
	private Rect initialScaleClipRect;

	private float minimumScale, maximumScale, doubleTapScale, initialScale;
	private final Matrix baseMatrix = new Matrix();
	private final Matrix transformMatrix = new Matrix();
	private final Matrix displayMatrix = new Matrix();
	private final RectF workRect = new RectF();
	private final float[] matrixValues = new float[9];

	private Listener listener;

	private final GestureDetector gestureDetector;
	private final ScaleGestureDetector scaleGestureDetector;

	private FlingRunnable flingRunnable;
	private ScrollEdge scrollEdgeX = ScrollEdge.BOTH;
	private ScrollEdge scrollEdgeY = ScrollEdge.BOTH;

	private TouchMode touchMode = TouchMode.UNDEFINED;
	private AnimatedRestoreSwipeRunnable animatedRestoreSwipeRunnable;

	private int activePointerId = INVALID_POINTER_ID;
	private int activePointerIndex = 0;

	private float lastTouchX;
	private float lastTouchY;

	private final float touchSlop;
	private final float minimumVelocity;

	private VelocityTracker velocityTracker;
	private boolean isDragging;

	private static final Method METHOD_IN_DOUBLE_TAP_MODE;

	static {
		Method inDoubleTapMode;
		try {
			inDoubleTapMode = ScaleGestureDetector.class.getDeclaredMethod("inDoubleTapMode");
			inDoubleTapMode.setAccessible(true);
		} catch (Exception e) {
			inDoubleTapMode = null;
		}
		METHOD_IN_DOUBLE_TAP_MODE = inDoubleTapMode;
	}

	public PhotoView(Context context, AttributeSet attr) {
		super(context, attr);
		ViewConfiguration configuration = ViewConfiguration.get(context);
		minimumVelocity = configuration.getScaledMinimumFlingVelocity();
		touchSlop = configuration.getScaledTouchSlop();
		gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
			@Override
			public boolean onDoubleTapEvent(MotionEvent e) {
				return PhotoView.this.onDoubleTapEvent(e);
			}

			@Override
			public boolean onSingleTapConfirmed(MotionEvent e) {
				return PhotoView.this.onSingleTapConfirmed(e);
			}

			@Override
			public void onLongPress(MotionEvent e) {
				if (!isDragging) {
					PhotoView.this.onLongPress(e);
				}
			}
		});
		scaleGestureDetector = new ScaleGestureDetector(getContext(), this);
		tile = new TransparentTileDrawable(context, true);
		initBaseMatrix(false);
	}

	private final Rect lastLayout = new Rect();

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		if (top != lastLayout.top || bottom != lastLayout.bottom
				|| left != lastLayout.left || right != lastLayout.right) {
			lastLayout.set(left, top, right, bottom);
			resetScale();
		}
		super.onLayout(changed, left, top, right, bottom);
	}

	public interface Listener {
		public void onClick(PhotoView photoView, boolean image, float x, float y);
		public void onLongClick(PhotoView photoView, float x, float y);
		public void onVerticalSwipe(PhotoView photoView, float value);
		public boolean onClose(PhotoView photoView, boolean down);
{'fraction_non_alphanumeric': 0.09154658981748319, 'fraction_numerical': 0.004995196926032661, 'mean_word_length': 5.531367628607278, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011148', 'n_tokens_mistral': 3210, 'n_tokens_neox': 2913, 'n_words': 873}