Home | History | Annotate | Download | only in states
      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.LauncherAnimUtils.SPRING_LOADED_TRANSITION_MS;
     19 import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK;
     20 
     21 import android.graphics.Rect;
     22 
     23 import com.android.launcher3.DeviceProfile;
     24 import com.android.launcher3.InstallShortcutReceiver;
     25 import com.android.launcher3.Launcher;
     26 import com.android.launcher3.LauncherState;
     27 import com.android.launcher3.Workspace;
     28 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
     29 
     30 /**
     31  * Definition for spring loaded state used during drag and drop.
     32  */
     33 public class SpringLoadedState extends LauncherState {
     34 
     35     private static final int STATE_FLAGS = FLAG_MULTI_PAGE |
     36             FLAG_DISABLE_ACCESSIBILITY | FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED |
     37             FLAG_DISABLE_PAGE_CLIPPING | FLAG_PAGE_BACKGROUNDS | FLAG_HIDE_BACK_BUTTON;
     38 
     39     public SpringLoadedState(int id) {
     40         super(id, ContainerType.OVERVIEW, SPRING_LOADED_TRANSITION_MS, STATE_FLAGS);
     41     }
     42 
     43     @Override
     44     public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
     45         DeviceProfile grid = launcher.getDeviceProfile();
     46         Workspace ws = launcher.getWorkspace();
     47         if (ws.getChildCount() == 0) {
     48             return super.getWorkspaceScaleAndTranslation(launcher);
     49         }
     50 
     51         if (grid.isVerticalBarLayout()) {
     52             float scale = grid.workspaceSpringLoadShrinkFactor;
     53             return new float[] {scale, 0, 0};
     54         }
     55 
     56         float scale = grid.workspaceSpringLoadShrinkFactor;
     57         Rect insets = launcher.getDragLayer().getInsets();
     58 
     59         float scaledHeight = scale * ws.getNormalChildHeight();
     60         float shrunkTop = insets.top + grid.dropTargetBarSizePx;
     61         float shrunkBottom = ws.getMeasuredHeight() - insets.bottom
     62                 - grid.workspacePadding.bottom
     63                 - grid.workspaceSpringLoadedBottomSpace;
     64         float totalShrunkSpace = shrunkBottom - shrunkTop;
     65 
     66         float desiredCellTop = shrunkTop + (totalShrunkSpace - scaledHeight) / 2;
     67 
     68         float halfHeight = ws.getHeight() / 2;
     69         float myCenter = ws.getTop() + halfHeight;
     70         float cellTopFromCenter = halfHeight - ws.getChildAt(0).getTop();
     71         float actualCellTop = myCenter - cellTopFromCenter * scale;
     72         return new float[] { scale, 0, (desiredCellTop - actualCellTop) / scale};
     73     }
     74 
     75     @Override
     76     public void onStateEnabled(Launcher launcher) {
     77         Workspace ws = launcher.getWorkspace();
     78         ws.showPageIndicatorAtCurrentScroll();
     79         ws.getPageIndicator().setShouldAutoHide(false);
     80 
     81         // Prevent any Un/InstallShortcutReceivers from updating the db while we are
     82         // in spring loaded mode
     83         InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_DRAG_AND_DROP);
     84         launcher.getRotationHelper().setCurrentStateRequest(REQUEST_LOCK);
     85     }
     86 
     87     @Override
     88     public float getWorkspaceScrimAlpha(Launcher launcher) {
     89         return 0.3f;
     90     }
     91 
     92     @Override
     93     public void onStateDisabled(final Launcher launcher) {
     94         launcher.getWorkspace().getPageIndicator().setShouldAutoHide(true);
     95 
     96         // Re-enable any Un/InstallShortcutReceiver and now process any queued items
     97         InstallShortcutReceiver.disableAndFlushInstallQueue(
     98                 InstallShortcutReceiver.FLAG_DRAG_AND_DROP, launcher);
     99     }
    100 }
    101