1 /* 2 * Copyright (C) 2020 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 17 package com.android.car.carlauncher; 18 19 import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW; 20 21 import static com.android.car.carlauncher.CarLauncher.TAG; 22 import static com.android.wm.shell.ShellTaskOrganizer.TASK_LISTENER_TYPE_FULLSCREEN; 23 24 import android.annotation.UiContext; 25 import android.app.ActivityTaskManager; 26 import android.app.TaskInfo; 27 import android.content.Context; 28 import android.util.Slog; 29 import android.window.TaskAppearedInfo; 30 31 import com.android.wm.shell.FullscreenTaskListener; 32 import com.android.wm.shell.ShellTaskOrganizer; 33 import com.android.wm.shell.TaskView; 34 import com.android.wm.shell.TaskViewFactory; 35 import com.android.wm.shell.TaskViewFactoryController; 36 import com.android.wm.shell.common.HandlerExecutor; 37 import com.android.wm.shell.common.SyncTransactionQueue; 38 import com.android.wm.shell.common.TransactionPool; 39 import com.android.wm.shell.startingsurface.StartingWindowController; 40 import com.android.wm.shell.startingsurface.phone.PhoneStartingWindowTypeAlgorithm; 41 42 import java.util.List; 43 import java.util.function.Consumer; 44 45 public final class TaskViewManager { 46 private static final boolean DBG = false; 47 48 private final Context mContext; 49 private final HandlerExecutor mExecutor; 50 private final TaskViewFactory mTaskViewFactory; 51 TaskViewManager(@iContext Context context, HandlerExecutor handlerExecutor)52 public TaskViewManager(@UiContext Context context, HandlerExecutor handlerExecutor) { 53 mContext = context; 54 mExecutor = handlerExecutor; 55 mTaskViewFactory = initWmShell(); 56 } 57 initWmShell()58 private TaskViewFactory initWmShell() { 59 ShellTaskOrganizer taskOrganizer = new ShellTaskOrganizer(mExecutor, mContext); 60 TransactionPool transactionPool = new TransactionPool(); 61 FullscreenTaskListener fullscreenTaskListener = 62 new FullscreenTaskListener(new SyncTransactionQueue(transactionPool, mExecutor)); 63 taskOrganizer.addListenerForType(fullscreenTaskListener, TASK_LISTENER_TYPE_FULLSCREEN); 64 StartingWindowController startingController = 65 new StartingWindowController(mContext, mExecutor, 66 new PhoneStartingWindowTypeAlgorithm(), transactionPool); 67 taskOrganizer.initStartingWindow(startingController); 68 List<TaskAppearedInfo> taskAppearedInfos = taskOrganizer.registerOrganizer(); 69 cleanUpExistingTaskViewTasks(taskAppearedInfos); 70 71 return new TaskViewFactoryController(taskOrganizer, mExecutor).asTaskViewFactory(); 72 } 73 createTaskView(Consumer<TaskView> onCreate)74 void createTaskView(Consumer<TaskView> onCreate) { 75 mTaskViewFactory.create(mContext, mExecutor, onCreate); 76 } 77 cleanUpExistingTaskViewTasks(List<TaskAppearedInfo> taskAppearedInfos)78 private static void cleanUpExistingTaskViewTasks(List<TaskAppearedInfo> taskAppearedInfos) { 79 ActivityTaskManager atm = ActivityTaskManager.getInstance(); 80 for (TaskAppearedInfo taskAppearedInfo : taskAppearedInfos) { 81 TaskInfo taskInfo = taskAppearedInfo.getTaskInfo(); 82 // Only TaskView tasks have WINDOWING_MODE_MULTI_WINDOW. 83 if (taskInfo.getWindowingMode() == WINDOWING_MODE_MULTI_WINDOW) { 84 if (DBG) Slog.d(TAG, "Found the dangling task, removing: " + taskInfo.taskId); 85 atm.removeTask(taskInfo.taskId); 86 } 87 } 88 } 89 } 90