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 17 package androidx.wear.widget.drawer; 18 19 import android.os.Handler; 20 import android.os.Looper; 21 import android.view.View; 22 import android.view.View.OnScrollChangeListener; 23 import android.widget.ScrollView; 24 25 import androidx.annotation.RestrictTo; 26 import androidx.annotation.RestrictTo.Scope; 27 import androidx.wear.widget.drawer.FlingWatcherFactory.FlingListener; 28 import androidx.wear.widget.drawer.FlingWatcherFactory.FlingWatcher; 29 30 import java.lang.ref.WeakReference; 31 32 /** 33 * {@link FlingWatcher} implementation for {@link ScrollView ScrollViews}. 34 * <p> 35 * Because {@link ScrollView} does not provide a way to listen to the scroll state, there's no 36 * callback which definitely indicates the fling has finished. So, we instead listen for scroll 37 * events. If we reach the top or bottom of the view or if there are no events within {@link 38 * #MAX_WAIT_TIME_MS}, we assume the fling has finished. 39 * 40 * @hide 41 */ 42 @RestrictTo(Scope.LIBRARY) 43 class ScrollViewFlingWatcher implements FlingWatcher, OnScrollChangeListener { 44 45 static final int MAX_WAIT_TIME_MS = 100; 46 private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); 47 private final FlingListener mListener; 48 private final WeakReference<ScrollView> mScrollView; 49 private final Runnable mNotifyListenerRunnable = new Runnable() { 50 @Override 51 public void run() { 52 onEndOfFlingFound(); 53 } 54 }; 55 56 ScrollViewFlingWatcher(FlingListener listener, ScrollView scrollView) { 57 mListener = listener; 58 mScrollView = new WeakReference<>(scrollView); 59 } 60 61 private static boolean isViewAtTopOrBottom(View view) { 62 return !view.canScrollVertically(-1 /* up */) || !view.canScrollVertically(1 /* down */); 63 } 64 65 @Override 66 public void watch() { 67 ScrollView scrollView = mScrollView.get(); 68 if (scrollView != null) { 69 scrollView.setOnScrollChangeListener(this); 70 scheduleNext(); 71 } 72 } 73 74 @Override 75 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, 76 int oldScrollY) { 77 if (isViewAtTopOrBottom(v)) { 78 onEndOfFlingFound(); 79 } else { 80 scheduleNext(); 81 } 82 } 83 84 private void onEndOfFlingFound() { 85 mMainThreadHandler.removeCallbacks(mNotifyListenerRunnable); 86 87 ScrollView scrollView = mScrollView.get(); 88 if (scrollView != null) { 89 scrollView.setOnScrollChangeListener(null); 90 mListener.onFlingComplete(scrollView); 91 } 92 } 93 94 private void scheduleNext() { 95 mMainThreadHandler.removeCallbacks(mNotifyListenerRunnable); 96 mMainThreadHandler.postDelayed(mNotifyListenerRunnable, MAX_WAIT_TIME_MS); 97 } 98 } 99