1/* 2 * Copyright (c) 2023 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 "VirtualLocation.h" 17 18#include <chrono> 19#include <cmath> 20#include <cstdlib> 21 22#include "PreviewerEngineLog.h" 23#include "SharedData.h" 24#include "SharedDataManager.h" 25 26VirtualLocation& VirtualLocation::GetInstance() 27{ 28 static VirtualLocation stance; 29 return stance; 30} 31 32const int8_t* VirtualLocation::GetMockPointer() const 33{ 34 return mockPointer; 35} 36 37uint32_t VirtualLocation::GetMockLen() const 38{ 39 return mockLen; 40} 41 42void VirtualLocation::SetCallBack(LocDataUpdateCallback func) 43{ 44 callBack = func; 45} 46 47void VirtualLocation::SetSubscribe(bool value) 48{ 49 isSubsribe = value; 50} 51 52bool VirtualLocation::IsSubscribe() const 53{ 54 return isSubsribe; 55} 56 57LocDataUpdateCallback VirtualLocation::GetCallBack() const 58{ 59 return callBack; 60} 61 62void VirtualLocation::ExecCallBack() const 63{ 64 if (callBack != nullptr && isSubsribe) { 65 callBack(mockPointer, mockLen); 66 } 67} 68 69VirtualLocation::VirtualLocation() 70 : isSubsribe(false), 71 longitudeChecked(0), 72 latitudeChecked(0), 73 accuracy(0), 74 mockPointer(nullptr), 75 mockLen(1), 76 callBack(nullptr) 77{ 78 accuracy = LOCATION_ACCURACY; 79 InitMockPointer(); 80} 81 82VirtualLocation::~VirtualLocation() 83{ 84 if (mockPointer != nullptr) { 85 delete mockPointer; 86 mockPointer = nullptr; 87 } 88} 89 90void VirtualLocation::InitMockPointer() 91{ 92 mockPointer = new(std::nothrow) int8_t; 93 if (mockPointer == nullptr) { 94 ELOG("Memory allocation failed: mockPointer."); 95 } 96} 97 98uint64_t VirtualLocation::GetTime() const 99{ 100 time_t curTime = time(nullptr); 101 if (curTime < 0) { 102 return 0; 103 } 104 return static_cast<uint64_t>(curTime); 105} 106 107float VirtualLocation::GetAccuracy() const 108{ 109 return accuracy; 110} 111 112bool VirtualLocation::IsPostionChanged() 113{ 114 double longitude = SharedData<double>::GetData(SharedDataType::LONGITUDE); 115 double latitude = SharedData<double>::GetData(SharedDataType::LATITUDE); 116 if (std::abs(longitude - longitudeChecked) < pow(PRECISION_BASE_NUMBER, -SharedDataManager::POSITIONPRECISION) && 117 std::abs(latitude - latitudeChecked) < pow(PRECISION_BASE_NUMBER, -SharedDataManager::POSITIONPRECISION)) { 118 return false; 119 } 120 longitudeChecked = longitude; 121 latitudeChecked = latitude; 122 return true; 123} 124