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
80 private:
81 #ifdef SCREENLOCK_MANAGER_ENABLED
82 void InitializeScreenLockStatus();
83 #endif // SCREENLOCK_MANAGER_ENABLED
84 void SubscribeCommonEvent();
85 void UnsubscribeCommonEvent();
86 void SetupMainAccount();
87 void OnCommonEvent(const EventFwk::CommonEventData &data);
88 void OnAddUser(const EventFwk::CommonEventData &data);
89 void OnRemoveUser(const EventFwk::CommonEventData &data);
90 void OnSwitchUser(const EventFwk::CommonEventData &data);
91
92 static std::shared_ptr<AccountManager> instance_;
93 static std::mutex mutex_;
94 std::mutex lock_;
95 int32_t timerId_ { -1 };
96 int32_t currentAccountId_ { -1 };
97 std::shared_ptr<CommonEventSubscriber> subscriber_;
98 std::map<int32_t, std::unique_ptr<AccountSetting>> accounts_;
99 std::map<std::string, std::function<void(const EventFwk::CommonEventData &)>> handlers_;
100 };
101
GetAccountId()102 inline int32_t AccountManager::AccountSetting::GetAccountId() const
103 {
104 return accountId_;
105 }
106
GetAccShortcutEnabled()107 inline bool AccountManager::AccountSetting::GetAccShortcutEnabled() const
108 {
109 return accShortcutEnabled_;
110 }
111
GetAccShortcutEnabledOnScreenLocked()112 inline bool AccountManager::AccountSetting::GetAccShortcutEnabledOnScreenLocked() const
113 {
114 return accShortcutEnabledOnScreenLocked_;
115 }
116
GetAccShortcutTimeout()117 inline int32_t AccountManager::AccountSetting::GetAccShortcutTimeout() const
118 {
119 return accShortcutTimeout_;
120 }
121
122 #define ACCOUNT_MGR ::OHOS::MMI::AccountManager::GetInstance()
123 } // namespace MMI
124 } // namespace OHOS
125 #endif // ACCOUNT_MANAGER_H
126