Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added compatibility with new flutter plugin api version V2 #165

Merged
merged 5 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ 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
import com.builttoroam.devicecalendar.common.Constants.Companion.ATTENDEE_RELATIONSHIP_INDEX
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.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
Expand Down Expand Up @@ -73,12 +74,12 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener {
private val BYSETPOS_PART = "BYSETPOS"

private val _cachedParametersMap: MutableMap<Int, CalendarMethodsParametersCacheModel> = 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())
Expand Down Expand Up @@ -151,12 +152,12 @@ class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener {
if (arePermissionsGranted()) {
val contentResolver: ContentResolver? = _context?.contentResolver
val uri: Uri = CalendarContract.Calendars.CONTENT_URI
val cursor: Cursor? = if (atLeastAPI(17)) {
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)
bhl09 marked this conversation as resolved.
Show resolved Hide resolved
val calendars: MutableList<Calendar> = mutableListOf()
try {
while (cursor?.moveToNext() == true) {
Expand Down Expand Up @@ -188,6 +189,8 @@ 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)
bhl09 marked this conversation as resolved.
Show resolved Hide resolved

val cursor: Cursor? = if (atLeastAPI(17)) {
contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null)
} else {
Expand Down Expand Up @@ -459,8 +462,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
}

Expand All @@ -474,7 +477,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)
}
}

Expand All @@ -489,15 +492,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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
bhl09 marked this conversation as resolved.
Show resolved Hide resolved

val calendarDelegate = CalendarDelegate(activity, context)
val calendarDelegate = CalendarDelegate(registrar, context)
val instance = DeviceCalendarPlugin(registrar, calendarDelegate)

val calendarsChannel = MethodChannel(registrar.messenger(), CHANNEL_NAME)
Expand Down