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 A Flow to Use Standard or Custom Rationale #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -22,11 +22,13 @@ import android.util.Log
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import com.vmadalin.easypermissions.EasyPermissions
import com.vmadalin.easypermissions.annotations.AfterPermissionGranted
import com.vmadalin.easypermissions.dialogs.DEFAULT_SETTINGS_REQ_CODE
import com.vmadalin.easypermissions.dialogs.SettingsDialog
import com.vmadalin.easypermissions.EasyPermissions
import com.vmadalin.easypermissions.helpers.base.PermissionsHelper
import com.vmadalin.easypermissions.models.PermissionRequest
import kotlinx.android.synthetic.main.activity_main.*

private const val TAG = "MainActivity"
private const val REQUEST_CODE_CAMERA_PERMISSION = 123
Expand Down Expand Up @@ -132,8 +134,11 @@ class MainActivity : AppCompatActivity(),
// Ask for one permission
EasyPermissions.requestPermissions(
this,
getString(R.string.permission_camera_rationale_message),
REQUEST_CODE_CAMERA_PERMISSION,
EasyPermissions.RationaleType.CustomRationale { request ->
//TODO: Show customize rational (Not required)
showTODORationalCustomDialog(request)
},
CAMERA
)
}
Expand All @@ -148,8 +153,8 @@ class MainActivity : AppCompatActivity(),
// Ask for one permission
EasyPermissions.requestPermissions(
this,
getString(R.string.permission_storage_rationale_message),
REQUEST_CODE_STORAGE_PERMISSION,
EasyPermissions.RationaleType.StandardRationale("TODO: Message"),
WRITE_EXTERNAL_STORAGE
)
}
Expand All @@ -164,8 +169,11 @@ class MainActivity : AppCompatActivity(),
// Ask for both permissions
EasyPermissions.requestPermissions(
this,
getString(R.string.permission_location_and_contacts_rationale_message),
REQUEST_CODE_LOCATION_AND_CONTACTS_PERMISSION,
EasyPermissions.RationaleType.CustomRationale { request ->
//TODO: Show customize rational (Not required)
showTODORationalCustomDialog(request)
},
ACCESS_FINE_LOCATION, READ_CONTACTS
)
}
Expand All @@ -186,4 +194,23 @@ class MainActivity : AppCompatActivity(),
private fun hasStoragePermission(): Boolean {
return EasyPermissions.hasPermissions(this, WRITE_EXTERNAL_STORAGE)
}

