1 /*
2  * Copyright (C) 2022 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.wallpaper.util
17 
18 import android.graphics.PointF
19 import android.os.Handler
20 import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
21 
22 /**
23  * Listens to the state change of [SubsamplingScaleImageView]. This also calls a "debounced" center
24  * changed event which only fires a [DEBOUNCE_THRESHOLD_MILLIS] time after the last center changed
25  * event from consecutive center changed events.
26  */
27 abstract class OnFullResImageViewStateChangedListener :
28     SubsamplingScaleImageView.OnStateChangedListener {
29     companion object {
30         private const val DEBOUNCE_THRESHOLD_MILLIS: Long = 100
31     }
32 
33     private val mHandler = Handler()
34 
35     /**
36      * Fires a [DEBOUNCE_THRESHOLD_MILLIS] time after the last center changed event from consecutive
37      * center changed events.
38      */
onDebouncedCenterChangednull39     abstract fun onDebouncedCenterChanged(newCenter: PointF?, origin: Int)
40 
41     /**
42      * When center changed, any undone delayed calls will be removed. And another delayed call for
43      * the "debounced" center changed event is scheduled.
44      */
45     override fun onCenterChanged(newCenter: PointF, origin: Int) {
46         mHandler.removeCallbacksAndMessages(null)
47         mHandler.postDelayed(
48             { onDebouncedCenterChanged(newCenter, origin) },
49             DEBOUNCE_THRESHOLD_MILLIS
50         )
51     }
52 
53     /** Do nothing intended. */
onScaleChangednull54     override fun onScaleChanged(newScale: Float, origin: Int) {}
55 }
56