Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 18 | |
| 19 | public class SpringLoadedDragController implements OnAlarmListener { |
| 20 | // how long the user must hover over a mini-screen before it unshrinks |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 21 | final long ENTER_SPRING_LOAD_HOVER_TIME = 500; |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 22 | final long ENTER_SPRING_LOAD_CANCEL_HOVER_TIME = 950; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 23 | final long EXIT_SPRING_LOAD_HOVER_TIME = 200; |
| 24 | |
| 25 | Alarm mAlarm; |
| 26 | |
| 27 | // the screen the user is currently hovering over, if any |
| 28 | private CellLayout mScreen; |
| 29 | private Launcher mLauncher; |
| 30 | |
| 31 | public SpringLoadedDragController(Launcher launcher) { |
| 32 | mLauncher = launcher; |
| 33 | mAlarm = new Alarm(); |
| 34 | mAlarm.setOnAlarmListener(this); |
| 35 | } |
| 36 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 37 | public void cancel() { |
| 38 | mAlarm.cancelAlarm(); |
| 39 | } |
| 40 | |
| 41 | // Set a new alarm to expire for the screen that we are hovering over now |
| 42 | public void setAlarm(CellLayout cl) { |
Winson Chung | b8dffaf | 2011-07-08 11:14:33 -0700 | [diff] [blame] | 43 | mAlarm.cancelAlarm(); |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 44 | mAlarm.setAlarm((cl == null) ? ENTER_SPRING_LOAD_CANCEL_HOVER_TIME : |
| 45 | ENTER_SPRING_LOAD_HOVER_TIME); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 46 | mScreen = cl; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // this is called when our timer runs out |
| 50 | public void onAlarm(Alarm alarm) { |
| 51 | if (mScreen != null) { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 52 | // Snap to the screen that we are hovering over now |
| 53 | Workspace w = mLauncher.getWorkspace(); |
| 54 | int page = w.indexOfChild(mScreen); |
Winson Chung | b8dffaf | 2011-07-08 11:14:33 -0700 | [diff] [blame] | 55 | if (page != w.getCurrentPage()) { |
| 56 | w.snapToPage(page); |
| 57 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 58 | } else { |
| 59 | mLauncher.getDragController().cancelDrag(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |