From 71e964a1b9eaf31023f6b9b235da24eed7d4fcaa Mon Sep 17 00:00:00 2001 From: Rohit Mishra Date: Tue, 4 Feb 2020 11:09:18 +0530 Subject: [PATCH 1/5] added compatibility with new flutter plugin api version V2 --- .../devicecalendar/CalendarDelegate.kt | 38 +++++-------------- .../devicecalendar/DeviceCalendarPlugin.kt | 4 +- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt index 00017472..b7796751 100644 --- a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt +++ b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt @@ -12,6 +12,7 @@ import android.database.Cursor import android.net.Uri import android.provider.CalendarContract import android.provider.CalendarContract.Events +import io.flutter.plugin.common.PluginRegistry.Registrar import com.builttoroam.devicecalendar.common.Constants.Companion.ATTENDEE_EMAIL_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.ATTENDEE_NAME_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.ATTENDEE_PROJECTION @@ -19,11 +20,9 @@ import com.builttoroam.devicecalendar.common.Constants.Companion.ATTENDEE_RELATI import com.builttoroam.devicecalendar.common.Constants.Companion.ATTENDEE_STATUS_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.ATTENDEE_TYPE_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION -import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_OLDER_API import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_ACCESS_LEVEL_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_DISPLAY_NAME_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_ID_INDEX -import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_IS_PRIMARY_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.EVENT_PROJECTION import com.builttoroam.devicecalendar.common.Constants.Companion.EVENT_PROJECTION_ALL_DAY_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.EVENT_PROJECTION_BEGIN_INDEX @@ -73,12 +72,12 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { private val BYSETPOS_PART = "BYSETPOS" private val _cachedParametersMap: MutableMap = mutableMapOf() - private var _activity: Activity? = null + private var _registrar: Registrar? = null private var _context: Context? = null private var _gson: Gson? = null - constructor(activity: Activity?, context: Context) { - _activity = activity + constructor(activity: Registrar?, context: Context) { + _registrar = activity _context = context val gsonBuilder = GsonBuilder() gsonBuilder.registerTypeAdapter(RecurrenceFrequency::class.java, RecurrenceFrequencySerializer()) @@ -151,12 +150,7 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { if (arePermissionsGranted()) { val contentResolver: ContentResolver? = _context?.contentResolver val uri: Uri = CalendarContract.Calendars.CONTENT_URI - val cursor: Cursor? = if (atLeastAPI(17)) { - contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null) - } else { - contentResolver?.query(uri, CALENDAR_PROJECTION_OLDER_API, null, null, null) - } - + val cursor: Cursor? = contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null) val calendars: MutableList = mutableListOf() try { while (cursor?.moveToNext() == true) { @@ -188,11 +182,7 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { val contentResolver: ContentResolver? = _context?.contentResolver val uri: Uri = CalendarContract.Calendars.CONTENT_URI - val cursor: Cursor? = if (atLeastAPI(17)) { - contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null) - } else { - contentResolver?.query(uri, CALENDAR_PROJECTION_OLDER_API, null, null, null) - } + val cursor: Cursor? = contentResolver?.query(ContentUris.withAppendedId(uri, calendarIdNumber), CALENDAR_PROJECTION, null, null, null) try { if (cursor?.moveToFirst() == true) { @@ -459,8 +449,8 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { private fun arePermissionsGranted(): Boolean { if (atLeastAPI(23)) { - val writeCalendarPermissionGranted = _activity?.checkSelfPermission(Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED - val readCalendarPermissionGranted = _activity?.checkSelfPermission(Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED + val writeCalendarPermissionGranted = _registrar!!.activity().checkSelfPermission(Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED + val readCalendarPermissionGranted = _registrar!!.activity().checkSelfPermission(Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED return writeCalendarPermissionGranted && readCalendarPermissionGranted } @@ -474,7 +464,7 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { private fun requestPermissions(requestCode: Int) { if (atLeastAPI(23)) { - _activity?.requestPermissions(arrayOf(Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR), requestCode) + _registrar!!.activity().requestPermissions(arrayOf(Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR), requestCode) } } @@ -490,14 +480,6 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { val calendar = Calendar(calId.toString(), displayName) calendar.isReadOnly = isCalendarReadOnly(accessLevel) - if (atLeastAPI(17)) { - val isPrimary = cursor.getString(CALENDAR_PROJECTION_IS_PRIMARY_INDEX) - calendar.isDefault = isPrimary == "1" - } - else { - calendar.isDefault = false - } - return calendar } @@ -517,7 +499,7 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { var url = cursor.getString(EVENT_PROJECTION_CUSTOM_APP_URI_INDEX) val event = Event() - event.title = title ?: "New Event" + event.title = title event.eventId = eventId.toString() event.calendarId = calendarId event.description = description diff --git a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt index 349de27f..8bd66a8f 100644 --- a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt +++ b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt @@ -65,9 +65,9 @@ class DeviceCalendarPlugin() : MethodCallHandler { @JvmStatic fun registerWith(registrar: Registrar) { val context: Context = registrar.context() - val activity: Activity? = registrar.activity() + //val activity: Activity? = registrar.activity() - val calendarDelegate = CalendarDelegate(activity, context) + val calendarDelegate = CalendarDelegate(registrar, context) val instance = DeviceCalendarPlugin(registrar, calendarDelegate) val calendarsChannel = MethodChannel(registrar.messenger(), CHANNEL_NAME) From af7570b4037e5746f91bae379d6ef01cc7679f38 Mon Sep 17 00:00:00 2001 From: Rohit Mishra Date: Wed, 5 Feb 2020 11:26:21 +0530 Subject: [PATCH 2/5] reverted condition for IS_PRIMARY for calendars in Android devices with API 16 or lower --- .../com/builttoroam/devicecalendar/CalendarDelegate.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt index b7796751..d7dc5731 100644 --- a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt +++ b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt @@ -150,6 +150,11 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { if (arePermissionsGranted()) { val contentResolver: ContentResolver? = _context?.contentResolver val uri: Uri = CalendarContract.Calendars.CONTENT_URI + val cursor: Cursor? = if (atLeastAPI(17)) { + contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null) + } else { + contentResolver?.query(uri, CALENDAR_PROJECTION_OLDER_API, null, null, null) + } val cursor: Cursor? = contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null) val calendars: MutableList = mutableListOf() try { From c166272564ea614e30de728c60ffb1c544d0a9d3 Mon Sep 17 00:00:00 2001 From: Rohit Mishra Date: Wed, 5 Feb 2020 11:31:06 +0530 Subject: [PATCH 3/5] reverted condition for IS_PRIMARY for calendars in Android devices with API 16 or lower --- .../com/builttoroam/devicecalendar/CalendarDelegate.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt index d7dc5731..e1bef302 100644 --- a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt +++ b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt @@ -484,7 +484,13 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { val calendar = Calendar(calId.toString(), displayName) calendar.isReadOnly = isCalendarReadOnly(accessLevel) - + if (atLeastAPI(17)) { + val isPrimary = cursor.getString(CALENDAR_PROJECTION_IS_PRIMARY_INDEX) + calendar.isDefault = isPrimary == "1" + } + else { + calendar.isDefault = false + } return calendar } @@ -504,7 +510,7 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { var url = cursor.getString(EVENT_PROJECTION_CUSTOM_APP_URI_INDEX) val event = Event() - event.title = title + event.title = title ?: "New Event" event.eventId = eventId.toString() event.calendarId = calendarId event.description = description From a39c06d49cc0b1f64343684dc466726278f7c609 Mon Sep 17 00:00:00 2001 From: Rohit Mishra Date: Wed, 5 Feb 2020 11:34:48 +0530 Subject: [PATCH 4/5] reverted condition for IS_PRIMARY for calendars in Android devices with API 16 or lower --- .../com/builttoroam/devicecalendar/CalendarDelegate.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt index e1bef302..c8d891f1 100644 --- a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt +++ b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt @@ -23,6 +23,8 @@ import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJEC import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_ACCESS_LEVEL_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_DISPLAY_NAME_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_ID_INDEX +import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_IS_PRIMARY_INDEX +import com.builttoroam.devicecalendar.common.Constants.Companion.CALENDAR_PROJECTION_OLDER_API import com.builttoroam.devicecalendar.common.Constants.Companion.EVENT_PROJECTION import com.builttoroam.devicecalendar.common.Constants.Companion.EVENT_PROJECTION_ALL_DAY_INDEX import com.builttoroam.devicecalendar.common.Constants.Companion.EVENT_PROJECTION_BEGIN_INDEX @@ -188,6 +190,12 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { val contentResolver: ContentResolver? = _context?.contentResolver val uri: Uri = CalendarContract.Calendars.CONTENT_URI val cursor: Cursor? = contentResolver?.query(ContentUris.withAppendedId(uri, calendarIdNumber), CALENDAR_PROJECTION, null, null, null) + + val cursor: Cursor? = if (atLeastAPI(17)) { + contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null) + } else { + contentResolver?.query(uri, CALENDAR_PROJECTION_OLDER_API, null, null, null) + } try { if (cursor?.moveToFirst() == true) { From 1c24b437c38ea002a5725ea304e830dffff98e70 Mon Sep 17 00:00:00 2001 From: Rohit Mishra Date: Wed, 5 Feb 2020 12:51:00 +0530 Subject: [PATCH 5/5] reverted condition for IS_PRIMARY for calendars in Android devices with API 16 or lower --- .../kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt index c8d891f1..ec037f02 100644 --- a/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt +++ b/device_calendar/android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt @@ -157,7 +157,6 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { } else { contentResolver?.query(uri, CALENDAR_PROJECTION_OLDER_API, null, null, null) } - val cursor: Cursor? = contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null) val calendars: MutableList = mutableListOf() try { while (cursor?.moveToNext() == true) { @@ -189,7 +188,6 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener { val contentResolver: ContentResolver? = _context?.contentResolver val uri: Uri = CalendarContract.Calendars.CONTENT_URI - val cursor: Cursor? = contentResolver?.query(ContentUris.withAppendedId(uri, calendarIdNumber), CALENDAR_PROJECTION, null, null, null) val cursor: Cursor? = if (atLeastAPI(17)) { contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null)