1e75ebbc8Sopenharmony_ci/*
2e75ebbc8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License.
5e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at
6e75ebbc8Sopenharmony_ci *
7e75ebbc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e75ebbc8Sopenharmony_ci *
9e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and
13e75ebbc8Sopenharmony_ci * limitations under the License.
14e75ebbc8Sopenharmony_ci */
15e75ebbc8Sopenharmony_ciimport inputConsumer from "@ohos.multimodalInput.inputConsumer";
16e75ebbc8Sopenharmony_ciimport Log from "./Log";
17e75ebbc8Sopenharmony_ciimport createOrGet from "./SingleInstanceHelper";
18e75ebbc8Sopenharmony_ci
19e75ebbc8Sopenharmony_ciexport type MultiCallback = (keyOptions: inputConsumer.KeyOptions) => void;
20e75ebbc8Sopenharmony_ciexport enum MultiKeyCode {
21e75ebbc8Sopenharmony_ci  WIN = 2076,
22e75ebbc8Sopenharmony_ci  N = 2030,
23e75ebbc8Sopenharmony_ci  I = 2025,
24e75ebbc8Sopenharmony_ci}
25e75ebbc8Sopenharmony_ci
26e75ebbc8Sopenharmony_ciconst TAG = "MultimodalInputManager";
27e75ebbc8Sopenharmony_ci
28e75ebbc8Sopenharmony_ciclass MultimodalInputManager {
29e75ebbc8Sopenharmony_ci  //win + N
30e75ebbc8Sopenharmony_ci  notificationKeyOptions: any = {
31e75ebbc8Sopenharmony_ci    preKeys: [2076],
32e75ebbc8Sopenharmony_ci    finalKey: 2030,
33e75ebbc8Sopenharmony_ci    isFinalKeyDown: true,
34e75ebbc8Sopenharmony_ci    finalKeyDownDuration: 0,
35e75ebbc8Sopenharmony_ci  };
36e75ebbc8Sopenharmony_ci  //win + I
37e75ebbc8Sopenharmony_ci  controlKeyOptions: any = {
38e75ebbc8Sopenharmony_ci    preKeys: [2076],
39e75ebbc8Sopenharmony_ci    finalKey: 2025,
40e75ebbc8Sopenharmony_ci    isFinalKeyDown: true,
41e75ebbc8Sopenharmony_ci    finalKeyDownDuration: 0,
42e75ebbc8Sopenharmony_ci  };
43e75ebbc8Sopenharmony_ci
44e75ebbc8Sopenharmony_ci  subscribeCombinationKey(keys: MultiKeyCode[], cb: MultiCallback): () => void {
45e75ebbc8Sopenharmony_ci    if (keys.length <= 1) {
46e75ebbc8Sopenharmony_ci      Log.showError(TAG, `Invalid keys, can't subscribe.`);
47e75ebbc8Sopenharmony_ci      return () => {};
48e75ebbc8Sopenharmony_ci    }
49e75ebbc8Sopenharmony_ci    let keyOptions = {
50e75ebbc8Sopenharmony_ci      preKeys: keys.slice(0, keys.length - 1),
51e75ebbc8Sopenharmony_ci      finalKey: keys[keys.length - 1],
52e75ebbc8Sopenharmony_ci      isFinalKeyDown: true,
53e75ebbc8Sopenharmony_ci      finalKeyDownDuration: 0,
54e75ebbc8Sopenharmony_ci    };
55e75ebbc8Sopenharmony_ci    inputConsumer.on("key", keyOptions, (options) => {
56e75ebbc8Sopenharmony_ci      Log.showInfo(TAG, `on CombinationKey, options:${JSON.stringify(options)}`);
57e75ebbc8Sopenharmony_ci      cb(options);
58e75ebbc8Sopenharmony_ci    });
59e75ebbc8Sopenharmony_ci    Log.showInfo(TAG, `subscribe CombinationKey, keys:${JSON.stringify(keys)}`);
60e75ebbc8Sopenharmony_ci    return () => {
61e75ebbc8Sopenharmony_ci      inputConsumer.off("key", keyOptions, (data) => {});
62e75ebbc8Sopenharmony_ci    };
63e75ebbc8Sopenharmony_ci  }
64e75ebbc8Sopenharmony_ci
65e75ebbc8Sopenharmony_ci  registerControlListener(callback) {
66e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `registerListener control`);
67e75ebbc8Sopenharmony_ci    inputConsumer.on("key", this.controlKeyOptions, (data) => {
68e75ebbc8Sopenharmony_ci      Log.showInfo(TAG, `controlRegisterCallBack data: ${JSON.stringify(data)}`);
69e75ebbc8Sopenharmony_ci      callback.onControlShowOrHide(data);
70e75ebbc8Sopenharmony_ci    });
71e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `registerListener end`);
72e75ebbc8Sopenharmony_ci  }
73e75ebbc8Sopenharmony_ci
74e75ebbc8Sopenharmony_ci  registerNotificationListener(callback) {
75e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `registerListener notification`);
76e75ebbc8Sopenharmony_ci    inputConsumer.on("key", this.notificationKeyOptions, ( data) => {
77e75ebbc8Sopenharmony_ci      Log.showInfo(TAG, `notificationRegisterCallBack data: ${JSON.stringify(data)}`);
78e75ebbc8Sopenharmony_ci      callback.onNotificationShowOrHide(data);
79e75ebbc8Sopenharmony_ci    });
80e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `registerListener end`);
81e75ebbc8Sopenharmony_ci  }
82e75ebbc8Sopenharmony_ci
83e75ebbc8Sopenharmony_ci  unregisterListener() {
84e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `unregisterListener start`);
85e75ebbc8Sopenharmony_ci    inputConsumer.off("key", this.notificationKeyOptions, (data) => {
86e75ebbc8Sopenharmony_ci      Log.showInfo(TAG, `notificationUnregisterCallBack data: ${JSON.stringify(data)}`);
87e75ebbc8Sopenharmony_ci    });
88e75ebbc8Sopenharmony_ci    inputConsumer.off("key", this.controlKeyOptions, (data) => {
89e75ebbc8Sopenharmony_ci      Log.showInfo(TAG, `controlUnregisterCallBack data: ${JSON.stringify(data)}`);
90e75ebbc8Sopenharmony_ci    });
91e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `unregisterListener end`);
92e75ebbc8Sopenharmony_ci  }
93e75ebbc8Sopenharmony_ci}
94e75ebbc8Sopenharmony_cilet sMultimodalInputManager = createOrGet(MultimodalInputManager, TAG);
95e75ebbc8Sopenharmony_ci
96e75ebbc8Sopenharmony_ciexport default sMultimodalInputManager as MultimodalInputManager;
97