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 "bundle_name_parser.h"
20 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
21 #include "dfx_hisysevent.h"
22 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
23 #include "setting_datashare.h"
24 #include "system_ability_definition.h"
25
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "StylusKeyHandler"
28
29 namespace OHOS {
30 namespace MMI {
31 const char* SHORTHAND_ABILITY_NAME { "HiNotePcMainAbility" };
32 const char* MEMORANDUM_ABILITY_NAME { "QuickNoteAbility" };
33 const char* IS_SCREEN_OFF { "is_sceen_off" };
34 const char* SHORTHAND_SWITCH { "shorthand_switch_state" };
35 const char* SHORTHAND_TARGET { "shorthand_target" };
36
StylusKeyHandler()37 StylusKeyHandler::StylusKeyHandler() {}
~StylusKeyHandler()38 StylusKeyHandler::~StylusKeyHandler() {}
39
HandleStylusKey(std::shared_ptr<KeyEvent> keyEvent)40 bool StylusKeyHandler::HandleStylusKey(std::shared_ptr<KeyEvent> keyEvent)
41 {
42 CHKPF(keyEvent);
43 if (!isShortHandConfig_) {
44 stylusKey_.statusConfig = SHORTHAND_SWITCH;
45 CreateStatusConfigObserver(stylusKey_);
46 shortHandTarget_.statusConfig = SHORTHAND_TARGET;
47 CreateStatusConfigObserver(shortHandTarget_);
48 isShortHandConfig_ = true;
49 }
50 if (keyEvent->GetKeyCode() != KeyEvent::KEYCODE_STYLUS_SCREEN) {
51 stylusKey_.lastEventIsStylus = false;
52 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
53 DfxHisysevent::ReportFailLaunchAbility("com.hmos.hinote",
54 DfxHisysevent::KEY_ERROR_CODE::INVALID_PARAMETER);
55 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
56 return false;
57 }
58 if (stylusKey_.isLaunchAbility) {
59 stylusKey_.isLaunchAbility = false;
60 return true;
61 }
62 stylusKey_.lastEventIsStylus = true;
63 return false;
64 }
65
66 template <class T>
CreateStatusConfigObserver(T & item)67 void StylusKeyHandler::CreateStatusConfigObserver(T& item)
68 {
69 CALL_DEBUG_ENTER;
70 SettingObserver::UpdateFunc updateFunc = [&item](const std::string& key) {
71 bool statusValue = true;
72 auto ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
73 .GetBoolValue(key, statusValue);
74 if (ret != RET_OK) {
75 MMI_HILOGE("Get value from setting date fail");
76 return;
77 }
78 MMI_HILOGI("Config changed key:%{public}s, value:%{public}d", key.c_str(), statusValue);
79 item.statusConfigValue = statusValue;
80 };
81 sptr<SettingObserver> statusObserver = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
82 .CreateObserver(item.statusConfig, updateFunc);
83 ErrCode ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).RegisterObserver(statusObserver);
84 if (ret != ERR_OK) {
85 MMI_HILOGE("Register setting observer failed, ret:%{public}d", ret);
86 statusObserver = nullptr;
87 }
88 bool configValue = true;
89 ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
90 .GetBoolValue(item.statusConfig, configValue);
91 if (ret != RET_OK) {
92 MMI_HILOGE("Get value from setting date fail");
93 return;
94 }
95 MMI_HILOGI("Get value success key:%{public}s, value:%{public}d", item.statusConfig.c_str(), configValue);
96 item.statusConfigValue = configValue;
97 }
98
IsLaunchAbility()99 void StylusKeyHandler::IsLaunchAbility()
100 {
101 if (stylusKey_.statusConfigValue && stylusKey_.lastEventIsStylus) {
102 if (shortHandTarget_.statusConfigValue) {
103 stylusKey_.ability.abilityName = SHORTHAND_ABILITY_NAME;
104 stylusKey_.ability.bundleName = BUNDLE_NAME_PARSER.GetBundleName("SHORTHAND_BUNDLE_NAME");
105 stylusKey_.ability.params.emplace(IS_SCREEN_OFF, "true");
106 } else {
107 stylusKey_.ability.abilityName = MEMORANDUM_ABILITY_NAME;
108 stylusKey_.ability.bundleName = BUNDLE_NAME_PARSER.GetBundleName("MEMORANDUM_BUNDLE_NAME");
109 }
110 LaunchAbility(stylusKey_.ability);
111 stylusKey_.lastEventIsStylus = false;
112 stylusKey_.isLaunchAbility = true;
113 }
114 }
115
LaunchAbility(const Ability & ability)116 void StylusKeyHandler::LaunchAbility(const Ability &ability)
117 {
118 AAFwk::Want want;
119 want.SetElementName(ability.deviceId, ability.bundleName, ability.abilityName);
120 want.SetAction(ability.action);
121 want.SetUri(ability.uri);
122 want.SetType(ability.type);
123 for (const auto &entity : ability.entities) {
124 want.AddEntity(entity);
125 }
126 for (const auto &item : ability.params) {
127 want.SetParam(item.first, item.second);
128 }
129
130 auto begin = std::chrono::high_resolution_clock::now();
131 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
132 auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
133 std::chrono::high_resolution_clock::now() - begin).count();
134 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
135 DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::ABILITY_MGR_CLIENT_START_ABILITY, durationMS);
136 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
137 if (err != ERR_OK) {
138 MMI_HILOGE("LaunchAbility failed, bundleName:%{public}s, err:%{public}d", ability.bundleName.c_str(), err);
139 }
140 MMI_HILOGD("End launch ability, bundleName:%{public}s", ability.bundleName.c_str());
141 }
142
SetLastEventState(bool state)143 void StylusKeyHandler::SetLastEventState(bool state)
144 {
145 stylusKey_.lastEventIsStylus = state;
146 }
147
148 } // namespace AppExecFwk
149 } // namespace OHOS
150