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.allapps;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.WindowInsets;
21 
22 import com.android.launcher3.Launcher;
23 import com.android.launcher3.LauncherState;
24 import com.android.launcher3.statemanager.StateManager;
25 
26 /**
27  * AllAppsContainerView with launcher specific callbacks
28  */
29 public class LauncherAllAppsContainerView extends ActivityAllAppsContainerView<Launcher> {
30 
LauncherAllAppsContainerView(Context context)31     public LauncherAllAppsContainerView(Context context) {
32         this(context, null);
33     }
34 
LauncherAllAppsContainerView(Context context, AttributeSet attrs)35     public LauncherAllAppsContainerView(Context context, AttributeSet attrs) {
36         this(context, attrs, 0);
37     }
38 
LauncherAllAppsContainerView(Context context, AttributeSet attrs, int defStyleAttr)39     public LauncherAllAppsContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
40         super(context, attrs, defStyleAttr);
41     }
42 
43     @Override
computeNavBarScrimHeight(WindowInsets insets)44     protected int computeNavBarScrimHeight(WindowInsets insets) {
45         return insets.getTappableElementInsets().bottom;
46     }
47 
48     @Override
isInAllApps()49     public boolean isInAllApps() {
50         return mActivityContext.getStateManager().isInStableState(LauncherState.ALL_APPS);
51     }
52 
53     @Override
shouldFloatingSearchBarBePillWhenUnfocused()54     public boolean shouldFloatingSearchBarBePillWhenUnfocused() {
55         if (!isSearchBarFloating()) {
56             return false;
57         }
58         Launcher launcher = mActivityContext;
59         StateManager<LauncherState, Launcher> manager = launcher.getStateManager();
60         if (manager.isInTransition() && manager.getTargetState() != null) {
61             return manager.getTargetState().shouldFloatingSearchBarUsePillWhenUnfocused(launcher);
62         }
63         return manager.getCurrentStableState()
64                 .shouldFloatingSearchBarUsePillWhenUnfocused(launcher);
65     }
66 
67     @Override
getFloatingSearchBarRestingMarginBottom()68     public int getFloatingSearchBarRestingMarginBottom() {
69         if (!isSearchBarFloating()) {
70             return super.getFloatingSearchBarRestingMarginBottom();
71         }
72         Launcher launcher = mActivityContext;
73         StateManager<LauncherState, Launcher> stateManager = launcher.getStateManager();
74 
75         // We want to rest at the current state's resting position, unless we are in transition and
76         // the target state's resting position is higher (that way if we are closing the keyboard,
77         // we can stop translating at that point).
78         int currentStateMarginBottom = stateManager.getCurrentStableState()
79                 .getFloatingSearchBarRestingMarginBottom(launcher);
80         int targetStateMarginBottom = -1;
81         if (stateManager.isInTransition() && stateManager.getTargetState() != null) {
82             targetStateMarginBottom = stateManager.getTargetState()
83                     .getFloatingSearchBarRestingMarginBottom(launcher);
84             if (targetStateMarginBottom < 0) {
85                 // Go ahead and move offscreen.
86                 return targetStateMarginBottom;
87             }
88         }
89         return Math.max(targetStateMarginBottom, currentStateMarginBottom);
90     }
91 
92     @Override
getFloatingSearchBarRestingMarginStart()93     public int getFloatingSearchBarRestingMarginStart() {
94         if (!isSearchBarFloating()) {
95             return super.getFloatingSearchBarRestingMarginStart();
96         }
97 
98         StateManager<LauncherState, Launcher> stateManager = mActivityContext.getStateManager();
99 
100         // Special case to not expand the search bar when exiting All Apps on phones.
101         if (stateManager.getCurrentStableState() == LauncherState.ALL_APPS
102                 && mActivityContext.getDeviceProfile().isPhone) {
103             return LauncherState.ALL_APPS.getFloatingSearchBarRestingMarginStart(mActivityContext);
104         }
105 
106         if (stateManager.isInTransition() && stateManager.getTargetState() != null) {
107             return stateManager.getTargetState()
108                     .getFloatingSearchBarRestingMarginStart(mActivityContext);
109         }
110         return stateManager.getCurrentStableState()
111                 .getFloatingSearchBarRestingMarginStart(mActivityContext);
112     }
113 
114     @Override
getFloatingSearchBarRestingMarginEnd()115     public int getFloatingSearchBarRestingMarginEnd() {
116         if (!isSearchBarFloating()) {
117             return super.getFloatingSearchBarRestingMarginEnd();
118         }
119 
120         StateManager<LauncherState, Launcher> stateManager = mActivityContext.getStateManager();
121 
122         // Special case to not expand the search bar when exiting All Apps on phones.
123         if (stateManager.getCurrentStableState() == LauncherState.ALL_APPS
124                 && mActivityContext.getDeviceProfile().isPhone) {
125             return LauncherState.ALL_APPS.getFloatingSearchBarRestingMarginEnd(mActivityContext);
126         }
127 
128         if (stateManager.isInTransition() && stateManager.getTargetState() != null) {
129             return stateManager.getTargetState()
130                     .getFloatingSearchBarRestingMarginEnd(mActivityContext);
131         }
132         return stateManager.getCurrentStableState()
133                 .getFloatingSearchBarRestingMarginEnd(mActivityContext);
134     }
135 }
136