diff --git a/android/app/build.gradle b/android/app/build.gradle index 2d91d1ba..a6fca5fa 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -243,5 +243,6 @@ task copyDownloadableDepsToLibs(type: Copy) { apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project) apply plugin: 'com.google.gms.google-services' +apply plugin: 'com.huawei.agconnect' diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 9adb0499..676fa2f2 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -49,5 +49,18 @@ + + + + + + + + + + diff --git a/android/build.gradle b/android/build.gradle index d751cf2e..385f5921 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -20,10 +20,14 @@ buildscript { jcenter() google() maven { url 'https://jitpack.io' } + maven { url 'https://developer.huawei.com/repo/' } + } dependencies { classpath 'com.android.tools.build:gradle:3.4.2' classpath('com.google.gms:google-services:4.3.3') + classpath 'com.huawei.agconnect:agcp:1.2.0.300' + // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } @@ -50,6 +54,8 @@ allprojects { // react-native-background-fetch url("${project(':react-native-background-fetch').projectDir}/libs") } + maven { url 'https://developer.huawei.com/repo/' } + } } subprojects { diff --git a/package.json b/package.json index a8807bf2..acb9476d 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@emotion/core": "^10.0.28", "@emotion/native": "^10.0.27", "@react-hook/throttle": "^1.0.12", + "@hmscore/react-native-hms-location": "^5.0.4-300", "@react-native-community/async-storage": "^1.6.1", "@react-native-community/clipboard": "^1.1.0", "@react-native-community/image-editor": "^2.3.0", diff --git a/src/services/background-tracking-hms.ts b/src/services/background-tracking-hms.ts new file mode 100644 index 00000000..a2a1000b --- /dev/null +++ b/src/services/background-tracking-hms.ts @@ -0,0 +1,100 @@ +import BackgroundGeolocation from 'react-native-background-geolocation' +import { getAnonymousHeaders } from '../api' +import { Platform } from 'react-native' +import { API_URL } from '../config' +import I18n from '../../i18n/i18n' +import DeviceInfo from 'react-native-device-info' +import HMSLocation from "@hmscore/react-native-hms-location"; + +class BackgroundTrackingHms { + +private locationHmsRequest:HMSLocation.LocationRequest = { + priority: HMSLocation.FusedLocation.PriorityConstants.PRIORITY_BALANCED_POWER_ACCURACY, + // interval: 5*60*1000, + interval: 1000, + numUpdates: 2147483647, + fastestInterval: 600000.0, + expirationTime: 9223372036854775807.0, + expirationTimeDuration: 10000.0, + smallestDisplacement: 0.0, + maxWaitTime: 0.0, + needAddress: false, + language: "", + countryCode: "", +}; + +private locationHms: HMSLocation.Location + +private locationSettingsRequest = { + locationRequests: [this.locationHmsRequest], + alwaysShow: false, + needBle: false, +}; + +private handleLocationUpdate = location => { + var locationResult:HMSLocation.LocationResult = location + this.locationHms = locationResult + console.log("Retrieved HMS Location Lat:"+locationResult.lastLocation.latitude); + console.log("Retrieved HMS Location Lat:"+locationResult.lastLocation.longitude); + +}; + + + setup(startImmediately?: boolean) { + if (startImmediately) { + this.start() + } + } + + start() { + if (!this.canUseGeoLocation) { + return Promise.resolve() + } + HMSLocation.LocationKit.Native.init() + .then(() => { + console.log("HMS,Done loading") + this.startHMSLocation() + }) + .catch((err: Error) => console.log('HMS,Fused Location Headless Task, data:', err.message)) + } + + private startHMSLocation(){ + HMSLocation.FusedLocation.Native.requestLocationUpdates(10, this.locationHmsRequest) + .then(({ requestCode }) => console.log("HMS Location RequestCode"+requestCode)) + .catch(err => console.log("Exception while requestLocationUpdates " + err)) + + HMSLocation.FusedLocation.Events.addFusedLocationEventListener( + this.handleLocationUpdate, + ); + } + + + stop() { + if (!this.canUseGeoLocation) { + return Promise.resolve() + } + HMSLocation.FusedLocation.Events.removeFusedLocationEventListener( + this.handleLocationUpdate, + ); + } + + destroyLocations() { + if (!this.canUseGeoLocation) { + return Promise.resolve() + } + } + + getLocation(extras: any = {}) { + if (!this.canUseGeoLocation) { + return Promise.resolve({ ...extras }) + } + return this.locationHms + } + + get canUseGeoLocation() { + const hasHMS = DeviceInfo.hasHmsSync() + return Platform.OS !== 'ios' && hasHMS + } +} + +export const backgroundTrackingHms = new BackgroundTrackingHms() diff --git a/yarn.lock b/yarn.lock index 5e64a692..41c59a9a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1180,6 +1180,11 @@ dependencies: "@hapi/hoek" "^8.3.0" +"@hmscore/react-native-hms-location@^5.0.4-300": + version "5.0.4-300" + resolved "https://registry.yarnpkg.com/@hmscore/react-native-hms-location/-/react-native-hms-location-5.0.4-300.tgz#73948ec1621cc75e40ec556ca704d9743ba10048" + integrity sha512-FM6HGqM88XpssOXb+XtvGbjo9ihIx2rQ2l9V1i+OOiKBtJpXBq98MzT6kbmqAcq5+pw8pnttwvpxqjgmZgGv6w== + "@istanbuljs/load-nyc-config@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"