Android Handling Permission Deny With Don't Ask Again In Kotlin

In the previous example we have seen how to make runtime permission request and handle once user action i.e allow and deny ,when you make request for permission system will generate popup for user action .


When you make request for permission there will be 3 actions user's can perform .

  • Action 1 :  Allow 
  • Action 2 :  Just Deny ( deny without selecting the check-box "don't ask again")
  • Action 3 :  Force Deny ( deny with selecting the check-box "don't ask again")

Action 1 and 2 we have seen in previous example , In this tutorial we will handling Action 3 .

When you deny permission by selecting the check-box "don't ask again" After that when you make subsequent request again for the same permission Nothing will happen ( i.e The system will not show "Allow Deny" popup again )  .

Lets See Sample Example .

  UseCase  
Scenerio
1
  Request Permission > Pop-up > allow > (TextView Updated With Permission Granted).

 2
   Request Permission > Pop-up > deny > (TextView Updated With Permission Denied).

 3

    Request Permission > Pop-up > deny with "don't ask again" selected >
    (TextView Updated With Permission Denied and Show Button for visit settings).
 
   Now on Click of "visit settings" you will navigate to app settings 
   once you grant permission and resume back to app  inside onResume
   again we will check if permission update the UI  (i.e TextView Updated 
   with Permission  Granted and hide Button for visit settings ).



Use Case 1 ( Permission Granted )



Use Case 2 ( Permission Denied )
 



Use Case 3 ( Pemission Deny By Selecting Don't Ask Again )



MainActivity


file : MainActivity.kt 
package com.tutorialsbuzz.permissiondeny

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(), View.OnClickListener {

    companion object {
        const val REQUEST_WRITE_PERMISSION = 100
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initViews()
    }

    private fun initViews() {
        requestbtn.setOnClickListener(this)
        allow_permission.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        if (checkPermissionForReadExtertalStorage()) {
               label!!.setText(R.string.permission_granted)
              allow_permission.visibility=View.GONE
        }
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.requestbtn -> if (checkPermissionForReadExtertalStorage()) {
                label!!.setText(R.string.permission_granted)
            } else {
                //Make Request
                requestPermission()
            }
            R.id.allow_permission -> {
                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                val uri = Uri.fromParts("package", packageName, null)
                intent.data = uri
                startActivity(intent)
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (grantResults.size > 0) {
            if (requestCode == REQUEST_WRITE_PERMISSION) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission granted
                    label!!.setText(R.string.permission_granted)
                    allow_permission!!.visibility = View.GONE
                } else {
                    // permission denied
                    label!!.setText(R.string.permission_denied)
                    // Check wheather checked dont ask again
                    checkUserRequestedDontAskAgain()
                }
            }
        }
    }

    fun checkPermissionForReadExtertalStorage(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_GRANTED)
        } else false
    }

    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                REQUEST_WRITE_PERMISSION
            )
        }
    }

    private fun checkUserRequestedDontAskAgain() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val rationalFalgREAD =
                shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            val rationalFalgWRITE =
                shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)
            if (!rationalFalgREAD && !rationalFalgWRITE) {
                label!!.setText(R.string.permission_denied_forcefully)
                allow_permission!!.visibility = View.VISIBLE
            }
        }
    }

}


No comments:

Post a Comment