• 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 "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 
Unmarshalling(Parcel & parcel)56 std::unique_ptr<GeoAddress> GeoAddress::Unmarshalling(Parcel& parcel)
57 {
58     std::unique_ptr<GeoAddress> geoAddress = std::make_unique<GeoAddress>();
59     geoAddress->ReadFromParcel(parcel);
60     return geoAddress;
61 }
62 
ReadFromParcel(Parcel & in)63 void GeoAddress::ReadFromParcel(Parcel& in)
64 {
65     latitude_ = in.ReadDouble();
66     longitude_ = in.ReadDouble();
67     locale_ = Str16ToStr8(in.ReadString16());
68     placeName_ = Str16ToStr8(in.ReadString16());
69     countryCode_ = Str16ToStr8(in.ReadString16());
70     countryName_ = Str16ToStr8(in.ReadString16());
71     administrativeArea_ = Str16ToStr8(in.ReadString16());
72     subAdministrativeArea_ = Str16ToStr8(in.ReadString16());
73     locality_ = Str16ToStr8(in.ReadString16());
74     subLocality_ = Str16ToStr8(in.ReadString16());
75     roadName_ = Str16ToStr8(in.ReadString16());
76     subRoadName_ = Str16ToStr8(in.ReadString16());
77     premises_ = Str16ToStr8(in.ReadString16());
78     postalCode_ = Str16ToStr8(in.ReadString16());
79     phoneNumber_ = Str16ToStr8(in.ReadString16());
80     addressUrl_ = Str16ToStr8(in.ReadString16());
81     int size = in.ReadInt32(); // descriptionsSize
82     if (size > 0 && size < MAXIMUM_INTERATION) {
83         for (int i = 0; i < size; i++) {
84             int index = in.ReadInt32();
85             if (index < 0 || index >= MAXIMUM_INTERATION) {
86                 continue;
87             }
88             std::string line = Str16ToStr8(in.ReadString16());
89             std::unique_lock<std::mutex> lock(mutex_);
90             descriptions_.insert(std::pair<int, std::string>(index, line));
91             descriptionsSize_ = std::max(descriptionsSize_, index + 1);
92         }
93     } else {
94         descriptionsSize_ = 0;
95     }
96     isFromMock_ = in.ReadBool();
97 }
98 
Marshalling(Parcel & parcel) const99 bool GeoAddress::Marshalling(Parcel& parcel) const
100 {
101     parcel.WriteDouble(latitude_);
102     parcel.WriteDouble(longitude_);
103     parcel.WriteString16(Str8ToStr16(locale_));
104     parcel.WriteString16(Str8ToStr16(placeName_));
105     parcel.WriteString16(Str8ToStr16(countryCode_));
106     parcel.WriteString16(Str8ToStr16(countryName_));
107     parcel.WriteString16(Str8ToStr16(administrativeArea_));
108     parcel.WriteString16(Str8ToStr16(subAdministrativeArea_));
109     parcel.WriteString16(Str8ToStr16(locality_));
110     parcel.WriteString16(Str8ToStr16(subLocality_));
111     parcel.WriteString16(Str8ToStr16(roadName_));
112     parcel.WriteString16(Str8ToStr16(subRoadName_));
113     parcel.WriteString16(Str8ToStr16(premises_));
114     parcel.WriteString16(Str8ToStr16(postalCode_));
115     parcel.WriteString16(Str8ToStr16(phoneNumber_));
116     parcel.WriteString16(Str8ToStr16(addressUrl_));
117     if (descriptions_.size() == 0) {
118         parcel.WriteInt32(0);
119     } else {
120         parcel.WriteInt32(descriptions_.size());
121         for (auto iter = descriptions_.begin(); iter != descriptions_.end(); iter++) {
122             parcel.WriteInt32(iter->first);
123             parcel.WriteString16(Str8ToStr16(iter->second));
124         }
125     }
126     parcel.WriteBool(isFromMock_);
127     return true;
128 }
129 } // namespace Location
130 } // namespace OHOS
131