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