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