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