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 17 package com.android.launcher3; 18 19 public class SpringLoadedDragController implements OnAlarmListener { 20 // how long the user must hover over a mini-screen before it unshrinks 21 final long ENTER_SPRING_LOAD_HOVER_TIME = 500; 22 final long ENTER_SPRING_LOAD_CANCEL_HOVER_TIME = 950; 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 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) { 43 mAlarm.cancelAlarm(); 44 mAlarm.setAlarm((cl == null) ? ENTER_SPRING_LOAD_CANCEL_HOVER_TIME : 45 ENTER_SPRING_LOAD_HOVER_TIME); 46 mScreen = cl; 47 } 48 49 // this is called when our timer runs out 50 public void onAlarm(Alarm alarm) { 51 if (mScreen != null) { 52 // Snap to the screen that we are hovering over now 53 Workspace w = mLauncher.getWorkspace(); 54 int page = w.indexOfChild(mScreen); 55 if (page != w.getCurrentPage()) { 56 w.snapToPage(page); 57 } 58 } else { 59 mLauncher.getDragController().cancelDrag(); 60 } 61 } 62 } 63