• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "continuous_task_record.h"
17 
18 #include "common_utils.h"
19 #include "iremote_object.h"
20 
21 namespace OHOS {
22 namespace BackgroundTaskMgr {
23 const char *g_continuousTaskModeName[10] = {
24     "dataTransfer",
25     "audioPlayback",
26     "audioRecording",
27     "location",
28     "bluetoothInteraction",
29     "multiDeviceConnection",
30     "wifiInteraction",
31     "voip",
32     "taskKeeping",
33     "default",
34 };
35 
GetBundleName() const36 std::string ContinuousTaskRecord::GetBundleName() const
37 {
38     return bundleName_;
39 }
40 
GetAbilityName() const41 std::string ContinuousTaskRecord::GetAbilityName() const
42 {
43     return abilityName_;
44 }
45 
IsNewApi() const46 bool ContinuousTaskRecord::IsNewApi() const
47 {
48     return isNewApi_;
49 }
50 
GetBgModeId() const51 uint32_t ContinuousTaskRecord::GetBgModeId() const
52 {
53     return bgModeId_;
54 }
55 
GetUserId() const56 int32_t ContinuousTaskRecord::GetUserId() const
57 {
58     return userId_;
59 }
60 
GetUid() const61 int32_t ContinuousTaskRecord::GetUid() const
62 {
63     return uid_;
64 }
65 
GetPid() const66 pid_t ContinuousTaskRecord::GetPid() const
67 {
68     return pid_;
69 }
70 
GetNotificationLabel() const71 std::string ContinuousTaskRecord::GetNotificationLabel() const
72 {
73     return notificationLabel_;
74 }
75 
GetWantAgent() const76 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> ContinuousTaskRecord::GetWantAgent() const
77 {
78     return wantAgent_;
79 }
80 
ParseToJsonStr()81 std::string ContinuousTaskRecord::ParseToJsonStr()
82 {
83     nlohmann::json root;
84     root["bundleName"] = bundleName_;
85     root["abilityName"] = abilityName_;
86     root["userId"] = userId_;
87     root["uid"] = uid_;
88     root["pid"] = pid_;
89     root["bgModeId"] = bgModeId_;
90     root["isNewApi"] = isNewApi_;
91     root["notificationLabel"] = notificationLabel_;
92     if (wantAgentInfo_ != nullptr) {
93         nlohmann::json info;
94         info["bundleName"] = wantAgentInfo_->bundleName_;
95         info["abilityName"] = wantAgentInfo_->abilityName_;
96         root["wantAgentInfo"] = info;
97     }
98     return root.dump(CommonUtils::jsonFormat_);
99 }
100 
ParseFromJson(const nlohmann::json & value)101 bool ContinuousTaskRecord::ParseFromJson(const nlohmann::json &value)
102 {
103     if (value.is_null() || !value.is_object() || !CommonUtils::CheckJsonValue(value, { "bundleName",
104         "abilityName", "userId", "uid", "pid", "bgModeId", "isNewApi", "notificationLabel" })) {
105         return false;
106     }
107     this->bundleName_ = value.at("bundleName").get<std::string>();
108     this->abilityName_ = value.at("abilityName").get<std::string>();
109     this->userId_ = value.at("userId").get<int32_t>();
110     this->uid_ = value.at("uid").get<int32_t>();
111     this->pid_ = value.at("pid").get<int32_t>();
112     this->bgModeId_ = value.at("bgModeId").get<uint32_t>();
113     this->isNewApi_ = value.at("isNewApi").get<bool>();
114     this->notificationLabel_ = value.at("notificationLabel").get<std::string>();
115 
116     if (value.find("wantAgentInfo") != value.end()) {
117         nlohmann::json infoVal = value["wantAgentInfo"];
118         if (!CommonUtils::CheckJsonValue(infoVal, { "bundleName", "abilityName" })) {
119             return false;
120         }
121         std::shared_ptr<WantAgentInfo> info = std::make_shared<WantAgentInfo>();
122         info->bundleName_ = infoVal.at("bundleName").get<std::string>();
123         info->abilityName_ = infoVal.at("abilityName").get<std::string>();
124         this->wantAgentInfo_ = info;
125     }
126     return true;
127 }
128 }  // namespace BackgroundTaskMgr
129 }  // namespace OHOS