Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 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.android.camera.ui;
     18 
     19 import android.content.Context;
     20 import android.view.GestureDetector;
     21 import android.view.MotionEvent;
     22 import android.view.ScaleGestureDetector;
     23 
     24 // This class aggregates two gesture detectors: GestureDetector,
     25 // ScaleGestureDetector.
     26 public class FilmStripGestureRecognizer {
     27     @SuppressWarnings("unused")
     28     private static final String TAG = "FilmStripGestureRecognizer";
     29 
     30     public interface Listener {
     31         boolean onSingleTapUp(float x, float y);
     32         boolean onDoubleTap(float x, float y);
     33         boolean onScroll(float x, float y, float dx, float dy);
     34         boolean onFling(float velocityX, float velocityY);
     35         boolean onScaleBegin(float focusX, float focusY);
     36         boolean onScale(float focusX, float focusY, float scale);
     37         boolean onDown(float x, float y);
     38         boolean onUp(float x, float y);
     39         void onScaleEnd();
     40     }
     41 
     42     private final GestureDetector mGestureDetector;
     43     private final ScaleGestureDetector mScaleDetector;
     44     private final Listener mListener;
     45 
     46     public FilmStripGestureRecognizer(Context context, Listener listener) {
     47         mListener = listener;
     48         mGestureDetector = new GestureDetector(context, new MyGestureListener(),
     49                 null, true /* ignoreMultitouch */);
     50         mGestureDetector.setOnDoubleTapListener(new MyDoubleTapListener());
     51         mScaleDetector = new ScaleGestureDetector(
     52                 context, new MyScaleListener());
     53     }
     54 
     55     public void onTouchEvent(MotionEvent event) {
     56         mGestureDetector.onTouchEvent(event);
     57         mScaleDetector.onTouchEvent(event);
     58         if (event.getAction() == MotionEvent.ACTION_UP) {
     59             mListener.onUp(event.getX(), event.getY());
     60         }
     61     }
     62 
     63     private class MyGestureListener
     64                 extends GestureDetector.SimpleOnGestureListener {
     65         @Override
     66         public boolean onScroll(
     67                 MotionEvent e1, MotionEvent e2, float dx, float dy) {
     68             return mListener.onScroll(e2.getX(), e2.getY(), dx, dy);
     69         }
     70 
     71         @Override
     72         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
     73                 float velocityY) {
     74             return mListener.onFling(velocityX, velocityY);
     75         }
     76 
     77         @Override
     78         public boolean onDown(MotionEvent e) {
     79             mListener.onDown(e.getX(), e.getY());
     80             return super.onDown(e);
     81         }
     82     }
     83 
     84     /**
     85      * The listener that is used to notify when a double-tap or a confirmed
     86      * single-tap occur.
     87      */
     88     private class MyDoubleTapListener implements GestureDetector.OnDoubleTapListener {
     89 
     90         @Override
     91         public boolean onSingleTapConfirmed(MotionEvent e) {
     92             return mListener.onSingleTapUp(e.getX(), e.getY());
     93         }
     94 
     95         @Override
     96         public boolean onDoubleTap(MotionEvent e) {
     97             return mListener.onDoubleTap(e.getX(), e.getY());
     98         }
     99 
    100         @Override
    101         public boolean onDoubleTapEvent(MotionEvent e) {
    102             return true;
    103         }
    104     }
    105 
    106     private class MyScaleListener
    107             extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    108         @Override
    109         public boolean onScaleBegin(ScaleGestureDetector detector) {
    110             return mListener.onScaleBegin(
    111                     detector.getFocusX(), detector.getFocusY());
    112         }
    113 
    114         @Override
    115         public boolean onScale(ScaleGestureDetector detector) {
    116             return mListener.onScale(detector.getFocusX(),
    117                     detector.getFocusY(), detector.getScaleFactor());
    118         }
    119 
    120         @Override
    121         public void onScaleEnd(ScaleGestureDetector detector) {
    122             mListener.onScaleEnd();
    123         }
    124     }
    125 }
    126