diff --git a/frameworks/src/background_task_subscriber_proxy.cpp b/frameworks/src/background_task_subscriber_proxy.cpp index acc38797def4613777bb9dc9e045f6fb04ed38e7..3fe7d5e55472391c061653673ad08862eee701d6 100644 --- a/frameworks/src/background_task_subscriber_proxy.cpp +++ b/frameworks/src/background_task_subscriber_proxy.cpp @@ -43,7 +43,6 @@ void BackgroundTaskSubscriberProxy::OnConnected() ErrCode ret = remote->SendRequest(ON_CONNECTED, data, reply, option); if (ret!= ERR_OK) { BGTASK_LOGE("SendRequest failed, error code: %d", ret); - return; } } @@ -65,7 +64,6 @@ void BackgroundTaskSubscriberProxy::OnDisconnected() ErrCode ret = remote->SendRequest(ON_DISCONNECTED, data, reply, option); if (ret != ERR_OK) { BGTASK_LOGE("SendRequest failed, error code: %d", ret); - return; } } @@ -92,7 +90,6 @@ void BackgroundTaskSubscriberProxy::OnTransientTaskStart(const std::shared_ptrSendRequest(ON_TRANSIENT_TASK_START, data, reply, option); if (ret != ERR_NONE) { BGTASK_LOGE("SendRequest failed, error code: %d", ret); - return; } } @@ -119,7 +116,6 @@ void BackgroundTaskSubscriberProxy::OnTransientTaskEnd(const std::shared_ptrSendRequest(ON_TRANSIENT_TASK_END, data, reply, option); if (ret != ERR_NONE) { BGTASK_LOGE("SendRequest failed, error code: %d", ret); - return; } } @@ -153,7 +149,6 @@ void BackgroundTaskSubscriberProxy::OnContinuousTaskStart( ErrCode result = remote->SendRequest(ON_CONTINUOUS_TASK_START, data, reply, option); if (result != ERR_OK) { BGTASK_LOGE("SendRequest error"); - return; } } @@ -187,7 +182,6 @@ void BackgroundTaskSubscriberProxy::OnContinuousTaskStop( ErrCode result = remote->SendRequest(ON_CONTINUOUS_TASK_STOP, data, reply, option); if (result != ERR_OK) { BGTASK_LOGE("SendRequest error"); - return; } } } // namespace BackgroundTaskMgr diff --git a/interfaces/test/unittest/continuous_task_jsunittest/config.json b/interfaces/test/unittest/continuous_task_jsunittest/config.json index b298035324c472d4d913d0ed1c176a4e6550feab..5fe3ce38aed17fda6f06cde98de2e628ed247df0 100644 --- a/interfaces/test/unittest/continuous_task_jsunittest/config.json +++ b/interfaces/test/unittest/continuous_task_jsunittest/config.json @@ -8,7 +8,7 @@ }, "apiVersion": { "compatible": 7, - "target": 7 + "target": 8 } }, "deviceConfig": {}, diff --git a/services/BUILD.gn b/services/BUILD.gn index 58e1b78fb5a2e83fa71685cf779c7405bc187f62..6abb3535bc5d3b1a16e43eea142df8ec26a48ac5 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("bgtaskmgr_service") { "continuous_task/src/bg_continuous_task_mgr.cpp", "continuous_task/src/bundle_manager_helper.cpp", "continuous_task/src/continuous_task_record.cpp", + "continuous_task/src/os_account_observer.cpp", "continuous_task/src/system_event_observer.cpp", "continuous_task/src/task_notification_subscriber.cpp", "core/src/background_task_mgr_service.cpp", diff --git a/services/continuous_task/include/bg_continuous_task_mgr.h b/services/continuous_task/include/bg_continuous_task_mgr.h index 915834b7d76067602b08e2bd551422b44b7b820a..d7c2e46bdd8aefc072504aa013a64e4e9c7f8cd9 100644 --- a/services/continuous_task/include/bg_continuous_task_mgr.h +++ b/services/continuous_task/include/bg_continuous_task_mgr.h @@ -32,6 +32,7 @@ #include "continuous_task_param.h" #include "continuous_task_record.h" #include "ibackground_task_subscriber.h" +#include "os_account_observer.h" #include "remote_death_recipient.h" #include "system_event_observer.h" @@ -58,6 +59,7 @@ public: ErrCode RemoveSubscriber(const sptr &subscriber); ErrCode ShellDump(const std::vector &dumpOption, std::vector &dumpInfo); bool StopContinuousTaskByUser(const std::string &mapKey); + void OnAccountsStateChanged(int id); void OnBundleInfoChanged(const std::string &action, const std::string &bundleName, uid_t uid); void OnProcessDied(int32_t pid); void OnRemoteSubscriberDied(const wptr &object); @@ -85,6 +87,7 @@ private: bool RegisterNotificationSubscriber(); bool RegisterSysCommEventListener(); bool RegisterAppStateObserver(); + bool RegisterOsAccountObserver(); bool GetContinuousTaskText(); bool SetCachedBundleInfo(uid_t uid, int32_t userId, std::string &bundleName); void OnRemoteSubscriberDiedInner(const wptr &object); @@ -99,6 +102,7 @@ private: std::shared_ptr subscriber_ {nullptr}; std::shared_ptr systemEventListener_ {nullptr}; std::shared_ptr appStateObserver_ {nullptr}; + std::shared_ptr osAccountObserver_ {nullptr}; std::list> bgTaskSubscribers_ {}; std::map, sptr> subscriberRecipients_ {}; std::unordered_map cachedBundleInfos_ {}; diff --git a/services/continuous_task/include/os_account_observer.h b/services/continuous_task/include/os_account_observer.h new file mode 100644 index 0000000000000000000000000000000000000000..8766d00996bba1b0c237685403a48b59b3821bf6 --- /dev/null +++ b/services/continuous_task/include/os_account_observer.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_OS_ACCOUNT_OBSERVER_H +#define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_OS_ACCOUNT_OBSERVER_H + +#include "event_handler.h" +#include "os_account_manager.h" + +namespace OHOS { +namespace BackgroundTaskMgr { +class BgContinuousTaskMgr; + +class OsAccountObserver final : public AccountSA::OsAccountSubscriber, + public std::enable_shared_from_this { +public: + OsAccountObserver() = default; + ~OsAccountObserver() = default; + explicit OsAccountObserver(const AccountSA::OsAccountSubscribeInfo &subscribeInfo); + + void OnAccountsChanged(const int &id) override; + void SetEventHandler(const std::shared_ptr &handler); + void SetBgContinuousTaskMgr(const std::shared_ptr &bgContinuousTaskMgr_); + bool Subscribe(); + bool Unsubscribe(); + +private: + std::weak_ptr handler_; + std::weak_ptr bgContinuousTaskMgr_; +}; +} +} +#endif \ No newline at end of file diff --git a/services/continuous_task/src/bg_continuous_task_mgr.cpp b/services/continuous_task/src/bg_continuous_task_mgr.cpp index 6692982d9f60582d5059d61c3cedc716b4360ba5..98f8c0bfda9acf214f4452afa941d6d6db0fec83 100644 --- a/services/continuous_task/src/bg_continuous_task_mgr.cpp +++ b/services/continuous_task/src/bg_continuous_task_mgr.cpp @@ -108,6 +108,7 @@ void BgContinuousTaskMgr::InitNecessaryState() || systemAbilityManager->CheckSystemAbility(APP_MGR_SERVICE_ID) == nullptr || systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) == nullptr || systemAbilityManager->CheckSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) == nullptr || systemAbilityManager->CheckSystemAbility(COMMON_EVENT_SERVICE_ID) == nullptr) { BGTASK_LOGW("request system service is not ready yet!"); auto task = [this]() { this->InitNecessaryState(); }; @@ -115,23 +116,24 @@ void BgContinuousTaskMgr::InitNecessaryState() return; } - bool isInitSucceed = true; if (!RegisterNotificationSubscriber()) { BGTASK_LOGE("RegisterNotificationSubscriber failed"); - isInitSucceed = false; + return; } if (!RegisterAppStateObserver()) { BGTASK_LOGE("RegisterAppStateObserver failed"); - isInitSucceed = false; + return; } if (!RegisterSysCommEventListener()) { BGTASK_LOGE("RegisterSysCommEventListener failed"); - isInitSucceed = false; + return; } - if (isInitSucceed) { - auto getPromptTask = [this]() { this->InitNotificationPrompt(); }; - handler_->PostTask(getPromptTask, DELAY_TIME); + if (!RegisterOsAccountObserver()) { + BGTASK_LOGE("RegisterOsAccountObserver failed"); + return; } + auto getPromptTask = [this]() { this->InitNotificationPrompt(); }; + handler_->PostTask(getPromptTask, DELAY_TIME); } void BgContinuousTaskMgr::InitNotificationPrompt() @@ -223,6 +225,21 @@ bool BgContinuousTaskMgr::RegisterSysCommEventListener() return res; } +bool BgContinuousTaskMgr::RegisterOsAccountObserver() +{ + bool res = true; + OHOS::AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo; + osAccountSubscribeInfo.SetOsAccountSubscribeType(OHOS::AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED); + osAccountSubscribeInfo.SetName("background_task_manager_service"); + osAccountObserver_ = std::make_shared(osAccountSubscribeInfo); + if (osAccountObserver_ != nullptr) { + osAccountObserver_->SetEventHandler(handler_); + osAccountObserver_->SetBgContinuousTaskMgr(shared_from_this()); + res = osAccountObserver_->Subscribe(); + } + return res; +} + bool BgContinuousTaskMgr::SetCachedBundleInfo(uid_t uid, int32_t userId, std::string &bundleName) { AppExecFwk::BundleInfo bundleInfo; @@ -377,7 +394,7 @@ ErrCode BgContinuousTaskMgr::StartBackgroundRunningInner(std::shared_ptruid_) + SEPARATOR + continuousTaskRecord->abilityName_ + SEPARATOR + abilityTokeLabel; if (continuousTaskInfosMap_.find(taskInfoMapKey) != continuousTaskInfosMap_.end()) { - BGTASK_LOGI("continuous task is already exist: %{public}s", taskInfoMapKey.c_str()); + BGTASK_LOGW("continuous task is already exist: %{public}s", taskInfoMapKey.c_str()); return ERR_BGTASK_OBJECT_EXISTS; } @@ -544,7 +561,7 @@ ErrCode BgContinuousTaskMgr::AddSubscriber(const sptr return ERR_BGTASK_SYS_NOT_READY; } if (subscriber == nullptr) { - BGTASK_LOGI("subscriber is null."); + BGTASK_LOGE("subscriber is null."); return ERR_BGTASK_INVALID_PARAM; } @@ -759,7 +776,7 @@ void BgContinuousTaskMgr::OnRemoteSubscriberDied(const wptr &obje return; } if (object == nullptr) { - BGTASK_LOGI("remote object is null."); + BGTASK_LOGE("remote object is null."); return; } @@ -859,5 +876,27 @@ void BgContinuousTaskMgr::OnBundleInfoChanged(const std::string &action, const s return; } } + +void BgContinuousTaskMgr::OnAccountsStateChanged(int id) +{ + std::vector activatedOsAccountIds; + + if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds) != ERR_OK) { + BGTASK_LOGE("query activated account failed"); + return; + } + auto iter = continuousTaskInfosMap_.begin(); + while (iter != continuousTaskInfosMap_.end()) { + auto idIter = find(activatedOsAccountIds.begin(), activatedOsAccountIds.end(), iter->second->GetUserId()); + if (idIter == activatedOsAccountIds.end()) { + OnContinuousTaskChanged(iter->second, ContinuousTaskEventTriggerType::TASK_CANCEL); + Notification::NotificationHelper::CancelContinuousTaskNotification( + iter->second->GetNotificationLabel(), DEFAULT_NOTIFICATION_ID); + iter = continuousTaskInfosMap_.erase(iter); + } else { + iter++; + } + } +} } } \ No newline at end of file diff --git a/services/continuous_task/src/os_account_observer.cpp b/services/continuous_task/src/os_account_observer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3c997ac486d68ff8c2a29a83be3d36bc760bd10 --- /dev/null +++ b/services/continuous_task/src/os_account_observer.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "os_account_observer.h" + +#include "bg_continuous_task_mgr.h" +#include "continuous_task_log.h" + +namespace OHOS { +namespace BackgroundTaskMgr { +namespace { +const std::string TASK_ON_OS_ACCOUNT_CHANGED = "OnOsAccountChanged"; +} + +OsAccountObserver::OsAccountObserver(const AccountSA::OsAccountSubscribeInfo &subscribeInfo) + : OsAccountSubscriber(subscribeInfo) {} + +bool OsAccountObserver::Subscribe() +{ + BGTASK_LOGI("Subscribe called"); + int ret = OHOS::AccountSA::OsAccountManager::SubscribeOsAccount(shared_from_this()); + if (ret != 0) { + BGTASK_LOGE("register os account failed ret %{public}d", ret); + return false; + } + return true; +} + +bool OsAccountObserver::Unsubscribe() +{ + BGTASK_LOGI("UnSubscribe called"); + int ret = OHOS::AccountSA::OsAccountManager::UnsubscribeOsAccount(shared_from_this()); + if (ret != 0) { + BGTASK_LOGE("register os account failed ret %{public}d", ret); + return false; + } + return true; +} + +void OsAccountObserver::SetEventHandler(const std::shared_ptr &handler) +{ + BGTASK_LOGI("set os account observer handler"); + handler_ = handler; +} + +void OsAccountObserver::SetBgContinuousTaskMgr(const std::shared_ptr &bgContinuousTaskMgr) +{ + BGTASK_LOGI("set os account oberver bgContinuousTaskMgr"); + bgContinuousTaskMgr_ = bgContinuousTaskMgr; +} + +void OsAccountObserver::OnAccountsChanged(const int &id) +{ + BGTASK_LOGI("on accounts state changed : %{public}d actived", id); + auto handler = handler_.lock(); + if (!handler) { + BGTASK_LOGE("handler is null"); + return; + } + auto bgContinuousTaskMgr = bgContinuousTaskMgr_.lock(); + if (!bgContinuousTaskMgr) { + BGTASK_LOGE("bgContinuousTaskMgr is null"); + return; + } + + auto task = [=]() { bgContinuousTaskMgr->OnAccountsStateChanged(id); }; + handler->PostTask(task, TASK_ON_OS_ACCOUNT_CHANGED); +} +} +} \ No newline at end of file diff --git a/services/test/unittest/BUILD.gn b/services/test/unittest/BUILD.gn index fa5f0ec425ac90a781f4e4948766c872656c2b87..6b9a48e82be54354ae16bc06457c2475a79593dc 100644 --- a/services/test/unittest/BUILD.gn +++ b/services/test/unittest/BUILD.gn @@ -60,6 +60,7 @@ ohos_unittest("bgtask_unit_test") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "os_account_standard:os_account_innerkits", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", "utils_base:utils",