1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "stylus_key_handler.h"
17 
18 #include "ability_manager_client.h"
19 #include "define_multimodal.h"
20 #include "error_multimodal.h"
21 #include "mmi_log.h"
22 #include "setting_datashare.h"
23 #include "system_ability_definition.h"
24 
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "StylusKeyHandler"
27 
28 namespace OHOS {
29 namespace MMI {
30 const std::string SHORTHAND_ABILITY_NAME { "HiNotePcMainAbility" };
31 const std::string SHORTHAND_BUNDLE_NAME { "com.hmos.hinote" };
32 const std::string MEMORANDUM_ABILITY_NAME { "MainAbility" };
33 const std::string MEMORANDUM_BUNDLE_NAME { "com.hmos.hinote.notepad" };
34 const std::string IS_SCREEN_OFF { "is_sceen_off" };
35 const std::string SHORTHAND_SWITCH { "shorthand_switch_state" };
36 const std::string SHORTHAND_TARGET { "shorthand_target" };
37 
StylusKeyHandler()38 StylusKeyHandler::StylusKeyHandler() {}
~StylusKeyHandler()39 StylusKeyHandler::~StylusKeyHandler() {}
40 
HandleStylusKey(std::shared_ptr<KeyEvent> keyEvent)41 bool StylusKeyHandler::HandleStylusKey(std::shared_ptr<KeyEvent> keyEvent)
42 {
43     CHKPF(keyEvent);
44     if (!isShortHandConfig_) {
45         stylusKey_.statusConfig = SHORTHAND_SWITCH;
46         CreateStatusConfigObserver(stylusKey_);
47         shortHandTarget_.statusConfig = SHORTHAND_TARGET;
48         CreateStatusConfigObserver(shortHandTarget_);
49         isShortHandConfig_ = true;
50     }
51     if (keyEvent->GetKeyCode() != KeyEvent::KEYCODE_STYLUS_SCREEN) {
52         stylusKey_.lastEventIsStylus = false;
53         return false;
54     }
55     if (stylusKey_.isLaunchAbility) {
56         stylusKey_.isLaunchAbility = false;
57         return true;
58     }
59     stylusKey_.lastEventIsStylus = true;
60     return false;
61 }
62 
63 template <class T>
CreateStatusConfigObserver(T & item)64 void StylusKeyHandler::CreateStatusConfigObserver(T& item)
65 {
66     CALL_DEBUG_ENTER;
67     SettingObserver::UpdateFunc updateFunc = [&item](const std::string& key) {
68         bool statusValue = true;
69         auto ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
70             .GetBoolValue(key, statusValue);
71         if (ret != RET_OK) {
72             MMI_HILOGE("Get value from setting date fail");
73             return;
74         }
75         MMI_HILOGI("Config changed key:%{public}s, value:%{public}d", key.c_str(), statusValue);
76         item.statusConfigValue = statusValue;
77     };
78     sptr<SettingObserver> statusObserver = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
79         .CreateObserver(item.statusConfig, updateFunc);
80     ErrCode ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).RegisterObserver(statusObserver);
81     if (ret != ERR_OK) {
82         MMI_HILOGE("Register setting observer failed, ret:%{public}d", ret);
83         statusObserver = nullptr;
84     }
85     bool configVlaue = true;
86     ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
87         .GetBoolValue(item.statusConfig, configVlaue);
88     if (ret != RET_OK) {
89         MMI_HILOGE("Get value from setting date fail");
90         return;
91     }
92     MMI_HILOGI("Get value success key:%{public}s, value:%{public}d", item.statusConfig.c_str(), configVlaue);
93     item.statusConfigValue = configVlaue;
94 }
95 
IsLaunchAbility()96 void StylusKeyHandler::IsLaunchAbility()
97 {
98     if (stylusKey_.statusConfigValue && stylusKey_.lastEventIsStylus) {
99         if (shortHandTarget_.statusConfigValue) {
100             stylusKey_.ability.abilityName = SHORTHAND_ABILITY_NAME;
101             stylusKey_.ability.bundleName = SHORTHAND_BUNDLE_NAME;
102             stylusKey_.ability.params.emplace(IS_SCREEN_OFF, "true");
103         } else {
104             stylusKey_.ability.abilityName = MEMORANDUM_ABILITY_NAME;
105             stylusKey_.ability.bundleName = MEMORANDUM_BUNDLE_NAME;
106         }
107         LaunchAbility(stylusKey_.ability);
108         stylusKey_.lastEventIsStylus = false;
109         stylusKey_.isLaunchAbility = true;
110     }
111 }
112 
LaunchAbility(const Ability & ability)113 void StylusKeyHandler::LaunchAbility(const Ability &ability)
114 {
115     AAFwk::Want want;
116     want.SetElementName(ability.deviceId, ability.bundleName, ability.abilityName);
117     want.SetAction(ability.action);
118     want.SetUri(ability.uri);
119     want.SetType(ability.type);
120     for (const auto &entity : ability.entities) {
121         want.AddEntity(entity);
122     }
123     for (const auto &item : ability.params) {
124         want.SetParam(item.first, item.second);
125     }
126 
127     ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
128     if (err != ERR_OK) {
129         MMI_HILOGE("LaunchAbility failed, bundleName:%{public}s, err:%{public}d", ability.bundleName.c_str(), err);
130     }
131     MMI_HILOGD("End launch ability, bundleName:%{public}s", ability.bundleName.c_str());
132 }
133 
SetLastEventState(bool state)134 void StylusKeyHandler::SetLastEventState(bool state)
135 {
136     stylusKey_.lastEventIsStylus = state;
137 }
138 
139 } // namespace AppExecFwk
140 } // namespace OHOS
141