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