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 #include "config_change_observer.h" 17 18 #include "bg_continuous_task_mgr.h" 19 #include "continuous_task_log.h" 20 21 namespace OHOS { 22 namespace BackgroundTaskMgr { ConfigChangeObserver(const std::shared_ptr<AppExecFwk::EventHandler> handler,const std::shared_ptr<BgContinuousTaskMgr> bgContinuousTaskMgr)23ConfigChangeObserver::ConfigChangeObserver(const std::shared_ptr<AppExecFwk::EventHandler> handler, 24 const std::shared_ptr<BgContinuousTaskMgr> bgContinuousTaskMgr) 25 { 26 handler_ = handler; 27 bgContinuousTaskMgr_ = bgContinuousTaskMgr; 28 } 29 CheckParamValid()30bool ConfigChangeObserver::CheckParamValid() 31 { 32 if (handler_.expired()) { 33 BGTASK_LOGE("ConfigChangeObserver handler is null"); 34 return false; 35 } 36 if (bgContinuousTaskMgr_.expired()) { 37 BGTASK_LOGE("ConfigChangeObserver bgContinuousTaskMgr is null"); 38 return false; 39 } 40 return true; 41 } 42 OnConfigurationUpdated(const AppExecFwk::Configuration & configuration)43void ConfigChangeObserver::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) 44 { 45 if (!CheckParamValid()) { 46 return; 47 } 48 auto task = [this, configuration]() { 49 this->bgContinuousTaskMgr_.lock()->OnConfigurationChanged(configuration); 50 }; 51 52 auto handler = handler_.lock(); 53 if (handler == nullptr) { 54 return; 55 } 56 handler->PostTask(task); 57 } 58 } 59 }