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.wm.shell;
18 
19 import android.annotation.UiContext;
20 import android.content.Context;
21 
22 import com.android.wm.shell.common.ShellExecutor;
23 import com.android.wm.shell.common.SyncTransactionQueue;
24 import com.android.wm.shell.common.annotations.ExternalThread;
25 
26 import java.util.concurrent.Executor;
27 import java.util.function.Consumer;
28 
29 /** Factory controller which can create {@link TaskView} */
30 public class TaskViewFactoryController {
31     private final ShellTaskOrganizer mTaskOrganizer;
32     private final ShellExecutor mShellExecutor;
33     private final SyncTransactionQueue mSyncQueue;
34     private final TaskViewFactory mImpl = new TaskViewFactoryImpl();
35 
TaskViewFactoryController(ShellTaskOrganizer taskOrganizer, ShellExecutor shellExecutor, SyncTransactionQueue syncQueue)36     public TaskViewFactoryController(ShellTaskOrganizer taskOrganizer,
37             ShellExecutor shellExecutor, SyncTransactionQueue syncQueue) {
38         mTaskOrganizer = taskOrganizer;
39         mShellExecutor = shellExecutor;
40         mSyncQueue = syncQueue;
41     }
42 
asTaskViewFactory()43     public TaskViewFactory asTaskViewFactory() {
44         return mImpl;
45     }
46 
47     /** Creates an {@link TaskView} */
create(@iContext Context context, Executor executor, Consumer<TaskView> onCreate)48     public void create(@UiContext Context context, Executor executor, Consumer<TaskView> onCreate) {
49         TaskView taskView = new TaskView(context, mTaskOrganizer, mSyncQueue);
50         executor.execute(() -> {
51             onCreate.accept(taskView);
52         });
53     }
54 
55     private class TaskViewFactoryImpl implements TaskViewFactory {
56         @ExternalThread
create(@iContext Context context, Executor executor, Consumer<TaskView> onCreate)57         public void create(@UiContext Context context,
58                 Executor executor, Consumer<TaskView> onCreate) {
59             mShellExecutor.execute(() -> {
60                 TaskViewFactoryController.this.create(context, executor, onCreate);
61             });
62         }
63     }
64 }
65