Explorar o código

Use Assent for permission requests in the sample project

Aidan Follestad %!s(int64=6) %!d(string=hai) anos
pai
achega
ded238d8f3

+ 2 - 1
dependencies.gradle

@@ -11,5 +11,6 @@ ext.versions = [
     bintrayPlugin     : '0.8.1',
 
     kotlin            : '1.2.70',
-    androidx          : '1.0.0'
+    androidx          : '1.0.0',
+    assent            : '2.2.0'
 ]

+ 2 - 0
sample/build.gradle

@@ -31,6 +31,8 @@ dependencies {
   implementation 'androidx.gridlayout:gridlayout:' + versions.androidx
   implementation 'androidx.appcompat:appcompat:' + versions.androidx
   implementation 'androidx.recyclerview:recyclerview:' + versions.androidx
+
+  implementation 'com.afollestad:assent:' + versions.assent
 }
 
 apply from: '../spotless.gradle'

+ 32 - 76
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.kt

@@ -7,8 +7,6 @@
 
 package com.afollestad.materialdialogssample
 
-import android.Manifest.permission.READ_EXTERNAL_STORAGE
-import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
 import android.content.SharedPreferences
 import android.graphics.Color
 import android.os.Bundle
@@ -20,6 +18,9 @@ import android.webkit.WebView
 import android.widget.CheckBox
 import android.widget.EditText
 import androidx.appcompat.app.AppCompatActivity
+import com.afollestad.assent.Permission.READ_EXTERNAL_STORAGE
+import com.afollestad.assent.Permission.WRITE_EXTERNAL_STORAGE
+import com.afollestad.assent.runWithPermissions
 import com.afollestad.materialdialogs.MaterialDialog
 import com.afollestad.materialdialogs.callbacks.onCancel
 import com.afollestad.materialdialogs.callbacks.onDismiss
