1 /*
2  * Copyright (c) 2023 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 "app_state_observer.h"
17 #include "distributed_sched_service.h"
18 #include "dtbschedmgr_log.h"
19 #include "element_name.h"
20 
21 namespace OHOS {
22 namespace DistributedSchedule {
23 namespace {
24 const std::string TAG = "AppStateObserver";
25 constexpr int32_t FOREGROUND = 2;
26 constexpr int32_t BACKGROUND = 4;
27 constexpr int32_t NOTIFY_COUNT = 2;
28 }
29 
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)30 void AppStateObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData)
31 {
32 }
33 
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)34 void AppStateObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData& abilityStateData)
35 {
36     HILOGD("OnAbilityStateChanged called.");
37     if (abilityStateData.abilityState != FOREGROUND && abilityStateData.abilityState != BACKGROUND) {
38         HILOGD("ability state neither foreground nor background");
39         return;
40     }
41     {
42         std::lock_guard<std::mutex> autoLock(countLock_);
43         if (connectAbilityMap_.count(abilityStateData.token) == 0) {
44             connectAbilityMap_[abilityStateData.token] = 1;
45         } else {
46             connectAbilityMap_[abilityStateData.token]++;
47         }
48         if (connectAbilityMap_[abilityStateData.token] != NOTIFY_COUNT &&
49             abilityStateData.abilityState == FOREGROUND) {
50             HILOGD("ignore first notify");
51             return;
52         }
53         connectAbilityMap_[abilityStateData.token] = 0;
54     }
55     HILOGD("abilityState = %{public}d", abilityStateData.abilityState);
56     AppExecFwk::ElementName element("", abilityStateData.bundleName, abilityStateData.abilityName,
57         abilityStateData.moduleName);
58     int32_t ret = DistributedSchedService::GetInstance().NotifyStateChanged(abilityStateData.abilityState,
59         element, abilityStateData.token);
60     HILOGD("notify callee state changed, ret = %{public}d", ret);
61 }
62 
OnExtensionStateChanged(const AppExecFwk::AbilityStateData & extensionStateData)63 void AppStateObserver::OnExtensionStateChanged(const AppExecFwk::AbilityStateData& extensionStateData)
64 {
65 }
66 
OnProcessCreated(const AppExecFwk::ProcessData & processData)67 void AppStateObserver::OnProcessCreated(const AppExecFwk::ProcessData& processData)
68 {
69 }
70 
OnProcessDied(const AppExecFwk::ProcessData & processData)71 void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData& processData)
72 {
73 }
74 } // namespace DistributedSchedule
75 } // namespace OHOS