• 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 "location.h"
17 
18 #include <parcel.h>
19 #include <string>
20 #include "string_ex.h"
21 
22 namespace OHOS {
23 namespace Location {
24 static constexpr double MIN_LATITUDE = -90.0;
25 static constexpr double MIN_LONGITUDE = -180.0;
26 
Location()27 Location::Location()
28 {
29     latitude_ = MIN_LATITUDE - 1;
30     longitude_ = MIN_LONGITUDE - 1;
31     altitude_ = 0.0;
32     accuracy_ = 0.0;
33     speed_ = 0.0;
34     direction_ = 0.0;
35     timeStamp_ = 0;
36     timeSinceBoot_ = 0;
37     additions_ = "";
38     additionSize_ = 0;
39     isFromMock_ = false;
40     sourceType_ = 0;
41     floorNo_ = 0;
42     floorAccuracy_ = 0.0;
43 }
44 
Location(Location & location)45 Location::Location(Location& location)
46 {
47     latitude_ = location.GetLatitude();
48     longitude_ = location.GetLongitude();
49     altitude_ = location.GetAltitude();
50     accuracy_ = location.GetAccuracy();
51     speed_ = location.GetSpeed();
52     direction_ = location.GetDirection();
53     timeStamp_ = location.GetTimeStamp();
54     timeSinceBoot_ = location.GetTimeSinceBoot();
55     additions_ = location.GetAdditions();
56     additionSize_ = location.GetAdditionSize();
57     isFromMock_ = location.GetIsFromMock();
58     sourceType_ = location.GetSourceType();
59     floorNo_ = location.GetFloorNo();
60     floorAccuracy_ = location.GetFloorAccuracy();
61 }
62 
ReadFromParcel(Parcel & parcel)63 void Location::ReadFromParcel(Parcel& parcel)
64 {
65     latitude_ = parcel.ReadDouble();
66     longitude_ = parcel.ReadDouble();
67     altitude_ = parcel.ReadDouble();
68     accuracy_ = parcel.ReadDouble();
69     speed_ = parcel.ReadDouble();
70     direction_ = parcel.ReadDouble();
71     timeStamp_ = parcel.ReadInt64();
72     timeSinceBoot_ = parcel.ReadInt64();
73     additions_ = Str16ToStr8(parcel.ReadString16());
74     additionSize_ = parcel.ReadInt64();
75     isFromMock_ = parcel.ReadBool();
76     sourceType_ = parcel.ReadInt32();
77     floorNo_ = parcel.ReadInt32();
78     floorAccuracy_ = parcel.ReadDouble();
79 }
80 
UnmarshallingShared(Parcel & parcel)81 std::shared_ptr<Location> Location::UnmarshallingShared(Parcel& parcel)
82 {
83     std::shared_ptr<Location> location = std::make_shared<Location>();
84     location->ReadFromParcel(parcel);
85     return location;
86 }
87 
Unmarshalling(Parcel & parcel)88 std::unique_ptr<Location> Location::Unmarshalling(Parcel& parcel)
89 {
90     std::unique_ptr<Location> location = std::make_unique<Location>();
91     location->ReadFromParcel(parcel);
92     return location;
93 }
94 
Marshalling(Parcel & parcel) const95 bool Location::Marshalling(Parcel& parcel) const
96 {
97     return parcel.WriteDouble(latitude_) &&
98            parcel.WriteDouble(longitude_) &&
99            parcel.WriteDouble(altitude_) &&
100            parcel.WriteDouble(accuracy_) &&
101            parcel.WriteDouble(speed_) &&
102            parcel.WriteDouble(direction_) &&
103            parcel.WriteInt64(timeStamp_) &&
104            parcel.WriteInt64(timeSinceBoot_) &&
105            parcel.WriteString16(Str8ToStr16(additions_)) &&
106            parcel.WriteInt64(additionSize_) &&
107            parcel.WriteBool(isFromMock_) &&
108            parcel.WriteInt32(sourceType_) &&
109            parcel.WriteInt32(floorNo_) &&
110            parcel.WriteDouble(floorAccuracy_);
111 }
112 
ToString() const113 std::string Location::ToString() const
114 {
115     std::string str = "latitude : " + std::to_string(latitude_) +
116         ", longitude : " + std::to_string(longitude_) +
117         ", altitude : " + std::to_string(altitude_) +
118         ", accuracy : " + std::to_string(accuracy_) +
119         ", speed : " + std::to_string(speed_) +
120         ", direction : " + std::to_string(direction_) +
121         ", timeStamp : " + std::to_string(timeStamp_) +
122         ", timeSinceBoot : " + std::to_string(timeSinceBoot_) +
123         ", additions : " + additions_ +
124         ", additionSize : " + std::to_string(additionSize_) +
125         ", isFromMock : " + std::to_string(isFromMock_) +
126         ", sourceType : " + std::to_string(sourceType_) +
127         ", floorNo : " + std::to_string(floorNo_) +
128         ", floorAccuracy : " + std::to_string(floorAccuracy_);
129     return str;
130 }
131 } // namespace Location
132 } // namespace OHOS