diff --git a/android/build.gradle b/android/build.gradle index 67749d1f..4f4f3a6c 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -2,14 +2,14 @@ group 'com.builttoroam.devicecalendar' version '1.0-SNAPSHOT' buildscript { - ext.kotlin_version = '1.3.41' + ext.kotlin_version = '1.3.50' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.4.2' + classpath 'com.android.tools.build:gradle:4.1.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } @@ -25,7 +25,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 29 + compileSdkVersion 30 sourceSets { main.java.srcDirs += 'src/main/kotlin' diff --git a/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt b/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt index bbe4bed2..e3dc8b24 100644 --- a/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt +++ b/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt @@ -68,9 +68,9 @@ import com.builttoroam.devicecalendar.models.* import com.builttoroam.devicecalendar.models.Calendar import com.google.gson.Gson import com.google.gson.GsonBuilder +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.PluginRegistry -import io.flutter.plugin.common.PluginRegistry.Registrar import kotlinx.coroutines.* import org.dmfs.rfc5545.DateTime import org.dmfs.rfc5545.Weekday @@ -92,14 +92,14 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { private val BYSETPOS_PART = "BYSETPOS" private val _cachedParametersMap: MutableMap = mutableMapOf() - private var _registrar: Registrar? = null + private var _binding: ActivityPluginBinding? = null private var _context: Context? = null private var _gson: Gson? = null private val uiThreadHandler = Handler(Looper.getMainLooper()) - constructor(registrar: Registrar?, context: Context) { - _registrar = registrar + constructor(binding: ActivityPluginBinding?, context: Context) { + _binding = binding _context = context val gsonBuilder = GsonBuilder() gsonBuilder.registerTypeAdapter(RecurrenceFrequency::class.java, RecurrenceFrequencySerializer()) @@ -652,8 +652,8 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { private fun arePermissionsGranted(): Boolean { if (atLeastAPI(23)) { - val writeCalendarPermissionGranted = _registrar!!.context().checkSelfPermission(Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED - val readCalendarPermissionGranted = _registrar!!.context().checkSelfPermission(Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED + val writeCalendarPermissionGranted = _binding!!.activity.checkSelfPermission(Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED + val readCalendarPermissionGranted = _binding!!.activity.checkSelfPermission(Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED return writeCalendarPermissionGranted && readCalendarPermissionGranted } @@ -667,7 +667,7 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { private fun requestPermissions(requestCode: Int) { if (atLeastAPI(23)) { - _registrar!!.activity().requestPermissions(arrayOf(Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR), requestCode) + _binding!!.activity.requestPermissions(arrayOf(Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR), requestCode) } } diff --git a/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt b/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt index d49ece49..a2cdba83 100644 --- a/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt +++ b/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt @@ -1,19 +1,32 @@ package com.builttoroam.devicecalendar +import android.app.Activity import android.content.Context +import androidx.annotation.NonNull import com.builttoroam.devicecalendar.common.Constants import com.builttoroam.devicecalendar.common.DayOfWeek import com.builttoroam.devicecalendar.common.RecurrenceFrequency import com.builttoroam.devicecalendar.models.* +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 const val CHANNEL_NAME = "plugins.builttoroam.com/device_calendar" -class DeviceCalendarPlugin() : MethodCallHandler { +class DeviceCalendarPlugin() : FlutterPlugin, MethodCallHandler, ActivityAware { + + /// 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 context: Context? = null + private var activity: Activity? = null + // Methods private val REQUEST_PERMISSIONS_METHOD = "requestPermissions" private val HAS_PERMISSIONS_METHOD = "hasPermissions" @@ -60,28 +73,36 @@ class DeviceCalendarPlugin() : MethodCallHandler { private val LOCAL_ACCOUNT_NAME_ARGUMENT = "localAccountName" private val EVENT_AVAILABILITY_ARGUMENT = "availability" - - private lateinit var _registrar: Registrar private lateinit var _calendarDelegate: CalendarDelegate - private constructor(registrar: Registrar, calendarDelegate: CalendarDelegate) : this() { - _registrar = registrar - _calendarDelegate = calendarDelegate + override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { + context = flutterPluginBinding.applicationContext + channel = MethodChannel(flutterPluginBinding.binaryMessenger, CHANNEL_NAME) + channel.setMethodCallHandler(this) } - companion object { - @JvmStatic - fun registerWith(registrar: Registrar) { - val context: Context = registrar.context() + override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { + channel.setMethodCallHandler(null) + } - val calendarDelegate = CalendarDelegate(registrar, context) - val instance = DeviceCalendarPlugin(registrar, calendarDelegate) + override fun onAttachedToActivity(binding: ActivityPluginBinding) { + activity = binding.activity + _calendarDelegate = CalendarDelegate(binding, context!!) + binding.addRequestPermissionsResultListener(_calendarDelegate) + } - val calendarsChannel = MethodChannel(registrar.messenger(), CHANNEL_NAME) - calendarsChannel.setMethodCallHandler(instance) + override fun onDetachedFromActivityForConfigChanges() { + activity = null + } - registrar.addRequestPermissionsResultListener(calendarDelegate) - } + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { + activity = binding.activity + _calendarDelegate = CalendarDelegate(binding, context!!) + binding.addRequestPermissionsResultListener(_calendarDelegate) + } + + override fun onDetachedFromActivity() { + activity = null } override fun onMethodCall(call: MethodCall, result: Result) {