1 /*
2  * Copyright (C) 2010 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.android.gallery3d.ui;
18 
19 import android.view.MotionEvent;
20 
21 public class DownUpDetector {
22     public interface DownUpListener {
onDown(MotionEvent e)23         void onDown(MotionEvent e);
onUp(MotionEvent e)24         void onUp(MotionEvent e);
25     }
26 
27     private boolean mStillDown;
28     private DownUpListener mListener;
29 
DownUpDetector(DownUpListener listener)30     public DownUpDetector(DownUpListener listener) {
31         mListener = listener;
32     }
33 
setState(boolean down, MotionEvent e)34     private void setState(boolean down, MotionEvent e) {
35         if (down == mStillDown) return;
36         mStillDown = down;
37         if (down) {
38             mListener.onDown(e);
39         } else {
40             mListener.onUp(e);
41         }
42     }
43 
onTouchEvent(MotionEvent ev)44     public void onTouchEvent(MotionEvent ev) {
45         switch (ev.getAction() & MotionEvent.ACTION_MASK) {
46         case MotionEvent.ACTION_DOWN:
47             setState(true, ev);
48             break;
49 
50         case MotionEvent.ACTION_UP:
51         case MotionEvent.ACTION_CANCEL:
52         case MotionEvent.ACTION_POINTER_DOWN:  // Multitouch event - abort.
53             setState(false, ev);
54             break;
55         }
56     }
57 
isDown()58     public boolean isDown() {
59         return mStillDown;
60     }
61 }
62