1 /*
2 * Copyright (c) 2023-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 "common_event_listener.h"
17
18 #include "datashare_manager.h"
19 #include "dtbschedmgr_log.h"
20 #include "dsched_continue_manager.h"
21 #include "mission/distributed_bm_storage.h"
22 #include "mission/dms_continue_recv_manager.h"
23 #include "mission/dms_continue_send_manager.h"
24 #include "os_account_manager.h"
25 #include "switch_status_dependency.h"
26
27 namespace OHOS {
28 namespace DistributedSchedule {
29 namespace {
30 const std::string TAG = "CommonEventListener";
31 const uint8_t SCREEN_LOCKED = 0;
32 const uint8_t SCREEN_OFF = 1;
33 const uint8_t SCREEN_UNLOCKED = 2;
34 const uint8_t SCREEN_ON = 3;
35 const uint8_t USER_SWITCHED = 4;
36 const uint8_t PACKAGE_ADDED = 5;
37 const uint8_t PACKAGE_CHANGED = 6;
38 const uint8_t PACKAGE_REMOVED = 7;
39 constexpr static int32_t INVALID_ID = 0;
40 std::map<std::string, uint8_t> receiveEvent = {
41 {EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED, SCREEN_LOCKED},
42 {EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF, SCREEN_OFF},
43 {EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED, SCREEN_UNLOCKED},
44 {EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON, SCREEN_ON},
45 {EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED, USER_SWITCHED},
46 {EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED, PACKAGE_ADDED},
47 {EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, PACKAGE_CHANGED},
48 {EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, PACKAGE_REMOVED},
49 };
50 }
OnReceiveEvent(const EventFwk::CommonEventData & eventData)51 void CommonEventListener::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
52 {
53 auto want = eventData.GetWant();
54 std::string action = want.GetAction();
55 HILOGI("OnReceiveEvent called, action = %{public}s", action.c_str());
56 switch (receiveEvent[action]) {
57 case SCREEN_LOCKED :
58 HILOGI("SCREEN_LOCKED");
59 DMSContinueSendMgr::GetInstance().OnDeviceScreenOff();
60 break;
61 case SCREEN_OFF :
62 HILOGI("SCREEN_OFF");
63 DMSContinueRecvMgr::GetInstance().OnDeviceScreenOff();
64 break;
65 case SCREEN_UNLOCKED :
66 HILOGI("SCREEN_UNLOCKED");
67 DMSContinueSendMgr::GetInstance().OnDeviceScreenOn();
68 break;
69 case SCREEN_ON :
70 HILOGI("SCREEN_ON");
71 break;
72 case USER_SWITCHED :
73 HILOGI("USER_SWITCHED");
74 OnUserSwitched();
75 break;
76 case PACKAGE_ADDED :
77 HILOGI("PACKAGE_ADDED: %{public}s", want.GetElement().GetBundleName().c_str());
78 DmsBmStorage::GetInstance()->SaveStorageDistributeInfo(want.GetElement().GetBundleName());
79 break;
80 case PACKAGE_CHANGED :
81 HILOGI("PACKAGE_CHANGED: %{public}s", want.GetElement().GetBundleName().c_str());
82 DmsBmStorage::GetInstance()->SaveStorageDistributeInfo(want.GetElement().GetBundleName(), true);
83 break;
84 case PACKAGE_REMOVED :
85 HILOGI("PACKAGE_REMOVED: %{public}s", want.GetElement().GetBundleName().c_str());
86 DmsBmStorage::GetInstance()->DeleteStorageDistributeInfo(want.GetElement().GetBundleName());
87 DMSContinueRecvMgr::GetInstance().NotifyPackageRemoved(want.GetElement().GetBundleName());
88 break;
89 default:
90 HILOGW("OnReceiveEvent undefined action");
91 }
92 }
93
GetForegroundOsAccountLocalId()94 int32_t CommonEventListener::GetForegroundOsAccountLocalId()
95 {
96 int32_t accountId = INVALID_ID;
97 ErrCode err = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(accountId);
98 if (err != ERR_OK || accountId == INVALID_ID) {
99 HILOGE("GetForegroundOsAccountLocalId passing param invalid or return error!, err : %{public}d", err);
100 return INVALID_PARAMETERS_ERR;
101 }
102 HILOGD("GetForegroundOsAccountLocalId accountId is: %{public}d", accountId);
103 return accountId;
104 }
105
GetOsAccountType(int32_t & accountId)106 AccountSA::OsAccountType CommonEventListener::GetOsAccountType(int32_t& accountId)
107 {
108 AccountSA::OsAccountType type;
109 ErrCode err = AccountSA::OsAccountManager::GetOsAccountType(accountId, type);
110 if (err != ERR_OK) {
111 HILOGE("GetOsAccountType passing param invalid or return error!, err : %{public}d", err);
112 }
113 return type;
114 }
115
OnUserSwitched()116 void CommonEventListener::OnUserSwitched()
117 {
118 int32_t accountId = GetForegroundOsAccountLocalId();
119 AccountSA::OsAccountType type = GetOsAccountType(accountId);
120 HILOGI("OnUserSwitched called, accountId = %{public}d, type = %{public}d", accountId, type);
121 if (type == AccountSA::OsAccountType::PRIVATE) {
122 HILOGI("GetOsAccountType : OsAccountType is PRIVATE, type : %{public}d", type);
123 DataShareManager::GetInstance().UpdateSwitchStatus(SwitchStatusDependency::GetInstance()
124 .CONTINUE_SWITCH_STATUS_KEY, SwitchStatusDependency::GetInstance().CONTINUE_SWITCH_OFF);
125 }
126
127 DataShareManager::GetInstance().SetCurrentContinueSwitch(SwitchStatusDependency::GetInstance()
128 .IsContinueSwitchOn());
129 if (DataShareManager::GetInstance().IsCurrentContinueSwitchOn()) {
130 DSchedContinueManager::GetInstance().Init();
131 } else {
132 DMSContinueRecvMgr::GetInstance().OnContinueSwitchOff();
133 HILOGI("ICurrentContinueSwitch is off, %{public}d", DataShareManager::GetInstance()
134 .IsCurrentContinueSwitchOn());
135 DSchedContinueManager::GetInstance().UnInit();
136 };
137 }
138 } // namespace DistributedSchedule
139 } // namespace OHOS