• 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 #ifndef ACCESSIBLE_ABILITY_MANAGER_SERVICE_H
17 #define ACCESSIBLE_ABILITY_MANAGER_SERVICE_H
18 
19 #include <future>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 
25 #include "accessibility_dumper.h"
26 #include "accessible_ability_manager_service_stub.h"
27 #include "accessible_ability_manager_service_event_handler.h"
28 #include "accessibility_account_data.h"
29 #include "accessibility_common_event.h"
30 #include "accessibility_display_manager.h"
31 #include "accessibility_input_interceptor.h"
32 #include "accessibility_keyevent_filter.h"
33 #include "accessibility_touchEvent_injector.h"
34 #include "accessibility_window_info.h"
35 #include "bundlemgr/bundle_mgr_interface.h"
36 #include "input_manager.h"
37 #include "singleton.h"
38 #include "system_ability.h"
39 #include "window_manager.h"
40 
41 namespace OHOS {
42 namespace Accessibility {
43 class AccessibilityAccountData;
44 class TouchEventInjector;
45 
46 enum CallBackID {
47     STATE_CALLBACK,
48     CAPTION_PROPERTY_CALLBACK,
49     ENABLE_ABILITY_LISTS_CALLBACK,
50     CONFIG_CALLBACK
51 };
52 
53 class AccessibleAbilityManagerService : public SystemAbility, public AccessibleAbilityManagerServiceStub {
54     DECLARE_SINGLETON(AccessibleAbilityManagerService)
55     DECLEAR_SYSTEM_ABILITY(AccessibleAbilityManagerService)
56 public:
57     /* For system ability */
58     void OnStart() override;
59     void OnStop() override;
60     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
61     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
62     int Dump(int fd, const std::vector<std::u16string>& args) override;
63 
64 public:
65     /* For AccessibleAbilityManagerServiceStub */
66     RetError SendEvent(const AccessibilityEventInfo &uiEvent) override;
67 
68     uint32_t RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> &callback) override;
69 
70     uint32_t RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver> &callback) override;
71 
72     void RegisterEnableAbilityListsObserver(
73         const sptr<IAccessibilityEnableAbilityListsObserver> &observer) override;
74 
75     RetError GetAbilityList(const uint32_t abilityTypes, const int32_t stateType,
76         std::vector<AccessibilityAbilityInfo> &infos) override;
77 
78     RetError RegisterElementOperator(const int32_t windowId,
79         const sptr<IAccessibilityElementOperator> &operation) override;
80 
81     RetError DeregisterElementOperator(const int32_t windowId) override;
82 
83     RetError GetCaptionProperty(AccessibilityConfig::CaptionProperty &caption) override;
84     RetError SetCaptionProperty(const AccessibilityConfig::CaptionProperty &caption) override;
85     RetError SetCaptionState(const bool state) override;
86 
87     bool GetEnabledState() override;
88     RetError GetCaptionState(bool &state) override;
89     bool GetTouchGuideState() override;
90     bool GetGestureState() override;
91     bool GetKeyEventObserverState() override;
92 
93     RetError EnableAbility(const std::string &name, const uint32_t capabilities) override;
94     RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override;
95     RetError DisableAbility(const std::string &name) override;
96     RetError EnableUITestAbility(const sptr<IRemoteObject>& obj) override;
97     RetError DisableUITestAbility() override;
98     int32_t GetActiveWindow() override;
99 
100 public:
101     /* For inner modules */
102     bool EnableShortKeyTargetAbility();
103     bool DisableShortKeyTargetAbility();
104 
105     void SetTouchEventInjector(const sptr<TouchEventInjector> &touchEventInjector);
106 
GetTouchEventInjector()107     inline sptr<TouchEventInjector> GetTouchEventInjector()
108     {
109         return touchEventInjector_;
110     }
111 
GetKeyEventFilter()112     inline sptr<KeyEventFilter> GetKeyEventFilter()
113     {
114         return keyEventFilter_;
115     }
116 
117     void SetKeyEventFilter(const sptr<KeyEventFilter> &keyEventFilter);
118 
119     /* For DisplayResize */
120     void NotifyDisplayResizeStateChanged(int32_t displayId, Rect& rect, float scale, float centerX, float centerY);
121 
IsServiceReady()122     inline bool IsServiceReady()
123     {
124         return isReady_;
125     }
126 
GetCurrentAccountId()127     inline int32_t GetCurrentAccountId()
128     {
129         return currentAccountId_;
130     }
131 
GetMainHandler()132     inline std::shared_ptr<AAMSEventHandler> &GetMainHandler()
133     {
134         return handler_;
135     }
136 
GetMainRunner()137     inline std::shared_ptr<AppExecFwk::EventRunner> &GetMainRunner()
138     {
139         return runner_;
140     }
141 
142     sptr<AccessibilityAccountData> GetAccountData(int32_t accountId) const;
143     sptr<AccessibilityAccountData> GetCurrentAccountData();
144     sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy();
145 
146     /* For common event */
147     void AddedUser(int32_t accountId);
148     void RemovedUser(int32_t accountId);
149     void SwitchedUser(int32_t accountId);
150     void PackageChanged(const std::string &bundleName);
151     void PackageRemoved(const std::string &bundleName);
152     void PackageAdd(const std::string &bundleName);
153 
154     void UpdateAccessibilityManagerService();
155 
156     RetError SetScreenMagnificationState(const bool state) override;
157     RetError SetShortKeyState(const bool state) override;
158     RetError SetMouseKeyState(const bool state) override;
159     RetError SetMouseAutoClick(const int32_t time) override;
160     RetError SetShortkeyTarget(const std::string &name) override;
161     RetError SetHighContrastTextState(const bool state) override;
162     RetError SetInvertColorState(const bool state) override;
163     RetError SetAnimationOffState(const bool state) override;
164     RetError SetAudioMonoState(const bool state) override;
165     RetError SetDaltonizationColorFilter(const uint32_t filter) override;
166     RetError SetContentTimeout(const uint32_t time) override;
167     RetError SetBrightnessDiscount(const float discount) override;
168     RetError SetAudioBalance(const float balance) override;
169 
170     RetError GetScreenMagnificationState(bool &state) override;
171     RetError GetShortKeyState(bool &state) override;
172     RetError GetMouseKeyState(bool &state) override;
173     RetError GetMouseAutoClick(int32_t &time) override;
174     RetError GetShortkeyTarget(std::string &name) override;
175     RetError GetHighContrastTextState(bool &state) override;
176     RetError GetInvertColorState(bool &state) override;
177     RetError GetAnimationOffState(bool &state) override;
178     RetError GetAudioMonoState(bool &state) override;
179     RetError GetDaltonizationColorFilter(uint32_t &type) override;
180     RetError GetContentTimeout(uint32_t &timer) override;
181     RetError GetBrightnessDiscount(float &brightness) override;
182     RetError GetAudioBalance(float &balance) override;
183     void GetAllConfigs(AccessibilityConfigData &configData) override;
184 
185     uint32_t RegisterConfigObserver(const sptr<IAccessibleAbilityManagerConfigObserver> &callback) override;
186     void UpdateConfigState();
187     void UpdateAudioBalance();
188     void UpdateBrightnessDiscount();
189     void UpdateContentTimeout();
190     void UpdateDaltonizationColorFilter();
191     void UpdateMouseAutoClick();
192     void UpdateShortkeyTarget();
193 
194 private:
195     class StateCallbackDeathRecipient final : public IRemoteObject::DeathRecipient {
196     public:
197         StateCallbackDeathRecipient() = default;
198         ~StateCallbackDeathRecipient() final = default;
199         DISALLOW_COPY_AND_MOVE(StateCallbackDeathRecipient);
200 
201         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
202     };
203 
204     class InteractionOperationDeathRecipient final : public IRemoteObject::DeathRecipient {
205     public:
InteractionOperationDeathRecipient(int32_t windowId)206         InteractionOperationDeathRecipient(int32_t windowId) : windowId_(windowId) {};
207         ~InteractionOperationDeathRecipient() final = default;
208         DISALLOW_COPY_AND_MOVE(InteractionOperationDeathRecipient);
209 
210         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
211         int32_t windowId_ = INVALID_WINDOW_ID;
212     };
213 
214     class CaptionPropertyCallbackDeathRecipient final : public IRemoteObject::DeathRecipient {
215     public:
216         CaptionPropertyCallbackDeathRecipient() = default;
217         ~CaptionPropertyCallbackDeathRecipient() final = default;
218         DISALLOW_COPY_AND_MOVE(CaptionPropertyCallbackDeathRecipient);
219 
220         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
221     };
222 
223     class EnableAbilityListsObserverDeathRecipient final : public IRemoteObject::DeathRecipient {
224     public:
225         EnableAbilityListsObserverDeathRecipient() = default;
226         ~EnableAbilityListsObserverDeathRecipient() final = default;
227         DISALLOW_COPY_AND_MOVE(EnableAbilityListsObserverDeathRecipient);
228 
229         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
230     };
231 
232     bool Init();
233 
234     class ConfigCallbackDeathRecipient final : public IRemoteObject::DeathRecipient {
235     public:
236         ConfigCallbackDeathRecipient() = default;
237         ~ConfigCallbackDeathRecipient() final = default;
238         DISALLOW_COPY_AND_MOVE(ConfigCallbackDeathRecipient);
239 
240         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
241     };
242 
243     class BundleManagerDeathRecipient final : public IRemoteObject::DeathRecipient {
244     public:
245         BundleManagerDeathRecipient() = default;
246         ~BundleManagerDeathRecipient() final = default;
247         DISALLOW_COPY_AND_MOVE(BundleManagerDeathRecipient);
248 
249         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
250     };
251 
252     RetError InnerEnableAbility(const std::string &name, const uint32_t capabilities);
253     RetError InnerDisableAbility(const std::string &name);
254 
255     sptr<AccessibilityWindowConnection> GetAccessibilityWindowConnection(int32_t windowId);
256     void ClearFocus(int32_t windowId);
257     void OutsideTouch(int32_t windowId);
258     void UpdateAccessibilityWindowStateByEvent(const AccessibilityEventInfo &event);
259 
260     void UpdateAccessibilityState();
261     void UpdateInputFilter();
262     void UpdateCaptionProperty();
263 
264     void RemoveCallback(CallBackID callback, const sptr<DeathRecipient> &recipient, const wptr<IRemoteObject> &remote);
265     void OnBundleManagerDied(const wptr<IRemoteObject> &remote);
266 
267     bool isReady_ = false;
268     bool isPublished_ = false;
269     std::map<int32_t, bool> dependentServicesStatus_;
270     int32_t currentAccountId_ = -1;
271     std::map<int32_t, sptr<AccessibilityAccountData>> a11yAccountsData_;
272     sptr<AppExecFwk::IBundleMgr> bundleManager_ = nullptr;
273 
274     sptr<AccessibilityInputInterceptor> inputInterceptor_ = nullptr;
275     sptr<TouchEventInjector> touchEventInjector_ = nullptr;
276     sptr<KeyEventFilter> keyEventFilter_ = nullptr;
277     sptr<AccessibilityDumper> accessibilityDumper_ = nullptr;
278 
279     std::shared_ptr<AppExecFwk::EventRunner> runner_;
280     std::shared_ptr<AAMSEventHandler> handler_;
281 
282     sptr<IRemoteObject::DeathRecipient> stateCallbackDeathRecipient_ = nullptr;
283     std::map<int32_t, sptr<IRemoteObject::DeathRecipient>> interactionOperationDeathRecipients_ {};
284     sptr<IRemoteObject::DeathRecipient> captionPropertyCallbackDeathRecipient_ = nullptr;
285     sptr<IRemoteObject::DeathRecipient> enableAbilityListsObserverDeathRecipient_ = nullptr;
286     sptr<IRemoteObject::DeathRecipient> configCallbackDeathRecipient_ = nullptr;
287     sptr<IRemoteObject::DeathRecipient> bundleManagerDeathRecipient_ = nullptr;
288     std::vector<sptr<IAccessibleAbilityManagerStateObserver>> stateCallbacks_;
289 };
290 } // namespace Accessibility
291 } // namespace OHOS
292 #endif // ACCESSIBLE_ABILITY_MANAGER_SERVICE_H