1 /*
2 * Copyright (c) 2024 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 ACCOUNT_MANAGER_H
17 #define ACCOUNT_MANAGER_H
18
19 #include <common_event_subscriber.h>
20
21 #include "setting_observer.h"
22
23 namespace OHOS {
24 namespace MMI {
25 class AccountManager final {
26 class CommonEventSubscriber : public EventFwk::CommonEventSubscriber {
27 public:
CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)28 CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
29 : EventFwk::CommonEventSubscriber(subscribeInfo) {}
30 ~CommonEventSubscriber() = default;
31
32 void OnReceiveEvent(const EventFwk::CommonEventData &data);
33 };
34
35 public:
36 class AccountSetting final {
37 public:
38 AccountSetting(int32_t accountId);
39 ~AccountSetting();
40 DISALLOW_MOVE(AccountSetting);
41 AccountSetting(const AccountSetting &other);
42 AccountSetting& operator=(const AccountSetting &other);
43
44 int32_t GetAccountId() const;
45 bool GetAccShortcutEnabled() const;
46 bool GetAccShortcutEnabledOnScreenLocked() const;
47 int32_t GetAccShortcutTimeout() const;
48
49 private:
50 static void AccShortcutTimeout(int32_t accountId, const std::string &key);
51 static void AccShortcutEnabled(int32_t accountId, const std::string &key);
52 static void AccShortcutEnabledOnScreenLocked(int32_t accountId, const std::string &key);
53 sptr<SettingObserver> RegisterSettingObserver(const std::string &key, SettingObserver::UpdateFunc onUpdate);
54 void InitializeSetting();
55 void OnAccShortcutTimeoutChanged(const std::string &key);
56 void OnAccShortcutEnabled(const std::string &key);
57 void OnAccShortcutEnabledOnScreenLocked(const std::string &key);
58 bool ReadSwitchStatus(const std::string &key, bool currentSwitchStatus);
59 void ReadLongPressTime();
60
61 int32_t accountId_ { -1 };
62 int32_t timerId_ { -1 };
63 int32_t accShortcutTimeout_ { 3000 }; // 3s
64 bool accShortcutEnabled_ {};
65 bool accShortcutEnabledOnScreenLocked_ {};
66 sptr<SettingObserver> switchObserver_;
67 sptr<SettingObserver> onScreenLockedSwitchObserver_;
68 sptr<SettingObserver> configObserver_;
69 };
70
71 static std::shared_ptr<AccountManager> GetInstance();
72
73 AccountManager();
74 ~AccountManager();
75 DISALLOW_COPY_AND_MOVE(AccountManager);
76
77 void Initialize();
78 AccountSetting GetCurrentAccountSetting();
79 void AccountManagerUnregister();
80
81 private:
82 #ifdef SCREENLOCK_MANAGER_ENABLED
83 void InitializeScreenLockStatus();
84 #endif // SCREENLOCK_MANAGER_ENABLED
85 void SubscribeCommonEvent();
86 void UnsubscribeCommonEvent();
87 void SetupMainAccount();
88 void OnCommonEvent(const EventFwk::CommonEventData &data);
89 void OnAddUser(const EventFwk::CommonEventData &data);
90 void OnRemoveUser(const EventFwk::CommonEventData &data);
91 void OnSwitchUser(const EventFwk::CommonEventData &data);
92
93 static std::shared_ptr<AccountManager> instance_;
94 static std::mutex mutex_;
95 std::mutex lock_;
96 int32_t timerId_ { -1 };
97 int32_t currentAccountId_ { -1 };
98 std::shared_ptr<CommonEventSubscriber> subscriber_;
99 std::map<int32_t, std::unique_ptr<AccountSetting>> accounts_;
100 std::map<std::string, std::function<void(const EventFwk::CommonEventData &)>> handlers_;
101 };
102
GetAccountId()103 inline int32_t AccountManager::AccountSetting::GetAccountId() const
104 {
105 return accountId_;
106 }
107
GetAccShortcutEnabled()108 inline bool AccountManager::AccountSetting::GetAccShortcutEnabled() const
109 {
110 return accShortcutEnabled_;
111 }
112
GetAccShortcutEnabledOnScreenLocked()113 inline bool AccountManager::AccountSetting::GetAccShortcutEnabledOnScreenLocked() const
114 {
115 return accShortcutEnabledOnScreenLocked_;
116 }
117
GetAccShortcutTimeout()118 inline int32_t AccountManager::AccountSetting::GetAccShortcutTimeout() const
119 {
120 return accShortcutTimeout_;
121 }
122
123 #define ACCOUNT_MGR ::OHOS::MMI::AccountManager::GetInstance()
124 } // namespace MMI
125 } // namespace OHOS
126 #endif // ACCOUNT_MANAGER_H
127