1/*
2 * Copyright (C) 2022 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 {Timestamp} from 'common/time';
18import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
19import {AbstractPresenterInputMethod} from 'viewers/common/abstract_presenter_input_method';
20import {ImeAdditionalProperties} from 'viewers/common/ime_additional_properties';
21import {ImeUtils} from 'viewers/common/ime_utils';
22
23export class PresenterInputMethodManagerService extends AbstractPresenterInputMethod {
24  protected getHierarchyTableProperties() {
25    const inputMethodManagerService = this.hierarchyPresenter
26      .getCurrentHierarchyTreesForTrace(this.imeTrace)
27      ?.at(0)
28      ?.getChildByName('inputMethodManagerService');
29
30    const curMethodId = inputMethodManagerService
31      ?.getEagerPropertyByName('curMethodId')
32      ?.formattedValue();
33    const curFocusedWindowName = inputMethodManagerService
34      ?.getEagerPropertyByName('curFocusedWindowName')
35      ?.formattedValue();
36    const lastImeTargetWindowName = inputMethodManagerService
37      ?.getEagerPropertyByName('lastImeTargetWindowName')
38      ?.formattedValue();
39    const inputShown =
40      inputMethodManagerService
41        ?.getEagerPropertyByName('inputShown')
42        ?.getValue() ?? false;
43
44    return {
45      ...new ImManagerServiceTableProperties(
46        curMethodId,
47        curFocusedWindowName,
48        lastImeTargetWindowName,
49        inputShown,
50      ),
51    };
52  }
53
54  protected override async getAdditionalProperties(
55    wmEntry: HierarchyTreeNode | undefined,
56    sfEntry: HierarchyTreeNode | undefined,
57    wmEntryTimestamp: Timestamp | undefined,
58    sfEntryTimestamp: Timestamp | undefined,
59  ): Promise<ImeAdditionalProperties> {
60    return new ImeAdditionalProperties(
61      wmEntry
62        ? ImeUtils.processWindowManagerTraceEntry(wmEntry, wmEntryTimestamp)
63        : undefined,
64      undefined,
65    );
66  }
67}
68
69class ImManagerServiceTableProperties {
70  constructor(
71    public inputMethodId: string | undefined,
72    public curFocusedWindow: string | undefined,
73    public lastImeTargetWindow: string | undefined,
74    public inputShown: boolean,
75  ) {}
76}
77