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.Utilities; 24 import com.android.launcher3.Workspace; 25 26 public class SpringLoadedDragController implements OnAlarmListener { 27 // how long the user must hover over a mini-screen before it unshrinks 28 private static final long ENTER_SPRING_LOAD_HOVER_TIME = 500; 29 private static final long ENTER_SPRING_LOAD_HOVER_TIME_IN_TEST = 2000; 30 private static final long ENTER_SPRING_LOAD_CANCEL_HOVER_TIME = 950; 31 32 Alarm mAlarm; 33 34 // the screen the user is currently hovering over, if any 35 private CellLayout mScreen; 36 private Launcher mLauncher; 37 SpringLoadedDragController(Launcher launcher)38 public SpringLoadedDragController(Launcher launcher) { 39 mLauncher = launcher; 40 mAlarm = new Alarm(); 41 mAlarm.setOnAlarmListener(this); 42 } 43 getEnterSpringLoadHoverTime()44 private long getEnterSpringLoadHoverTime() { 45 // Some TAPL tests are flaky on Cuttlefish with a low waiting time 46 return Utilities.isRunningInTestHarness() 47 ? ENTER_SPRING_LOAD_HOVER_TIME_IN_TEST 48 : ENTER_SPRING_LOAD_HOVER_TIME; 49 } 50 cancel()51 public void cancel() { 52 mAlarm.cancelAlarm(); 53 } 54 55 // Set a new alarm to expire for the screen that we are hovering over now setAlarm(CellLayout cl)56 public void setAlarm(CellLayout cl) { 57 mAlarm.cancelAlarm(); 58 mAlarm.setAlarm((cl == null) ? ENTER_SPRING_LOAD_CANCEL_HOVER_TIME 59 : getEnterSpringLoadHoverTime()); 60 mScreen = cl; 61 } 62 63 // this is called when our timer runs out onAlarm(Alarm alarm)64 public void onAlarm(Alarm alarm) { 65 if (mScreen != null) { 66 // Snap to the screen that we are hovering over now 67 Workspace<?> w = mLauncher.getWorkspace(); 68 if (!w.isVisible(mScreen)) { 69 w.snapToPage(w.indexOfChild(mScreen)); 70 } 71 } else { 72 mLauncher.getDragController().cancelDrag(); 73 } 74 } 75 } 76