• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.basicgesturedetect;
18 
19 import android.annotation.TargetApi;
20 import android.os.Build;
21 import android.view.GestureDetector;
22 import android.view.MotionEvent;
23 
24 import com.example.android.common.logger.Log;
25 
26 public class GestureListener extends GestureDetector.SimpleOnGestureListener {
27 
28     public static final String TAG = "GestureListener";
29 
30     // BEGIN_INCLUDE(init_gestureListener)
31     @Override
onSingleTapUp(MotionEvent e)32     public boolean onSingleTapUp(MotionEvent e) {
33         // Up motion completing a single tap occurred.
34         Log.i(TAG, "Single Tap Up" + getTouchType(e));
35         return false;
36     }
37 
38     @Override
onLongPress(MotionEvent e)39     public void onLongPress(MotionEvent e) {
40         // Touch has been long enough to indicate a long press.
41         // Does not indicate motion is complete yet (no up event necessarily)
42         Log.i(TAG, "Long Press" + getTouchType(e));
43     }
44 
45     @Override
onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)46     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
47     float distanceY) {
48         // User attempted to scroll
49         Log.i(TAG, "Scroll" + getTouchType(e1));
50         return false;
51     }
52 
53     @Override
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)54     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
55     float velocityY) {
56         // Fling event occurred.  Notification of this one happens after an "up" event.
57         Log.i(TAG, "Fling" + getTouchType(e1));
58         return false;
59     }
60 
61     @Override
onShowPress(MotionEvent e)62     public void onShowPress(MotionEvent e) {
63         // User performed a down event, and hasn't moved yet.
64         Log.i(TAG, "Show Press" + getTouchType(e));
65     }
66 
67     @Override
onDown(MotionEvent e)68     public boolean onDown(MotionEvent e) {
69         // "Down" event - User touched the screen.
70         Log.i(TAG, "Down" + getTouchType(e));
71         return false;
72     }
73 
74     @Override
onDoubleTap(MotionEvent e)75     public boolean onDoubleTap(MotionEvent e) {
76         // User tapped the screen twice.
77         Log.i(TAG, "Double tap" + getTouchType(e));
78         return false;
79     }
80 
81     @Override
onDoubleTapEvent(MotionEvent e)82     public boolean onDoubleTapEvent(MotionEvent e) {
83         // Since double-tap is actually several events which are considered one aggregate
84         // gesture, there's a separate callback for an individual event within the doubletap
85         // occurring.  This occurs for down, up, and move.
86         Log.i(TAG, "Event within double tap" + getTouchType(e));
87         return false;
88     }
89 
90     @Override
onSingleTapConfirmed(MotionEvent e)91     public boolean onSingleTapConfirmed(MotionEvent e) {
92         // A confirmed single-tap event has occurred.  Only called when the detector has
93         // determined that the first tap stands alone, and is not part of a double tap.
94         Log.i(TAG, "Single tap confirmed" + getTouchType(e));
95         return false;
96     }
97     // END_INCLUDE(init_gestureListener)
98 
99 
100     /**
101      * Returns a human-readable string describing the type of touch that triggered a MotionEvent.
102      */
103 
getTouchType(MotionEvent e)104     private static String getTouchType(MotionEvent e){
105 
106         String touchTypeDescription = " ";
107         int touchType = e.getToolType(0);
108 
109         switch (touchType) {
110             case MotionEvent.TOOL_TYPE_FINGER:
111                 touchTypeDescription += "(finger)";
112                 break;
113             case MotionEvent.TOOL_TYPE_STYLUS:
114                 touchTypeDescription += "(stylus, ";
115                 //Get some additional information about the stylus touch
116                 float stylusPressure = e.getPressure();
117                 touchTypeDescription += "pressure: " + stylusPressure;
118 
119                 if(Build.VERSION.SDK_INT >= 21) {
120                     touchTypeDescription += ", buttons pressed: " + getButtonsPressed(e);
121                 }
122 
123                 touchTypeDescription += ")";
124                 break;
125             case MotionEvent.TOOL_TYPE_ERASER:
126                 touchTypeDescription += "(eraser)";
127                 break;
128             case MotionEvent.TOOL_TYPE_MOUSE:
129                 touchTypeDescription += "(mouse)";
130                 break;
131             default:
132                 touchTypeDescription += "(unknown tool)";
133                 break;
134         }
135 
136         return touchTypeDescription;
137     }
138 
139     /**
140      * Returns a human-readable string listing all the stylus buttons that were pressed when the
141      * input MotionEvent occurred.
142      */
143     @TargetApi(21)
getButtonsPressed(MotionEvent e)144     private static String getButtonsPressed(MotionEvent e){
145         String buttons = "";
146 
147         if(e.isButtonPressed(MotionEvent.BUTTON_PRIMARY)){
148             buttons += " primary";
149         }
150 
151         if(e.isButtonPressed(MotionEvent.BUTTON_SECONDARY)){
152             buttons += " secondary";
153         }
154 
155         if(e.isButtonPressed(MotionEvent.BUTTON_TERTIARY)){
156             buttons += " tertiary";
157         }
158 
159         if(e.isButtonPressed(MotionEvent.BUTTON_BACK)){
160             buttons += " back";
161         }
162 
163         if(e.isButtonPressed(MotionEvent.BUTTON_FORWARD)){
164             buttons += " forward";
165         }
166 
167         if (buttons.equals("")){
168             buttons = "none";
169         }
170 
171         return buttons;
172     }
173 
174 }
175