Home | History | Annotate | Download | only in dirlist
      1 /*
      2  * Copyright (C) 2017 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.documentsui.dirlist;
     17 
     18 import static android.support.v4.util.Preconditions.checkState;
     19 import static com.android.documentsui.base.SharedMinimal.VERBOSE;
     20 
     21 import android.content.Context;
     22 import android.os.Build;
     23 import android.support.annotation.Nullable;
     24 import android.support.v7.widget.RecyclerView;
     25 import android.support.v7.widget.RecyclerView.OnItemTouchListener;
     26 import android.util.Log;
     27 import android.view.MotionEvent;
     28 import android.view.ScaleGestureDetector;
     29 
     30 import com.android.documentsui.base.Features;
     31 
     32 import java.util.function.Consumer;
     33 
     34 /**
     35  * Class providing support for gluing gesture scaling of the ui into RecyclerView + DocsUI.
     36  */
     37 final class ScaleHelper {
     38 
     39     private static final String TAG = "ScaleHelper";
     40 
     41     private final Context mContext;
     42     private final Features mFeatures;
     43     private final Consumer<Float> mScaleCallback;
     44 
     45     private @Nullable ScaleGestureDetector mScaleDetector;
     46 
     47     public ScaleHelper(Context context, Features features, Consumer<Float> scaleCallback) {
     48         mContext = context;
     49         mFeatures = features;
     50         mScaleCallback = scaleCallback;
     51     }
     52 
     53     private boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
     54         // Checking feature is deferred to runtime so the feature can be enabled
     55         // after this class has been setup.
     56         if (mFeatures.isGestureScaleEnabled() && mScaleDetector != null) {
     57             mScaleDetector.onTouchEvent(e);
     58         }
     59 
     60         return false;
     61     }
     62 
     63     void attach(RecyclerView view) {
     64         checkState(Build.IS_DEBUGGABLE);
     65         checkState(mScaleDetector == null);
     66 
     67         mScaleDetector = new ScaleGestureDetector(
     68             mContext,
     69             new ScaleGestureDetector.SimpleOnScaleGestureListener() {
     70                 @Override
     71                 public boolean onScale(ScaleGestureDetector detector) {
     72                     if (VERBOSE) Log.v(TAG,
     73                             "Received scale event: " + detector.getScaleFactor());
     74                     mScaleCallback.accept(detector.getScaleFactor());
     75                     return true;
     76                 }
     77             });
     78 
     79         view.addOnItemTouchListener(
     80                 new OnItemTouchListener() {
     81                     @Override
     82                     public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
     83                         return ScaleHelper.this.onInterceptTouchEvent(rv, e);
     84                     }
     85 
     86                     @Override
     87                     public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
     88 
     89                     @Override
     90                     public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
     91         });
     92     }
     93 }
     94