Skip to content

Instantly share code, notes, and snippets.

@10zgurr
Last active November 7, 2022 11:29
Show Gist options
  • Save 10zgurr/5939bd11bf6352654deb8b0729abf94b to your computer and use it in GitHub Desktop.
Save 10zgurr/5939bd11bf6352654deb8b0729abf94b to your computer and use it in GitHub Desktop.
AppPermissionsExtension
# your package name
import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
/** checking permissions */
fun Context.isReadingContactsPermissionGranted() = getReadContactsPermission() == PackageManager.PERMISSION_GRANTED
fun Context.isReadingCallLogsPermissionGranted() = getReadCallLogsPermission() == PackageManager.PERMISSION_GRANTED
fun Context.isReadingPhoneStatePermissionGranted() = getReadPhoneStatePermission() == PackageManager.PERMISSION_GRANTED
fun Context.areReadingCallLogsAndPhoneStatePermissionsGranted() = getReadCallLogsPermission() == PackageManager.PERMISSION_GRANTED && getReadPhoneStatePermission() == PackageManager.PERMISSION_GRANTED
fun Context.isDrawOverAppsPermissionGranted() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) Settings.canDrawOverlays(this) else true
fun Context.areReadingCallLogsAndContactsPermissionsGranted() = getReadCallLogsPermission() == PackageManager.PERMISSION_GRANTED && getReadContactsPermission() == PackageManager.PERMISSION_GRANTED
fun Context.areReadingCallLogsContactsAndPhoneStatePermissionsGranted() = getReadCallLogsPermission() == PackageManager.PERMISSION_GRANTED && getReadContactsPermission() == PackageManager.PERMISSION_GRANTED && getReadPhoneStatePermission() == PackageManager.PERMISSION_GRANTED
fun Context.isCallingPhonePermissionGranted() = getCallPhonePermission() == PackageManager.PERMISSION_GRANTED
fun Context.isExternalStoragePermissionGranted() = isWritingExternalPermissionGranted() && isCameraPermissionGranted() && isReadingExternalPermissionGranted()
fun Context.isReadingSMSPermissionGranted() = getReadSMSPermission() == PackageManager.PERMISSION_GRANTED
fun Context.isReadingPhoneNumbersPermissionGranted() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) getReadPhoneNumbersPermission() == PackageManager.PERMISSION_GRANTED else true
private fun Context.isWritingExternalPermissionGranted() = getWriteExternalPermission() == PackageManager.PERMISSION_GRANTED
private fun Context.isReadingExternalPermissionGranted() = getReadExternalPermission() == PackageManager.PERMISSION_GRANTED
private fun Context.isCameraPermissionGranted() = getCameraPermission() == PackageManager.PERMISSION_GRANTED
private fun Context.getReadCallLogsPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG)
private fun Context.getReadContactsPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
private fun Context.getReadPhoneStatePermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
private fun Context.getCallPhonePermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
private fun Context.getWriteExternalPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
private fun Context.getReadExternalPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
private fun Context.getCameraPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
private fun Context.getReadSMSPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
@RequiresApi(Build.VERSION_CODES.O)
private fun Context.getReadPhoneNumbersPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS)
/** requesting permissions */
fun Fragment.requestCallLogsContactsAndPhoneStatePermissions() {
val listPermissionsNeeded = arrayListOf<String>()
if (!this.requireContext().isReadingContactsPermissionGranted())
listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS)
if (!this.requireContext().isReadingCallLogsPermissionGranted())
listPermissionsNeeded.add(Manifest.permission.READ_CALL_LOG)
if (!this.requireContext().isReadingPhoneStatePermissionGranted())
listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE)
if (listPermissionsNeeded.isNotEmpty())
this.requestPermissions((listPermissionsNeeded.toTypedArray()), REQUEST_CODE_MULTIPLE_PERMISSIONS)
}
fun Fragment.checkPermissionsRationaleDialog(activity: FragmentActivity?) {
if (shouldShowRequestPermissionRationaleDialog())
showPermissionsRationaleDialog(activity)
}
private fun Fragment.shouldShowRequestPermissionRationaleDialog() = !shouldShowRequestPermissionRationale(Manifest.permission.READ_CALL_LOG) || !shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS) || !shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)
private fun showPermissionsRationaleDialog(activity: FragmentActivity?) {} // TODO showPermissionsRationaleDialog
fun Fragment.requestReadingPhoneStatePermission() { requestPermissions(arrayOf(Manifest.permission.READ_PHONE_STATE), REQUEST_CODE_READ_PHONE_STATE_PERMISSION) }
fun Fragment.requestDrawOverAppsPermission(context: Context) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !context.isDrawOverAppsPermissionGranted())
startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.packageName)), REQUEST_CODE_MANAGE_OVERLAY_PERMISSION)
} catch (e: Exception) {
Log.e(TAG, "requestDrawOverAppsPermission failed -> ${e.message}")
}
}
fun Fragment.requestReadingContactsPermission() { requestPermissions(arrayOf(Manifest.permission.READ_CONTACTS), REQUEST_CODE_READ_CONTACTS_PERMISSION) }
fun Fragment.requestCallPhonePermission() { requestPermissions(arrayOf(Manifest.permission.CALL_PHONE), REQUEST_CODE_CALL_PHONE_PERMISSION) }
fun Fragment.requestReadingPhoneNumbersPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) requestPermissions(arrayOf(Manifest.permission.READ_PHONE_NUMBERS), REQUEST_CODE_READ_PHONE_NUMBERS) }
fun Fragment.requestExternalStoragePermission() {
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA),
REQUEST_CODE_EXTERNAL_STORAGE
)
}
fun Fragment.shouldShowReadWritePermissionsRationaleDialog() =
!shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)
|| !shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| !shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)
/** request codes */
const val REQUEST_CODE_MULTIPLE_PERMISSIONS = 888
const val REQUEST_CODE_READ_PHONE_STATE_PERMISSION = 777
const val REQUEST_CODE_MANAGE_OVERLAY_PERMISSION = 999
const val REQUEST_CODE_READ_CONTACTS_PERMISSION = 666
const val REQUEST_CODE_CALL_PHONE_PERMISSION = 555
const val REQUEST_CODE_EXTERNAL_STORAGE = 444
const val REQUEST_CODE_READ_PHONE_NUMBERS = 333
/** actions */
fun Fragment.makePhoneCall(phoneNumber: String) {
try {
startActivity(Intent(Intent.ACTION_CALL, Uri.parse("tel:$phoneNumber")))
} catch(e: Exception) {
Log.e(TAG, "makePhoneCall failure -> ${e.message}")
}
}
/** TAG */
private const val TAG = "AppPermissionsExt"
@10zgurr
Copy link
Author

10zgurr commented Jun 22, 2021

This is great thanks for sharing!

happy coding :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment