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 "geo_convert_service.h"
17 #include <file_ex.h>
18 #include "geo_address.h"
19 #include "location_dumper.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Location {
24 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(
25 DelayedSingleton<GeoConvertService>::GetInstance().get());
26
GeoConvertService()27 GeoConvertService::GeoConvertService() : SystemAbility(LOCATION_GEO_CONVERT_SA_ID, true)
28 {
29 LBSLOGI(GEO_CONVERT, "GeoConvertService constructed.");
30 }
31
~GeoConvertService()32 GeoConvertService::~GeoConvertService()
33 {
34 }
35
OnStart()36 void GeoConvertService::OnStart()
37 {
38 if (state_ == ServiceRunningState::STATE_RUNNING) {
39 LBSLOGI(GEO_CONVERT, "GeoConvertService has already started.");
40 return;
41 }
42 if (!Init()) {
43 LBSLOGE(GEO_CONVERT, "failed to init GeoConvertService");
44 OnStop();
45 return;
46 }
47 state_ = ServiceRunningState::STATE_RUNNING;
48 LBSLOGI(GEO_CONVERT, "GeoConvertService::OnStart start service success.");
49 }
50
OnStop()51 void GeoConvertService::OnStop()
52 {
53 state_ = ServiceRunningState::STATE_NOT_START;
54 registerToService_ = false;
55 LBSLOGI(GEO_CONVERT, "GeoConvertService::OnStop service stopped.");
56 }
57
Init()58 bool GeoConvertService::Init()
59 {
60 if (!registerToService_) {
61 bool ret = Publish(AsObject());
62 if (!ret) {
63 LBSLOGE(GEO_CONVERT, "GeoConvertService::Init Publish failed!");
64 return false;
65 }
66 registerToService_ = true;
67 }
68 return true;
69 }
70
IsGeoConvertAvailable(MessageParcel & reply)71 int GeoConvertService::IsGeoConvertAvailable(MessageParcel &reply)
72 {
73 if (!mockEnabled_) {
74 reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
75 return ERRCODE_NOT_SUPPORTED;
76 }
77 reply.WriteInt32(ERRCODE_SUCCESS);
78 reply.WriteBool(true);
79 return ERRCODE_SUCCESS;
80 }
81
GetAddressByCoordinate(MessageParcel & data,MessageParcel & reply)82 int GeoConvertService::GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply)
83 {
84 LBSLOGD(GEO_CONVERT, "GetAddressByCoordinate");
85 if (!mockEnabled_) {
86 reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
87 return ERRCODE_NOT_SUPPORTED;
88 }
89 ReportAddressMock(data, reply);
90 return ERRCODE_SUCCESS;
91 }
92
ReportAddressMock(MessageParcel & data,MessageParcel & reply)93 void GeoConvertService::ReportAddressMock(MessageParcel &data, MessageParcel &reply)
94 {
95 int arraySize = 0;
96 std::vector<std::shared_ptr<GeoAddress>> array;
97 ReverseGeocodeRequest request;
98 request.latitude = data.ReadDouble();
99 request.longitude = data.ReadDouble();
100 request.maxItems = data.ReadInt32();
101 data.ReadInt32(); // locale size
102 request.locale = Str16ToStr8(data.ReadString16());
103 std::unique_lock<std::mutex> lock(mockInfoMutex_, std::defer_lock);
104 lock.lock();
105 for (size_t i = 0; i < mockInfo_.size(); i++) {
106 std::shared_ptr<GeocodingMockInfo> info = mockInfo_[i];
107 if (!CommonUtils::DoubleEqual(request.latitude, info->GetLocation()->latitude) ||
108 !CommonUtils::DoubleEqual(request.longitude, info->GetLocation()->longitude)) {
109 continue;
110 }
111 arraySize++;
112 array.push_back(info->GetGeoAddressInfo());
113 }
114 lock.unlock();
115 reply.WriteInt32(ERRCODE_SUCCESS);
116 if (arraySize > 0) {
117 reply.WriteInt32(arraySize);
118 for (size_t i = 0; i < array.size(); i++) {
119 array[i]->Marshalling(reply);
120 }
121 } else {
122 reply.WriteInt32(0);
123 }
124 }
125
GetAddressByLocationName(MessageParcel & data,MessageParcel & reply)126 int GeoConvertService::GetAddressByLocationName(MessageParcel &data, MessageParcel &reply)
127 {
128 LBSLOGD(GEO_CONVERT, "GetAddressByLocationName");
129 reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
130 return ERRCODE_NOT_SUPPORTED;
131 }
132
EnableReverseGeocodingMock()133 bool GeoConvertService::EnableReverseGeocodingMock()
134 {
135 LBSLOGD(GEO_CONVERT, "EnableReverseGeocodingMock");
136 mockEnabled_ = true;
137 return true;
138 }
139
DisableReverseGeocodingMock()140 bool GeoConvertService::DisableReverseGeocodingMock()
141 {
142 LBSLOGD(GEO_CONVERT, "DisableReverseGeocodingMock");
143 mockEnabled_ = false;
144 return true;
145 }
146
SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)147 LocationErrCode GeoConvertService::SetReverseGeocodingMockInfo(
148 std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
149 {
150 LBSLOGD(GEO_CONVERT, "SetReverseGeocodingMockInfo");
151 std::lock_guard lock(mockInfoMutex_);
152 mockInfo_.assign(mockInfo.begin(), mockInfo.end());
153 return ERRCODE_SUCCESS;
154 }
155
SaDumpInfo(std::string & result)156 void GeoConvertService::SaDumpInfo(std::string& result)
157 {
158 result += "GeoConvert enable status: false";
159 result += "\n";
160 }
161
Dump(int32_t fd,const std::vector<std::u16string> & args)162 int32_t GeoConvertService::Dump(int32_t fd, const std::vector<std::u16string>& args)
163 {
164 std::vector<std::string> vecArgs;
165 std::transform(args.begin(), args.end(), std::back_inserter(vecArgs), [](const std::u16string &arg) {
166 return Str16ToStr8(arg);
167 });
168
169 LocationDumper dumper;
170 std::string result;
171 dumper.GeocodeDump(SaDumpInfo, vecArgs, result);
172 if (!SaveStringToFd(fd, result)) {
173 LBSLOGE(GEO_CONVERT, "Geocode save string to fd failed!");
174 return ERR_OK;
175 }
176 return ERR_OK;
177 }
178 } // namespace Location
179 } // namespace OHOS
180