summaryrefslogtreecommitdiff
path: root/quickstep/src/com/android/quickstep/RecentsModel.java
blob: 89351aa2d297a9852e8c9185baaacf90a5fc53ef (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright (C) 2018 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.quickstep;

import static android.os.Process.THREAD_PRIORITY_BACKGROUND;

import static com.android.launcher3.Flags.enableGridOnlyOverview;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.quickstep.TaskUtils.checkCurrentOrManagedUserId;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.content.ComponentCallbacks;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Process;
import android.os.UserHandle;

import androidx.annotation.VisibleForTesting;

import com.android.launcher3.icons.IconProvider;
import com.android.launcher3.icons.IconProvider.IconChangeListener;
import com.android.launcher3.util.Executors.SimpleThreadFactory;
import com.android.launcher3.util.MainThreadInitializedObject;
import com.android.launcher3.util.SafeCloseable;
import com.android.quickstep.util.GroupTask;
import com.android.quickstep.util.TaskVisualsChangeListener;
import com.android.systemui.shared.recents.model.Task;
import com.android.systemui.shared.recents.model.ThumbnailData;
import com.android.systemui.shared.system.ActivityManagerWrapper;
import com.android.systemui.shared.system.TaskStackChangeListener;
import com.android.systemui.shared.system.TaskStackChangeListeners;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * Singleton class to load and manage recents model.
 */
@TargetApi(Build.VERSION_CODES.O)
public class RecentsModel implements IconChangeListener, TaskStackChangeListener,
        TaskVisualsChangeListener, SafeCloseable {

    // We do not need any synchronization for this variable as its only written on UI thread.
    public static final MainThreadInitializedObject<RecentsModel> INSTANCE =
            new MainThreadInitializedObject<>(RecentsModel::new);

    private static final Executor RECENTS_MODEL_EXECUTOR = Executors.newSingleThreadExecutor(
            new SimpleThreadFactory("TaskThumbnailIconCache-", THREAD_PRIORITY_BACKGROUND));

    private final List<TaskVisualsChangeListener> mThumbnailChangeListeners = new ArrayList<>();
    private final Context mContext;

    private final RecentTasksList mTaskList;
    private final TaskIconCache mIconCache;
    private final TaskThumbnailCache mThumbnailCache;
    private final ComponentCallbacks mCallbacks;

    private final TaskStackChangeListeners mTaskStackChangeListeners;

    private RecentsModel(Context context) {
        this(context, new IconProvider(context));
    }

    private RecentsModel(Context context, IconProvider iconProvider) {
        this(context,
                new RecentTasksList(MAIN_EXECUTOR,
                        context.getSystemService(KeyguardManager.class),
                        SystemUiProxy.INSTANCE.get(context)),
                new TaskIconCache(context, RECENTS_MODEL_EXECUTOR, iconProvider),
                new TaskThumbnailCache(context, RECENTS_MODEL_EXECUTOR),
                iconProvider,
                TaskStackChangeListeners.getInstance());
    }

    @VisibleForTesting
    RecentsModel(Context context, RecentTasksList taskList, TaskIconCache iconCache,
            TaskThumbnailCache thumbnailCache, IconProvider iconProvider,
            TaskStackChangeListeners taskStackChangeListeners) {
        mContext = context;
        mTaskList = taskList;
        mIconCache = iconCache;
        mIconCache.registerTaskVisualsChangeListener(this);
        mThumbnailCache = thumbnailCache;
        if (enableGridOnlyOverview()) {
            mCallbacks = new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged(Configuration configuration) {
                    updateCacheSizeAndPreloadIfNeeded();
                }

                @Override
                public void onLowMemory() {
                }
            };
            context.registerComponentCallbacks(mCallbacks);
        } else {
            mCallbacks = null;
        }

        mTaskStackChangeListeners = taskStackChangeListeners;
        mTaskStackChangeListeners.registerTaskStackListener(this);
        iconProvider.registerIconChangeListener(this, MAIN_EXECUTOR.getHandler());
    }

    public TaskIconCache getIconCache() {
        return mIconCache;
    }

    public TaskThumbnailCache getThumbnailCache() {
        return mThumbnailCache;
    }

    /**
     * Fetches the list of recent tasks. Tasks are ordered by recency, with the latest active tasks
     * at the end of the list.
     *
     * @param callback The callback to receive the task plan once its complete or null. This is
     *                always called on the UI thread.
     * @return the request id associated with this call.
     */
    public int getTasks(Consumer<ArrayList<GroupTask>> callback) {
        return mTaskList.getTasks(false /* loadKeysOnly */, callback,
                RecentsFilterState.DEFAULT_FILTER);
    }

    /**
     * Fetches the list of recent tasks, based on a filter
     *
     * @param callback The callback to receive the task plan once its complete or null. This is
     *                always called on the UI thread.
     * @param filter  Returns true if a GroupTask should be included into the list passed into
     *                callback.
     * @return the request id associated with this call.
     */
    public int getTasks(Consumer<ArrayList<GroupTask>> callback, Predicate<GroupTask> filter) {
        return mTaskList.getTasks(false /* loadKeysOnly */, callback, filter);
    }

    /**
     * @return Whether the provided {@param changeId} is the latest recent tasks list id.
     */
    public boolean isTaskListValid(int changeId) {
        return mTaskList.isTaskListValid(changeId);
    }

    /**
     * @return Whether the task list is currently updating in the background
     */
    @VisibleForTesting
    public boolean isLoadingTasksInBackground() {
        return mTaskList.isLoadingTasksInBackground();
    }

    /**
     * Checks if a task has been removed or not.
     *
     * @param callback Receives true if task is removed, false otherwise
     * @param filter Returns true if GroupTask should be in the list of considerations
     */
    public void isTaskRemoved(int taskId, Consumer<Boolean> callback, Predicate<GroupTask> filter) {
        // Invalidate the existing list before checking to ensure this reflects the current state in
        // the system
        mTaskList.onRecentTasksChanged();
        mTaskList.getTasks(true /* loadKeysOnly */, (taskGroups) -> {
            for (GroupTask group : taskGroups) {
                if (group.containsTask(taskId)) {
                    callback.accept(false);
                    return;
                }
            }
            callback.accept(true);
        }, filter);
    }

    @Override
    public void onTaskStackChangedBackground() {
        if (!mThumbnailCache.isPreloadingEnabled()) {
            // Skip if we aren't preloading
            return;
        }

        int currentUserId = Process.myUserHandle().getIdentifier();
        if (!checkCurrentOrManagedUserId(currentUserId, mContext)) {
            // Skip if we are not the current user
            return;
        }

        // Keep the cache up to date with the latest thumbnails
        ActivityManager.RunningTaskInfo runningTask =
                ActivityManagerWrapper.getInstance().getRunningTask();
        int runningTaskId = runningTask != null ? runningTask.id : -1;
        mTaskList.getTaskKeys(mThumbnailCache.getCacheSize(), taskGroups -> {
            for (GroupTask group : taskGroups) {
                if (group.containsTask(runningTaskId)) {
                    // Skip the running task, it's not going to have an up-to-date snapshot by the
                    // time the user next enters overview
                    continue;
                }
                mThumbnailCache.updateThumbnailInCache(group.task1, /* lowResolution= */ true);
                mThumbnailCache.updateThumbnailInCache(group.task2, /* lowResolution= */ true);
            }
        });
    }

    @Override
    public boolean onTaskSnapshotChanged(int taskId, ThumbnailData snapshot) {
        mThumbnailCache.updateTaskSnapShot(taskId, snapshot);

        for (int i = mThumbnailChangeListeners.size() - 1; i >= 0; i--) {
            Task task = mThumbnailChangeListeners.get(i).onTaskThumbnailChanged(taskId, snapshot);
            if (task != null) {
                task.thumbnail = snapshot;
            }
        }
        return true;
    }

    @Override
    public void onTaskRemoved(int taskId) {
        Task.TaskKey stubKey = new Task.TaskKey(taskId, 0, new Intent(), null, 0, 0);
        mThumbnailCache.remove(stubKey);
        mIconCache.onTaskRemoved(stubKey);
    }

    public void onTrimMemory(int level) {
        if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
            mThumbnailCache.getHighResLoadingState().setVisible(false);
        }
        if (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
            // Clear everything once we reach a low-mem situation
            mThumbnailCache.clear();
            mIconCache.clearCache();
        }
    }

    @Override
    public void onAppIconChanged(String packageName, UserHandle user) {
        mIconCache.invalidateCacheEntries(packageName, user);
        for (int i = mThumbnailChangeListeners.size() - 1; i >= 0; i--) {
            mThumbnailChangeListeners.get(i).onTaskIconChanged(packageName, user);
        }
    }

    @Override
    public void onTaskIconChanged(int taskId) {
        for (TaskVisualsChangeListener listener : mThumbnailChangeListeners) {
            listener.onTaskIconChanged(taskId);
        }
    }

    @Override
    public void onSystemIconStateChanged(String iconState) {
        mIconCache.clearCache();
    }

    /**
     * Adds a listener for visuals changes
     */
    public void addThumbnailChangeListener(TaskVisualsChangeListener listener) {
        mThumbnailChangeListeners.add(listener);
    }

    /**
     * Removes a previously added listener
     */
    public void removeThumbnailChangeListener(TaskVisualsChangeListener listener) {
        mThumbnailChangeListeners.remove(listener);
    }

    public void dump(String prefix, PrintWriter writer) {
        writer.println(prefix + "RecentsModel:");
        mTaskList.dump("  ", writer);
    }

    /**
     * Registers a listener for running tasks
     */
    public void registerRunningTasksListener(RunningTasksListener listener) {
        mTaskList.registerRunningTasksListener(listener);
    }

    /**
     * Removes the previously registered running tasks listener
     */
    public void unregisterRunningTasksListener() {
        mTaskList.unregisterRunningTasksListener();
    }

    /**
     * Gets the set of running tasks.
     */
    public ArrayList<ActivityManager.RunningTaskInfo> getRunningTasks() {
        return mTaskList.getRunningTasks();
    }

    /**
     * Preloads cache if enableGridOnlyOverview is true, preloading is enabled and
     * highResLoadingState is enabled
     */
    public void preloadCacheIfNeeded() {
        if (!enableGridOnlyOverview()) {
            return;
        }

        if (!mThumbnailCache.isPreloadingEnabled()) {
            // Skip if we aren't preloading.
            return;
        }

        if (!mThumbnailCache.getHighResLoadingState().isEnabled()) {
            // Skip if high-res loading state is disabled.
            return;
        }

        mTaskList.getTaskKeys(mThumbnailCache.getCacheSize(), taskGroups -> {
            for (GroupTask group : taskGroups) {
                mThumbnailCache.updateThumbnailInCache(group.task1, /* lowResolution= */ false);
                mThumbnailCache.updateThumbnailInCache(group.task2, /* lowResolution= */ false);
            }
        });
    }

    /**
     * Updates cache size and preloads more tasks if cache size increases
     */
    public void updateCacheSizeAndPreloadIfNeeded() {
        if (!enableGridOnlyOverview()) {
            return;
        }

        // If new size is larger than original size, preload more cache to fill the gap
        if (mThumbnailCache.updateCacheSizeAndRemoveExcess()) {
            preloadCacheIfNeeded();
        }
    }

    @Override
    public void close() {
        if (mCallbacks != null) {
            mContext.unregisterComponentCallbacks(mCallbacks);
        }
        mIconCache.removeTaskVisualsChangeListener();
        mTaskStackChangeListeners.unregisterTaskStackListener(this);
    }

    /**
     * Listener for receiving running tasks changes
     */
    public interface RunningTasksListener {
        /**
         * Called when there's a change to running tasks
         */
        void onRunningTasksChanged();
    }
}