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 UI_APPEARANCE_ABILITY_H
17 #define UI_APPEARANCE_ABILITY_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "appmgr/app_mgr_proxy.h"
23 #include "common_event_manager.h"
24 #include "common_event_support.h"
25 #include "system_ability.h"
26 #include "ui_appearance_ability_stub.h"
27 
28 namespace OHOS {
29 namespace ArkUi::UiAppearance {
30 class UserSwitchEventSubscriber : public EventFwk::CommonEventSubscriber {
31 public:
32     explicit UserSwitchEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo,
33         const std::function<void(const int32_t)>& userSwitchCallback);
34     ~UserSwitchEventSubscriber() override = default;
35     void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
36 
37 private:
38     std::function<void(const int32_t)> userSwitchCallback_;
39 };
40 
41 class UiAppearanceAbility : public SystemAbility, public UiAppearanceAbilityStub {
42     DECLARE_SYSTEM_ABILITY(UiAppearanceAbility);
43 
44 public:
45     struct UiAppearanceParam {
46         DarkMode darkMode = DarkMode::ALWAYS_LIGHT;
47         std::string fontScale = "1";
48         std::string fontWeightScale = "1";
49     };
50     UiAppearanceAbility(int32_t saId, bool runOnCreate);
51     ~UiAppearanceAbility() = default;
52 
53     int32_t SetDarkMode(DarkMode mode) override;
54     int32_t GetDarkMode() override;
55     int32_t GetFontScale(std::string& fontScale) override;
56     int32_t SetFontScale(std::string& fontScale) override;
57     int32_t GetFontWeightScale(std::string& fontWeightScale) override;
58     int32_t SetFontWeightScale(std::string& fontWeightScale) override;
59 
60 protected:
61     void OnStart() override;
62     void OnStop() override;
63 
64     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
65     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
66 
67 private:
68     sptr<AppExecFwk::IAppMgr> GetAppManagerInstance();
69     bool VerifyAccessToken(const std::string& permissionName);
70     void Init();
71     void SubscribeUserSwitchEvent();
72     bool IsUserExist(const int32_t userId);
73     bool UpdateConfiguration(const AppExecFwk::Configuration& configuration, const int32_t userId);
74     void DoCompatibleProcess();
75     int32_t GetCallingUserId();
76     std::vector<int32_t> GetUserIds();
77     void UserSwitchFunc(const int32_t userId);
78     void DoInitProcess();
79 
80     bool GetParameterWrap(const std::string& paramName, std::string& value, const std::string& defaultValue);
81     bool GetParameterWrap(const std::string& paramName, std::string& value);
82     bool SetParameterWrap(const std::string& paramName, const std::string& value);
83 
84     void UpdateCurrentUserConfiguration(const int32_t userId);
85     int32_t OnSetDarkMode(const int32_t userId, DarkMode mode);
86     UiAppearanceAbility::DarkMode InitGetDarkMode(const int32_t userId);
87     int32_t OnSetFontScale(const int32_t userId, std::string& fontScale);
88     int32_t OnSetFontWeightScale(const int32_t userId, std::string& fontWeightScale);
89     std::string DarkNodeConfigurationAssignUser(const int32_t userId);
90     std::string FontScaleConfigurationAssignUser(const int32_t userId);
91     std::string FontWeightScaleConfigurationAssignUser(const int32_t userId);
92     std::string DarkModeParamAssignUser(const int32_t userId);
93     std::string FontScaleParamAssignUser(const int32_t userId);
94     std::string FontWeightScaleParamAssignUser(const int32_t userId);
95 
96     std::shared_ptr<UserSwitchEventSubscriber> userSwitchSubscriber_;
97     std::recursive_mutex usersParamMutex_;
98     std::map<int32_t, UiAppearanceParam> usersParam_;
99     std::atomic<bool> isNeedDoCompatibleProcess_ = false;
100     std::atomic<bool> isInitializationFinished_ = false;
101 };
102 } // namespace ArkUi::UiAppearance
103 } // namespace OHOS
104 #endif // UI_APPEARANCE_ABILITY_H
105