summaryrefslogtreecommitdiff
path: root/quickstep/src/com/android/launcher3/taskbar/allapps/TaskbarAllAppsContext.java
blob: e2f752212e2916f15ad73a839c5eece72a9f62bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.taskbar.allapps;

import static android.view.KeyEvent.ACTION_UP;
import static android.view.KeyEvent.KEYCODE_BACK;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

import static com.android.systemui.shared.system.ViewTreeObserverWrapper.InsetsInfo.TOUCHABLE_INSETS_REGION;

import android.content.Context;
import android.graphics.Insets;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowInsets;

import com.android.launcher3.AbstractFloatingView;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.allapps.ActivityAllAppsContainerView;
import com.android.launcher3.allapps.search.DefaultSearchAdapterProvider;
import com.android.launcher3.allapps.search.SearchAdapterProvider;
import com.android.launcher3.dot.DotInfo;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.popup.PopupDataProvider;
import com.android.launcher3.taskbar.BaseTaskbarContext;
import com.android.launcher3.taskbar.TaskbarActivityContext;
import com.android.launcher3.taskbar.TaskbarDragController;
import com.android.launcher3.taskbar.TaskbarStashController;
import com.android.launcher3.testing.TestLogging;
import com.android.launcher3.testing.TestProtocol;
import com.android.launcher3.util.OnboardingPrefs;
import com.android.launcher3.util.TouchController;
import com.android.launcher3.views.BaseDragLayer;
import com.android.systemui.shared.system.ViewTreeObserverWrapper;
import com.android.systemui.shared.system.ViewTreeObserverWrapper.InsetsInfo;
import com.android.systemui.shared.system.ViewTreeObserverWrapper.OnComputeInsetsListener;

/**
 * Window context for the taskbar all apps overlay.
 * <p>
 * All apps has its own window and needs a window context. Some properties are delegated to the
 * {@link TaskbarActivityContext} such as {@link DeviceProfile} and {@link PopupDataProvider}.
 */
class TaskbarAllAppsContext extends BaseTaskbarContext {
    private final TaskbarActivityContext mTaskbarContext;
    private final OnboardingPrefs<TaskbarAllAppsContext> mOnboardingPrefs;

    private final TaskbarAllAppsController mWindowController;
    private final TaskbarAllAppsViewController mAllAppsViewController;
    private final TaskbarDragController mDragController;
    private final TaskbarAllAppsDragLayer mDragLayer;
    private final TaskbarAllAppsContainerView mAppsView;

    // We automatically stash taskbar when all apps is opened in gesture navigation mode.
    private final boolean mWillTaskbarBeVisuallyStashed;
    private final int mStashedTaskbarHeight;

    TaskbarAllAppsContext(
            TaskbarActivityContext taskbarContext,
            TaskbarAllAppsController windowController,
            TaskbarStashController taskbarStashController) {
        super(taskbarContext.createWindowContext(TYPE_APPLICATION_OVERLAY, null));
        mTaskbarContext = taskbarContext;
        mWindowController = windowController;
        mDragController = new TaskbarDragController(this);
        mOnboardingPrefs = new OnboardingPrefs<>(this, Utilities.getPrefs(this));

        mDragLayer = new TaskbarAllAppsDragLayer(this);
        TaskbarAllAppsSlideInView slideInView = (TaskbarAllAppsSlideInView) mLayoutInflater.inflate(
                R.layout.taskbar_all_apps, mDragLayer, false);
        mAllAppsViewController = new TaskbarAllAppsViewController(
                this,
                slideInView,
                windowController,
                taskbarStashController);
        mAppsView = slideInView.getAppsView();

        mWillTaskbarBeVisuallyStashed = taskbarStashController.supportsVisualStashing();
        mStashedTaskbarHeight = taskbarStashController.getStashedHeight();
    }

    TaskbarAllAppsViewController getAllAppsViewController() {
        return mAllAppsViewController;
    }

    @Override
    public DeviceProfile getDeviceProfile() {
        return mWindowController.getDeviceProfile();
    }

    @Override
    public TaskbarDragController getDragController() {
        return mDragController;
    }

    @Override
    public TaskbarAllAppsDragLayer getDragLayer() {
        return mDragLayer;
    }

