GestureActivity.java revision f69eb9ac2856f470cb79f57141f711ed3ceed99d
1/*
2 * Copyright (C) 2014 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
17package com.google.android.apps.common.testing.ui.testapp;
18
19import com.google.common.collect.Lists;
20
21import android.app.Activity;
22import android.os.Bundle;
23import android.os.SystemClock;
24import android.util.Log;
25import android.view.GestureDetector;
26import android.view.MotionEvent;
27import android.view.View;
28
29import java.util.List;
30
31/**
32 * Displays a large touchable area and logs the events it receives.
33 */
34public class GestureActivity extends Activity {
35  private static final String TAG = GestureActivity.class.getSimpleName();
36
37
38  private View gestureArea;
39  private List<MotionEvent> downEvents = Lists.newArrayList();
40  private List<MotionEvent> scrollEvents = Lists.newArrayList();
41  private List<MotionEvent> longPressEvents = Lists.newArrayList();
42  private List<MotionEvent> showPresses = Lists.newArrayList();
43  private List<MotionEvent> singleTaps = Lists.newArrayList();
44  private List<MotionEvent> confirmedSingleTaps = Lists.newArrayList();
45  private List<MotionEvent> doubleTapEvents = Lists.newArrayList();
46  private List<MotionEvent> doubleTaps = Lists.newArrayList();
47
48  public void clearDownEvents() {
49    downEvents.clear();
50  }
51
52  public void clearScrollEvents() {
53    scrollEvents.clear();
54  }
55
56  public void clearLongPressEvents() {
57    longPressEvents.clear();
58  }
59
60  public void clearShowPresses() {
61    showPresses.clear();
62  }
63
64  public void clearSingleTaps() {
65    singleTaps.clear();
66  }
67
68  public void clearConfirmedSingleTaps() {
69    confirmedSingleTaps.clear();
70  }
71
72  public void clearDoubleTapEvents() {
73    doubleTapEvents.clear();
74  }
75
76  public void clearDoubleTaps() {
77    doubleTaps.clear();
78  }
79
80  public List<MotionEvent> getDownEvents() {
81    return Lists.newArrayList(downEvents);
82  }
83
84  public List<MotionEvent> getScrollEvents() {
85    return Lists.newArrayList(scrollEvents);
86  }
87
88  public List<MotionEvent> getLongPressEvents() {
89    return Lists.newArrayList(longPressEvents);
90  }
91
92  public List<MotionEvent> getShowPresses() {
93    return Lists.newArrayList(showPresses);
94  }
95
96  public List<MotionEvent> getSingleTaps() {
97    return Lists.newArrayList(singleTaps);
98  }
99
100  public List<MotionEvent> getConfirmedSingleTaps() {
101    return Lists.newArrayList(confirmedSingleTaps);
102  }
103
104  public List<MotionEvent> getDoubleTapEvents() {
105    return Lists.newArrayList(doubleTapEvents);
106  }
107
108  public List<MotionEvent> getDoubleTaps() {
109    return Lists.newArrayList(doubleTaps);
110  }
111
112  @Override
113  public void onCreate(Bundle icicle) {
114    super.onCreate(icicle);
115    setContentView(R.layout.gesture_activity);
116    gestureArea = findViewById(R.id.gesture_area);
117    final GestureDetector simpleDetector = new GestureDetector(this, new GestureListener());
118    simpleDetector.setIsLongpressEnabled(true);
119    simpleDetector.setOnDoubleTapListener(new DoubleTapListener());
120    gestureArea.setOnTouchListener(new View.OnTouchListener() {
121      @Override
122      public boolean onTouch(View v, MotionEvent m) {
123        boolean res = simpleDetector.onTouchEvent(m);
124        if (-1 != touchDelay) {
125          Log.i(TAG, "sleeping for: " + touchDelay);
126          SystemClock.sleep(touchDelay);
127
128        }
129        return res;
130      }
131    });
132  }
133
134  private volatile long touchDelay = -1;
135
136  public void setTouchDelay(long touchDelay) {
137    this.touchDelay = touchDelay;
138  }
139
140  public void areaClicked(@SuppressWarnings("unused") View v) {
141    Log.v(TAG, "onClick called!");
142  }
143
144  private class DoubleTapListener implements GestureDetector.OnDoubleTapListener {
145    @Override
146    public boolean onDoubleTap(MotionEvent e) {
147      doubleTaps.add(MotionEvent.obtain(e));
148      Log.v(TAG, "onDoubleTap: " + e);
149      setVisible(R.id.text_double_click);
150      return false;
151    }
152
153    @Override
154    public boolean onDoubleTapEvent(MotionEvent e) {
155      doubleTapEvents.add(MotionEvent.obtain(e));
156      Log.v(TAG, "onDoubleTapEvent: " + e);
157      return false;
158    }
159
160    @Override
161    public boolean onSingleTapConfirmed(MotionEvent e) {
162      confirmedSingleTaps.add(MotionEvent.obtain(e));
163      Log.v(TAG, "onSingleTapConfirmed: " + e);
164      return false;
165    }
166  }
167
168  private class GestureListener implements GestureDetector.OnGestureListener {
169    @Override
170    public boolean onDown(MotionEvent e) {
171      downEvents.add(MotionEvent.obtain(e));
172      Log.v(TAG, "Down: " + e);
173      return false;
174    }
175
176    @Override
177    public boolean onSingleTapUp(MotionEvent e) {
178      singleTaps.add(MotionEvent.obtain(e));
179      Log.v(TAG, "on single tap: " + e);
180      setVisible(R.id.text_click);
181      return false;
182    }
183
184    @Override
185    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distX, float distY) {
186      scrollEvents.add(MotionEvent.obtain(e1));
187      scrollEvents.add(MotionEvent.obtain(e2));
188      Log.v(TAG, "Scroll: e1: " + e1 + " e2: " + e2 + " distX: " + distX + " distY: " + distY);
189      setVisible(R.id.text_swipe);
190      return false;
191    }
192
193    @Override
194    public void onShowPress(MotionEvent e) {
195      showPresses.add(MotionEvent.obtain(e));
196      Log.v(TAG, "ShowPress: " + e);
197    }
198
199    @Override
200    public void onLongPress(MotionEvent e) {
201      longPressEvents.add(MotionEvent.obtain(e));
202      Log.v(TAG, "LongPress: " + e);
203      setVisible(R.id.text_long_click);
204    }
205
206    @Override
207    public boolean onFling(MotionEvent e1, MotionEvent e2, float veloX, float veloY) {
208      Log.v(TAG, "Fling: e1: " + e1 + " e2: " + e2 + " veloX: " + veloX + " veloY: " + veloY);
209      return false;
210    }
211  }
212
213  private void setVisible(int id) {
214    hideAll();
215    findViewById(id).setVisibility(View.VISIBLE);
216  }
217
218  private void hideAll() {
219    findViewById(R.id.text_click).setVisibility(View.GONE);
220    findViewById(R.id.text_long_click).setVisibility(View.GONE);
221    findViewById(R.id.text_swipe).setVisibility(View.GONE);
222    findViewById(R.id.text_double_click).setVisibility(View.GONE);
223  }
224}
225