17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#include "VirtualLocation.h"
177c804472Sopenharmony_ci
187c804472Sopenharmony_ci#include <chrono>
197c804472Sopenharmony_ci#include <cmath>
207c804472Sopenharmony_ci#include <cstdlib>
217c804472Sopenharmony_ci
227c804472Sopenharmony_ci#include "PreviewerEngineLog.h"
237c804472Sopenharmony_ci#include "SharedData.h"
247c804472Sopenharmony_ci#include "SharedDataManager.h"
257c804472Sopenharmony_ci
267c804472Sopenharmony_ciVirtualLocation& VirtualLocation::GetInstance()
277c804472Sopenharmony_ci{
287c804472Sopenharmony_ci    static VirtualLocation stance;
297c804472Sopenharmony_ci    return stance;
307c804472Sopenharmony_ci}
317c804472Sopenharmony_ci
327c804472Sopenharmony_ciconst int8_t* VirtualLocation::GetMockPointer() const
337c804472Sopenharmony_ci{
347c804472Sopenharmony_ci    return mockPointer;
357c804472Sopenharmony_ci}
367c804472Sopenharmony_ci
377c804472Sopenharmony_ciuint32_t VirtualLocation::GetMockLen() const
387c804472Sopenharmony_ci{
397c804472Sopenharmony_ci    return mockLen;
407c804472Sopenharmony_ci}
417c804472Sopenharmony_ci
427c804472Sopenharmony_civoid VirtualLocation::SetCallBack(LocDataUpdateCallback func)
437c804472Sopenharmony_ci{
447c804472Sopenharmony_ci    callBack = func;
457c804472Sopenharmony_ci}
467c804472Sopenharmony_ci
477c804472Sopenharmony_civoid VirtualLocation::SetSubscribe(bool value)
487c804472Sopenharmony_ci{
497c804472Sopenharmony_ci    isSubsribe = value;
507c804472Sopenharmony_ci}
517c804472Sopenharmony_ci
527c804472Sopenharmony_cibool VirtualLocation::IsSubscribe() const
537c804472Sopenharmony_ci{
547c804472Sopenharmony_ci    return isSubsribe;
557c804472Sopenharmony_ci}
567c804472Sopenharmony_ci
577c804472Sopenharmony_ciLocDataUpdateCallback VirtualLocation::GetCallBack() const
587c804472Sopenharmony_ci{
597c804472Sopenharmony_ci    return callBack;
607c804472Sopenharmony_ci}
617c804472Sopenharmony_ci
627c804472Sopenharmony_civoid VirtualLocation::ExecCallBack() const
637c804472Sopenharmony_ci{
647c804472Sopenharmony_ci    if (callBack != nullptr && isSubsribe) {
657c804472Sopenharmony_ci        callBack(mockPointer, mockLen);
667c804472Sopenharmony_ci    }
677c804472Sopenharmony_ci}
687c804472Sopenharmony_ci
697c804472Sopenharmony_ciVirtualLocation::VirtualLocation()
707c804472Sopenharmony_ci    : isSubsribe(false),
717c804472Sopenharmony_ci      longitudeChecked(0),
727c804472Sopenharmony_ci      latitudeChecked(0),
737c804472Sopenharmony_ci      accuracy(0),
747c804472Sopenharmony_ci      mockPointer(nullptr),
757c804472Sopenharmony_ci      mockLen(1),
767c804472Sopenharmony_ci      callBack(nullptr)
777c804472Sopenharmony_ci{
787c804472Sopenharmony_ci    accuracy = LOCATION_ACCURACY;
797c804472Sopenharmony_ci    InitMockPointer();
807c804472Sopenharmony_ci}
817c804472Sopenharmony_ci
827c804472Sopenharmony_ciVirtualLocation::~VirtualLocation()
837c804472Sopenharmony_ci{
847c804472Sopenharmony_ci    if (mockPointer != nullptr) {
857c804472Sopenharmony_ci        delete mockPointer;
867c804472Sopenharmony_ci        mockPointer = nullptr;
877c804472Sopenharmony_ci    }
887c804472Sopenharmony_ci}
897c804472Sopenharmony_ci
907c804472Sopenharmony_civoid VirtualLocation::InitMockPointer()
917c804472Sopenharmony_ci{
927c804472Sopenharmony_ci    mockPointer = new(std::nothrow) int8_t;
937c804472Sopenharmony_ci    if (mockPointer == nullptr) {
947c804472Sopenharmony_ci        ELOG("Memory allocation failed: mockPointer.");
957c804472Sopenharmony_ci    }
967c804472Sopenharmony_ci}
977c804472Sopenharmony_ci
987c804472Sopenharmony_ciuint64_t VirtualLocation::GetTime() const
997c804472Sopenharmony_ci{
1007c804472Sopenharmony_ci    time_t curTime = time(nullptr);
1017c804472Sopenharmony_ci    if (curTime < 0) {
1027c804472Sopenharmony_ci        return 0;
1037c804472Sopenharmony_ci    }
1047c804472Sopenharmony_ci    return static_cast<uint64_t>(curTime);
1057c804472Sopenharmony_ci}
1067c804472Sopenharmony_ci
1077c804472Sopenharmony_cifloat VirtualLocation::GetAccuracy() const
1087c804472Sopenharmony_ci{
1097c804472Sopenharmony_ci    return accuracy;
1107c804472Sopenharmony_ci}
1117c804472Sopenharmony_ci
1127c804472Sopenharmony_cibool VirtualLocation::IsPostionChanged()
1137c804472Sopenharmony_ci{
1147c804472Sopenharmony_ci    double longitude = SharedData<double>::GetData(SharedDataType::LONGITUDE);
1157c804472Sopenharmony_ci    double latitude = SharedData<double>::GetData(SharedDataType::LATITUDE);
1167c804472Sopenharmony_ci    if (std::abs(longitude - longitudeChecked) < pow(PRECISION_BASE_NUMBER, -SharedDataManager::POSITIONPRECISION) &&
1177c804472Sopenharmony_ci        std::abs(latitude - latitudeChecked) < pow(PRECISION_BASE_NUMBER, -SharedDataManager::POSITIONPRECISION)) {
1187c804472Sopenharmony_ci        return false;
1197c804472Sopenharmony_ci    }
1207c804472Sopenharmony_ci    longitudeChecked = longitude;
1217c804472Sopenharmony_ci    latitudeChecked = latitude;
1227c804472Sopenharmony_ci    return true;
1237c804472Sopenharmony_ci}
124