Last active
February 4, 2020 19:02
-
-
Save waqasakram117/1317013dd31a8e8dc52d3c3deb25c665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.lightbeem.myapplication.common | |
| import android.annotation.SuppressLint | |
| import android.content.pm.PackageManager | |
| import androidx.core.app.ActivityCompat | |
| import androidx.core.content.ContextCompat | |
| import androidx.core.content.PermissionChecker | |
| import androidx.appcompat.app.AlertDialog | |
| import androidx.appcompat.app.AppCompatActivity | |
| import androidx.core.content.PermissionChecker.* | |
| import java.util.* | |
| @SuppressLint("Registered") | |
| open class ExperimentalPermissionActivity :AppCompatActivity(){ | |
| private val TAG = "Base Activity" | |
| private val allPermissionRequestCode by lazy { 1000 } | |
| private var totalRequestPermissions :Int = 0 | |
| private var grandedResults :Int = 0 | |
| private var deinedResults :Int = 0 | |
| private var list:LinkedList<Permission> = LinkedList() | |
| private var permissionGranted: (permission: String) -> Unit = {} | |
| private var permissionDenied: (permission: String) -> Unit = {} | |
| private var permissionBlocked: (permission:String) -> Unit = {} | |
| private var permissionPermanentBlocked: (permission: String)-> Unit = {} | |
| private var allPermissionsAreBlocked: () -> Unit = {} | |
| private var allPermissionsAreGranted: () -> Unit ={} | |
| fun requestAllPermissions(vararg permissionList : Permission, | |
| permissionBlocked:(String)->Unit = {}, | |
| permissionGranted: (String) -> Unit = {}, | |
| permissionDenied: (String) -> Unit = {}, | |
| permissionPermanentBlocked: (String) -> Unit = {}, | |
| allPermissionsAreGranted : () -> Unit = {}, | |
| allPermissionsAreBlocked : () -> Unit ={} | |
| ){ | |
| this.permissionBlocked = permissionBlocked | |
| this.permissionGranted = permissionGranted | |
| this.permissionDenied = permissionDenied | |
| this.permissionPermanentBlocked ={} | |
| this.allPermissionsAreBlocked = allPermissionsAreBlocked | |
| this.allPermissionsAreGranted = allPermissionsAreGranted | |
| this.permissionPermanentBlocked = permissionPermanentBlocked | |
| grandedResults = 0 | |
| deinedResults = 0 | |
| totalRequestPermissions = permissionList.size | |
| list.addAll(permissionList) | |
| permissionChecker() | |
| } | |
| private fun permissionChecker(){ | |
| if (list.isNotEmpty()){ | |
| list.pop()?.let { | |
| checkForSinglePermission(it) | |
| } | |
| } | |
| } | |
| private fun checkForSinglePermission(permission: Permission) { | |
| // Here, thisActivity is the current activity | |
| if (ContextCompat.checkSelfPermission(this, | |
| permission.manifestPermission) | |
| != PackageManager.PERMISSION_GRANTED) { | |
| // Permission is not granted | |
| // Should we show an explanation? | |
| if (ActivityCompat.shouldShowRequestPermissionRationale(this, | |
| permission.manifestPermission)) { | |
| // Show an explanation to the user *asynchronously* -- don't block | |
| // this thread waiting for the user's response! After the user | |
| // sees the explanation, try again to request the permission. | |
| val alertBuilder = AlertDialog.Builder(this) | |
| alertBuilder.setCancelable(true) | |
| alertBuilder.setTitle(permission.rationalTitle) | |
| alertBuilder.setMessage(permission.rationalDescription) | |
| alertBuilder.setPositiveButton(android.R.string.yes) { dialog, which -> | |
| ActivityCompat.requestPermissions( | |
| this@ExperimentalPermissionActivity, | |
| arrayOf(permission.manifestPermission), | |
| allPermissionRequestCode | |
| ) | |
| } | |
| val alert = alertBuilder.create() | |
| alert.show() | |
| } else { | |
| // No explanation needed, we can request the permission. | |
| ActivityCompat.requestPermissions(this, | |
| arrayOf(permission.manifestPermission), | |
| allPermissionRequestCode) | |
| // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an | |
| // app-defined int constant. The callback method gets the | |
| // result of the request. | |
| } | |
| } else { | |
| // Permission has already been granted | |
| permissionGranted.invoke(permission.manifestPermission) | |
| grandedResults++ | |
| showLogMsg( "${permission.manifestPermission} is already granted with result $grandedResults") | |
| permissionChecker() | |
| checkTotalNumbersOfResults() | |
| } | |
| } | |
| override fun onRequestPermissionsResult(reqCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
| super.onRequestPermissionsResult(reqCode, permissions, grantResults) | |
| val currentPermission = permissions[0] | |
| when { | |
| PermissionChecker.checkSelfPermission(this , currentPermission) == PERMISSION_DENIED_APP_OP -> { | |
| permissionPermanentBlocked(currentPermission) | |
| deinedResults++ | |
| } | |
| PermissionChecker.checkSelfPermission(this , currentPermission) == PERMISSION_GRANTED -> { | |
| permissionGranted(currentPermission) | |
| grandedResults++ | |
| showLogMsg("$currentPermission is granted with result $grandedResults") | |
| } | |
| PermissionChecker.checkSelfPermission(this , currentPermission) == PERMISSION_DENIED -> { | |
| var permanent = ActivityCompat.shouldShowRequestPermissionRationale(this, | |
| currentPermission) | |
| if (permanent.not()){ | |
| permissionPermanentBlocked(currentPermission) | |
| deinedResults++ | |
| showLogMsg("$currentPermission is permanent blocked ${permanent.not()} and falg is $deinedResults") | |
| permissionChecker() | |
| }else{ | |
| permissionBlocked.invoke(currentPermission) | |
| deinedResults++ | |
| showLogMsg("$currentPermission is blocked with flag $deinedResults") | |
| } | |
| } | |
| } | |
| checkTotalNumbersOfResults() | |
| permissionChecker() | |
| } | |
| private fun checkTotalNumbersOfResults(){ | |
| if (totalRequestPermissions != 0 && deinedResults == totalRequestPermissions){ | |
| allPermissionsAreBlocked.invoke() | |
| }else if (totalRequestPermissions != 0 && grandedResults == totalRequestPermissions){ | |
| allPermissionsAreGranted.invoke() | |
| } | |
| } | |
| override fun onDestroy() { | |
| permissionBlocked = {} | |
| permissionGranted = {} | |
| permissionDenied ={} | |
| permissionPermanentBlocked ={} | |
| allPermissionsAreBlocked = {} | |
| allPermissionsAreGranted ={} | |
| super.onDestroy() | |
| } | |
| } | |
| data class Permission(val manifestPermission :String, | |
| val rationalTitle :String, | |
| val rationalDescription:String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment