• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.launcher3.dragndrop
17 
18 import com.android.launcher3.Alarm
19 import com.android.launcher3.CellLayout
20 import com.android.launcher3.Launcher
21 import com.android.launcher3.OnAlarmListener
22 import com.android.launcher3.Utilities
23 
24 class SpringLoadedDragController(private val launcher: Launcher) : OnAlarmListener {
<lambda>null25     internal val alarm = Alarm().also { it.setOnAlarmListener(this) }
26 
27     // the screen the user is currently hovering over, if any
28     private var screen: CellLayout? = null
29 
cancelnull30     fun cancel() = alarm.cancelAlarm()
31 
32     // Set a new alarm to expire for the screen that we are hovering over now
33     fun setAlarm(cl: CellLayout?) {
34         cancel()
35         alarm.setAlarm(
36             when {
37                 cl == null -> ENTER_SPRING_LOAD_CANCEL_HOVER_TIME
38                 // Some TAPL tests are flaky on Cuttlefish with a low waiting time
39                 Utilities.isRunningInTestHarness() -> ENTER_SPRING_LOAD_HOVER_TIME_IN_TEST
40                 else -> ENTER_SPRING_LOAD_HOVER_TIME
41             }
42         )
43         screen = cl
44     }
45 
46     // this is called when our timer runs out
onAlarmnull47     override fun onAlarm(alarm: Alarm) {
48         if (screen != null) {
49             // Snap to the screen that we are hovering over now
50             with(launcher.workspace) {
51                 if (!isVisible(screen) && launcher.dragController.mDistanceSinceScroll != 0) {
52                     snapToPage(indexOfChild(screen))
53                 }
54             }
55         } else {
56             launcher.dragController.cancelDrag()
57         }
58     }
59 
60     companion object {
61         // how long the user must hover over a mini-screen before it unshrinks
62         private const val ENTER_SPRING_LOAD_HOVER_TIME: Long = 500
63         private const val ENTER_SPRING_LOAD_HOVER_TIME_IN_TEST: Long = 3000
64         private const val ENTER_SPRING_LOAD_CANCEL_HOVER_TIME: Long = 950
65     }
66 }
67