1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2024 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from typing import List
20
21from src.beans.base_bean import BaseBean
22from src.beans.event_node import EventNode
23from src.beans.frame_node import FrameNode
24from src.keywords import event_procedure_keyword, keywords_dict, get_sample_key, get_dict_value, \
25    get_sample_separator_count
26from src.utils.log_wrapper import log_info
27from src.utils.value_parser import get_value_as_int, pack_string_until_next_keyword
28
29
30class EventScope(BaseBean):
31    finger = 0
32    event_nodes = []
33
34    def __init__(self, input_str):
35        super().__init__()
36        self.event_nodes = []
37        texts = input_str.split('\n')
38        if texts is None or len(texts) == 0:
39            self.parse_failed()
40            return
41        finger_str = texts[0]
42        self.finger = get_value_as_int(finger_str, get_sample_key(event_procedure_keyword, 'finger'),
43                                       get_sample_separator_count(event_procedure_keyword, 'finger'), True)
44        self.parse_event_nodes(texts, 1)
45        self.check_parse_result()
46
47    #   finger:0
48    #    frameNodeId: 84, type: TouchEventActuator, depth: 0, id: 0xf072b240, parentId: 0x0
49    #     stateHistory:
50    #      procedure: HandleTouchDown, timestamp: 2017-08-25 15:00:22.247
51    #      procedure: HandleTouchUp, timestamp: 2017-08-25 15:00:22.295
52    #    frameNodeId: 84, type: ExclusiveRecognizer, depth: 0, id: 0xf063eed0, parentId: 0x0
53    #     stateHistory:
54    #      procedure: HandleTouchDown, state: READY, disposal: NONE, timestamp: 2017-08-25 15:00:22.247
55    #      procedure: HandleTouchUp, state: FAIL, disposal: REJECT, timestamp: 2017-08-25 15:00:22.295
56    #     frameNodeId: 88, type: LongPressRecognizer, depth: 1, id: 0xef748aa0, parentId: 0xf063eed0, customInfo:
57    #     duration: 500, isForDrag: 0, repeat: 0, fingers: 1
58    #      stateHistory:
59    #       procedure: HandleTouchDown, state: DETECTING, disposal: NONE, timestamp: 2017-08-25 15:00:22.247
60    #       procedure: HandleTouchUp, state: FAIL, disposal: REJECT, timestamp: 2017-08-25 15:00:22.295
61    #     frameNodeId: 84, type: PanRecognizer, depth: 1, id: 0xf3d6bfc0, parentId: 0xf063eed0, customInfo:
62    #     direction: 3, isForDrag: 0, distance: 7.5, fingers: 1
63    #      stateHistory:
64    #       procedure: HandleTouchDown, state: DETECTING, disposal: NONE, timestamp: 2017-08-25 15:00:22.247
65    #       procedure: HandleTouchUp, state: FAIL, disposal: REJECT, timestamp: 2017-08-25 15:00:22.295
66    def parse_event_nodes(self, spliced_lines, start_index):
67        start_keyword = get_sample_key(event_procedure_keyword, 'frameNodeId')
68        end_keywords = [start_keyword, get_dict_value(keywords_dict, 'event tree')]
69        current_index = 0
70        while current_index < len(spliced_lines):
71            if current_index < start_index:
72                current_index += 1
73                continue
74            current_line = spliced_lines[current_index]
75            if current_line.find('frameNodeId') != -1:
76                packed_result = pack_string_until_next_keyword(spliced_lines, current_index, start_keyword,
77                                                               end_keywords)
78                packed_str = packed_result[0]
79                if packed_str is None or len(packed_str) == 0:
80                    current_index += 1
81                    continue
82                current_index = packed_result[1]
83                event_node = EventNode(packed_str)
84                if event_node is not None:
85                    self.event_nodes.append(event_node)
86            else:
87                current_index += 1
88
89    def check_parse_result(self):
90        if self.finger is None or self.event_nodes is None or len(self.event_nodes) == 0:
91            self.parse_failed()
92        else:
93            self.parse_succeed()
94
95    def to_string(self):
96        result_str = '  finger: ' + str(self.finger)
97        for event_node in self.event_nodes:
98            result_str += '\n' + event_node.to_string()
99        return result_str
100
101    def dump(self):
102        log_info(self.to_string())
103
104    def update_event_nodes_with_frame_nodes(self, frame_nodes: List[FrameNode]):
105        for event_node in self.event_nodes:
106            event_node.update_tag_from_frame_nodes_info(frame_nodes)
107