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 androidx.core.util.Preconditions.checkState; 19 20 import androidx.recyclerview.widget.RecyclerView; 21 import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener; 22 import android.view.MotionEvent; 23 24 import com.android.documentsui.base.BooleanConsumer; 25 import com.android.documentsui.base.Events; 26 27 /** 28 * Class providing support for gluing gesture scaling of the ui into RecyclerView + DocsUI. 29 */ 30 final class RefreshHelper { 31 32 private final BooleanConsumer mRefreshLayoutEnabler; 33 34 private boolean mAttached; 35 RefreshHelper(BooleanConsumer refreshLayoutEnabler)36 public RefreshHelper(BooleanConsumer refreshLayoutEnabler) { 37 mRefreshLayoutEnabler = refreshLayoutEnabler; 38 } 39 onInterceptTouchEvent(RecyclerView rv, MotionEvent e)40 private boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 41 if (Events.isMouseEvent(e)) { 42 if (Events.isActionDown(e)) { 43 mRefreshLayoutEnabler.accept(false); 44 } 45 } else { 46 // If user has started some gesture while RecyclerView is not at the top, disable 47 // refresh 48 if (Events.isActionDown(e) && rv.computeVerticalScrollOffset() != 0) { 49 mRefreshLayoutEnabler.accept(false); 50 } 51 } 52 53 if (Events.isActionUp(e)) { 54 mRefreshLayoutEnabler.accept(true); 55 } 56 57 return false; 58 } 59 onTouchEvent(RecyclerView rv, MotionEvent e)60 private void onTouchEvent(RecyclerView rv, MotionEvent e) { 61 if (Events.isActionUp(e)) { 62 mRefreshLayoutEnabler.accept(true); 63 } 64 } 65 attach(RecyclerView view)66 void attach(RecyclerView view) { 67 checkState(!mAttached); 68 69 view.addOnItemTouchListener( 70 new OnItemTouchListener() { 71 @Override 72 public void onTouchEvent(RecyclerView rv, MotionEvent e) { 73 RefreshHelper.this.onTouchEvent(rv, e); 74 } 75 76 @Override 77 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 78 return RefreshHelper.this.onInterceptTouchEvent(rv, e); 79 } 80 81 @Override 82 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {} 83 }); 84 85 mAttached = true; 86 } 87 } 88