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 "subability_common.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "string_ex.h"
21
22 #include "common_utils.h"
23 #include "locator_ability.h"
24
25 namespace OHOS {
26 namespace Location {
SubAbility()27 SubAbility::SubAbility()
28 {
29 label_ = { LOG_CORE, LOCATOR_LOG_ID, "unknown" };
30 newRecord_ = std::make_unique<WorkRecord>();
31 lastRecord_ = std::make_unique<WorkRecord>();
32 }
33
~SubAbility()34 SubAbility::~SubAbility()
35 {
36 newRecord_ = nullptr;
37 lastRecord_ = nullptr;
38 }
39
SetAbility(std::string name)40 void SubAbility::SetAbility(std::string name)
41 {
42 name_ = name;
43 label_ = CommonUtils::GetLabel(name);
44 capability_ = CommonUtils::GetCapability(name);
45 }
46
LocationRequest(WorkRecord & workRecord)47 void SubAbility::LocationRequest(WorkRecord &workRecord)
48 {
49 interval_ = workRecord.GetTimeInterval(0);
50 newRecord_->Clear();
51 newRecord_->Set(workRecord);
52 HandleRefrashRequirements();
53 }
54
HandleRefrashRequirements()55 void SubAbility::HandleRefrashRequirements()
56 {
57 LBSLOGI(label_, "refrash requirements");
58
59 // send local request
60 HandleLocalRequest(*newRecord_);
61 lastRecord_->Clear();
62 lastRecord_->Set(*newRecord_);
63 }
64
GetRequestNum()65 int SubAbility::GetRequestNum()
66 {
67 if (newRecord_ == nullptr) {
68 return 0;
69 }
70 return newRecord_->Size();
71 }
72
HandleLocalRequest(WorkRecord & record)73 void SubAbility::HandleLocalRequest(WorkRecord &record)
74 {
75 HandleRemoveRecord(record);
76 HandleAddRecord(record);
77 }
78
HandleRemoveRecord(WorkRecord & newRecord)79 void SubAbility::HandleRemoveRecord(WorkRecord &newRecord)
80 {
81 for (int i = 0; i < lastRecord_->Size(); i++) {
82 int uid = lastRecord_->GetUid(i);
83 bool isFind = newRecord.Find(uid, lastRecord_->GetName(i), lastRecord_->GetUuid(i));
84 LBSLOGD(label_, "remove record isFind:%{public}d, uid:%{public}d, lastRecord:%{public}s, newRecord:%{public}s",
85 isFind, uid, lastRecord_->ToString().c_str(), newRecord.ToString().c_str());
86 if (!isFind) {
87 std::unique_ptr<WorkRecord> workRecord = std::make_unique<WorkRecord>();
88 workRecord->Add(uid, lastRecord_->GetPid(i), lastRecord_->GetName(i),
89 lastRecord_->GetTimeInterval(i), lastRecord_->GetUuid(i));
90 workRecord->SetDeviceId(newRecord.GetDeviceId());
91 RequestRecord(*workRecord, false);
92 }
93 }
94 }
95
HandleAddRecord(WorkRecord & newRecord)96 void SubAbility::HandleAddRecord(WorkRecord &newRecord)
97 {
98 for (int i = 0; i < newRecord.Size(); i++) {
99 int uid = newRecord.GetUid(i);
100 bool isFind = lastRecord_->Find(uid, newRecord.GetName(i), lastRecord_->GetUuid(i));
101 LBSLOGD(label_, "add record isFind:%{public}d, uid:%{public}d, lastRecord:%{public}s, newRecord:%{public}s",
102 isFind, uid, lastRecord_->ToString().c_str(), newRecord.ToString().c_str());
103 if (!isFind) {
104 std::unique_ptr<WorkRecord> workRecord = std::make_unique<WorkRecord>();
105 workRecord->Add(uid, newRecord.GetPid(i), newRecord.GetName(i),
106 newRecord.GetTimeInterval(i), newRecord.GetUuid(i));
107 workRecord->SetDeviceId(newRecord.GetDeviceId());
108 RequestRecord(*workRecord, true);
109 }
110 }
111 }
112
Enable(bool state,const sptr<IRemoteObject> ability)113 void SubAbility::Enable(bool state, const sptr<IRemoteObject> ability)
114 {
115 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
116 if (sam == nullptr) {
117 LBSLOGE(label_, "Enable can not get SystemAbilityManager");
118 return;
119 }
120
121 int saId = CommonUtils::AbilityConvertToId(name_);
122 if (state) {
123 if (sam->CheckSystemAbility(saId) == nullptr) {
124 sam->AddSystemAbility(saId, ability, ISystemAbilityManager::SAExtraProp(true, 1, capability_, u""));
125 LBSLOGI(label_, "enable %{public}s ability", name_.c_str());
126 }
127 } else {
128 if (sam->CheckSystemAbility(saId) != nullptr) {
129 sam->RemoveSystemAbility(saId);
130 LBSLOGI(label_, "disable %{public}s ability", name_.c_str());
131 }
132 }
133 }
134
HandleSelfRequest(pid_t pid,pid_t uid,bool state)135 void SubAbility::HandleSelfRequest(pid_t pid, pid_t uid, bool state)
136 {
137 std::unique_ptr<WorkRecord> records = std::make_unique<WorkRecord>();
138 std::string name = Str16ToStr8(u"ohos");
139 std::string uuid = std::to_string(CommonUtils::IntRandom(MIN_INT_RANDOM, MAX_INT_RANDOM));
140 records->Set(*lastRecord_);
141 if (state) {
142 records->Add(uid, pid, name, interval_, uuid);
143 } else {
144 records->Remove(uid, pid, name, uuid);
145 }
146 LocationRequest(*records);
147 records->Clear();
148 }
149
EnableLocationMock()150 bool SubAbility::EnableLocationMock()
151 {
152 LBSLOGI(label_, "EnableLocationMock current state is %{public}d", mockEnabled_);
153 mockEnabled_ = true;
154 return true;
155 }
156
DisableLocationMock()157 bool SubAbility::DisableLocationMock()
158 {
159 LBSLOGI(label_, "DisableLocationMock current state is %{public}d", mockEnabled_);
160 mockEnabled_ = false;
161 return true;
162 }
163
SetMockedLocations(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)164 bool SubAbility::SetMockedLocations(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
165 {
166 if (!mockEnabled_) {
167 LBSLOGE(label_, "SetMockedLocations current state is %{public}d, need enbale it", mockEnabled_);
168 return false;
169 }
170 CacheLocationMock(location);
171 mockTimeInterval_ = timeInterval;
172 SendReportMockLocationEvent();
173 return true;
174 }
175
CacheLocationMock(const std::vector<std::shared_ptr<Location>> & location)176 void SubAbility::CacheLocationMock(const std::vector<std::shared_ptr<Location>> &location)
177 {
178 int locationSize = static_cast<int>(location.size());
179 ClearLocationMock();
180 for (int i = 0; i < locationSize; i++) {
181 mockLoc_.push_back(std::make_shared<Location>(*location.at(i)));
182 }
183 }
184
IsLocationMocked()185 bool SubAbility::IsLocationMocked()
186 {
187 return mockEnabled_;
188 }
189
GetTimeIntervalMock()190 int SubAbility::GetTimeIntervalMock()
191 {
192 return mockTimeInterval_;
193 }
194
GetLocationMock()195 std::vector<std::shared_ptr<Location>> SubAbility::GetLocationMock()
196 {
197 return mockLoc_;
198 }
199
ClearLocationMock()200 void SubAbility::ClearLocationMock()
201 {
202 mockLoc_.clear();
203 }
204 } // namespace Location
205 } // namespace OHOS
206