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
ContinuousTaskRecord(const std::string & bundleName,const std::string & abilityName,int32_t uid,int32_t pid,uint32_t bgModeId)36 ContinuousTaskRecord::ContinuousTaskRecord(const std::string &bundleName, const std::string &abilityName,
37 int32_t uid, int32_t pid, uint32_t bgModeId) : bundleName_(bundleName), abilityName_(abilityName),
38 uid_(uid), pid_(pid), bgModeId_(bgModeId) {}
39
GetBundleName() const40 std::string ContinuousTaskRecord::GetBundleName() const
41 {
42 return bundleName_;
43 }
44
GetAbilityName() const45 std::string ContinuousTaskRecord::GetAbilityName() const
46 {
47 return abilityName_;
48 }
49
IsNewApi() const50 bool ContinuousTaskRecord::IsNewApi() const
51 {
52 return isNewApi_;
53 }
54
IsFromWebview() const55 bool ContinuousTaskRecord::IsFromWebview() const
56 {
57 return isFromWebview_;
58 }
59
GetBgModeId() const60 uint32_t ContinuousTaskRecord::GetBgModeId() const
61 {
62 return bgModeId_;
63 }
64
GetUserId() const65 int32_t ContinuousTaskRecord::GetUserId() const
66 {
67 return userId_;
68 }
69
GetUid() const70 int32_t ContinuousTaskRecord::GetUid() const
71 {
72 return uid_;
73 }
74
GetPid() const75 pid_t ContinuousTaskRecord::GetPid() const
76 {
77 return pid_;
78 }
79
GetNotificationLabel() const80 std::string ContinuousTaskRecord::GetNotificationLabel() const
81 {
82 return notificationLabel_;
83 }
84
GetWantAgent() const85 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> ContinuousTaskRecord::GetWantAgent() const
86 {
87 return wantAgent_;
88 }
89
ParseToJsonStr()90 std::string ContinuousTaskRecord::ParseToJsonStr()
91 {
92 nlohmann::json root;
93 root["bundleName"] = bundleName_;
94 root["abilityName"] = abilityName_;
95 root["userId"] = userId_;
96 root["uid"] = uid_;
97 root["pid"] = pid_;
98 root["bgModeId"] = bgModeId_;
99 root["isNewApi"] = isNewApi_;
100 root["isFromWebview"] = isFromWebview_;
101 root["notificationLabel"] = notificationLabel_;
102 if (wantAgentInfo_ != nullptr) {
103 nlohmann::json info;
104 info["bundleName"] = wantAgentInfo_->bundleName_;
105 info["abilityName"] = wantAgentInfo_->abilityName_;
106 root["wantAgentInfo"] = info;
107 }
108 return root.dump(CommonUtils::jsonFormat_);
109 }
110
ParseFromJson(const nlohmann::json & value)111 bool ContinuousTaskRecord::ParseFromJson(const nlohmann::json &value)
112 {
113 if (value.is_null() || !value.is_object() || !CommonUtils::CheckJsonValue(value, { "bundleName",
114 "abilityName", "userId", "uid", "pid", "bgModeId", "isNewApi", "isFromWebview", "notificationLabel" })) {
115 return false;
116 }
117 this->bundleName_ = value.at("bundleName").get<std::string>();
118 this->abilityName_ = value.at("abilityName").get<std::string>();
119 this->userId_ = value.at("userId").get<int32_t>();
120 this->uid_ = value.at("uid").get<int32_t>();
121 this->pid_ = value.at("pid").get<int32_t>();
122 this->bgModeId_ = value.at("bgModeId").get<uint32_t>();
123 this->isNewApi_ = value.at("isNewApi").get<bool>();
124 this->isFromWebview_ = value.at("isFromWebview").get<bool>();
125 this->notificationLabel_ = value.at("notificationLabel").get<std::string>();
126
127 if (value.find("wantAgentInfo") != value.end()) {
128 nlohmann::json infoVal = value["wantAgentInfo"];
129 if (!CommonUtils::CheckJsonValue(infoVal, { "bundleName", "abilityName" })) {
130 return false;
131 }
132 std::shared_ptr<WantAgentInfo> info = std::make_shared<WantAgentInfo>();
133 info->bundleName_ = infoVal.at("bundleName").get<std::string>();
134 info->abilityName_ = infoVal.at("abilityName").get<std::string>();
135 this->wantAgentInfo_ = info;
136 }
137 return true;
138 }
139 } // namespace BackgroundTaskMgr
140 } // namespace OHOS