• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "accessibility_account_data.h"
17 #include "accessibility_display_manager.h"
18 #include "accessible_ability_manager_service.h"
19 #include "extension_ability_info.h"
20 #include "hilog_wrapper.h"
21 
22 namespace OHOS {
23 namespace Accessibility {
24 const static std::string PREF_PATH =
25     "/data/service/el1/public/barrierfree/accessibility_ability_manager_service/";
26 
AccessibilityAccountData(int accountId)27 AccessibilityAccountData::AccessibilityAccountData(int accountId)
28 {
29     id_ = accountId;
30 }
31 
~AccessibilityAccountData()32 AccessibilityAccountData::~AccessibilityAccountData()
33 {}
34 
GetAccountId()35 int AccessibilityAccountData::GetAccountId()
36 {
37     HILOG_DEBUG("start.");
38     return id_;
39 }
40 
41 // get client state.
GetAccessibilityState()42 uint32_t AccessibilityAccountData::GetAccessibilityState()
43 {
44     HILOG_DEBUG("start.");
45     uint32_t state = 0;
46     if (!connectedA11yAbilities_.empty() || !connectingA11yAbilities_.empty()) {
47         state |= AccessibilitySystemAbilityClient::STATE_ACCESSIBILITY_ENABLED;
48         if (!isEnabled_) {
49             SetEnabled(true);
50         }
51     } else {
52         if (isEnabled_) {
53             SetEnabled(false);
54         }
55     }
56 
57     if (isEventTouchGuideState_) {
58         state |= AccessibilitySystemAbilityClient::STATE_EXPLORATION_ENABLED;
59     }
60 
61     if (isCaptionState_) {
62         state |= AccessibilitySystemAbilityClient::STATE_CAPTION_ENABLED;
63     }
64 
65     if (isFilteringKeyEvents_) {
66         state |= AccessibilitySystemAbilityClient::STATE_KEYEVENT_ENABLED;
67     }
68 
69     if (isGesturesSimulation_) {
70         state |= AccessibilitySystemAbilityClient::STATE_GESTURE_ENABLED;
71     }
72 
73     return state;
74 }
75 
76 // switch the user causes state changed.
OnAccountSwitched()77 void AccessibilityAccountData::OnAccountSwitched()
78 {
79     HILOG_DEBUG("start.");
80     // reset AccessibleAbilityConnection
81     for (auto itr = connectedA11yAbilities_.begin(); itr != connectedA11yAbilities_.end(); itr++) {
82         itr->second->Disconnect();
83     }
84 
85     // Clear all abilities.
86     connectedA11yAbilities_.clear();
87     connectingA11yAbilities_.clear();
88     enabledAbilities_.clear();
89     installedAbilities_.clear();
90 
91     // Clear Setting State.
92     isEventTouchGuideState_ = false;
93     isScreenMagnification_ = false;
94     isFilteringKeyEvents_ = false;
95     isGesturesSimulation_ = false;
96 }
97 
98 // add connect ability.
AddConnectedAbility(sptr<AccessibleAbilityConnection> & connection)99 void AccessibilityAccountData::AddConnectedAbility(sptr<AccessibleAbilityConnection>& connection)
100 {
101     HILOG_DEBUG("URI is %{public}s", connection->GetElementName().GetURI().c_str());
102     if (!connectedA11yAbilities_.count(connection->GetElementName().GetURI())) {
103         connectedA11yAbilities_.insert(make_pair(connection->GetElementName().GetURI(), connection));
104     }
105 
106     HILOG_DEBUG("Add ConnectedAbility: %{public}d", connectedA11yAbilities_.size());
107 }
108 
109 // remove connect ability.
RemoveConnectedAbility(sptr<AccessibleAbilityConnection> & connection)110 void AccessibilityAccountData::RemoveConnectedAbility(sptr<AccessibleAbilityConnection>& connection)
111 {
112     HILOG_DEBUG("URI is %{public}s", connection->GetElementName().GetURI().c_str());
113     std::map<std::string, sptr<AccessibleAbilityConnection>>::iterator it =
114         connectedA11yAbilities_.find(connection->GetElementName().GetURI());
115     if (it != connectedA11yAbilities_.end()) {
116         connectedA11yAbilities_.erase(it);
117     }
118 
119     HILOG_DEBUG("Remove ConnectedAbility: %{public}d", connectedA11yAbilities_.size());
120 }
121 
AddStateCallback(const sptr<IAccessibleAbilityManagerServiceState> & callback)122 void AccessibilityAccountData::AddStateCallback(const sptr<IAccessibleAbilityManagerServiceState>& callback)
123 {
124     HILOG_DEBUG("start.");
125     stateCallbacks_.push_back(callback);
126 }
127 
128 // remove IAccessibleAbilityManagerServiceState
RemoveStateCallback(const wptr<IRemoteObject> & callback)129 void AccessibilityAccountData::RemoveStateCallback(const wptr<IRemoteObject>& callback)
130 {
131     HILOG_DEBUG("start.");
132     for (auto itr = stateCallbacks_.begin(); itr != stateCallbacks_.end(); itr++) {
133         if ((*itr)->AsObject() == callback) {
134             stateCallbacks_.erase(itr);
135             break;
136         }
137     }
138 }
139 
AddCaptionPropertyCallback(const sptr<IAccessibleAbilityManagerServiceCaptionProperty> & callback)140 void AccessibilityAccountData::AddCaptionPropertyCallback(
141     const sptr<IAccessibleAbilityManagerServiceCaptionProperty>& callback)
142 {
143     HILOG_DEBUG("start.");
144     captionPropertyCallbacks_.push_back(callback);
145 }
146 
RemoveCaptionPropertyCallback(const wptr<IRemoteObject> & callback)147 void AccessibilityAccountData::RemoveCaptionPropertyCallback(const wptr<IRemoteObject>& callback)
148 {
149     HILOG_DEBUG("start.");
150     for (auto itr = captionPropertyCallbacks_.begin(); itr != captionPropertyCallbacks_.end(); itr++) {
151         if ((*itr)->AsObject() == callback) {
152             captionPropertyCallbacks_.erase(itr);
153             break;
154         }
155     }
156 }
157 // add AccessibilityInteractionConnection
AddAccessibilityWindowConnection(const int windowId,const sptr<AccessibilityWindowConnection> & interactionConnection)158 void AccessibilityAccountData::AddAccessibilityWindowConnection(
159     const int windowId, const sptr<AccessibilityWindowConnection>& interactionConnection)
160 {
161     HILOG_DEBUG("windowId(%{public}d)", windowId);
162     if (!asacConnections_.count(windowId)) {
163         asacConnections_.insert(std::make_pair(windowId, interactionConnection));
164     }
165 }
166 
167 // remove AccessibilityWindowConnection
RemoveAccessibilityWindowConnection(const int windowId)168 void AccessibilityAccountData::RemoveAccessibilityWindowConnection(const int windowId)
169 {
170     HILOG_DEBUG("windowId(%{public}d)", windowId);
171     std::map<int, sptr<AccessibilityWindowConnection>>::iterator it = asacConnections_.find(windowId);
172     if (it != asacConnections_.end()) {
173         asacConnections_.erase(it);
174     }
175 }
176 
AddConnectingA11yAbility(const AppExecFwk::ElementName & elementName)177 void AccessibilityAccountData::AddConnectingA11yAbility(const AppExecFwk::ElementName& elementName)
178 {
179     HILOG_DEBUG("start.");
180     if (!connectingA11yAbilities_.count(elementName.GetURI())) {
181         connectingA11yAbilities_.insert(make_pair(elementName.GetURI(), elementName));
182     }
183 
184     HILOG_DEBUG("Add ConnectingA11yAbility: %{public}d", connectingA11yAbilities_.size());
185 }
186 
RemoveConnectingA11yAbility(const AppExecFwk::ElementName & elementName)187 void AccessibilityAccountData::RemoveConnectingA11yAbility(const AppExecFwk::ElementName& elementName)
188 {
189     HILOG_DEBUG("start.");
190     std::map<std::string, AppExecFwk::ElementName>::iterator it = connectingA11yAbilities_.find(elementName.GetURI());
191     if (it != connectingA11yAbilities_.end()) {
192         connectingA11yAbilities_.erase(it);
193     }
194 
195     HILOG_DEBUG("Remove ConnectingA11yAbility: %{public}d", connectingA11yAbilities_.size());
196 }
197 
198 // For UT
AddEnabledAbility(const AppExecFwk::ElementName & elementName)199 void AccessibilityAccountData::AddEnabledAbility(const AppExecFwk::ElementName& elementName)
200 {
201     HILOG_DEBUG("start.");
202     if (!enabledAbilities_.count(elementName.GetURI())) {
203         enabledAbilities_.insert(make_pair(elementName.GetURI(), elementName));
204     }
205 
206     HILOG_DEBUG("Add EnabledAbility: %{public}d", enabledAbilities_.size());
207 }
208 
RemoveEnabledFromPref(const std::string bundleName)209 void AccessibilityAccountData::RemoveEnabledFromPref(const std::string bundleName)
210 {
211     if (!pref_) {
212         HILOG_ERROR("pref_ is null!");
213         return;
214     }
215     std::string strValue = pref_->GetString("BundleName", "");
216     HILOG_DEBUG("strValue = %{public}s", strValue.c_str());
217 
218     std::vector<std::string> vecvalue;
219     StringToVector(strValue, vecvalue);
220     for (std::vector<std::string>::iterator val = vecvalue.begin();val != vecvalue.end();) {
221         if (!std::strcmp(val->c_str(), bundleName.c_str())) {
222             val = vecvalue.erase(val);
223             HILOG_DEBUG("remove val = %{public}s", val->c_str());
224         } else {
225             ++val;
226         }
227     }
228     std::string stringOut = "";
229     VectorToString(vecvalue, stringOut);
230     int errCode = pref_->PutString("BundleName", stringOut);
231     if (errCode) {
232         HILOG_ERROR("pref_->PutString() error(%{public}d).", errCode);
233     }
234 
235     errCode = pref_->FlushSync();
236     if (errCode) {
237         HILOG_ERROR("pref_->FlushSync() error(%{public}d).", errCode);
238     }
239 }
240 
RemoveEnabledAbility(const AppExecFwk::ElementName & elementName)241 void AccessibilityAccountData::RemoveEnabledAbility(const AppExecFwk::ElementName& elementName)
242 {
243     std::map<std::string, AppExecFwk::ElementName>::iterator it = enabledAbilities_.find(elementName.GetURI());
244     HILOG_DEBUG("EnabledAbility size(%{public}d)", enabledAbilities_.size());
245     if (it != enabledAbilities_.end()) {
246         HILOG_DEBUG("Removed %{public}s from EnabledAbility: ", elementName.GetBundleName().c_str());
247         enabledAbilities_.erase(it);
248     }
249     RemoveEnabledFromPref(elementName.GetBundleName());
250 }
251 
252 // For UT
AddInstalledAbility(AccessibilityAbilityInfo & abilityInfo)253 void AccessibilityAccountData::AddInstalledAbility(AccessibilityAbilityInfo& abilityInfo)
254 {
255     HILOG_DEBUG("abilityInfo's bundle name is %{public}s", abilityInfo.GetPackageName().c_str());
256     for (size_t i = 0; i < installedAbilities_.size(); i++) {
257         if (installedAbilities_[i].GetPackageName() == abilityInfo.GetPackageName()) {
258             HILOG_DEBUG("the ability is already exist.");
259             return;
260         }
261     }
262     installedAbilities_.push_back(abilityInfo);
263     HILOG_DEBUG("push back installed ability successfully and installedAbilities_'s size is %{public}d",
264         installedAbilities_.size());
265 }
266 
RemoveInstalledAbility(std::string bundleName)267 void AccessibilityAccountData::RemoveInstalledAbility(std::string bundleName)
268 {
269     HILOG_DEBUG("start.");
270     for (auto it = installedAbilities_.begin(); it != installedAbilities_.end();) {
271         if (it->GetPackageName() == bundleName) {
272             HILOG_DEBUG("Removed %{public}s from InstalledAbility: ", bundleName.c_str());
273             installedAbilities_.erase(it);
274         } else {
275             ++it;
276         }
277     }
278 }
279 
ClearInstalledAbility()280 void AccessibilityAccountData::ClearInstalledAbility()
281 {
282     HILOG_DEBUG("start.");
283     installedAbilities_.clear();
284 }
285 
286 // get AccessibleAbilityConnection
GetAccessibleAbilityConnection(const std::string elementName)287 const sptr<AccessibleAbilityConnection> AccessibilityAccountData::GetAccessibleAbilityConnection(
288     const std::string elementName)
289 {
290     HILOG_DEBUG("URI is %{public}s", elementName.c_str());
291     for (auto& connected : connectedA11yAbilities_) {
292         std::string::size_type  idx = connected.first.find(elementName);
293         if (idx == std::string::npos) {
294             continue;
295         } else {
296             HILOG_DEBUG("founded URI = %{public}s ", connected.first.c_str());
297             return connected.second;
298         }
299     }
300     HILOG_DEBUG("URI %{public}s not found ", elementName.c_str());
301     return nullptr;
302 }
303 
304 // get AccessibilityWindowConnection.
GetAccessibilityWindowConnection(const int windowId)305 const sptr<AccessibilityWindowConnection> AccessibilityAccountData::GetAccessibilityWindowConnection(
306     const int windowId)
307 {
308     HILOG_DEBUG("windowId(%{public}d).", windowId);
309     if (asacConnections_.count(windowId) > 0) {
310         return asacConnections_[windowId];
311     }
312 
313     return nullptr;
314 }
315 
316 // get map<std::string, sptr<AccessibleAbilityConnection>> connectedA11yAbilities_
GetConnectedA11yAbilities()317 const std::map<std::string, sptr<AccessibleAbilityConnection>> AccessibilityAccountData::GetConnectedA11yAbilities()
318 {
319     HILOG_DEBUG("start.");
320     return connectedA11yAbilities_;
321 }
322 
GetStateCallbacks()323 const std::vector<sptr<IAccessibleAbilityManagerServiceState>> AccessibilityAccountData::GetStateCallbacks()
324 {
325     HILOG_DEBUG("start.");
326     return stateCallbacks_;
327 }
328 
329 // get map<int, sptr<AccessibilityWindowConnection>> asacConnections_
GetAsacConnections()330 const std::map<int, sptr<AccessibilityWindowConnection>> AccessibilityAccountData::GetAsacConnections()
331 {
332     HILOG_DEBUG("start.");
333     return asacConnections_;
334 }
335 
GetCaptionPropertyCallbacks()336 const CaptionPropertyCallbacks AccessibilityAccountData::GetCaptionPropertyCallbacks()
337 {
338     HILOG_DEBUG("start.");
339     return captionPropertyCallbacks_;
340 }
341 
342 // get connectingA11yAbilities_.
GetConnectingA11yAbilities()343 const std::map<std::string, AppExecFwk::ElementName> AccessibilityAccountData::GetConnectingA11yAbilities()
344 {
345     HILOG_DEBUG("start.");
346     return connectingA11yAbilities_;
347 }
348 
349 // get enabledAbilities_.
GetEnabledAbilities()350 const std::map<std::string, AppExecFwk::ElementName> AccessibilityAccountData::GetEnabledAbilities()
351 {
352     HILOG_DEBUG("enabledAbilities_ size is (%{public}d).", enabledAbilities_.size());
353     for (auto& abilitie : enabledAbilities_) {
354         std::string bundleName = abilitie.second.GetBundleName();
355         HILOG_DEBUG("bundleName = %{public}s ", bundleName.c_str());
356     }
357     return enabledAbilities_;
358 }
359 
360 // get installedAbilities_.
GetInstalledAbilities()361 const std::vector<AccessibilityAbilityInfo> AccessibilityAccountData::GetInstalledAbilities()
362 {
363     HILOG_DEBUG("start.");
364     return installedAbilities_;
365 }
366 
GetAbilitiesByState(AbilityStateType state)367 const std::vector<AccessibilityAbilityInfo> AccessibilityAccountData::GetAbilitiesByState(AbilityStateType state)
368 {
369     HILOG_DEBUG("state(%{public}d) start.", state);
370     if (state == ABILITY_STATE_ENABLE) {
371         std::vector<AccessibilityAbilityInfo> enabledAbilities;
372         for (auto ability : connectedA11yAbilities_) {
373             enabledAbilities.push_back(ability.second->GetAbilityInfo());
374         }
375         HILOG_DEBUG("connectedA11yAbilities_ is (%{public}d) enabledAbilities is (%{public}d).",
376             connectedA11yAbilities_.size(), enabledAbilities.size());
377         return enabledAbilities;
378     } else if (state == ABILITY_STATE_DISABLE) {
379         std::vector<AccessibilityAbilityInfo> disabledAbilities = installedAbilities_;
380         for (auto enabledAbility : connectedA11yAbilities_) {
381             for (auto itr = disabledAbilities.begin(); itr != disabledAbilities.end();) {
382                 if (itr->GetId() == enabledAbility.second->GetAbilityInfo().GetId()) {
383                     disabledAbilities.erase(itr);
384                 } else {
385                     itr++;
386                 }
387             }
388         }
389         return disabledAbilities;
390     } else {
391         return installedAbilities_;
392     }
393 }
394 
UpdateAccountCapabilities()395 void AccessibilityAccountData::UpdateAccountCapabilities()
396 {
397     HILOG_DEBUG("start.");
398     UpdateFilteringKeyEventsCapability();
399     UpdateEventTouchGuideCapability();
400     UpdateGesturesSimulationCapability();
401     UpdateMagnificationCapability();
402 
403     SetTouchGuideState(isEventTouchGuideState_);
404     SetGestureState(isGesturesSimulation_);
405     SetKeyEventObserverState(isFilteringKeyEvents_);
406 }
407 
UpdateEventTouchGuideCapability()408 void AccessibilityAccountData::UpdateEventTouchGuideCapability()
409 {
410     HILOG_DEBUG("start.");
411     for (auto itr = connectedA11yAbilities_.begin(); itr != connectedA11yAbilities_.end(); itr++) {
412         if (itr->second->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_TOUCH_GUIDE) {
413             isEventTouchGuideState_ = true;
414             return;
415         }
416     }
417     isEventTouchGuideState_ = false; // temp deal
418 }
419 
UpdateGesturesSimulationCapability()420 void AccessibilityAccountData::UpdateGesturesSimulationCapability()
421 {
422     HILOG_DEBUG("start.");
423     for (auto itr = connectedA11yAbilities_.begin(); itr != connectedA11yAbilities_.end(); itr++) {
424         if (itr->second->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_GESTURE) {
425             isGesturesSimulation_ = true;
426             return;
427         }
428     }
429     isGesturesSimulation_ = false;
430 }
431 
UpdateFilteringKeyEventsCapability()432 void AccessibilityAccountData::UpdateFilteringKeyEventsCapability()
433 {
434     HILOG_DEBUG("start.");
435     for (auto itr = connectedA11yAbilities_.begin(); itr != connectedA11yAbilities_.end(); itr++) {
436         if (itr->second->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_KEY_EVENT_OBSERVER) {
437             isFilteringKeyEvents_ = true;
438             return;
439         }
440     }
441     isFilteringKeyEvents_ = false;
442 }
443 
UpdateMagnificationCapability()444 void AccessibilityAccountData::UpdateMagnificationCapability()
445 {
446     HILOG_DEBUG("start.");
447     for (auto itr = connectedA11yAbilities_.begin(); itr != connectedA11yAbilities_.end(); itr++) {
448         if (itr->second->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_ZOOM) {
449             isScreenMagnification_ = true;
450             return;
451         }
452     }
453     isScreenMagnification_ = false;
454 }
455 
SetCaptionState(const bool state)456 bool AccessibilityAccountData::SetCaptionState(const bool state)
457 {
458     HILOG_DEBUG("start.");
459     isCaptionState_ = state;
460     SetStatePref(STATE::CAPTION);
461 
462     return true;
463 }
464 
SetCaptionPropertyPref()465 bool AccessibilityAccountData::SetCaptionPropertyPref()
466 {
467     if (!pref_) {
468         HILOG_ERROR("pref_ is null!");
469         return false;
470     }
471     std::string FONTFAMILY = captionProperty_.GetFontFamily();
472     int FONTSCALE = captionProperty_.GetFontScale();
473     uint32_t FONTCOLOR = captionProperty_.GetFontColor();
474     std::string FONTEDGETYPE = captionProperty_.GetFontEdgeType();
475     uint32_t BACKGROUNDCOLOR = captionProperty_.GetBackgroundColor();
476     uint32_t WINDOWCOLOR = captionProperty_.GetWindowColor();
477 
478     pref_->PutString("fontFamily", FONTFAMILY);
479     pref_->PutInt("fontColor", (int)FONTCOLOR);
480     pref_->PutString("fontEdgeType", FONTEDGETYPE);
481     pref_->PutInt("backgroundColor", (int)BACKGROUNDCOLOR);
482     pref_->PutInt("windowColor", (int)WINDOWCOLOR);
483     pref_->PutInt("fontScale", FONTSCALE);
484     pref_->FlushSync();
485     return true;
486 }
487 
SetCaptionProperty(const CaptionProperty & caption)488 bool AccessibilityAccountData::SetCaptionProperty(const CaptionProperty& caption)
489 {
490     HILOG_DEBUG("start.");
491     captionProperty_ = caption;
492     SetCaptionPropertyPref();
493     return true;
494 }
495 
StateChange(bool state)496 std::string AccessibilityAccountData::StateChange(bool state)
497 {
498     std::string STATE_TRUE = "on";
499     std::string STATE_FALSE = "off";
500     if (state) {
501         return STATE_TRUE;
502     } else {
503         return STATE_FALSE;
504     }
505 }
506 
SetStatePref(int type)507 bool AccessibilityAccountData::SetStatePref(int type)
508 {
509     if (!pref_) {
510         HILOG_ERROR("pref_ is null!");
511         return false;
512     }
513 
514     std::string strValue = "";
515     switch (type) {
516         case STATE::ACCESSIBILITY:
517             strValue = StateChange(isEnabled_);
518             pref_->PutString("accessible", strValue);
519             break;
520         case STATE::TOUCHGUIDE:
521             strValue = StateChange(isEventTouchGuideState_);
522             pref_->PutString("touchGuide", strValue);
523             break;
524         case STATE::GESTURE:
525             strValue = StateChange(isGesturesSimulation_);
526             pref_->PutString("gesture", strValue);
527             break;
528         case STATE::KEYEVENT:
529             strValue = StateChange(isFilteringKeyEvents_);
530             pref_->PutString("keyEventObserver", strValue);
531             break;
532         case STATE::CAPTION:
533             strValue = StateChange(isCaptionState_);
534             pref_->PutString("CaptionState", strValue);
535             break;
536         default:
537             break;
538     }
539 
540     pref_->FlushSync();
541     return true;
542 }
543 
SetEnabled(const bool state)544 bool AccessibilityAccountData::SetEnabled(const bool state)
545 {
546     HILOG_DEBUG("start.");
547     isEnabled_ = state;
548     SetStatePref(STATE::ACCESSIBILITY);
549     return true;
550 }
551 
SetTouchGuideState(const bool state)552 bool AccessibilityAccountData::SetTouchGuideState(const bool state)
553 {
554     HILOG_DEBUG("start.");
555     isEventTouchGuideState_ = state;
556     SetStatePref(STATE::TOUCHGUIDE);
557     return true;
558 }
559 
SetGestureState(const bool state)560 bool AccessibilityAccountData::SetGestureState(const bool state)
561 {
562     HILOG_DEBUG("start.");
563     isGesturesSimulation_ = state;
564     SetStatePref(STATE::GESTURE);
565     return true;
566 }
567 
SetKeyEventObserverState(const bool state)568 bool AccessibilityAccountData::SetKeyEventObserverState(const bool state)
569 {
570     HILOG_DEBUG("start.");
571     isFilteringKeyEvents_ = state;
572     SetStatePref(STATE::KEYEVENT);
573     return true;
574 }
575 
GetEnabledState()576 bool AccessibilityAccountData::GetEnabledState()
577 {
578     return isEnabled_;
579 }
580 
GetTouchGuideState()581 bool AccessibilityAccountData::GetTouchGuideState()
582 {
583     HILOG_DEBUG("start. isEventTouchGuideState_ = %{public}s", isEventTouchGuideState_ ? "true" : "false");
584     return isEventTouchGuideState_;
585 }
586 
GetGestureState()587 bool AccessibilityAccountData::GetGestureState()
588 {
589     return isGesturesSimulation_;
590 }
591 
GetKeyEventObserverState()592 bool AccessibilityAccountData::GetKeyEventObserverState()
593 {
594     return isFilteringKeyEvents_;
595 }
596 
GetCaptionState()597 bool AccessibilityAccountData::GetCaptionState()
598 {
599     return isCaptionState_;
600 }
601 
UpdateEnabledFromPref()602 void AccessibilityAccountData::UpdateEnabledFromPref()
603 {
604     if (!pref_) {
605         HILOG_ERROR("pref_ is null!");
606         return;
607     }
608     std::string bundleName = "";
609     std::vector<std::string> vecvalue;
610     for (auto& abilitie : enabledAbilities_) {
611         bundleName = abilitie.second.GetBundleName();
612         vecvalue.push_back(bundleName);
613         HILOG_DEBUG("bundleName = %{public}s ", bundleName.c_str());
614     }
615     std::string stringOut = "";
616     VectorToString(vecvalue, stringOut);
617     pref_->PutString("BundleName", stringOut);
618     pref_->FlushSync();
619 }
620 
SetEnabledObj(std::map<std::string,AppExecFwk::ElementName> it)621 bool AccessibilityAccountData::SetEnabledObj(std::map<std::string, AppExecFwk::ElementName> it)
622 {
623     HILOG_DEBUG("start.");
624 
625     // add ability to the last of enabledAbilities_
626     for (auto& ability : it) {
627         enabledAbilities_.insert(std::pair<std::string, AppExecFwk::ElementName>(ability.first, ability.second));
628     }
629     UpdateEnabledFromPref();
630     return true;
631 }
632 
ReadConfigurationForAccountData()633 bool AccessibilityAccountData::ReadConfigurationForAccountData()
634 {
635     HILOG_DEBUG("start.");
636     // temp deal: read the user confige data.
637     return true;
638 }
639 
640 // get installedAbilities_.
GetInstalledAbilitiesFromBMS()641 bool AccessibilityAccountData::GetInstalledAbilitiesFromBMS()
642 {
643     HILOG_DEBUG("start.");
644 
645     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
646     auto aams = DelayedSingleton<AccessibleAbilityManagerService>::GetInstance();
647     sptr<AppExecFwk::IBundleMgr> bms = nullptr;
648     bms = aams->GetBundleMgrProxy();
649     if (!bms) {
650         HILOG_ERROR("GetBundleMgrProxy failed.");
651         return false;
652     }
653     bms->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::ACCESSIBILITY, id_, extensionInfos);
654     HILOG_DEBUG("query extensionAbilityInfos' size is %{public}d.", extensionInfos.size());
655     for (auto& info : extensionInfos) {
656         std::shared_ptr<AccessibilityAbilityInfo> accessibilityInfo =
657             std::make_shared<AccessibilityAbilityInfo>(info);
658         AddInstalledAbility(*accessibilityInfo);
659     }
660     return true;
661 }
662 
CaptionInit(const std::shared_ptr<NativePreferences::Preferences> & pref)663 void AccessibilityAccountData::CaptionInit(const std::shared_ptr<NativePreferences::Preferences> &pref)
664 {
665     HILOG_DEBUG("start.");
666     if (!pref) {
667         HILOG_ERROR("Input Parameter is nullptr");
668         return;
669     }
670 
671     std::string strValue = pref->GetString("CaptionState", "");
672     HILOG_DEBUG(" pref->GetString() = %{public}s.", strValue.c_str());
673     if (!std::strcmp(strValue.c_str(), "on")) {
674         isCaptionState_ = true;
675     } else {
676         isCaptionState_ = false;
677     }
678 
679     std::string FONTFAMILY = pref->GetString("fontFamily", "default");
680     HILOG_DEBUG(" pref->GetString() = %{public}s.", FONTFAMILY.c_str());
681 
682     int FONTSCALE = pref->GetInt("fontScale", 0);
683     HILOG_DEBUG(" pref->GetString() = %{public}d.", FONTSCALE);
684 
685     uint32_t FONTCOLOR = (uint32_t)pref->GetInt("fontColor", 0xff000000);
686     HILOG_DEBUG(" pref->GetString() = 0x%{public}x.", FONTCOLOR);
687 
688     std::string FONTEDGETYPE = pref->GetString("fontEdgeType", "none");
689     HILOG_DEBUG(" pref->GetString() = 0x%{public}s.", FONTEDGETYPE.c_str());
690 
691     uint32_t BACKGROUNDCOLOR = (uint32_t)pref->GetInt("backgroundColor", 0xff000000);
692     HILOG_DEBUG(" pref->GetString() = 0x%{public}x.", BACKGROUNDCOLOR);
693 
694     uint32_t WINDOWCOLOR = (uint32_t)pref->GetInt("windowColor", 0xff000000);
695     HILOG_DEBUG(" pref->GetString() = 0x%{public}x.", WINDOWCOLOR);
696 
697     captionProperty_.SetFontFamily(FONTFAMILY);
698     captionProperty_.SetFontScale(FONTSCALE);
699     captionProperty_.SetFontColor(FONTCOLOR);
700     captionProperty_.SetFontEdgeType(FONTEDGETYPE);
701     captionProperty_.SetBackgroundColor(BACKGROUNDCOLOR);
702     captionProperty_.SetWindowColor(WINDOWCOLOR);
703 }
704 
CapbilityInit(const std::shared_ptr<NativePreferences::Preferences> & pref)705 void AccessibilityAccountData::CapbilityInit(const std::shared_ptr<NativePreferences::Preferences> &pref)
706 {
707     HILOG_DEBUG("start.");
708     if (!pref) {
709         HILOG_ERROR("Input Parameter is nullptr");
710         return;
711     }
712 
713     std::string strValue = pref->GetString("accessible", "");
714     HILOG_DEBUG("strValue = %{public}s", strValue.c_str());
715     if (!std::strcmp(strValue.c_str(), "on")) {
716         isEnabled_ = true;
717     } else {
718         isEnabled_ = false;
719     }
720 
721     strValue = pref->GetString("touchGuide", "");
722     HILOG_DEBUG("strValue = %{public}s", strValue.c_str());
723     if (!std::strcmp(strValue.c_str(), "on")) {
724         HILOG_DEBUG("isEventTouchGuideState_ = true");
725         isEventTouchGuideState_ = true;
726     } else {
727         HILOG_DEBUG("isEventTouchGuideState_ = false");
728         isEventTouchGuideState_ = false;
729     }
730 
731     strValue = pref->GetString("gesture", "");
732     HILOG_DEBUG("strValue = %{public}s", strValue.c_str());
733     if (!std::strcmp(strValue.c_str(), "on")) {
734         isFilteringKeyEvents_ = true;
735     } else {
736         isFilteringKeyEvents_ = false;
737     }
738 
739     strValue = pref->GetString("keyEventObserver", "");
740     HILOG_DEBUG("strValue = %{public}s", strValue.c_str());
741     if (!std::strcmp(strValue.c_str(), "on")) {
742         isGesturesSimulation_ = true;
743     } else {
744         isGesturesSimulation_ = false;
745     }
746 }
747 
EnabledListInit(const std::shared_ptr<NativePreferences::Preferences> & pref)748 void AccessibilityAccountData::EnabledListInit(const std::shared_ptr<NativePreferences::Preferences> &pref)
749 {
750     HILOG_DEBUG("start.");
751     if (!pref) {
752         HILOG_ERROR("Input Parameter is nullptr");
753         return;
754     }
755 
756     std::string BundleName = "";
757     AccessibilityAbilityInfo abilityInfo;
758 
759     std::string strValue = pref->GetString("BundleName", "");
760     HILOG_DEBUG("strValue = %{public}s", strValue.c_str());
761 
762     std::vector<std::string> vecvalue;
763     StringToVector(strValue, vecvalue);
764     for (auto i = vecvalue.begin(); i != vecvalue.end(); i++) {
765         BundleName = *i;
766         HILOG_DEBUG("BundleName = %{public}s", BundleName.c_str());
767         AppExecFwk::ElementName elementName("", BundleName, "");
768         enabledAbilities_.insert(std::pair<std::string, AppExecFwk::ElementName>(elementName.GetURI(), elementName));
769     }
770 }
771 
StringToVector(std::string & stringIn,std::vector<std::string> & vectorResult)772 void AccessibilityAccountData::StringToVector(std::string &stringIn, std::vector<std::string> &vectorResult)
773 {
774     HILOG_DEBUG("start.");
775     int strLength = (int)stringIn.size();
776     std::vector<int> position;
777     int wrodCount = 0;
778 
779     if (strLength == 0) {
780         return;
781     }
782 
783     for (int i = 0; i < strLength; i++) {
784         if (stringIn[i] == ',') {
785             position.push_back(i);
786         }
787     }
788 
789     wrodCount = (int)position.size();
790     if ((wrodCount == 0) && (strLength > 0)) {
791         vectorResult.push_back(stringIn);
792     } else {
793         int startWrod = 0;
794         int length = 0;
795         for (int i = 0; i <= wrodCount; i++) {
796             if (i == 0) {
797                 startWrod = 0;
798                 length = position[i];
799                 vectorResult.push_back(stringIn.substr(startWrod, length)); // first string
800             } else if (i < wrodCount) {
801                 startWrod = position[i - 1] + 1;
802                 length = position[i] - position[i - 1] - 1;
803                 vectorResult.push_back(stringIn.substr(startWrod, length)); // second string to last-1 string
804             } else {
805                 startWrod = position[i - 1] + 1;
806                 length = strLength - position[i - 1] - 1;
807                 vectorResult.push_back(stringIn.substr(startWrod, length)); // last string
808             }
809         }
810     }
811     HILOG_DEBUG("strLength = %{public}d, wrodCount = %{public}d, stringIn : %{public}s",
812         strLength, wrodCount, stringIn.c_str());
813     for (auto& var : vectorResult) {
814         HILOG_DEBUG("vectorResult = %{public}s ", var.c_str());
815     }
816     HILOG_DEBUG("end.");
817 }
818 
init()819 void AccessibilityAccountData::init()
820 {
821     int errCode = 0;
822     pref_ = NativePreferences::PreferencesHelper::GetPreferences(PREF_PATH + "100.xml", errCode);
823     if (errCode) {
824         HILOG_ERROR("GetPreferences failed! errCode(%{public}d).", errCode);
825         return;
826     }
827 
828     CaptionInit(pref_);
829     CapbilityInit(pref_);
830     EnabledListInit(pref_);
831 }
832 
DisableAbilities(std::map<std::string,AppExecFwk::ElementName> it)833 bool AccessibilityAccountData::DisableAbilities(std::map<std::string, AppExecFwk::ElementName> it)
834 {
835     HILOG_DEBUG("start.");
836     for (auto &disableAbility : it) {
837         HILOG_DEBUG("DisableAbilities URI(%{public}s)", disableAbility.first.c_str());
838         enabledAbilities_.erase(disableAbility.first);
839         RemoveEnabledFromPref(disableAbility.second.GetBundleName());
840     }
841     return true;
842 }
843 
VectorToString(std::vector<std::string> & vectorVal,std::string & stringOut)844 void AccessibilityAccountData::VectorToString(std::vector<std::string> &vectorVal, std::string &stringOut)
845 {
846     HILOG_DEBUG("start.");
847     int i = 0;
848     for (auto& var : vectorVal) {
849         if (i > 0) {
850             stringOut = stringOut + ',';
851         }
852         stringOut = stringOut + var.c_str();
853         i++;
854     }
855     HILOG_DEBUG("end stringOUT = %{public}s .", stringOut.c_str());
856 }
857 }  // namespace Accessibility
858 }  // namespace OHOS
859