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 #include "account_status_listener.h"
17 
18 #include <fcntl.h>
19 
20 #include "cloud_daemon_statistic.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "account_status.h"
24 #include "utils_log.h"
25 
26 namespace OHOS {
27 namespace FileManagement {
28 namespace CloudDisk {
29 
SwapMemory()30 static void SwapMemory()
31 {
32     auto fd = open("/proc/self/reclaim", O_WRONLY);
33     if (fd < 0) {
34         LOGE("Failed to open reclaim, errno:%{public}d", errno);
35         return;
36     }
37     std::string content = "1";
38     if (write(fd, content.c_str(), content.size()) < 0) {
39         LOGE("Failed to write reclaim, errno:%{public}d", errno);
40     }
41     close(fd);
42     return;
43 }
44 
AccountStatusSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)45 AccountStatusSubscriber::AccountStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
46     : EventFwk::CommonEventSubscriber(subscribeInfo)
47 {
48 }
49 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)50 void AccountStatusSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
51 {
52     auto action = eventData.GetWant().GetAction();
53     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN) {
54         LOGI("Account Login!");
55         AccountStatus::SetAccountState(AccountStatus::AccountState::ACCOUNT_LOGIN);
56     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
57         LOGI("Account Logout!");
58         AccountStatus::SetAccountState(AccountStatus::AccountState::ACCOUNT_LOGOUT);
59         SwapMemory();
60     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF &&
61         AccountStatus::GetAccountState() == AccountStatus::AccountState::ACCOUNT_LOGIN) {
62         LOGI("Screen Off and Swap Memory!");
63         SwapMemory();
64         CloudFile::CloudDaemonStatistic::GetInstance().UpdateStatData();
65     }
66 }
67 
~AccountStatusListener()68 AccountStatusListener::~AccountStatusListener()
69 {
70     Stop();
71 }
72 
Start()73 void AccountStatusListener::Start()
74 {
75     /* subscribe Account login and logout status */
76     EventFwk::MatchingSkills matchingSkills;
77     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
78     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
79     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
80     EventFwk::CommonEventSubscribeInfo info(matchingSkills);
81     commonEventSubscriber_ = std::make_shared<AccountStatusSubscriber>(info);
82     auto subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_);
83     LOGI("Subscriber end, SubscribeResult = %{public}d", subRet);
84 }
85 
Stop()86 void AccountStatusListener::Stop()
87 {
88     if (commonEventSubscriber_ != nullptr) {
89         EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_);
90         commonEventSubscriber_ = nullptr;
91     }
92 }
93 } // namespace CloudDisk
94 } // namespace FileManagement
95 } // namespace OHOS