1/*
2 * Copyright 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
17import { shortenName } from '../mixin'
18import { Task, toRect } from "../common"
19import WindowContainer from "./WindowContainer"
20
21Task.fromProto = function (proto: any, isActivityInTree: Boolean): Task {
22    if (proto == null) {
23        return null;
24    } else {
25        const windowContainerProto = proto.taskFragment?.windowContainer ?? proto.windowContainer;
26        const windowContainer = WindowContainer.fromProto(
27            /* proto */ windowContainerProto,
28            /* protoChildren */ windowContainerProto.children.reverse(),
29            /* isActivityInTree */ isActivityInTree
30        );
31
32        const entry = new Task(
33            proto.taskFragment?.activityType ?? proto.activityType,
34            proto.fillsParent,
35            toRect(proto.bounds),
36            proto.id,
37            proto.rootTaskId,
38            proto.taskFragment?.displayId,
39            toRect(proto.lastNonFullscreenBounds),
40            proto.realActivity,
41            proto.origActivity,
42            proto.resizeMode,
43            proto.resumedActivity?.title ?? "",
44            proto.animatingBounds,
45            proto.surfaceWidth,
46            proto.surfaceHeight,
47            proto.createdByOrganizer,
48            proto.taskFragment?.minWidth ?? proto.minWidth,
49            proto.taskFragment?.minHeight ?? proto.minHeight,
50            windowContainer
51        );
52
53        addAttributes(entry, proto);
54        console.warn("Created ", entry.kind, " stableId=", entry.stableId);
55        return entry;
56    }
57}
58
59function addAttributes(entry: Task, proto: any) {
60    entry.proto = proto;
61    entry.kind = entry.constructor.name;
62    entry.shortName = shortenName(entry.name);
63}
64
65export default Task;
66