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
17package com.android.launcher3.dragndrop;
18
19import com.android.launcher3.Alarm;
20import com.android.launcher3.CellLayout;
21import com.android.launcher3.Launcher;
22import com.android.launcher3.OnAlarmListener;
23import com.android.launcher3.Workspace;
24
25public class SpringLoadedDragController implements OnAlarmListener {
26    // how long the user must hover over a mini-screen before it unshrinks
27    final long ENTER_SPRING_LOAD_HOVER_TIME = 500;
28    final long ENTER_SPRING_LOAD_CANCEL_HOVER_TIME = 950;
29
30    Alarm mAlarm;
31
32    // the screen the user is currently hovering over, if any
33    private CellLayout mScreen;
34    private Launcher mLauncher;
35
36    public SpringLoadedDragController(Launcher launcher) {
37        mLauncher = launcher;
38        mAlarm = new Alarm();
39        mAlarm.setOnAlarmListener(this);
40    }
41
42    public void cancel() {
43        mAlarm.cancelAlarm();
44    }
45
46    // Set a new alarm to expire for the screen that we are hovering over now
47    public void setAlarm(CellLayout cl) {
48        mAlarm.cancelAlarm();
49        mAlarm.setAlarm((cl == null) ? ENTER_SPRING_LOAD_CANCEL_HOVER_TIME :
50            ENTER_SPRING_LOAD_HOVER_TIME);
51        mScreen = cl;
52    }
53
54    // this is called when our timer runs out
55    public void onAlarm(Alarm alarm) {
56        if (mScreen != null) {
57            // Snap to the screen that we are hovering over now
58            Workspace w = mLauncher.getWorkspace();
59            int page = w.indexOfChild(mScreen);
60            if (page != w.getCurrentPage()) {
61                w.snapToPage(page);
62            }
63        } else {
64            mLauncher.getDragController().cancelDrag();
65        }
66    }
67}
68