From 438a8dce0337961401a4b2bf87189c44f3620f58 Mon Sep 17 00:00:00 2001 From: umasoodch Date: Tue, 14 Sep 2021 12:37:15 +0300 Subject: [PATCH] added hmg_utils file --- Mohem/src/app/hmg-common/hmg_utils.ts | 121 ++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 Mohem/src/app/hmg-common/hmg_utils.ts diff --git a/Mohem/src/app/hmg-common/hmg_utils.ts b/Mohem/src/app/hmg-common/hmg_utils.ts new file mode 100644 index 00000000..f8a7737b --- /dev/null +++ b/Mohem/src/app/hmg-common/hmg_utils.ts @@ -0,0 +1,121 @@ +import { Injectable } from "@angular/core"; +import { BackgroundGeolocation } from '@ionic-native/background-geolocation/ngx'; +import { CommonService } from "src/app/hmg-common/services/common/common.service"; +import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service"; +import { DevicePermissionsService } from 'src/app/hmg-common/services/device-permissions/device-permissions.service'; +import { Geolocation } from '@ionic-native/geolocation/ngx'; +import { Platform } from "@ionic/angular"; + +declare var cordova: any; + +@Injectable({ + providedIn: 'root' +}) +export class HMGUtils { + constructor( + public backgroundGeolocation: BackgroundGeolocation, + public common: CommonService, + public ts: TranslatorService, + private geolocation: Geolocation, + public platform: Platform, + public devicePermissionsService:DevicePermissionsService, + ) { } + + async getCurrentLocation(callBack: Function) { + if (this.platform.is('android')) { + this.devicePermissionsService.requestLocationAutherization().then( async granted => { + if(granted == true){ + if ((await this.isHuaweiDevice())) { + this.getHMSLocation(callBack); + } else { + this.getGMSLocation(callBack); + } + }else{ + this.common.presentAlert(this.ts.trPK('general', 'location-permission-dialog')); + } + }); + } else { + this.getIOSLocation(callBack); + } + } + + + async isHuaweiDevice(): Promise { + var result = await new Promise((resolve, reject) => { + cordova.plugins.CordovaHMSGMSCheckPlugin.isHmsAvailable( + "index.js", + (_res) => { + var hmsAvailable = _res === "true"; + resolve(hmsAvailable); + }, + (_err) => { + reject({ "status": false, "error": JSON.stringify(_err) }); + alert("Error checking device HMS/GMS") + } + ); + }); + + return (result == true); + } + + private getHMSLocation(callBack: Function) { + try { + + console.log("Huawei Location [Getting]"); + cordova.plugins.CordovaHMSLocationPlugin.requestLocation( + "index.js", + (_res) => { + console.log("Huawei Location Success: [Stop Getting]"); + cordova.plugins.CordovaHMSLocationPlugin.removeLocation("", (_res) => { }, (_err) => { }); + var location = _res.split(',') + if (location.length == 2) { + var lat = Number(_res.split(',')[0]); + var long = Number(_res.split(',')[1]); + callBack({"latitude":lat, "longitude":long, "isfake":false}) + } else { + alert("Invalid Huawei location from plugin."); + } + }, + (_err) => { + console.log("Huawei Location [Getting Error]: " + + JSON.stringify(_err)); + alert("Error while getting Huawei location"); + } + ); + + } catch (_err) { + console.log("Huawei Location Plugin [Error]: " + + JSON.stringify(_err)); + alert("Error while initializing Huawei Location Plugin"); + } + } + + private getGMSLocation(callBack: Function) { + this.backgroundGeolocation.getCurrentLocation({ timeout: 10000, enableHighAccuracy: true, maximumAge: 3000 }).then((resp) => { + if (resp && (resp.latitude && resp.longitude)) { + var isFakeLocation = resp.isFromMockProvider || resp.mockLocationsEnabled; + var lat = resp.latitude; + var long = resp.longitude; + callBack({"latitude":lat, "longitude":long, "isfake":isFakeLocation}) + } else { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + } + }, (error) => { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + }); + } + + private getIOSLocation(callBack: Function) { + this.geolocation.getCurrentPosition({ maximumAge: 3000, timeout: 10000, enableHighAccuracy: true }).then(resp => { + if (resp && resp.coords.latitude && resp.coords.longitude) { + var lat = resp.coords.latitude; + var long = resp.coords.longitude; + callBack({"latitude":lat, "longitude":long, "isfake":false}) + } else { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + } + }).catch(error => { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + }); + } + + +} \ No newline at end of file