summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/statemanager/StatefulActivity.java
blob: 30ba703fd79c175e944f35c48d983c5ea4d79aad (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
/*
 * Copyright (C) 2020 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.statemanager;

import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;

import static com.android.launcher3.LauncherState.FLAG_CLOSE_POPUPS;
import static com.android.launcher3.LauncherState.FLAG_NON_INTERACTIVE;

import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.CallSuper;

import com.android.launcher3.AbstractFloatingView;
import com.android.launcher3.BaseDraggingActivity;
import com.android.launcher3.LauncherRootView;
import com.android.launcher3.Utilities;
import com.android.launcher3.statemanager.StateManager.AtomicAnimationFactory;
import com.android.launcher3.statemanager.StateManager.StateHandler;
import com.android.launcher3.util.window.WindowManagerProxy;
import com.android.launcher3.views.BaseDragLayer;

import java.util.List;

/**
 * Abstract activity with state management
 * @param <STATE_TYPE> Type of state object
 */
public abstract class StatefulActivity<STATE_TYPE extends BaseState<STATE_TYPE>>
        extends BaseDraggingActivity {

    public final Handler mHandler = new Handler();
    private final Runnable mHandleDeferredResume = this::handleDeferredResume;
    private boolean mDeferredResumePending;

    private LauncherRootView mRootView;

    protected Configuration mOldConfig;
    private int mOldRotation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mOldConfig = new Configuration(getResources().getConfiguration());
        mOldRotation = WindowManagerProxy.INSTANCE.get(this).getRotation(this);
    }

    /**
     * Create handlers to control the property changes for this activity
     */
    protected abstract void collectStateHandlers(List<StateHandler> out);

    /**
     * Returns true if the activity is in the provided state
     */
    public boolean isInState(STATE_TYPE state) {
        return getStateManager().getState() == state;
    }

    /**
     * Returns the state manager for this activity
     */
    public abstract StateManager<STATE_TYPE> getStateManager();

    protected void inflateRootView(int layoutId) {
        mRootView = (LauncherRootView) LayoutInflater.from(this).inflate(layoutId, null);
        mRootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }

    @Override
    public final LauncherRootView getRootView() {
        return mRootView;
    }

    @Override
    public <T extends View> T findViewById(int id) {
        return mRootView.findViewById(id);
    }

    /**
     * Called when transition to the state starts
     */
    @CallSuper
    public void onStateSetStart(STATE_TYPE state) {
        if (mDeferredResumePending) {
            handleDeferredResume();
        }

        if (state.hasFlag(FLAG_CLOSE_POPUPS)) {
            AbstractFloatingView.closeAllOpenViews(this, !state.hasFlag(FLAG_NON_INTERACTIVE));
        }
    }

    /**
     * Called when transition to state ends
     */
    public void onStateSetEnd(STATE_TYPE state) { }

    /**
     * Creates a factory for atomic state animations
     */
    public AtomicAnimationFactory<STATE_TYPE> createAtomicAnimationFactory() {
        return new AtomicAnimationFactory(0);
    }

    @Override
    public void reapplyUi() {
        getRootView().dispatchInsets();
        getStateManager().reapplyState(true /* cancelCurrentAnimation */);
    }

    @Override
    protected void onStop() {
        BaseDragLayer dragLayer = getDragLayer();
        final boolean wasActive = isUserActive();
        final STATE_TYPE origState = getStateManager().getState();
        final int origDragLayerChildCount = dragLayer.getChildCount();
        super.onStop();

        if (!isChangingConfigurations()) {
            getStateManager().moveToRestState();
        }

        // Workaround for b/78520668, explicitly trim memory once UI is hidden
        onTrimMemory(TRIM_MEMORY_UI_HIDDEN);

        if (wasActive) {
            // The expected condition is that this activity is stopped because the device goes to
            // sleep and the UI may have noticeable changes.
            dragLayer.post(() -> {
                if ((!getStateManager().isInStableState(origState)
                        // The drag layer may be animating (e.g. dismissing QSB).
                        || dragLayer.getAlpha() < 1
                        // Maybe an ArrowPopup is closed.
                        || dragLayer.getChildCount() != origDragLayerChildCount)) {
                    onUiChangedWhileSleeping();
                }
            });
        }
    }

    /**
     * Called if the Activity UI changed while the activity was not visible
     */
    public void onUiChangedWhileSleeping() { }

    private void handleDeferredResume() {
        if (hasBeenResumed() && !getStateManager().getState().hasFlag(FLAG_NON_INTERACTIVE)) {
            addActivityFlags(ACTIVITY_STATE_DEFERRED_RESUMED);
            onDeferredResumed();

            mDeferredResumePending = false;
        } else {
            mDeferredResumePending = true;
        }
    }

    /**
     * Called want the activity has stayed resumed for 1 frame.
     */
    protected void onDeferredResumed() { }

    @Override
    protected void onResume() {
        super.onResume();

        mHandler.removeCallbacks(mHandleDeferredResume);
        Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
    }

    /**
     * Runs the given {@param r} runnable when this activity binds to the touch interaction service.
     */
    public void runOnBindToTouchInteractionService(Runnable r) {
        r.run();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        handleConfigurationChanged(newConfig);
        super.onConfigurationChanged(newConfig);
    }

    /**
     * Handles configuration change when system calls {@link #onConfigurationChanged}, or on other
     * situations that configuration might change.
     */
    public void handleConfigurationChanged(Configuration newConfig) {
        int diff = newConfig.diff(mOldConfig);
        int rotation = WindowManagerProxy.INSTANCE.get(this).getRotation(this);
        if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0
                || rotation != mOldRotation) {
            onHandleConfigurationChanged();
        }

        mOldConfig.setTo(newConfig);
        mOldRotation = rotation;
    }

    /**
     * Logic for when device configuration changes (rotation, screen size change, multi-window,
     * etc.)
     */
    protected abstract void onHandleConfigurationChanged();

    /**
     * Enter staged split directly from the current running app.
     * @param leftOrTop if the staged split will be positioned left or top.
     */
    public void enterStageSplitFromRunningApp(boolean leftOrTop) { }
}