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