summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/InnerGestureListener.cs
blob: 8611af2dea9e931db61037c6ca15fcd2010321de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using Android.Runtime;
using Android.Views;
using Object = Java.Lang.Object;

namespace Xamarin.Forms.Platform.Android
{
	internal class InnerGestureListener : Object, GestureDetector.IOnGestureListener, GestureDetector.IOnDoubleTapListener
	{
		bool _isScrolling;
		Func<bool> _scrollCompleteDelegate;
		Func<float, float, int, bool> _scrollDelegate;
		Func<int, bool> _scrollStartedDelegate;
		Func<int, bool> _tapDelegate;
		Func<int, IEnumerable<TapGestureRecognizer>> _tapGestureRecognizers;

		public InnerGestureListener(Func<int, bool> tapDelegate, Func<int, IEnumerable<TapGestureRecognizer>> tapGestureRecognizers, Func<float, float, int, bool> scrollDelegate,
									Func<int, bool> scrollStartedDelegate, Func<bool> scrollCompleteDelegate)
		{
			if (tapDelegate == null)
				throw new ArgumentNullException("tapDelegate");
			if (tapGestureRecognizers == null)
				throw new ArgumentNullException("tapGestureRecognizers");
			if (scrollDelegate == null)
				throw new ArgumentNullException("scrollDelegate");
			if (scrollStartedDelegate == null)
				throw new ArgumentNullException("scrollStartedDelegate");
			if (scrollCompleteDelegate == null)
				throw new ArgumentNullException("scrollCompleteDelegate");

			_tapDelegate = tapDelegate;
			_tapGestureRecognizers = tapGestureRecognizers;
			_scrollDelegate = scrollDelegate;
			_scrollStartedDelegate = scrollStartedDelegate;
			_scrollCompleteDelegate = scrollCompleteDelegate;
		}

		// This is needed because GestureRecognizer callbacks can be delayed several hundred milliseconds
		// which can result in the need to resurrect this object if it has already been disposed. We dispose
		// eagerly to allow easier garbage collection of the renderer
		internal InnerGestureListener(IntPtr handle, JniHandleOwnership ownership) : base(handle, ownership)
		{
		}

		internal void OnTouchEvent(MotionEvent e)
		{
			if (e.Action == MotionEventActions.Up)
				EndScrolling();
		}

		bool GestureDetector.IOnDoubleTapListener.OnDoubleTap(MotionEvent e)
		{
			if (_tapDelegate == null || _tapGestureRecognizers == null)
				return false;
			return _tapDelegate(2);
		}

		bool GestureDetector.IOnDoubleTapListener.OnDoubleTapEvent(MotionEvent e)
		{
			return false;
		}

		bool GestureDetector.IOnDoubleTapListener.OnSingleTapConfirmed(MotionEvent e)
		{
			if (_tapDelegate == null || _tapGestureRecognizers == null)
				return false;

			// optimization: only wait for a second tap if there is a double tap handler
			if (!HasDoubleTapHandler())
				return false;

			return _tapDelegate(1);
		}

		bool GestureDetector.IOnGestureListener.OnDown(MotionEvent e)
		{
			return false;
		}

		bool GestureDetector.IOnGestureListener.OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
		{
			EndScrolling();

			return false;
		}

		void GestureDetector.IOnGestureListener.OnLongPress(MotionEvent e)
		{
		}

		bool GestureDetector.IOnGestureListener.OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
		{
			if (_scrollDelegate == null || e1 == null || e2 == null)
				return false;

			if (!_isScrolling && _scrollStartedDelegate != null)
				_scrollStartedDelegate(e2.PointerCount);

			_isScrolling = true;

			float totalX = e2.GetX() - e1.GetX();
			float totalY = e2.GetY() - e1.GetY();

			return _scrollDelegate(totalX, totalY, e2.PointerCount);
		}

		void GestureDetector.IOnGestureListener.OnShowPress(MotionEvent e)
		{
		}

		bool GestureDetector.IOnGestureListener.OnSingleTapUp(MotionEvent e)
		{
			if (_tapDelegate == null || _tapGestureRecognizers == null)
				return false;

			// optimization: do not wait for a second tap if there is no double tap handler
			if (HasDoubleTapHandler())
				return false;

			return _tapDelegate(1);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				_tapDelegate = null;
				_tapGestureRecognizers = null;
				_scrollDelegate = null;
				_scrollStartedDelegate = null;
				_scrollCompleteDelegate = null;
			}

			base.Dispose(disposing);
		}

		void EndScrolling()
		{
			if (_isScrolling && _scrollCompleteDelegate != null)
				_scrollCompleteDelegate();

			_isScrolling = false;
		}

		bool HasDoubleTapHandler()
		{
			return _tapGestureRecognizers(2).Any();
		}
	}
}