forked from DwayneCoussement/flutter_geofence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Android embedding v2 only (DwayneCoussement#31)
* Android embedding v2 only * Cleanup
- Loading branch information
1 parent
3b2182b
commit 11c9b64
Showing
21 changed files
with
248 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/.idea | ||
/.packages | ||
/example/ios/Flutter/.last_build_id | ||
/flutter_geofence.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: f30b7f4db93ee747cd727df747941a28ead25ff5 | ||
channel: stable | ||
|
||
project_type: plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
org.gradle.jvmargs=-Xmx1536M | ||
android.enableR8=true | ||
android.useAndroidX=true | ||
android.enableJetifier=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
rootProject.name = 'geofence' | ||
rootProject.name = 'flutter_geofence' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.intivoto.geofence"> | ||
|
||
package="com.intivoto.flutter_geofence"> | ||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
</manifest> |
194 changes: 194 additions & 0 deletions
194
android/src/main/kotlin/com/intivoto/flutter_geofence/FlutterGeofencePlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package com.intivoto.flutter_geofence | ||
|
||
import android.Manifest | ||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import androidx.annotation.NonNull | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat | ||
|
||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import io.flutter.embedding.engine.plugins.activity.ActivityAware | ||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
import io.flutter.plugin.common.MethodChannel.Result | ||
import io.flutter.plugin.common.PluginRegistry | ||
|
||
/** FlutterGeofencePlugin */ | ||
class FlutterGeofencePlugin : FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.RequestPermissionsResultListener { | ||
/// The MethodChannel that will the communication between Flutter and native Android | ||
/// | ||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
/// when the Flutter Engine is detached from the Activity | ||
private lateinit var channel: MethodChannel | ||
private var geofenceManager: GeofenceManager? = null | ||
private var currentActivity: Activity? = null | ||
|
||
|
||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "geofence") | ||
channel.setMethodCallHandler(this) | ||
} | ||
|
||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||
if (call.method == "addRegion") { | ||
val arguments = call.arguments as? HashMap<*, *> | ||
if (arguments != null) { | ||
val region = safeLet(arguments["id"] as? String, | ||
arguments["radius"] as? Double, | ||
arguments["lat"] as? Double, | ||
arguments["lng"] as? Double, | ||
arguments["event"] as? String) | ||
{ id, radius, latitude, longitude, event -> | ||
GeoRegion( | ||
id, | ||
radius.toFloat(), | ||
latitude, | ||
longitude, | ||
events = when (event) { | ||
"GeolocationEvent.entry" -> listOf(GeoEvent.entry) | ||
"GeolocationEvent.exit" -> listOf(GeoEvent.exit) | ||
else -> GeoEvent.values().toList() | ||
}) | ||
} | ||
if (region != null) { | ||
geofenceManager?.startMonitoring(region) | ||
result.success(null) | ||
} else { | ||
result.error("Invalid arguments", "Has invalid arguments", "Has invalid arguments") | ||
} | ||
} else { | ||
result.error("Invalid arguments", "Has invalid arguments", "Has invalid arguments") | ||
} | ||
} else if (call.method == "removeRegion") { | ||
val arguments = call.arguments as? HashMap<*, *> | ||
if (arguments != null) { | ||
val region = safeLet(arguments["id"] as? String, | ||
arguments["radius"] as? Double, | ||
arguments["lat"] as? Double, | ||
arguments["lng"] as? Double, | ||
arguments["event"] as? String) | ||
{ id, radius, latitude, longitude, event -> | ||
GeoRegion( | ||
id, | ||
radius.toFloat(), | ||
latitude, | ||
longitude, | ||
events = when (event) { | ||
"GeolocationEvent.entry" -> listOf(GeoEvent.entry) | ||
"GeolocationEvent.exit" -> listOf(GeoEvent.exit) | ||
else -> GeoEvent.values().toList() | ||
}) | ||
} | ||
if (region != null) { | ||
geofenceManager?.stopMonitoring(region) | ||
result.success(null) | ||
} else { | ||
result.error("Invalid arguments", "Has invalid arguments", "Has invalid arguments") | ||
} | ||
} else { | ||
result.error("Invalid arguments", "Has invalid arguments", "Has invalid arguments") | ||
} | ||
} else if (call.method == "removeRegions") { | ||
geofenceManager?.stopMonitoringAllRegions() | ||
result.success(null) | ||
} else if (call.method == "getUserLocation") { | ||
geofenceManager?.getUserLocation() | ||
result.success(null) | ||
} else if (call.method == "requestPermissions") { | ||
requestPermissions() | ||
} else if (call.method == "startListeningForLocationChanges") { | ||
geofenceManager?.startListeningForLocationChanges() | ||
result.success(null) | ||
} else if (call.method == "stopListeningForLocationChanges") { | ||
geofenceManager?.stopListeningForLocationChanges() | ||
result.success(null) | ||
} else { | ||
result.notImplemented() | ||
} | ||
} | ||
|
||
private fun requestPermissions() { | ||
safeLet(currentActivity, currentActivity?.applicationContext) { activity, context -> | ||
checkPermissions(context, activity) | ||
} | ||
} | ||
|
||
@SuppressLint("InlinedApi") | ||
private fun checkPermissions(context: Context, activity: Activity) { | ||
// Here, thisActivity is the current activity | ||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_BACKGROUND_LOCATION) | ||
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(activity, | ||
Manifest.permission.ACCESS_COARSE_LOCATION) | ||
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, | ||
Manifest.permission.ACCESS_FINE_LOCATION) | ||
!= PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(activity, | ||
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION), | ||
999) | ||
} else { | ||
// Permission has already been granted | ||
startGeofencing(context) | ||
} | ||
} | ||
|
||
private fun startGeofencing(context: Context) { | ||
context.let { | ||
geofenceManager = GeofenceManager(it, { | ||
handleGeofenceEvent(it) | ||
}, { | ||
channel.invokeMethod("userLocationUpdated", hashMapOf("lat" to it.latitude, "lng" to it.longitude)) | ||
}, { | ||
channel.invokeMethod("backgroundLocationUpdated", hashMapOf("lat" to it.latitude, "lng" to it.longitude)) | ||
}) | ||
} | ||
} | ||
|
||
private fun handleGeofenceEvent(region: GeoRegion) { | ||
if (region.events.contains(GeoEvent.entry)) { | ||
channel.invokeMethod("entry", region.serialized()) | ||
} else { | ||
channel.invokeMethod("exit", region.serialized()) | ||
} | ||
} | ||
|
||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { | ||
channel.setMethodCallHandler(null) | ||
} | ||
|
||
override fun onAttachedToActivity(binding: ActivityPluginBinding) { | ||
currentActivity = binding.activity | ||
binding.addRequestPermissionsResultListener(this) | ||
} | ||
|
||
override fun onDetachedFromActivityForConfigChanges() { | ||
currentActivity = null | ||
} | ||
|
||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { | ||
currentActivity = binding.activity | ||
binding.addRequestPermissionsResultListener(this) | ||
} | ||
|
||
override fun onDetachedFromActivity() { | ||
currentActivity = null | ||
} | ||
|
||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>?, grantResults: IntArray?): Boolean = | ||
when (requestCode) { | ||
999 -> { | ||
if (grantResults != null && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
currentActivity?.let { | ||
startGeofencing(it.applicationContext) | ||
} | ||
true | ||
} else { | ||
false | ||
} | ||
} else -> false | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...oto/geofence/GeofenceBroadcastReceiver.kt → ...ter_geofence/GeofenceBroadcastReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../com/intivoto/geofence/GeofenceManager.kt → ...ivoto/flutter_geofence/GeofenceManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...n/kotlin/com/intivoto/geofence/SafeLet.kt → .../com/intivoto/flutter_geofence/SafeLet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.