Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2018 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.launcher3.util;
     18 
     19 import android.os.SystemClock;
     20 
     21 /**
     22  * Determines whether a fling should be blocked. Currently we block flings when crossing thresholds
     23  * to new states, and unblock after a short duration.
     24  */
     25 public class FlingBlockCheck {
     26     // Allow flinging to a new state after waiting this many milliseconds.
     27     private static final long UNBLOCK_FLING_PAUSE_DURATION = 200;
     28 
     29     private boolean mBlockFling;
     30     private long mBlockFlingTime;
     31 
     32     public void blockFling() {
     33         mBlockFling = true;
     34         mBlockFlingTime = SystemClock.uptimeMillis();
     35     }
     36 
     37     public void unblockFling() {
     38         mBlockFling = false;
     39         mBlockFlingTime = 0;
     40     }
     41 
     42     public void onEvent() {
     43         // We prevent flinging after passing a state, but allow it if the user pauses briefly.
     44         if (SystemClock.uptimeMillis() - mBlockFlingTime >= UNBLOCK_FLING_PAUSE_DURATION) {
     45             mBlockFling = false;
     46         }
     47     }
     48 
     49     public boolean isBlocked() {
     50         return mBlockFling;
     51     }
     52 }
     53