@@ -100,7 +101,6 @@ class MainActivity : AppCompatActivity() {
 
   private var debugMode = false
   private lateinit var prefs: SharedPreferences
-  private val permission = Permission(this)
 
   override fun onCreate(savedInstanceState: Bundle?) {
     prefs = getSharedPreferences(KEY_PREFS, MODE_PRIVATE)
@@ -706,87 +706,52 @@ class MainActivity : AppCompatActivity() {
     }
   }
 
-  private fun showFileChooser() {
-    permission.request(arrayOf(READ_EXTERNAL_STORAGE)) { result ->
-      if (!result.allGranted()) {
-        toast("Storage permission is needed for file choosers")
-        return@request
-      }
-
-      MaterialDialog(this).show {
-        fileChooser { _, file ->
-          toast("Selected file: ${file.absolutePath}")
-        }
-        debugMode(debugMode)
+  private fun showFileChooser() = runWithPermissions(READ_EXTERNAL_STORAGE) {
+    MaterialDialog(this).show {
+      fileChooser { _, file ->
+        toast("Selected file: ${file.absolutePath}")
       }
+      debugMode(debugMode)
     }
   }
 
-  private fun showFileChooserButtons() {
-    permission.request(arrayOf(WRITE_EXTERNAL_STORAGE)) { result ->
-      if (!result.allGranted()) {
-        toast("Storage permission is needed for file choosers")
-        return@request
-      }
-
-      MaterialDialog(this).show {
-        fileChooser(allowFolderCreation = true) { _, file ->
-          toast("Selected file: ${file.absolutePath}")
-        }
-        negativeButton(android.R.string.cancel)
-        positiveButton(R.string.select)
-        debugMode(debugMode)
+  private fun showFileChooserButtons() = runWithPermissions(WRITE_EXTERNAL_STORAGE) {
+    MaterialDialog(this).show {
+      fileChooser(allowFolderCreation = true) { _, file ->
+        toast("Selected file: ${file.absolutePath}")
       }
+      negativeButton(android.R.string.cancel)
+      positiveButton(R.string.select)
+      debugMode(debugMode)
     }
   }
 
-  private fun showFileChooserFilter() {
-    permission.request(arrayOf(READ_EXTERNAL_STORAGE)) { result ->
-      if (!result.allGranted()) {
-        toast("Storage permission is needed for file choosers")
-        return@request
-      }
-
-      MaterialDialog(this).show {
-        fileChooser(filter = { it.extension == "txt" }) { _, file ->
-          toast("Selected file: ${file.absolutePath}")
-        }
-        debugMode(debugMode)
+  private fun showFileChooserFilter() = runWithPermissions(READ_EXTERNAL_STORAGE) {
+    MaterialDialog(this).show {
+      fileChooser(filter = { it.extension == "txt" }) { _, file ->
+        toast("Selected file: ${file.absolutePath}")
       }
+      debugMode(debugMode)
     }
   }
 
-  private fun showFolderChooserButtons() {
-    permission.request(arrayOf(WRITE_EXTERNAL_STORAGE)) { result ->
-      if (!result.allGranted()) {
-        toast("Storage permission is needed for file choosers")
-        return@request
-      }
-
-      MaterialDialog(this).show {
-        folderChooser(allowFolderCreation = true) { _, folder ->
-          toast("Selected folder: ${folder.absolutePath}")
-        }
-        negativeButton(android.R.string.cancel)
-        positiveButton(R.string.select)
-        debugMode(debugMode)
+  private fun showFolderChooserButtons() = runWithPermissions(WRITE_EXTERNAL_STORAGE) {
+    MaterialDialog(this).show {
+      folderChooser(allowFolderCreation = true) { _, folder ->
+        toast("Selected folder: ${folder.absolutePath}")
       }
+      negativeButton(android.R.string.cancel)
+      positiveButton(R.string.select)
+      debugMode(debugMode)
     }
   }
 
-  private fun showFolderChooserFilter() {
-    permission.request(arrayOf(READ_EXTERNAL_STORAGE)) { result ->
-      if (!result.allGranted()) {
-        toast("Storage permission is needed for file choosers")
-        return@request
-      }
-
-      MaterialDialog(this).show {
-        folderChooser(filter = { it.name.startsWith("a", true) }) { _, folder ->
-          toast("Selected folder: ${folder.absolutePath}")
-        }
-        debugMode(debugMode)
+  private fun showFolderChooserFilter() = runWithPermissions(READ_EXTERNAL_STORAGE) {
+    MaterialDialog(this).show {
+      folderChooser(filter = { it.name.startsWith("a", true) }) { _, folder ->
+        toast("Selected folder: ${folder.absolutePath}")
       }
+      debugMode(debugMode)
     }
   }
 
@@ -844,13 +809,4 @@ class MainActivity : AppCompatActivity() {
     }
     return super.onOptionsItemSelected(item)
   }
-
-  override fun onRequestPermissionsResult(
-    requestCode: Int,
-    permissions: Array<out String>,
-    grantResults: IntArray
-  ) {
-    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
-    permission.response(requestCode, permissions, grantResults)
-  }
 }

+ 0 - 98
sample/src/main/java/com/afollestad/materialdialogssample/Permission.kt

@@ -1,98 +0,0 @@
-/*
- * Licensed under Apache-2.0
- *
- * Designed and developed by Aidan Follestad (@afollestad)
- */
-package com.afollestad.materialdialogssample
-
-import android.app.Activity
-import android.content.pm.PackageManager.PERMISSION_GRANTED
-import androidx.annotation.CheckResult
-import androidx.core.app.ActivityCompat
-import androidx.core.content.ContextCompat
-import java.util.Arrays
-
-typealias Callback = (Result) -> Unit
-
-class Permission(private val activity: Activity) {
-
-  companion object {
-    const val PERM_RQ = 79
-  }
-
-  private val callbacks = mutableMapOf<Array<String>, Callback>()
-
-  @CheckResult fun has(permissions: String): Boolean {
-    return ContextCompat.checkSelfPermission(activity, permissions) ==
-        PERMISSION_GRANTED
-  }
-
-  @CheckResult fun hasAll(permissions: Array<String>): Boolean {
-    for (perm in permissions) {
-      if (!has(perm)) return false
-    }
-    return true
-  }
-
-  fun request(
-    permissions: Array<String>,
-    callback: Callback
-  ) {
-    if (hasAll(permissions)) {
-      val result = Result(permissions,
-          IntArray(permissions.size) { PERMISSION_GRANTED })
-      callback.invoke(result)
-      return
-    }
-    callbacks[permissions] = callback
-    ActivityCompat.requestPermissions(activity, permissions, PERM_RQ)
-  }
-
-  fun response(
-    requestCode: Int,
-    permissions: Array<out String>,
-    grantResults: IntArray
-  ) {
-    if (requestCode != PERM_RQ) return
-    val callback = callbacks[permissions]
-    val result = Result(permissions, grantResults)
-    callback?.invoke(result)
-  }
-}
-
-class Result(
-  val permissions: Array<out String>,
-  val grantResults: IntArray
-) {
-
-  @CheckResult fun granted(permission: String): Boolean {
-    val index = permissions.indexOf(permission)
-    if (index == -1) return false
-    return grantResults[index] == PERMISSION_GRANTED
-  }
-
-  @CheckResult fun allGranted(): Boolean {
-    for (perm in permissions) {
-      if (!granted(perm)) return false
-    }
-    return true
-  }
-
-  override fun equals(other: Any?): Boolean {
-    if (this === other) return true
-    if (javaClass != other?.javaClass) return false
-
-    other as Result
-
-    if (!Arrays.equals(permissions, other.permissions)) return false
-    if (!Arrays.equals(grantResults, other.grantResults)) return false
-
-    return true
-  }
-
-  override fun hashCode(): Int {
-    var result = Arrays.hashCode(permissions)
-    result = 31 * result + Arrays.hashCode(grantResults)
-    return result
-  }
-}