From 125fcc3055da51f2253a43c234d37d05c8339ca0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 21 Oct 2017 17:38:07 +0200 Subject: [PATCH] update Commons to 2.30.6 for easy runtime permissions handling --- app/build.gradle | 2 +- .../calendar/activities/MainActivity.kt | 54 +++++++------------ .../calendar/activities/SettingsActivity.kt | 23 ++------ .../calendar/extensions/Activity.kt | 5 -- .../calendar/extensions/Context.kt | 5 -- .../calendar/helpers/CalDAVHandler.kt | 9 ++-- 6 files changed, 27 insertions(+), 71 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f509d1787..8660ace42 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,7 +44,7 @@ ext { } dependencies { - compile 'com.simplemobiletools:commons:2.30.4' + compile 'com.simplemobiletools:commons:2.30.6' compile 'joda-time:joda-time:2.9.1' compile 'com.facebook.stetho:stetho:1.4.1' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 9a66a4aa2..d5c6e1b84 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -1,10 +1,8 @@ package com.simplemobiletools.calendar.activities -import android.Manifest import android.content.ContentResolver import android.content.Intent import android.content.pm.ActivityInfo -import android.content.pm.PackageManager import android.database.ContentObserver import android.database.Cursor import android.net.Uri @@ -12,7 +10,6 @@ import android.os.Bundle import android.os.Handler import android.provider.CalendarContract import android.provider.ContactsContract -import android.support.v4.app.ActivityCompat import android.support.v4.view.ViewPager import android.util.SparseIntArray import android.view.Menu @@ -39,9 +36,7 @@ import com.simplemobiletools.calendar.views.MyScrollView import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.LICENSE_JODA -import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN -import com.simplemobiletools.commons.helpers.LICENSE_STETHO +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.Release import kotlinx.android.synthetic.main.activity_main.* @@ -56,9 +51,6 @@ class MainActivity : SimpleActivity(), NavigationListener { private val PREFILLED_MONTHS = 97 private val PREFILLED_YEARS = 31 private val PREFILLED_WEEKS = 61 - private val PERMISSION_STORAGE_IMPORT = 1 - private val PERMISSION_STORAGE_EXPORT = 2 - private val PERMISSION_CONTACTS = 3 private var mIsMonthSelected = false private var mStoredTextColor = 0 @@ -108,7 +100,7 @@ class MainActivity : SimpleActivity(), NavigationListener { storeStateVariables() updateViewPager() - if (!hasCalendarPermission()) { + if (!hasPermission(PERMISSION_WRITE_CALENDAR)) { config.caldavSync = false } @@ -293,12 +285,14 @@ class MainActivity : SimpleActivity(), NavigationListener { } private fun tryAddBirthdays() { - if (hasContactsPermission()) { - Thread({ - addBirthdays() - }).start() - } else { - ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), PERMISSION_CONTACTS) + handlePermission(PERMISSION_READ_CONTACTS) { + if (it) { + Thread({ + addBirthdays() + }).start() + } else { + toast(R.string.no_contacts_permission) + } } } @@ -399,10 +393,10 @@ class MainActivity : SimpleActivity(), NavigationListener { } private fun tryImportEvents() { - if (hasReadStoragePermission()) { - importEvents() - } else { - ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), PERMISSION_STORAGE_IMPORT) + handlePermission(PERMISSION_READ_STORAGE) { + if (it) { + importEvents() + } } } @@ -442,10 +436,10 @@ class MainActivity : SimpleActivity(), NavigationListener { } private fun tryExportEvents() { - if (hasReadStoragePermission()) { - exportEvents() - } else { - ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), PERMISSION_STORAGE_EXPORT) + handlePermission(PERMISSION_WRITE_STORAGE) { + if (it) { + exportEvents() + } } } @@ -673,18 +667,6 @@ class MainActivity : SimpleActivity(), NavigationListener { } } - override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults) - - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - when (requestCode) { - PERMISSION_STORAGE_IMPORT -> importEvents() - PERMISSION_STORAGE_EXPORT -> exportEvents() - PERMISSION_CONTACTS -> Thread({ addBirthdays() }).start() - } - } - } - private fun getHolidayRadioItems(): ArrayList { val items = ArrayList() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt index 9775fd538..09e2b10f0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -1,14 +1,11 @@ package com.simplemobiletools.calendar.activities -import android.Manifest import android.content.Intent -import android.content.pm.PackageManager import android.content.res.Resources import android.media.RingtoneManager import android.net.Uri import android.os.Bundle import android.os.Parcelable -import android.support.v4.app.ActivityCompat import android.text.TextUtils import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog @@ -25,12 +22,12 @@ import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.updateTextColors +import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALENDAR import com.simplemobiletools.commons.models.RadioItem import kotlinx.android.synthetic.main.activity_settings.* class SettingsActivity : SimpleActivity() { private val GET_RINGTONE_URI = 1 - private val CALENDAR_PERMISSION = 5 lateinit var res: Resources private var mStoredPrimaryColor = 0 @@ -106,10 +103,10 @@ class SettingsActivity : SimpleActivity() { if (config.caldavSync) { toggleCaldavSync(false) } else { - if (hasCalendarPermission()) { - toggleCaldavSync(true) - } else { - ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_CALENDAR), CALENDAR_PERMISSION) + handlePermission(PERMISSION_WRITE_CALENDAR) { + if (it) { + toggleCaldavSync(true) + } } } } @@ -352,14 +349,4 @@ class SettingsActivity : SimpleActivity() { } } } - - override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults) - - if (requestCode == CALENDAR_PERMISSION) { - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - toggleCaldavSync(true) - } - } - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt index fea0176ec..370f7af43 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt @@ -1,10 +1,7 @@ package com.simplemobiletools.calendar.extensions -import android.Manifest import android.app.Activity import android.content.Intent -import android.content.pm.PackageManager -import android.support.v4.content.ContextCompat import android.support.v4.content.FileProvider import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.activities.SimpleActivity @@ -12,8 +9,6 @@ import com.simplemobiletools.calendar.helpers.IcsExporter import com.simplemobiletools.commons.extensions.toast import java.io.File -fun Activity.hasContactsPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED - fun SimpleActivity.shareEvents(ids: List) { val file = getTempFile() if (file == null) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index 6648fd952..fb3fb185e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.calendar.extensions -import android.Manifest import android.annotation.TargetApi import android.app.AlarmManager import android.app.Notification @@ -10,13 +9,11 @@ import android.appwidget.AppWidgetManager import android.content.ComponentName import android.content.Context import android.content.Intent -import android.content.pm.PackageManager import android.content.res.Resources import android.graphics.Color import android.graphics.PorterDuff import android.net.Uri import android.os.Build -import android.support.v4.content.ContextCompat import android.support.v7.app.NotificationCompat import android.view.Gravity import android.view.View @@ -38,8 +35,6 @@ import org.joda.time.DateTimeZone import java.text.SimpleDateFormat import java.util.* -fun Context.hasCalendarPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED - fun Context.updateWidgets() { val widgetsCnt = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetMonthlyProvider::class.java)) if (widgetsCnt.isNotEmpty()) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt index 0d3315bcd..ba4aa3a6c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt @@ -10,15 +10,12 @@ import android.util.SparseIntArray import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.dbHelper -import com.simplemobiletools.calendar.extensions.hasCalendarPermission import com.simplemobiletools.calendar.extensions.scheduleCalDAVSync import com.simplemobiletools.calendar.models.CalDAVCalendar import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.EventType -import com.simplemobiletools.commons.extensions.getIntValue -import com.simplemobiletools.commons.extensions.getLongValue -import com.simplemobiletools.commons.extensions.getStringValue -import com.simplemobiletools.commons.extensions.showErrorToast +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALENDAR import java.util.* import kotlin.collections.ArrayList @@ -43,7 +40,7 @@ class CalDAVHandler(val context: Context) { fun getCalDAVCalendars(activity: SimpleActivity? = null, ids: String = ""): List { val calendars = ArrayList() - if (!context.hasCalendarPermission()) { + if (!context.hasPermission(PERMISSION_WRITE_CALENDAR)) { return calendars }