mirror of
https://github.com/flutter/samples.git
synced 2026-03-27 23:01:59 +00:00
[federated_plugin] adds platform interface implementation and plugin implementation for Android (#503)
This commit is contained in:
@@ -40,4 +40,5 @@ android {
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
implementation 'com.google.android.gms:play-services-location:17.0.0'
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="dev.flutter.federated_plugin">
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
</manifest>
|
||||
|
||||
@@ -4,37 +4,109 @@
|
||||
|
||||
package dev.flutter.federated_plugin
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.annotation.NonNull
|
||||
import androidx.core.app.ActivityCompat.requestPermissions
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.google.android.gms.location.LocationServices.getFusedLocationProviderClient
|
||||
|
||||
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.Registrar
|
||||
import io.flutter.plugin.common.PluginRegistry
|
||||
|
||||
/** FederatedPlugin */
|
||||
public class FederatedPlugin: FlutterPlugin, MethodCallHandler {
|
||||
/// 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
|
||||
|
||||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "federated_plugin")
|
||||
channel.setMethodCallHandler(this);
|
||||
}
|
||||
class FederatedPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.RequestPermissionsResultListener {
|
||||
private lateinit var channel: MethodChannel
|
||||
private lateinit var context: Context
|
||||
private lateinit var activity: Activity
|
||||
private lateinit var result: Result
|
||||
private val REQUEST_CODE = 1001
|
||||
|
||||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
|
||||
if (call.method == "getPlatformVersion") {
|
||||
result.success("Android ${android.os.Build.VERSION.RELEASE}")
|
||||
} else {
|
||||
result.notImplemented()
|
||||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "location")
|
||||
channel.setMethodCallHandler(this)
|
||||
context = flutterPluginBinding.applicationContext
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel.setMethodCallHandler(null)
|
||||
}
|
||||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
|
||||
this.result = result
|
||||
if (call.method == "getLocation") {
|
||||
// Check for the runtime permission if SDK version is greater than 23. If permissions
|
||||
// are granted, send the location data back to Dart. If permissions are not granted,
|
||||
// request for the runtime permissions.
|
||||
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP && !checkPermissions()) {
|
||||
requestPermissions(activity, arrayOf(
|
||||
Manifest.permission.ACCESS_FINE_LOCATION,
|
||||
Manifest.permission.ACCESS_COARSE_LOCATION
|
||||
), REQUEST_CODE)
|
||||
} else {
|
||||
provideLocation()
|
||||
}
|
||||
} else {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
|
||||
// Method to fetch and send the last known location of the device to Dart.
|
||||
private fun provideLocation() {
|
||||
getFusedLocationProviderClient(context).lastLocation
|
||||
.addOnSuccessListener { location ->
|
||||
if (location != null) {
|
||||
result.success(listOf(location.longitude, location.latitude))
|
||||
} else {
|
||||
result.error("NOT_DETERMINED", "Not able to determine location", null)
|
||||
}
|
||||
}.addOnFailureListener { exception ->
|
||||
result.error("Error", exception.message, null)
|
||||
}
|
||||
}
|
||||
|
||||
// Method to check permissions to access the location data.
|
||||
private fun checkPermissions(): Boolean {
|
||||
val fineLocationPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
val coarseLocationPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
|
||||
|
||||
return fineLocationPermission == PackageManager.PERMISSION_GRANTED &&
|
||||
coarseLocationPermission == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
|
||||
|
||||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel.setMethodCallHandler(null)
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivity() {}
|
||||
|
||||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
|
||||
activity = binding.activity
|
||||
}
|
||||
|
||||
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
|
||||
activity = binding.activity
|
||||
binding.addRequestPermissionsResultListener(this)
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivityForConfigChanges() {}
|
||||
|
||||
// Callback for the result after requesting for runtime permissions. If permissions
|
||||
// are granted, send the location data, or send an error back to Dart if permissions
|
||||
// are not granted.
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>?, grantResults: IntArray): Boolean {
|
||||
if (requestCode == REQUEST_CODE && grantResults.isNotEmpty()) {
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
|
||||
provideLocation()
|
||||
} else {
|
||||
result.error("PERMISSION_DENIED", "Permission denied from User", null)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user