    @Override
    public TaskbarAllAppsContainerView getAppsView() {
        return mAppsView;
    }

    @Override
    public OnboardingPrefs<TaskbarAllAppsContext> getOnboardingPrefs() {
        return mOnboardingPrefs;
    }

    @Override
    public boolean isBindingItems() {
        return mTaskbarContext.isBindingItems();
    }

    @Override
    public View.OnClickListener getItemOnClickListener() {
        return mTaskbarContext.getItemOnClickListener();
    }

    @Override
    public PopupDataProvider getPopupDataProvider() {
        return mTaskbarContext.getPopupDataProvider();
    }

    @Override
    public DotInfo getDotInfoForItem(ItemInfo info) {
        return mTaskbarContext.getDotInfoForItem(info);
    }

    @Override
    public void onDragStart() {}

    @Override
    public void onDragEnd() {
        mWindowController.maybeCloseWindow();
    }

    @Override
    public void onPopupVisibilityChanged(boolean isVisible) {}

    @Override
    public SearchAdapterProvider<?> createSearchAdapterProvider(
            ActivityAllAppsContainerView<?> appsView) {
        return new DefaultSearchAdapterProvider(this);
    }

    /** Root drag layer for this context. */
    private static class TaskbarAllAppsDragLayer extends
            BaseDragLayer<TaskbarAllAppsContext> implements OnComputeInsetsListener {

        private TaskbarAllAppsDragLayer(Context context) {
            super(context, null, 1);
            setClipChildren(false);
            recreateControllers();
        }

        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            ViewTreeObserverWrapper.addOnComputeInsetsListener(
                    getViewTreeObserver(), this);
        }

        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            ViewTreeObserverWrapper.removeOnComputeInsetsListener(this);
        }

        @Override
        public void recreateControllers() {
            mControllers = new TouchController[]{mActivity.mDragController};
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            TestLogging.recordMotionEvent(TestProtocol.SEQUENCE_MAIN, "Touch event", ev);
            return super.dispatchTouchEvent(ev);
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event.getAction() == ACTION_UP && event.getKeyCode() == KEYCODE_BACK) {
                AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(mActivity);
                if (topView != null && topView.onBackPressed()) {
                    return true;
                }
            }
            return super.dispatchKeyEvent(event);
        }

        @Override
        public void onComputeInsets(InsetsInfo inoutInfo) {
            if (mActivity.mDragController.isSystemDragInProgress()) {
                inoutInfo.touchableRegion.setEmpty();
                inoutInfo.setTouchableInsets(TOUCHABLE_INSETS_REGION);
            }
        }

        @Override
        public WindowInsets onApplyWindowInsets(WindowInsets insets) {
            return updateInsetsDueToStashing(insets);
        }

        /**
         * Taskbar automatically stashes when opening all apps, but we don't report the insets as
         * changing to avoid moving the underlying app. But internally, the apps view should still
         * layout according to the stashed insets rather than the unstashed insets. So this method
         * does two things:
         * 1) Sets navigationBars bottom inset to stashedHeight.
         * 2) Sets tappableInsets bottom inset to 0.
         */
        private WindowInsets updateInsetsDueToStashing(WindowInsets oldInsets) {
            if (!mActivity.mWillTaskbarBeVisuallyStashed) {
                return oldInsets;
            }
            WindowInsets.Builder updatedInsetsBuilder = new WindowInsets.Builder(oldInsets);

            Insets oldNavInsets = oldInsets.getInsets(WindowInsets.Type.navigationBars());
            Insets newNavInsets = Insets.of(oldNavInsets.left, oldNavInsets.top, oldNavInsets.right,
                    mActivity.mStashedTaskbarHeight);
            updatedInsetsBuilder.setInsets(WindowInsets.Type.navigationBars(), newNavInsets);

            Insets oldTappableInsets = oldInsets.getInsets(WindowInsets.Type.tappableElement());
            Insets newTappableInsets = Insets.of(oldTappableInsets.left, oldTappableInsets.top,
                    oldTappableInsets.right, 0);
            updatedInsetsBuilder.setInsets(WindowInsets.Type.tappableElement(), newTappableInsets);

            return updatedInsetsBuilder.build();
        }
    }
}