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 OHOS_MEMORY_MEMMGR_MULTI_ACCOUNT_MANAGER_H
17 #define OHOS_MEMORY_MEMMGR_MULTI_ACCOUNT_MANAGER_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include "single_instance.h"
23 #include "account_priority_info.h"
24 #include "multi_account_strategy.h"
25 #include "account_bundle_info.h"
26 #include "event_handler.h"
27 #include "os_account_manager.h"
28 
29 namespace OHOS {
30 namespace Memory {
31 class MultiAccountManager {
32     DECLARE_SINGLE_INSTANCE_BASE(MultiAccountManager);
33 public:
34     void Init();
35     bool SetAccountPriority(int accountId, std::string accountName,
36                             AccountSA::OsAccountType accountType, bool isActived);
37     int RecalcBundlePriority(int accountId, int bundlePriority);
38     std::shared_ptr<AccountPriorityInfo> GetAccountPriorityInfo(int accountId);
39     void AddAccountPriorityInfo(std::shared_ptr<AccountPriorityInfo> accountPriorityInfo);
40     std::shared_ptr<MultiAccountStrategy> GetMultiAccountStratgy();
41     bool SetMultiAccountStrategy(std::shared_ptr<MultiAccountStrategy> strategy);
42     bool GetSwitchedAccountIds(std::vector<int> &accountIds);
43     bool UpdateAccountPriorityInfo(std::vector<int> &accountIds);
44     bool HandleOsAccountsChanged(int accountId, AccountSA::OS_ACCOUNT_SWITCH_MOD switchMod,
45                                  std::map<int, std::shared_ptr<AccountBundleInfo>> &osAccountsInfoMap_);
46     bool HandleAccountColdSwitch(std::vector<int> &switchedAccountIds,
47                                  std::map<int, std::shared_ptr<AccountBundleInfo>> &osAccountsInfoMap_);
48     bool HandleAccountHotSwitch(std::vector<int> &updatedAccountIds,
49                                 std::map<int, std::shared_ptr<AccountBundleInfo>> &osAccountsInfoMap_);
50 
51 private:
52     int retryTimes_ = 0;
53     std::shared_ptr<AppExecFwk::EventHandler> handler_;
54     bool initialized_ = false;
55     std::map<int, std::shared_ptr<AccountPriorityInfo>> accountMap_;
56     std::shared_ptr<MultiAccountStrategy> strategy_;
57     std::vector<int> oldActiveAccountIds_;
58     std::shared_ptr<AccountBundleInfo> GetAccountBundleInfo(int accountId,
59         std::map<int, std::shared_ptr<AccountBundleInfo>> &osAccountsInfoMap_);
60     void KillProcessesOfAccount(int accountId, std::map<int, std::shared_ptr<AccountBundleInfo>> &osAccountsInfoMap_);
61     MultiAccountManager();
62     ~MultiAccountManager();
63 };
64 } // namespace Memory
65 } // namespace OHOS
66 
67 #endif // OHOS_MEMORY_MEMMGR_MULTI_ACCOUNT_MANAGER_H
68