Home | History | Annotate | Download | only in selection
      1 /*
      2  * Copyright (C) 2016 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.documentsui.selection;
     18 
     19 import static android.support.v4.util.Preconditions.checkNotNull;
     20 
     21 import android.support.annotation.Nullable;
     22 import android.view.GestureDetector.OnDoubleTapListener;
     23 import android.view.GestureDetector.OnGestureListener;
     24 import android.view.GestureDetector.SimpleOnGestureListener;
     25 import android.view.MotionEvent;
     26 
     27 /**
     28  * GestureRouter is responsible for routing gestures detected by a GestureDetector
     29  * to registered handlers. The primary function is to divide events by tool-type
     30  * allowing handlers to cleanly implement tool-type specific policies.
     31  */
     32 public final class GestureRouter<T extends OnGestureListener & OnDoubleTapListener>
     33         implements OnGestureListener, OnDoubleTapListener {
     34 
     35     private final ToolHandlerRegistry<T> mDelegates;
     36 
     37     public GestureRouter(T defaultDelegate) {
     38         checkNotNull(defaultDelegate);
     39         mDelegates = new ToolHandlerRegistry<>(defaultDelegate);
     40     }
     41 
     42     public GestureRouter() {
     43         this((T) new SimpleOnGestureListener());
     44     }
     45 
     46     /**
     47      * @param toolType
     48      * @param delegate the delegate, or null to unregister.
     49      */
     50     public void register(int toolType, @Nullable T delegate) {
     51         mDelegates.set(toolType, delegate);
     52     }
     53 
     54     @Override
     55     public boolean onSingleTapConfirmed(MotionEvent e) {
     56         return mDelegates.get(e).onSingleTapConfirmed(e);
     57     }
     58 
     59     @Override
     60     public boolean onDoubleTap(MotionEvent e) {
     61         return mDelegates.get(e).onDoubleTap(e);
     62     }
     63 
     64     @Override
     65     public boolean onDoubleTapEvent(MotionEvent e) {
     66         return mDelegates.get(e).onDoubleTapEvent(e);
     67     }
     68 
     69     @Override
     70     public boolean onDown(MotionEvent e) {
     71         return mDelegates.get(e).onDown(e);
     72     }
     73 
     74     @Override
     75     public void onShowPress(MotionEvent e) {
     76         mDelegates.get(e).onShowPress(e);
     77     }
     78 
     79     @Override
     80     public boolean onSingleTapUp(MotionEvent e) {
     81         return mDelegates.get(e).onSingleTapUp(e);
     82     }
     83 
     84     @Override
     85     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
     86         return mDelegates.get(e2).onScroll(e1, e2, distanceX, distanceY);
     87     }
     88 
     89     @Override
     90     public void onLongPress(MotionEvent e) {
     91         mDelegates.get(e).onLongPress(e);
     92     }
     93 
     94     @Override
     95     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
     96         return mDelegates.get(e2).onFling(e1, e2, velocityX, velocityY);
     97     }
     98 }
     99