From 17fe501a6d1c4e5680eded9073292aa2cb3fd238 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 7 Aug 2017 11:04:27 +0200 Subject: [PATCH] Ask permissions once. Fixes #892 --- .../ui/base/controller/ConductorExtensions.kt | 15 +++++++++++++++ .../kanade/tachiyomi/ui/manga/MangaController.kt | 5 ++--- .../ui/setting/SettingsBackupController.kt | 5 ++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt index 509ec5ae7..19150e132 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt @@ -1,5 +1,9 @@ package eu.kanade.tachiyomi.ui.base.controller +import android.content.pm.PackageManager.PERMISSION_GRANTED +import android.os.Build +import android.support.v4.content.ContextCompat +import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.Router fun Router.popControllerWithTag(tag: String): Boolean { @@ -9,4 +13,15 @@ fun Router.popControllerWithTag(tag: String): Boolean { return true } return false +} + +fun Controller.requestPermissionsSafe(permissions: Array, requestCode: Int) { + val activity = activity ?: return + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + permissions.forEach { permission -> + if (ContextCompat.checkSelfPermission(activity, permission) != PERMISSION_GRANTED) { + requestPermissions(arrayOf(permission), requestCode) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 7babfa5af..562874bee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -26,6 +26,7 @@ import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.base.controller.RouterPagerAdapter import eu.kanade.tachiyomi.ui.base.controller.RxController import eu.kanade.tachiyomi.ui.base.controller.TabbedController +import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe import eu.kanade.tachiyomi.ui.manga.chapter.ChaptersController import eu.kanade.tachiyomi.ui.manga.info.MangaInfoController import eu.kanade.tachiyomi.ui.manga.track.TrackController @@ -85,9 +86,7 @@ class MangaController : RxController, TabbedController { if (manga == null || source == null) return - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - requestPermissions(arrayOf(WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE), 301) - } + requestPermissionsSafe(arrayOf(WRITE_EXTERNAL_STORAGE), 301) with(view) { adapter = MangaDetailAdapter() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index e2a2edcde..6c1994835 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -26,6 +26,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.popControllerWithTag +import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe import eu.kanade.tachiyomi.util.getUriCompat import eu.kanade.tachiyomi.util.registerLocalReceiver import eu.kanade.tachiyomi.util.toast @@ -52,9 +53,7 @@ class SettingsBackupController : SettingsController() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - requestPermissions(arrayOf(WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE), 500) - } + requestPermissionsSafe(arrayOf(WRITE_EXTERNAL_STORAGE), 500) } override fun onDestroy() {