From 6a10cc9c558479a08729bbfdeddb63724addab84 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 28 Sep 2019 04:23:21 -0400 Subject: [PATCH] Remove dependency Dexter --- app/build.gradle | 1 - .../com/topjohnwu/magisk/base/BaseActivity.kt | 57 ++++++++++++------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 348df90e3..8485a5d61 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -62,7 +62,6 @@ dependencies { implementation 'com.jakewharton.timber:timber:4.7.1' implementation 'com.ncapdevi:frag-nav:3.2.0' implementation 'com.github.pwittchen:reactivenetwork-rx2:3.0.6' - implementation 'com.karumi:dexter:6.0.0' implementation "io.reactivex.rxjava2:rxjava:2.2.12" implementation "io.reactivex.rxjava2:rxkotlin:2.4.0" diff --git a/app/src/main/java/com/topjohnwu/magisk/base/BaseActivity.kt b/app/src/main/java/com/topjohnwu/magisk/base/BaseActivity.kt index def4f4aea..e9c049ff2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/base/BaseActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/base/BaseActivity.kt @@ -3,19 +3,17 @@ package com.topjohnwu.magisk.base import android.Manifest import android.content.Context import android.content.Intent +import android.content.pm.PackageManager import android.content.res.Configuration import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate import androidx.collection.SparseArrayCompat +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat import androidx.core.net.toUri import androidx.databinding.DataBindingUtil import androidx.databinding.ViewDataBinding -import com.karumi.dexter.Dexter -import com.karumi.dexter.MultiplePermissionsReport -import com.karumi.dexter.PermissionToken -import com.karumi.dexter.listener.PermissionRequest -import com.karumi.dexter.listener.multi.MultiplePermissionsListener import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.Config import com.topjohnwu.magisk.base.viewmodel.BaseViewModel @@ -25,6 +23,7 @@ import com.topjohnwu.magisk.model.permissions.PermissionRequestBuilder import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.currentLocale +import kotlin.random.Random typealias RequestCallback = BaseActivity<*, *>.(Int, Intent?) -> Unit @@ -38,7 +37,7 @@ abstract class BaseActivity() + private val resultCallbacks by lazy { SparseArrayCompat() } init { val theme = if (Config.darkTheme) { @@ -74,22 +73,38 @@ abstract class BaseActivity Unit) { val request = PermissionRequestBuilder().apply(builder).build() - Dexter.withActivity(this) - .withPermissions(*permissions) - .withListener(object : MultiplePermissionsListener { - override fun onPermissionsChecked(report: MultiplePermissionsReport) { - if (report.areAllPermissionsGranted()) { - request.onSuccess() - } else { - request.onFailure() - } - } + val ungranted = permissions.filter { + ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED + } + + if (ungranted.isEmpty()) { + request.onSuccess() + } else { + val requestCode = Random.nextInt(256, 512) + resultCallbacks[requestCode] = { result, _ -> + if (result > 0) + request.onSuccess() + else + request.onFailure() + } + ActivityCompat.requestPermissions(this, ungranted.toTypedArray(), requestCode) + } + } + + override fun onRequestPermissionsResult( + requestCode: Int, permissions: Array, grantResults: IntArray) { + var success = true + for (res in grantResults) { + if (res != PackageManager.PERMISSION_GRANTED) { + success = false + break + } + } + resultCallbacks[requestCode]?.apply { + resultCallbacks.remove(requestCode) + invoke(this@BaseActivity, if (success) 1 else -1, null) + } - override fun onPermissionRationaleShouldBeShown( - permissions: MutableList, - token: PermissionToken - ) = token.continuePermissionRequest() - }).check() } fun withExternalRW(builder: PermissionRequestBuilder.() -> Unit) {