From d8996cf87905a7ba9e1595b3ebcced38b6c9372e Mon Sep 17 00:00:00 2001 From: Zohaib Kambrani Date: Thu, 17 Dec 2020 12:38:59 +0300 Subject: [PATCH] Geofence Android Finished --- android/app/src/main/AndroidManifest.xml | 10 +++- .../geofence/GeofenceBroadcastReceiver.kt | 30 +---------- .../geofence/GeofenceErrorMessages.kt | 30 +---------- .../GeofencingRebootBroadcastReceiver.kt | 22 +++++--- .../geofence/HMG_Geofence.kt | 16 ++++-- .../diplomaticquarterapp/utils/HMGUtils.kt | 12 +++++ .../geofencing/GeofencingServices.dart | 1 + lib/pages/landing/landing_page.dart | 50 +++++++++++++------ 8 files changed, 85 insertions(+), 86 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 0b8b7998..1032b29b 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -26,6 +26,9 @@ + + + - + + + + + + diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceBroadcastReceiver.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceBroadcastReceiver.kt index e9accb04..8fc1faae 100644 --- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceBroadcastReceiver.kt +++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceBroadcastReceiver.kt @@ -1,32 +1,4 @@ -/* - * Copyright (c) 2018 Razeware LLC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, - * distribute, sublicense, create a derivative work, and/or sell copies of the - * Software in any work that is designed, intended, or marketed for pedagogical or - * instructional purposes related to programming, coding, application development, - * or information technology. Permission for such use, copying, modification, - * merger, publication, distribution, sublicensing, creation of derivative works, - * or sale is expressly withheld. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ + package com.cloud.diplomaticquarterapp.geofence diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceErrorMessages.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceErrorMessages.kt index e85c7ea3..4890f7cb 100755 --- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceErrorMessages.kt +++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceErrorMessages.kt @@ -1,32 +1,4 @@ -/* - * Copyright (c) 2018 Razeware LLC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, - * distribute, sublicense, create a derivative work, and/or sell copies of the - * Software in any work that is designed, intended, or marketed for pedagogical or - * instructional purposes related to programming, coding, application development, - * or information technology. Permission for such use, copying, modification, - * merger, publication, distribution, sublicensing, creation of derivative works, - * or sale is expressly withheld. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ + package com.cloud.diplomaticquarterapp.geofence diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofencingRebootBroadcastReceiver.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofencingRebootBroadcastReceiver.kt index 641e1522..08a0c93f 100644 --- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofencingRebootBroadcastReceiver.kt +++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofencingRebootBroadcastReceiver.kt @@ -1,21 +1,27 @@ -// Copyright 2019 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. -package io.flutter.plugins.geofencing + +package com.cloud.diplomaticquarterapp.geofence import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import android.util.Log +import android.os.Handler +import android.os.Message import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence +import com.cloud.diplomaticquarterapp.utils.HMGUtils -class GeofencingRebootBroadcastReceiver : BroadcastReceiver() { +class GeofencingRebootBroadcastReceiver : BroadcastReceiver(){ override fun onReceive(context: Context, intent: Intent) { - if (intent.action.equals("android.intent.action.BOOT_COMPLETED")) { + + if (Intent.ACTION_BOOT_COMPLETED.equals(intent.action)) { + val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE) + pref.edit().putString("REBOOT_DETECTED","YES").apply() + HMG_Geofence.shared(context).unRegisterAll { status, exception -> - HMG_Geofence.shared(context).re + val geoZones = HMGUtils.getGeoZonesFromPreference(context) + HMG_Geofence.shared(context).register(geoZones) } } } + } \ No newline at end of file diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt index fbef8749..4d2c48b3 100644 --- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt +++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt @@ -36,6 +36,7 @@ enum class GeofenceTransition(val value: Int) { const val PREFS_STORAGE = "FlutterSharedPreferences" const val PREF_KEY_SUCCESS = "HMG_GEOFENCE_SUCCESS" const val PREF_KEY_FAILED = "HMG_GEOFENCE_FAILED" +const val PREF_KEY_HMG_ZONES = "flutter.hmg-geo-fences" class HMG_Geofence { // https://developer.android.com/training/location/geofencing#java @@ -69,6 +70,8 @@ class HMG_Geofence { } fun register(geoZones: List){ + if (geoZones.isEmpty()) + return fun buildGeofencingRequest(geofences: List): GeofencingRequest { return GeofencingRequest.Builder() @@ -92,7 +95,7 @@ class HMG_Geofence { geofencingClient .addGeofences(buildGeofencingRequest(geofences), geofencePendingIntent) .addOnSuccessListener { - saveGeofenceResults(geofences.map { it.requestId }, listOf()) + saveActiveGeofence(geofences.map { it.requestId }, listOf()) } .addOnFailureListener { print(it.localizedMessage) @@ -103,6 +106,8 @@ class HMG_Geofence { fun unRegisterAll(completion: (status: Boolean, exception:Exception?) -> Unit){ getActiveGeofences({ success -> + val mList = success.toMutableList() + mList.add("12345") geofencingClient .removeGeofences(success) .addOnSuccessListener { @@ -111,17 +116,22 @@ class HMG_Geofence { .addOnFailureListener { completion(false, it) } + removeActiveGeofences() }, { failed -> // Nothing to do with failed geofences. }) } - fun saveGeofenceResults(success: List, failed: List){ + fun saveActiveGeofence(success: List, failed: List){ val jsonSuccess = gson.toJson(success) val jsonFailure = gson.toJson(failed) preferences.edit().putString(PREF_KEY_SUCCESS, jsonSuccess).apply() preferences.edit().putString(PREF_KEY_FAILED, jsonFailure).apply() + } + fun removeActiveGeofences(){ + preferences.edit().putString(PREF_KEY_SUCCESS,"[]").apply() + preferences.edit().putString(PREF_KEY_FAILED,"[]").apply() } fun getActiveGeofences(success: (success: List) -> Unit, failure: ((failed: List) -> Unit)?){ @@ -144,7 +154,7 @@ class HMG_Geofence { } fun getPatientID():Int?{ - val profileJson = preferences.getString("flutter.user-profile", "{}") + val profileJson = preferences.getString("flutter.imei-user-data", "{}") val type = object : TypeToken?>() {}.type return gson.fromJson?>(profileJson,type) ?.get("PatientID") diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/utils/HMGUtils.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/utils/HMGUtils.kt index 5b0201f2..bebd0101 100644 --- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/utils/HMGUtils.kt +++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/utils/HMGUtils.kt @@ -13,8 +13,12 @@ import androidx.core.app.TaskStackBuilder import com.cloud.diplomaticquarterapp.BuildConfig import com.cloud.diplomaticquarterapp.MainActivity import com.cloud.diplomaticquarterapp.R +import com.cloud.diplomaticquarterapp.geofence.GeoZoneModel +import com.cloud.diplomaticquarterapp.geofence.PREFS_STORAGE +import com.cloud.diplomaticquarterapp.geofence.PREF_KEY_HMG_ZONES import com.github.kittinunf.fuel.core.extensions.jsonBody import com.github.kittinunf.fuel.httpPost +import com.google.android.gms.location.Geofence import com.google.gson.Gson import com.google.gson.reflect.TypeToken import io.flutter.plugin.common.MethodChannel @@ -64,6 +68,14 @@ class HMGUtils { } } + fun getGeoZonesFromPreference(context: Context):List{ + val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE) + val json = pref.getString(PREF_KEY_HMG_ZONES,"[]") + + val geoZones = GeoZoneModel().listFrom(json) + return geoZones + } + } } diff --git a/lib/core/service/geofencing/GeofencingServices.dart b/lib/core/service/geofencing/GeofencingServices.dart index e8308480..56b4f56e 100644 --- a/lib/core/service/geofencing/GeofencingServices.dart +++ b/lib/core/service/geofencing/GeofencingServices.dart @@ -38,6 +38,7 @@ class GeofencingServices extends BaseService { AppSharedPreferences pref = AppSharedPreferences(); await pref.setString(HMG_GEOFENCES, _zonesJsonString); + debugPrint("Finished Fetching GEO ZONES from HMG service..."); debugPrint("GEO ZONES saved to AppPreferences with key '$HMG_GEOFENCES'"); return geoZones; diff --git a/lib/pages/landing/landing_page.dart b/lib/pages/landing/landing_page.dart index b8c88eca..acbf8d78 100644 --- a/lib/pages/landing/landing_page.dart +++ b/lib/pages/landing/landing_page.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; @@ -147,20 +148,6 @@ class _LandingPageState extends State with WidgetsBindingObserver { // Flip Permission Checks [Zohaib Kambrani] requestPermissions().then((results) { - if (results[Permission.location].isGranted) { - debugPrint("Fetching GEO ZONES from HMG service..."); - locator().getAllGeoZones(GeoZonesRequestModel()).then((geoZones) { - debugPrint("Finished Fetching GEO ZONES from HMG service..."); - debugPrint("GEO ZONES saved to AppPreferences with key '$HMG_GEOFENCES'"); - authProvider.authenticatedUserObject.getUser().then((value) { - debugPrint(value.toString()); - }); - if (authProvider.authenticatedUserObject.user != null) { - projectViewModel.platformBridge().registerHmgGeofences(); - } - }); - } - if (results[Permission.notification].isGranted) _firebaseMessaging.getToken().then((String token) { sharedPref.setString(PUSH_TOKEN, token); @@ -170,6 +157,7 @@ class _LandingPageState extends State with WidgetsBindingObserver { } }); + if (results[Permission.location].isGranted) ; if (results[Permission.storage].isGranted) ; if (results[Permission.camera].isGranted) ; if (results[Permission.photos].isGranted) ; @@ -447,7 +435,10 @@ class _LandingPageState extends State with WidgetsBindingObserver { } void checkUserStatus(token) async { - authService.selectDeviceImei(token).then((SelectDeviceIMEIRES value) => setUserValues(value)); + var result = await authService.selectDeviceImei(token); + setUserValues(result); + registerGeofences(); + if (await sharedPref.getObject(USER_PROFILE) != null) { var data = AuthenticatedUser.fromJson(await sharedPref.getObject(USER_PROFILE)); if (data != null) { @@ -477,7 +468,12 @@ class _LandingPageState extends State with WidgetsBindingObserver { } void setUserValues(value) async { - sharedPref.setObject(IMEI_USER_DATA, value); + if (value != null) sharedPref.setObject(IMEI_USER_DATA, value); + } + + Future getUserInformation() async { + var userInfoJson = sharedPref.getObject(IMEI_USER_DATA); + return userInfoJson; } checkValue(projectProvider) { @@ -485,4 +481,26 @@ class _LandingPageState extends State with WidgetsBindingObserver { _changeCurrentTab(2); } } + + registerGeofences() async { + await locator().getAllGeoZones(GeoZonesRequestModel()); + + void doIt() { + getUserInformation().then((value) { + if (value != null) projectViewModel.platformBridge().registerHmgGeofences(); + }); + } + + if (await Permission.location.isGranted) { + doIt(); + } else { + [Permission.location].request().then((value) async { + if (await Permission.location.isGranted) { + doIt(); + } + }); + } + + requestPermissions().then((results) {}); + } }