Home | History | Annotate | Download | only in phototable
      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 package com.android.dreams.phototable;
     17 
     18 import android.content.Context;
     19 import android.content.res.Resources;
     20 import android.view.MotionEvent;
     21 
     22 /**
     23  * Detect and dispatch edge events.
     24  */
     25 public class EdgeSwipeDetector {
     26     @SuppressWarnings("unused")
     27     private static final String TAG = "EdgeSwipeDetector";
     28     private float mEdgeSwipeGutter;
     29     private float mEdgeSwipeThreshold;
     30     private boolean mEdgeSwipe;
     31 
     32     private final PhotoTable mTable;
     33 
     34     public EdgeSwipeDetector(Context context, PhotoTable table) {
     35         mTable = table;
     36         final Resources resources = context.getResources();
     37         mEdgeSwipeGutter = resources.getInteger(R.integer.table_edge_swipe_gutter) / 1000000f;
     38         mEdgeSwipeThreshold = resources.getInteger(R.integer.table_edge_swipe_threshold) / 1000000f;
     39     }
     40 
     41     public boolean onTouchEvent(MotionEvent event) {
     42         switch (event.getAction()) {
     43             case MotionEvent.ACTION_DOWN:
     44                 float edgeGutter = event.getDevice().getMotionRange(MotionEvent.AXIS_X).getMax()
     45                         * mEdgeSwipeGutter;
     46                 if (event.getX() < edgeGutter) {
     47                     mEdgeSwipe = true;
     48                     return true;
     49                 }
     50                 break;
     51 
     52             case MotionEvent.ACTION_MOVE:
     53                 if (mEdgeSwipe) {
     54                     return true;
     55                 }
     56                 break;
     57 
     58             case MotionEvent.ACTION_UP:
     59                 if (mEdgeSwipe) {
     60                     mEdgeSwipe = false;
     61                     float enough = event.getDevice().getMotionRange(MotionEvent.AXIS_X).getMax()
     62                             * mEdgeSwipeThreshold;
     63                     if (event.getX() > enough) {
     64                         if (mTable.hasFocus()) {
     65                             mTable.fling(mTable.getFocus());
     66                         } else if (mTable.hasSelection()) {
     67                             mTable.clearSelection();
     68                         }
     69                     }
     70                     return true;
     71                 }
     72                 break;
     73         }
     74         return false;
     75     }
     76 }
     77