• 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 
19 namespace OHOS {
20 namespace Location {
GeoAddress()21 GeoAddress::GeoAddress()
22 {
23     m_latitude = 0.0;
24     m_longitude = 0.0;
25 }
26 
GetDescriptions(int index)27 std::string GeoAddress::GetDescriptions(int index)
28 {
29     if (index < 0) {
30         return "";
31     }
32     if (m_descriptionsSize <= 0) {
33         return "";
34     }
35     std::map<int, std::string>::iterator it = m_descriptions.find(index);
36     if (it == m_descriptions.end()) {
37         return "";
38     }
39 
40     return it->second;
41 }
42 
GetLatitude()43 double GeoAddress::GetLatitude()
44 {
45     if (m_hasLatitude) {
46         return m_latitude;
47     }
48     return 0.0;
49 }
50 
GetLongitude()51 double GeoAddress::GetLongitude()
52 {
53     if (m_hasLongitude) {
54         return m_longitude;
55     }
56     return 0.0;
57 }
58 
Unmarshalling(Parcel & parcel)59 std::unique_ptr<GeoAddress> GeoAddress::Unmarshalling(Parcel& parcel)
60 {
61     std::unique_ptr<GeoAddress> geoAddress = std::make_unique<GeoAddress>();
62     geoAddress->ReadFromParcel(parcel);
63     return geoAddress;
64 }
65 
ReadFromParcel(Parcel & in)66 void GeoAddress::ReadFromParcel(Parcel& in)
67 {
68     m_localeLanguage = Str16ToStr8(in.ReadString16());
69     m_localeCountry = Str16ToStr8(in.ReadString16());
70     int size = in.ReadInt32();
71     if (size > 0 && size < MAX_PARCEL_SIZE) {
72         for (int i = 0; i < size; i++) {
73             int index = in.ReadInt32();
74             std::string line = Str16ToStr8(in.ReadString16());
75             m_descriptions.insert(std::pair<int, std::string>(index, line));
76             m_descriptionsSize = std::max(m_descriptionsSize, index + 1);
77         }
78     } else {
79         m_descriptionsSize = 0;
80     }
81     m_placeName = Str16ToStr8(in.ReadString16());
82     m_administrativeArea = Str16ToStr8(in.ReadString16());
83     m_subAdministrativeArea = Str16ToStr8(in.ReadString16());
84     m_locality = Str16ToStr8(in.ReadString16());
85     m_subLocality = Str16ToStr8(in.ReadString16());
86     m_roadName = Str16ToStr8(in.ReadString16());
87     m_subRoadName = Str16ToStr8(in.ReadString16());
88     m_premises = Str16ToStr8(in.ReadString16());
89     m_postalCode = Str16ToStr8(in.ReadString16());
90     m_countryCode = Str16ToStr8(in.ReadString16());
91     m_countryName = Str16ToStr8(in.ReadString16());
92     m_hasLatitude = (in.ReadInt32() != 0);
93     if (m_hasLatitude) {
94         m_latitude = in.ReadDouble();
95     }
96     m_hasLongitude = (in.ReadInt32() != 0);
97     if (m_hasLongitude) {
98         m_longitude = in.ReadDouble();
99     }
100     m_phoneNumber = Str16ToStr8(in.ReadString16());
101     m_addressUrl = Str16ToStr8(in.ReadString16());
102     m_isFromMock = in.ReadBool();
103 }
104 
Marshalling(Parcel & parcel) const105 bool GeoAddress::Marshalling(Parcel& parcel) const
106 {
107     parcel.WriteString16(Str8ToStr16(m_localeLanguage));
108     parcel.WriteString16(Str8ToStr16(m_localeCountry));
109     if (m_descriptions.size() == 0) {
110         parcel.WriteInt32(0);
111     } else {
112         parcel.WriteInt32(m_descriptions.size());
113         for (auto iter = m_descriptions.begin(); iter != m_descriptions.end(); iter++) {
114             parcel.WriteInt32(iter->first);
115             parcel.WriteString16(Str8ToStr16(iter->second));
116         }
117     }
118     parcel.WriteString16(Str8ToStr16(m_placeName));
119     parcel.WriteString16(Str8ToStr16(m_administrativeArea));
120     parcel.WriteString16(Str8ToStr16(m_subAdministrativeArea));
121     parcel.WriteString16(Str8ToStr16(m_locality));
122     parcel.WriteString16(Str8ToStr16(m_subLocality));
123     parcel.WriteString16(Str8ToStr16(m_roadName));
124     parcel.WriteString16(Str8ToStr16(m_subRoadName));
125     parcel.WriteString16(Str8ToStr16(m_premises));
126     parcel.WriteString16(Str8ToStr16(m_postalCode));
127     parcel.WriteString16(Str8ToStr16(m_countryCode));
128     parcel.WriteString16(Str8ToStr16(m_countryName));
129     parcel.WriteInt32(m_hasLatitude ? 1 : 0);
130     if (m_hasLatitude) {
131         parcel.WriteDouble(m_latitude);
132     }
133     parcel.WriteInt32(m_hasLongitude ? 1 : 0);
134     if (m_hasLongitude) {
135         parcel.WriteDouble(m_longitude);
136     }
137     parcel.WriteString16(Str8ToStr16(m_phoneNumber));
138     parcel.WriteString16(Str8ToStr16(m_addressUrl));
139     parcel.WriteBool(m_isFromMock);
140     return true;
141 }
142 } // namespace Location
143 } // namespace OHOS
144