SpringLoadedDragController.java revision fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834
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    final long EXIT_SPRING_LOAD_HOVER_TIME = 200;
30
31    Alarm mAlarm;
32
33    // the screen the user is currently hovering over, if any
34    private CellLayout mScreen;
35    private Launcher mLauncher;
36
37    public SpringLoadedDragController(Launcher launcher) {
38        mLauncher = launcher;
39        mAlarm = new Alarm();
40        mAlarm.setOnAlarmListener(this);
41    }
42
43    public void cancel() {
44        mAlarm.cancelAlarm();
45    }
46
47    // Set a new alarm to expire for the screen that we are hovering over now
48    public void setAlarm(CellLayout cl) {
49        mAlarm.cancelAlarm();
50        mAlarm.setAlarm((cl == null) ? ENTER_SPRING_LOAD_CANCEL_HOVER_TIME :
51            ENTER_SPRING_LOAD_HOVER_TIME);
52        mScreen = cl;
53    }
54
55    // this is called when our timer runs out
56    public void onAlarm(Alarm alarm) {
57        if (mScreen != null) {
58            // Snap to the screen that we are hovering over now
59            Workspace w = mLauncher.getWorkspace();
60            int page = w.indexOfChild(mScreen);
61            if (page != w.getCurrentPage()) {
62                w.snapToPage(page);
63            }
64        } else {
65            mLauncher.getDragController().cancelDrag();
66        }
67    }
68}
69