1 /*
2 * Copyright (c) 2025 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 "smart_gesture_manager.h"
17
18 #include "setting_data_manager.h"
19 #include "ui_appearance_log.h"
20
21 namespace OHOS::ArkUi::UiAppearance {
22 namespace {
23 const std::string SETTING_SMART_GESTURE_SWITCH = "persist.gesture.smart_gesture_enable";
24 }
25
GetInstance()26 SmartGestureManager &SmartGestureManager::GetInstance()
27 {
28 static SmartGestureManager instance;
29 return instance;
30 }
31
Initialize(const std::function<void (bool,int32_t)> & updateCallback)32 ErrCode SmartGestureManager::Initialize(const std::function<void(bool, int32_t)>& updateCallback)
33 {
34 LoadSettingDataObserversCallback();
35 updateCallback_ = updateCallback;
36 return ERR_OK;
37 }
38
LoadSettingDataObserversCallback()39 void SmartGestureManager::LoadSettingDataObserversCallback()
40 {
41 observer_ = std::make_pair(SETTING_SMART_GESTURE_SWITCH, [&](const std::string& key, int32_t userId) {
42 UpdateSmartGestureValue(key, userId);
43 });
44 }
45
RegisterSettingDataObserver() const46 ErrCode SmartGestureManager::RegisterSettingDataObserver() const
47 {
48 SettingDataManager& manager = SettingDataManager::GetInstance();
49 if (manager.RegisterObserver(observer_.first, observer_.second, INVALID_USER_ID) != ERR_OK) {
50 LOGE("setting data observer is not initialized");
51 return ERR_NO_INIT;
52 }
53 LOGD("setting data observer is initialized");
54 return ERR_OK;
55 }
56
UpdateSmartGestureValue(const std::string & key,const int32_t userId)57 void SmartGestureManager::UpdateSmartGestureValue(const std::string& key, const int32_t userId)
58 {
59 SettingDataManager& manager = SettingDataManager::GetInstance();
60 int32_t value = SMART_GESTURE_INVALID;
61 ErrCode code = manager.GetInt32ValueStrictly(key, value, userId);
62 if (code != ERR_OK) {
63 LOGE("get smart gesture value failed, key: %{public}s, userId: %{public}d, code: %{public}d",
64 key.c_str(), userId, code);
65 return;
66 }
67
68 if (value < SMART_GESTURE_DISABLE || value > SMART_GESTURE_AUTO) {
69 LOGE("smart gesture value is invalid, key: %{public}s, userId: %{public}d, value: %{public}d",
70 key.c_str(), userId, value);
71 return;
72 }
73
74 auto mode = static_cast<SmartGestureMode>(value);
75 LOGD("smart gesture mode change, key: %{public}s, userId: %{public}d, value: %{public}d",
76 key.c_str(), userId, value);
77 OnChangeSmartGestureMode(mode, userId);
78 }
79
UpdateSmartGestureInitialValue()80 void SmartGestureManager::UpdateSmartGestureInitialValue()
81 {
82 UpdateSmartGestureValue(SETTING_SMART_GESTURE_SWITCH, INVALID_USER_ID);
83 }
84
OnChangeSmartGestureMode(const SmartGestureMode mode,const int32_t userId)85 void SmartGestureManager::OnChangeSmartGestureMode(const SmartGestureMode mode, const int32_t userId)
86 {
87 if (!updateCallback_) {
88 LOGE("no update callback, mode: %{public}d, userId: %{public}d", mode, userId);
89 return;
90 }
91 updateCallback_(mode == SMART_GESTURE_AUTO, userId);
92 }
93 } // namespace OHOS::ArkUi::UiAppearance
94