Merge branch 'ZohaibIqbalKambrani' into 'master'

Geofence Android Finished

See merge request Cloud_Solution/diplomatic-quarter!201
merge-update-with-lab-changes
Mohammad Aljammal 5 years ago
commit f87bcfe064

@ -26,6 +26,9 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!-- Detect Reboot Permission -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
@ -73,8 +76,13 @@
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<!-- Geofencing -->
<receiver android:name=".geofence.GeofenceBroadcastReceiver" android:enabled="true" android:exported="true" />
<service android:name=".geofence.GeofenceTransitionsJobIntentService" android:exported="true" android:permission="android.permission.BIND_JOB_SERVICE" />
<receiver android:name=".geofence.GeofenceBroadcastReceiver" android:enabled="true" android:exported="true" />
<receiver android:name=".geofence.GeofencingRebootBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCmevVlr2Bh-c8W1VUzo8gt8JRY7n5PANw"/>

@ -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

@ -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

@ -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)
}
}
}
}

@ -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<GeoZoneModel>){
if (geoZones.isEmpty())
return
fun buildGeofencingRequest(geofences: List<Geofence>): 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<String>, failed: List<String>){
fun saveActiveGeofence(success: List<String>, failed: List<String>){
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<String>) -> Unit, failure: ((failed: List<String>) -> 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<Map<String?, Any?>?>() {}.type
return gson.fromJson<Map<String?, Any?>?>(profileJson,type)
?.get("PatientID")

@ -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<GeoZoneModel>{
val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE)
val json = pref.getString(PREF_KEY_HMG_ZONES,"[]")
val geoZones = GeoZoneModel().listFrom(json)
return geoZones
}
}
}

@ -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;

@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
@ -147,20 +148,6 @@ class _LandingPageState extends State<LandingPage> with WidgetsBindingObserver {
// Flip Permission Checks [Zohaib Kambrani]
requestPermissions().then((results) {
if (results[Permission.location].isGranted) {
debugPrint("Fetching GEO ZONES from HMG service...");
locator<GeofencingServices>().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<LandingPage> 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<LandingPage> 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<LandingPage> with WidgetsBindingObserver {
}
void setUserValues(value) async {
sharedPref.setObject(IMEI_USER_DATA, value);
if (value != null) sharedPref.setObject(IMEI_USER_DATA, value);
}
Future<dynamic> getUserInformation() async {
var userInfoJson = sharedPref.getObject(IMEI_USER_DATA);
return userInfoJson;
}
checkValue(projectProvider) {
@ -485,4 +481,26 @@ class _LandingPageState extends State<LandingPage> with WidgetsBindingObserver {
_changeCurrentTab(2);
}
}
registerGeofences() async {
await locator<GeofencingServices>().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) {});
}
}

Loading…
Cancel
Save