1 /* 2 * Copyright (C) 2017 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 package com.android.launcher3.states; 17 18 import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_HOME; 19 20 import android.content.Context; 21 import android.graphics.Rect; 22 23 import com.android.launcher3.DeviceProfile; 24 import com.android.launcher3.Launcher; 25 import com.android.launcher3.LauncherState; 26 import com.android.launcher3.Workspace; 27 28 /** 29 * Definition for spring loaded state used during drag and drop. 30 */ 31 public class SpringLoadedState extends LauncherState { 32 33 private static final int STATE_FLAGS = FLAG_MULTI_PAGE 34 | FLAG_WORKSPACE_INACCESSIBLE | FLAG_DISABLE_RESTORE 35 | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_WORKSPACE_HAS_BACKGROUNDS 36 | FLAG_HIDE_BACK_BUTTON; 37 SpringLoadedState(int id)38 public SpringLoadedState(int id) { 39 super(id, LAUNCHER_STATE_HOME, STATE_FLAGS); 40 } 41 42 @Override getTransitionDuration(Context context)43 public int getTransitionDuration(Context context) { 44 return 150; 45 } 46 47 @Override getWorkspaceScaleAndTranslation(Launcher launcher)48 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) { 49 DeviceProfile grid = launcher.getDeviceProfile(); 50 Workspace ws = launcher.getWorkspace(); 51 if (ws.getChildCount() == 0) { 52 return super.getWorkspaceScaleAndTranslation(launcher); 53 } 54 55 if (grid.isVerticalBarLayout()) { 56 float scale = grid.workspaceSpringLoadShrinkFactor; 57 return new ScaleAndTranslation(scale, 0, 0); 58 } 59 60 float scale = grid.workspaceSpringLoadShrinkFactor; 61 Rect insets = launcher.getDragLayer().getInsets(); 62 63 float scaledHeight = scale * ws.getNormalChildHeight(); 64 float shrunkTop = insets.top + grid.dropTargetBarSizePx; 65 float shrunkBottom = ws.getMeasuredHeight() - insets.bottom 66 - grid.workspacePadding.bottom 67 - grid.workspaceSpringLoadedBottomSpace; 68 float totalShrunkSpace = shrunkBottom - shrunkTop; 69 70 float desiredCellTop = shrunkTop + (totalShrunkSpace - scaledHeight) / 2; 71 72 float halfHeight = ws.getHeight() / 2; 73 float myCenter = ws.getTop() + halfHeight; 74 float cellTopFromCenter = halfHeight - ws.getChildAt(0).getTop(); 75 float actualCellTop = myCenter - cellTopFromCenter * scale; 76 return new ScaleAndTranslation(scale, 0, (desiredCellTop - actualCellTop) / scale); 77 } 78 79 @Override getDepthUnchecked(Context context)80 protected float getDepthUnchecked(Context context) { 81 return 0.5f; 82 } 83 84 @Override getHotseatScaleAndTranslation(Launcher launcher)85 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) { 86 return new ScaleAndTranslation(1, 0, 0); 87 } 88 89 @Override getWorkspaceBackgroundAlpha(Launcher launcher)90 public float getWorkspaceBackgroundAlpha(Launcher launcher) { 91 return 0.2f; 92 } 93 } 94