private fun showTODORationalCustomDialog(request: PermissionRequest) {
android.app.AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("TODO: custom Rational Message")
.setPositiveButton("Yes") { view, _ ->
PermissionsHelper
.newInstance(this)
.directRequestPermissions(
request.code,
request.perms
)
view.dismiss()
}
.setNegativeButton("No") { view, _ ->
view.dismiss()
}
.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_main.*
import com.vmadalin.easypermissions.annotations.AfterPermissionGranted
import com.vmadalin.easypermissions.EasyPermissions
import com.vmadalin.easypermissions.helpers.base.PermissionsHelper
import com.vmadalin.easypermissions.models.PermissionRequest

private const val TAG = "MainFragment"
private const val REQUEST_CODE_SMS_PERMISSION = 126
Expand Down Expand Up @@ -89,8 +91,8 @@ class MainFragment : Fragment(), EasyPermissions.PermissionCallbacks {
// Request one permission
EasyPermissions.requestPermissions(
this,
getString(R.string.permission_sms_rationale_message),
REQUEST_CODE_SMS_PERMISSION,
EasyPermissions.RationaleType.StandardRationale("TODO: Message"),
Manifest.permission.READ_SMS
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ object EasyPermissions {
@JvmStatic
fun requestPermissions(
host: Activity,
rationale: String,
requestCode: Int,
//Changed rationale param by Rationale Type which can allows Standard or Custom Rationales
rationale: RationaleType,
@Size(min = 1) vararg perms: String
) {
val request = PermissionRequest.Builder(host)
Expand All @@ -110,7 +111,6 @@ object EasyPermissions {
.build()
requestPermissions(host, request)
}

/**
* Request permissions from a Support Fragment with standard OK/Cancel buttons.
*
Expand All @@ -119,8 +119,9 @@ object EasyPermissions {
@JvmStatic
fun requestPermissions(
host: Fragment,
rationale: String,
requestCode: Int,
//Changed rationale param by Rationale Type which can allows Standard or Custom Rationales
rationale: RationaleType,
@Size(min = 1) vararg perms: String
) {
val request = PermissionRequest.Builder(host.context)
Expand Down Expand Up @@ -325,4 +326,14 @@ object EasyPermissions {
val grantResults = IntArray(perms.size) { PackageManager.PERMISSION_GRANTED }
onRequestPermissionsResult(requestCode, perms, grantResults, receiver)
}

/**
* Class which define type of Rationale. Can be used a Standard Rationale and send a string which is provided by EasyPermission library
* or Custom Rationale where you can send your own Rationale.
*
*/
sealed class RationaleType {
class StandardRationale(val rationale: String) : RationaleType()
class CustomRationale(val rationale: (PermissionRequest) -> Unit) : RationaleType()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import com.vmadalin.easypermissions.models.PermissionRequest
*/
class RationaleDialog(
private val context: Context,
private val model: PermissionRequest
private val model: PermissionRequest,
private val rationale: String
) : DialogInterface.OnClickListener {

private val permissionCallbacks: EasyPermissions.PermissionCallbacks?
Expand All @@ -47,7 +48,7 @@ class RationaleDialog(
fun showCompatDialog() {
AlertDialog.Builder(context, model.theme)
.setCancelable(false)
.setMessage(model.rationale)
.setMessage(rationale)
.setPositiveButton(model.positiveButtonText, this)
.setNegativeButton(model.negativeButtonText, this)
.show()
Expand All @@ -56,7 +57,7 @@ class RationaleDialog(
fun showDialog() {
android.app.AlertDialog.Builder(context, model.theme)
.setCancelable(false)
.setMessage(model.rationale)
.setMessage(rationale)
.setPositiveButton(model.positiveButtonText, this)
.setNegativeButton(model.negativeButtonText, this)
.show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.vmadalin.easypermissions.helpers
import android.app.Activity
import android.content.Context
import androidx.core.app.ActivityCompat
import com.vmadalin.easypermissions.EasyPermissions
import com.vmadalin.easypermissions.dialogs.RationaleDialog
import com.vmadalin.easypermissions.helpers.base.PermissionsHelper
import com.vmadalin.easypermissions.models.PermissionRequest
Expand All @@ -40,6 +41,17 @@ internal class ActivityPermissionsHelper(
}

override fun showRequestPermissionRationale(permissionRequest: PermissionRequest) {
RationaleDialog(host, permissionRequest).showDialog()
when(permissionRequest.rationale) {
is EasyPermissions.RationaleType.StandardRationale -> {
RationaleDialog(
host,
permissionRequest,
(permissionRequest.rationale as EasyPermissions.RationaleType.StandardRationale).rationale
).showDialog()
}
is EasyPermissions.RationaleType.CustomRationale -> {
(permissionRequest.rationale as EasyPermissions.RationaleType.CustomRationale).rationale.invoke(permissionRequest)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.vmadalin.easypermissions.helpers
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.vmadalin.easypermissions.EasyPermissions
import com.vmadalin.easypermissions.dialogs.RationaleDialog
import com.vmadalin.easypermissions.helpers.base.PermissionsHelper
import com.vmadalin.easypermissions.models.PermissionRequest
Expand All @@ -40,6 +41,17 @@ internal class AppCompatActivityPermissionsHelper(
}

override fun showRequestPermissionRationale(permissionRequest: PermissionRequest) {
RationaleDialog(host, permissionRequest).showCompatDialog()
when(permissionRequest.rationale) {
is EasyPermissions.RationaleType.StandardRationale -> {
RationaleDialog(
host,
permissionRequest,
(permissionRequest.rationale as EasyPermissions.RationaleType.StandardRationale).rationale
).showCompatDialog()
}
is EasyPermissions.RationaleType.CustomRationale -> {
(permissionRequest.rationale as EasyPermissions.RationaleType.CustomRationale).rationale.invoke(permissionRequest)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.vmadalin.easypermissions.helpers

import android.content.Context
import androidx.fragment.app.Fragment
import com.vmadalin.easypermissions.EasyPermissions
import com.vmadalin.easypermissions.dialogs.RationaleDialog
import com.vmadalin.easypermissions.helpers.base.PermissionsHelper
import com.vmadalin.easypermissions.models.PermissionRequest
Expand All @@ -40,7 +41,18 @@ internal class FragmentPermissionsHelper(

override fun showRequestPermissionRationale(permissionRequest: PermissionRequest) {
context?.let {
RationaleDialog(it, permissionRequest).showCompatDialog()
when(permissionRequest.rationale) {
is EasyPermissions.RationaleType.StandardRationale -> {
RationaleDialog(
it,
permissionRequest,
(permissionRequest.rationale as EasyPermissions.RationaleType.StandardRationale).rationale
).showCompatDialog()
}
is EasyPermissions.RationaleType.CustomRationale -> {
(permissionRequest.rationale as EasyPermissions.RationaleType.CustomRationale).rationale.invoke(permissionRequest)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.vmadalin.easypermissions.models
import android.content.Context
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import com.vmadalin.easypermissions.EasyPermissions
import com.vmadalin.easypermissions.R

/**
Expand All @@ -31,11 +32,18 @@ data class PermissionRequest(
var theme: Int,
var code: Int,
var perms: Array<out String>,
var rationale: String?,
var rationale: EasyPermissions.RationaleType,
var positiveButtonText: String?,
var negativeButtonText: String?
) {

//Override invoke to pass model which contains code and perms as parameter
operator fun invoke (model: PermissionRequest) {
if(rationale is EasyPermissions.RationaleType.CustomRationale) {
(rationale as EasyPermissions.RationaleType.CustomRationale).rationale(model)
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand All @@ -56,7 +64,7 @@ data class PermissionRequest(
var result = theme
result = 31 * result + code
result = 31 * result + perms.contentHashCode()
result = 31 * result + (rationale?.hashCode() ?: 0)
result = 31 * result + rationale.hashCode()
result = 31 * result + (positiveButtonText?.hashCode() ?: 0)
result = 31 * result + (negativeButtonText?.hashCode() ?: 0)
return result
Expand All @@ -73,15 +81,16 @@ data class PermissionRequest(
private var theme = 0
private var code = 0
private var perms: Array<out String> = emptyArray()
private var rationale = context?.getString(R.string.rationale_ask)
//Changed rationale param by Unit to implement lambda functions on callback
private var rationale: EasyPermissions.RationaleType = EasyPermissions.RationaleType.StandardRationale(
context?.getString(R.string.rationale_ask) ?: "")
private var positiveButtonText = context?.getString(android.R.string.ok)
private var negativeButtonText = context?.getString(android.R.string.cancel)

fun theme(@StyleRes theme: Int) = apply { this.theme = theme }
fun code(code: Int) = apply { this.code = code }
fun perms(perms: Array<out String>) = apply { this.perms = perms }
fun rationale(rationale: String) = apply { this.rationale = rationale }
fun rationale(@StringRes resId: Int) = apply { this.rationale = context?.getString(resId) }
fun rationale(rationale: EasyPermissions.RationaleType) = apply { this.rationale = rationale }
fun positiveButtonText(positiveButtonText: String) =
apply { this.positiveButtonText = positiveButtonText }

Expand All @@ -105,4 +114,4 @@ data class PermissionRequest(
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ package com.vmadalin.easypermissions

import android.Manifest
import androidx.test.core.app.ApplicationProvider
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import com.google.common.truth.Truth.assertThat
import com.nhaarman.mockitokotlin2.capture
import com.vmadalin.easypermissions.components.TestActivity
import com.vmadalin.easypermissions.components.TestAppCompatActivity
import com.vmadalin.easypermissions.components.TestFragment
import com.vmadalin.easypermissions.components.TestSupportFragmentActivity
import com.vmadalin.easypermissions.controllers.ActivityController
import com.vmadalin.easypermissions.controllers.FragmentController

import com.google.common.truth.Truth.assertThat
import org.mockito.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.Mockito.*
import com.nhaarman.mockitokotlin2.capture
import org.mockito.Captor
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

private const val RATIONALE = "RATIONALE"
private val ALL_PERMS = arrayOf(Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION)
Expand Down Expand Up @@ -85,8 +86,8 @@ class EasyPermissionsLowApiTest {
fun shouldCallbackOnPermissionGranted_whenRequestFromActivity() {
EasyPermissions.requestPermissions(
spyActivity,
RATIONALE,
TestActivity.REQUEST_CODE,
EasyPermissions.RationaleType.StandardRationale(RATIONALE),
*ALL_PERMS
)

Expand All @@ -99,8 +100,10 @@ class EasyPermissionsLowApiTest {
fun shouldCallbackOnPermissionGranted_whenRequestFromSupportFragmentActivity() {
EasyPermissions.requestPermissions(
spySupportFragmentActivity,
RATIONALE,
TestSupportFragmentActivity.REQUEST_CODE,
EasyPermissions.RationaleType.CustomRationale {

},
*ALL_PERMS
)

Expand All @@ -113,8 +116,8 @@ class EasyPermissionsLowApiTest {
fun shouldCallbackOnPermissionGranted_whenRequestFromAppCompatActivity() {
EasyPermissions.requestPermissions(
spyAppCompatActivity,
RATIONALE,
TestAppCompatActivity.REQUEST_CODE,
EasyPermissions.RationaleType.StandardRationale(RATIONALE),
*ALL_PERMS
)

Expand All @@ -127,8 +130,10 @@ class EasyPermissionsLowApiTest {
fun shouldCallbackOnPermissionGranted_whenRequestFromFragment() {
EasyPermissions.requestPermissions(
spyFragment,
RATIONALE,
TestFragment.REQUEST_CODE,
EasyPermissions.RationaleType.CustomRationale {

},
*ALL_PERMS
)

Expand Down
Loading