• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "multimodal_input_preferences_manager.h"
17 
18 #include "mmi_log.h"
19 
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "MultiModalInputPreferencesManager" };
24 constexpr int32_t KEYBOARD_REPEATRATE = 50;
25 constexpr int32_t KEYBOARD_REPEATDELAY = 500;
26 constexpr int32_t MOUSE_SCROLL_ROWS = 3;
27 constexpr int32_t PRIMARY_BUTTON = 0;
28 constexpr int32_t POINTER_SPEED = 5;
29 constexpr int32_t TOUCHPAD_POINTER_SPEED = 9;
30 constexpr int32_t RIGHT_CLICK_TYPE = 1;
31 constexpr int32_t POINTER_COLOR = -1;
32 constexpr int32_t POINTER_SIZE = 1;
33 constexpr int32_t POINTER_STYLE = 0;
34 constexpr int32_t ERROR_DELAY_VALUE = -1000;
35 constexpr bool BOOL_DEFAULT = true;
36 const std::string path = "/data/service/el1/public/multimodalinput/";
37 const std::string shortKeyFileName = "Settings.xml";
38 const std::string mouseFileName = "mouse_settings.xml";
39 const std::string keyboarFileName = "keyboard_settings.xml";
40 const std::string touchpadFileName = "touchpad_settings.xml";
41 } // namespace
42 
MultiModalInputPreferencesManager()43 MultiModalInputPreferencesManager::MultiModalInputPreferencesManager() {}
44 
~MultiModalInputPreferencesManager()45 MultiModalInputPreferencesManager::~MultiModalInputPreferencesManager() {}
46 
InitPreferences()47 int32_t MultiModalInputPreferencesManager::InitPreferences()
48 {
49     CALL_DEBUG_ENTER;
50     int32_t ret = GetPreferencesSettings();
51     if (ret != RET_OK) {
52         MMI_HILOGE("Get multimodal input preferences settings failed");
53         return RET_ERR;
54     }
55     ret = InitPreferencesMap();
56     if (ret != RET_OK) {
57         MMI_HILOGE("Init multimodal input preferences map failed");
58         return RET_ERR;
59     }
60     return RET_OK;
61 }
62 
GetPreferencesSettings()63 int32_t MultiModalInputPreferencesManager::GetPreferencesSettings()
64 {
65     int32_t errCode = RET_OK;
66     std::shared_ptr<NativePreferences::Preferences> mousePref =
67         NativePreferences::PreferencesHelper::GetPreferences(path + mouseFileName, errCode);
68     CHKPR(mousePref, errno);
69     std::shared_ptr<NativePreferences::Preferences> keyboardPref =
70         NativePreferences::PreferencesHelper::GetPreferences(path + keyboarFileName, errCode);
71     CHKPR(keyboardPref, errno);
72     std::shared_ptr<NativePreferences::Preferences> touchpadPref =
73         NativePreferences::PreferencesHelper::GetPreferences(path + touchpadFileName, errCode);
74     CHKPR(touchpadPref, errno);
75     g_pointerSize = mousePref->GetInt(pointerSize, POINTER_SIZE);
76     g_pointerSpeed = mousePref->GetInt(pointerSpeed, POINTER_SPEED);
77     g_pointerColor = mousePref->GetInt(pointerColor, POINTER_COLOR);
78     g_pointerStyle = mousePref->GetInt(pointerStyle, POINTER_STYLE);
79     g_mouseScrollRows = mousePref->GetInt(mouseScrollRows, MOUSE_SCROLL_ROWS);
80     g_hoverScrollState = mousePref->GetBool(hoverScrollState, BOOL_DEFAULT);
81     g_mousePrimaryButton = mousePref->GetInt(mousePrimaryButton, PRIMARY_BUTTON);
82     g_touchpadTapSwitch = touchpadPref->GetBool(touchpadTapSwitch, BOOL_DEFAULT);
83     g_keyboardRepeatRate = keyboardPref->GetInt(keyboardRepeatRate, KEYBOARD_REPEATRATE);
84     g_keyboardRepeatDelay = keyboardPref->GetInt(keyboardRepeatDelay, KEYBOARD_REPEATDELAY);
85     g_touchpadPinchSwitch = touchpadPref->GetBool(touchpadPinchSwitch, BOOL_DEFAULT);
86     g_touchpadSwipeSwitch = touchpadPref->GetBool(touchpadSwipeSwitch, BOOL_DEFAULT);
87     g_touchpadPointerSpeed = touchpadPref->GetInt(touchpadPointerSpeed, TOUCHPAD_POINTER_SPEED);
88     g_touchpadScrollSwitch = touchpadPref->GetBool(touchpadScrollSwitch, BOOL_DEFAULT);
89     g_touchpadRightClickType = touchpadPref->GetInt(touchpadRightClickType, RIGHT_CLICK_TYPE);
90     g_touchpadScrollDirection = touchpadPref->GetBool(touchpadScrollDirection, BOOL_DEFAULT);
91     NativePreferences::PreferencesHelper::RemovePreferencesFromCache(path + mouseFileName);
92     NativePreferences::PreferencesHelper::RemovePreferencesFromCache(path + keyboarFileName);
93     NativePreferences::PreferencesHelper::RemovePreferencesFromCache(path + touchpadFileName);
94     return RET_OK;
95 }
96 
InitPreferencesMap()97 int32_t MultiModalInputPreferencesManager::InitPreferencesMap()
98 {
99     preferencesMap[pointerSize] = {mouseFileName, g_pointerSize};
100     preferencesMap[pointerSpeed] = {mouseFileName, g_pointerSpeed};
101     preferencesMap[pointerColor] = {mouseFileName, g_pointerColor};
102     preferencesMap[pointerStyle] = {mouseFileName, g_pointerStyle};
103     preferencesMap[mouseScrollRows] = {mouseFileName, g_mouseScrollRows};
104     preferencesMap[hoverScrollState] = {mouseFileName, static_cast<int32_t>(g_hoverScrollState)};
105     preferencesMap[mousePrimaryButton] = {mouseFileName, g_mousePrimaryButton};
106     preferencesMap[touchpadTapSwitch] = {touchpadFileName, static_cast<int32_t>(g_touchpadTapSwitch)};
107     preferencesMap[keyboardRepeatRate] = {keyboarFileName, g_keyboardRepeatRate};
108     preferencesMap[keyboardRepeatDelay] = {keyboarFileName, g_keyboardRepeatDelay};
109     preferencesMap[touchpadPinchSwitch] = {touchpadFileName, static_cast<int32_t>(g_touchpadPinchSwitch)};
110     preferencesMap[touchpadSwipeSwitch] = {touchpadFileName, static_cast<int32_t>(g_touchpadSwipeSwitch)};
111     preferencesMap[touchpadPointerSpeed] = {touchpadFileName, g_touchpadPointerSpeed};
112     preferencesMap[touchpadScrollSwitch] = {touchpadFileName, static_cast<int32_t>(g_touchpadScrollSwitch)};
113     preferencesMap[touchpadRightClickType] = {touchpadFileName, g_touchpadRightClickType};
114     preferencesMap[touchpadScrollDirection] = {touchpadFileName, static_cast<int32_t>(g_touchpadScrollDirection)};
115     return RET_OK;
116 }
117 
GetIntValue(const std::string & key,int32_t defaultValue)118 int32_t MultiModalInputPreferencesManager::GetIntValue(const std::string &key, int32_t defaultValue)
119 {
120     auto iter = preferencesMap.find(key);
121     if (iter == preferencesMap.end()) {
122         return defaultValue;
123     }
124     auto [fileName, value] = iter->second;
125     return value;
126 }
127 
GetBoolValue(const std::string & key,bool defaultValue)128 bool MultiModalInputPreferencesManager::GetBoolValue(const std::string &key, bool defaultValue)
129 {
130     auto iter = preferencesMap.find(key);
131     if (iter == preferencesMap.end()) {
132         return defaultValue;
133     }
134     auto [fileName, value] = iter->second;
135     return static_cast<bool>(value);
136 }
137 
SetIntValue(const std::string & key,const std::string & setFile,int32_t setValue)138 int32_t MultiModalInputPreferencesManager::SetIntValue(const std::string &key, const std::string &setFile,
139     int32_t setValue)
140 {
141     auto iter = preferencesMap.find(key);
142     std::string filePath = "";
143     if (iter == preferencesMap.end()) {
144         preferencesMap[key] = {setFile, setValue};
145         filePath = path + setFile;
146     } else {
147         auto [fileName, value] = iter->second;
148         if (value == setValue) {
149             MMI_HILOGD("The set value is same");
150             return RET_OK;
151         }
152         filePath = path + fileName;
153         preferencesMap[key].second = setValue;
154     }
155 
156     int32_t errCode = RET_OK;
157     std::shared_ptr<NativePreferences::Preferences> pref =
158         NativePreferences::PreferencesHelper::GetPreferences(filePath, errCode);
159     CHKPR(pref, errno);
160     int32_t ret = pref->PutInt(key, setValue);
161     if (ret != RET_OK) {
162         MMI_HILOGE("Put value is failed, ret:%{public}d", ret);
163         return RET_ERR;
164     }
165     ret = pref->FlushSync();
166     if (ret != RET_OK) {
167         MMI_HILOGE("Flush sync is failed, ret:%{public}d", ret);
168         return RET_ERR;
169     }
170     NativePreferences::PreferencesHelper::RemovePreferencesFromCache(filePath);
171     return RET_OK;
172 }
173 
SetBoolValue(const std::string & key,const std::string & setFile,bool setValue)174 int32_t MultiModalInputPreferencesManager::SetBoolValue(const std::string &key, const std::string &setFile,
175     bool setValue)
176 {
177     auto iter = preferencesMap.find(key);
178     std::string filePath = "";
179     if (iter == preferencesMap.end()) {
180         preferencesMap[key] = {setFile, static_cast<int32_t>(setValue)};
181         filePath = path + setFile;
182     } else {
183         auto [fileName, value] = iter->second;
184         if (static_cast<bool>(value) == setValue) {
185             MMI_HILOGD("The set value is same");
186             return RET_OK;
187         }
188         filePath = path + fileName;
189         preferencesMap[key].second = setValue;
190     }
191 
192     int32_t errCode = RET_OK;
193     std::shared_ptr<NativePreferences::Preferences> pref =
194         NativePreferences::PreferencesHelper::GetPreferences(filePath, errCode);
195     CHKPR(pref, errno);
196     int32_t ret = pref->PutBool(key, setValue);
197     if (ret != RET_OK) {
198         MMI_HILOGE("Put value is failed, ret:%{public}d", ret);
199         return RET_ERR;
200     }
201     ret = pref->FlushSync();
202     if (ret != RET_OK) {
203         MMI_HILOGE("Flush sync is failed, ret:%{public}d", ret);
204         return RET_ERR;
205     }
206     NativePreferences::PreferencesHelper::RemovePreferencesFromCache(filePath);
207     return RET_OK;
208 }
209 
GetShortKeyDuration(const std::string & key)210 int32_t MultiModalInputPreferencesManager::GetShortKeyDuration(const std::string &key)
211 {
212     int32_t errCode = RET_OK;
213     if (g_shortcutKeyMap.empty() || g_shortcutKeyMap.find(key) == g_shortcutKeyMap.end()) {
214         std::shared_ptr<NativePreferences::Preferences> pref =
215             NativePreferences::PreferencesHelper::GetPreferences(path + shortKeyFileName, errCode);
216         CHKPR(pref, errno);
217         int32_t duration = pref->GetInt(key, ERROR_DELAY_VALUE);
218         NativePreferences::PreferencesHelper::RemovePreferencesFromCache(path + shortKeyFileName);
219         g_shortcutKeyMap.emplace(key, duration);
220         return duration;
221     }
222     return g_shortcutKeyMap[key];
223 }
224 
SetShortKeyDuration(const std::string & key,int32_t setValue)225 int32_t MultiModalInputPreferencesManager::SetShortKeyDuration(const std::string &key, int32_t setValue)
226 {
227     auto iter = g_shortcutKeyMap.find(key);
228     if (iter != g_shortcutKeyMap.end() && iter->second == setValue) {
229         MMI_HILOGD("The set value is same");
230         return RET_OK;
231     }
232 
233     g_shortcutKeyMap[key] = setValue;
234     int32_t errCode = RET_OK;
235     std::shared_ptr<NativePreferences::Preferences> pref =
236         NativePreferences::PreferencesHelper::GetPreferences(path + shortKeyFileName, errCode);
237     CHKPR(pref, errno);
238     int32_t ret = pref->PutInt(key, setValue);
239     if (ret != RET_OK) {
240         MMI_HILOGE("Put value is failed, ret:%{public}d", ret);
241         return RET_ERR;
242     }
243     ret = pref->FlushSync();
244     if (ret != RET_OK) {
245         MMI_HILOGE("Flush sync is failed, ret:%{public}d", ret);
246         return RET_ERR;
247     }
248     NativePreferences::PreferencesHelper::RemovePreferencesFromCache(path + shortKeyFileName);
249     return RET_OK;
250 }
251 } // namespace MMI
252 } // namespace OHOS