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.dragndrop; 18 19 import com.android.launcher3.Alarm; 20 import com.android.launcher3.CellLayout; 21 import com.android.launcher3.Launcher; 22 import com.android.launcher3.OnAlarmListener; 23 import com.android.launcher3.Workspace; 24 25 public 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 SpringLoadedDragController(Launcher launcher)36 public SpringLoadedDragController(Launcher launcher) { 37 mLauncher = launcher; 38 mAlarm = new Alarm(); 39 mAlarm.setOnAlarmListener(this); 40 } 41 cancel()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 setAlarm(CellLayout cl)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 onAlarm(Alarm alarm)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 if (!w.isVisible(mScreen)) { 60 w.snapToPage(w.indexOfChild(mScreen)); 61 } 62 } else { 63 mLauncher.getDragController().cancelDrag(); 64 } 65 } 66 } 67