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_address.h"
17 #include "string_ex.h"
18 #include "common_utils.h"
19 
20 namespace OHOS {
21 namespace Location {
GeoAddress()22 GeoAddress::GeoAddress()
23 {
24     latitude_ = 0.0;
25     longitude_ = 0.0;
26 }
27 
GetDescriptions(int index)28 std::string GeoAddress::GetDescriptions(int index)
29 {
30     if (index < 0) {
31         return "";
32     }
33     if (descriptionsSize_ <= 0) {
34         return "";
35     }
36 
37     std::unique_lock<std::mutex> lock(mutex_);
38     std::map<int, std::string>::iterator it = descriptions_.find(index);
39     if (it == descriptions_.end()) {
40         return "";
41     }
42 
43     return it->second;
44 }
45 
GetLatitude()46 double GeoAddress::GetLatitude()
47 {
48     return latitude_;
49 }
50 
GetLongitude()51 double GeoAddress::GetLongitude()
52 {
53     return longitude_;
54 }
55 
GetIsSystemApp()56 bool GeoAddress::GetIsSystemApp()
57 {
58     return isSystemApp_;
59 }
60 
SetIsSystemApp(bool isSystemApp)61 void GeoAddress::SetIsSystemApp(bool isSystemApp)
62 {
63     isSystemApp_ = isSystemApp;
64 }
65 
Unmarshalling(Parcel & parcel)66 std::unique_ptr<GeoAddress> GeoAddress::Unmarshalling(Parcel& parcel)
67 {
68     std::unique_ptr<GeoAddress> geoAddress = std::make_unique<GeoAddress>();
69     geoAddress->ReadFromParcel(parcel);
70     return geoAddress;
71 }
72 
ReadFromParcel(Parcel & in)73 void GeoAddress::ReadFromParcel(Parcel& in)
74 {
75     latitude_ = in.ReadDouble();
76     longitude_ = in.ReadDouble();
77     locale_ = Str16ToStr8(in.ReadString16());
78     placeName_ = Str16ToStr8(in.ReadString16());
79     countryCode_ = Str16ToStr8(in.ReadString16());
80     countryName_ = Str16ToStr8(in.ReadString16());
81     administrativeArea_ = Str16ToStr8(in.ReadString16());
82     subAdministrativeArea_ = Str16ToStr8(in.ReadString16());
83     locality_ = Str16ToStr8(in.ReadString16());
84     subLocality_ = Str16ToStr8(in.ReadString16());
85     roadName_ = Str16ToStr8(in.ReadString16());
86     subRoadName_ = Str16ToStr8(in.ReadString16());
87     premises_ = Str16ToStr8(in.ReadString16());
88     postalCode_ = Str16ToStr8(in.ReadString16());
89     phoneNumber_ = Str16ToStr8(in.ReadString16());
90     addressUrl_ = Str16ToStr8(in.ReadString16());
91     int size = in.ReadInt32(); // descriptionsSize
92     if (size > 0 && size < MAXIMUM_INTERATION) {
93         for (int i = 0; i < size; i++) {
94             int index = in.ReadInt32();
95             if (index < 0 || index >= MAXIMUM_INTERATION) {
96                 continue;
97             }
98             std::string line = Str16ToStr8(in.ReadString16());
99             std::unique_lock<std::mutex> lock(mutex_);
100             descriptions_.insert(std::pair<int, std::string>(index, line));
101             descriptionsSize_ = std::max(descriptionsSize_, index + 1);
102         }
103     } else {
104         descriptionsSize_ = 0;
105     }
106     isFromMock_ = in.ReadBool();
107 }
108 
Marshalling(Parcel & parcel) const109 bool GeoAddress::Marshalling(Parcel& parcel) const
110 {
111     parcel.WriteDouble(latitude_);
112     parcel.WriteDouble(longitude_);
113     parcel.WriteString16(Str8ToStr16(locale_));
114     parcel.WriteString16(Str8ToStr16(placeName_));
115     parcel.WriteString16(Str8ToStr16(countryCode_));
116     parcel.WriteString16(Str8ToStr16(countryName_));
117     parcel.WriteString16(Str8ToStr16(administrativeArea_));
118     parcel.WriteString16(Str8ToStr16(subAdministrativeArea_));
119     parcel.WriteString16(Str8ToStr16(locality_));
120     parcel.WriteString16(Str8ToStr16(subLocality_));
121     parcel.WriteString16(Str8ToStr16(roadName_));
122     parcel.WriteString16(Str8ToStr16(subRoadName_));
123     parcel.WriteString16(Str8ToStr16(premises_));
124     parcel.WriteString16(Str8ToStr16(postalCode_));
125     parcel.WriteString16(Str8ToStr16(phoneNumber_));
126     parcel.WriteString16(Str8ToStr16(addressUrl_));
127     if (descriptions_.size() == 0) {
128         parcel.WriteInt32(0);
129     } else {
130         parcel.WriteInt32(descriptions_.size());
131         for (auto iter = descriptions_.begin(); iter != descriptions_.end(); iter++) {
132             parcel.WriteInt32(iter->first);
133             parcel.WriteString16(Str8ToStr16(iter->second));
134         }
135     }
136     parcel.WriteBool(isFromMock_);
137     return true;
138 }
139 } // namespace Location
140 } // namespace OHOS
141