couple permission handling improvements
This commit is contained in:
parent
aded444048
commit
307de07f30
2 changed files with 35 additions and 53 deletions
|
@ -1,33 +1,32 @@
|
|||
package com.simplemobiletools.commons.activities
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.annotation.TargetApi
|
||||
import android.app.Activity
|
||||
import android.bluetooth.BluetoothClass.Service.AUDIO
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.DocumentsContract
|
||||
import android.support.v4.app.ActivityCompat
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.support.v4.util.Pair
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.MenuItem
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.asynctasks.CopyMoveTask
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.*
|
||||
import com.simplemobiletools.commons.helpers.APP_LICENSES
|
||||
import com.simplemobiletools.commons.helpers.APP_NAME
|
||||
import com.simplemobiletools.commons.helpers.APP_VERSION_NAME
|
||||
import com.simplemobiletools.commons.helpers.OPEN_DOCUMENT_TREE
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
open class BaseSimpleActivity : AppCompatActivity() {
|
||||
var copyMoveCallback: (() -> Unit)? = null
|
||||
var actionOnPermission: ((granted: Boolean) -> Unit)? = null
|
||||
private val GENERIC_PERM_HANDLER = 100
|
||||
|
||||
companion object {
|
||||
var funAfterSAFPermission: (() -> Unit)? = null
|
||||
|
@ -186,34 +185,21 @@ open class BaseSimpleActivity : AppCompatActivity() {
|
|||
|
||||
fun handlePermission(permissionId: Int, callback: (granted: Boolean) -> Unit) {
|
||||
actionOnPermission = null
|
||||
val permString = getPermissionString(permissionId)
|
||||
if (hasPermission(permString)) {
|
||||
if (hasPermission(permissionId)) {
|
||||
callback(true)
|
||||
} else {
|
||||
actionOnPermission = callback
|
||||
ActivityCompat.requestPermissions(this, arrayOf(permString), 1)
|
||||
ActivityCompat.requestPermissions(this, arrayOf(getPermissionString(permissionId)), GENERIC_PERM_HANDLER)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
if (grantResults.isNotEmpty()) {
|
||||
if (requestCode == GENERIC_PERM_HANDLER && grantResults.isNotEmpty()) {
|
||||
actionOnPermission?.invoke(grantResults[0] == 0)
|
||||
}
|
||||
}
|
||||
|
||||
private fun hasPermission(permString: String) = ContextCompat.checkSelfPermission(this, permString) == PackageManager.PERMISSION_GRANTED
|
||||
|
||||
private fun getPermissionString(id: Int) = when (id) {
|
||||
PERMISSION_READ_STORAGE -> Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
PERMISSION_WRITE_STORAGE -> Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||
PERMISSION_CAMERA -> Manifest.permission.CAMERA
|
||||
PERMISSION_RECORD_AUDIO -> Manifest.permission.RECORD_AUDIO
|
||||
PERMISSION_READ_CONTACTS -> Manifest.permission.READ_CONTACTS
|
||||
PERMISSION_WRITE_CALENDAR -> Manifest.permission.WRITE_CALENDAR
|
||||
else -> ""
|
||||
}
|
||||
|
||||
private val copyMoveListener = object : CopyMoveTask.CopyMoveListener {
|
||||
override fun copySucceeded(copyOnly: Boolean, copiedAll: Boolean) {
|
||||
if (copyOnly) {
|
||||
|
|
|
@ -21,27 +21,17 @@ import android.widget.TextView
|
|||
import android.widget.Toast
|
||||
import com.github.ajalt.reprint.core.Reprint
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.helpers.BaseConfig
|
||||
import com.simplemobiletools.commons.helpers.PREFS_KEY
|
||||
import com.simplemobiletools.commons.helpers.*
|
||||
import com.simplemobiletools.commons.views.*
|
||||
import kotlinx.android.synthetic.main.dialog_title.view.*
|
||||
|
||||
fun Context.isOnMainThread() = Looper.myLooper() == Looper.getMainLooper()
|
||||
fun Context.getSharedPrefs() = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
|
||||
|
||||
fun Context.hasReadStoragePermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|
||||
fun Context.hasWriteStoragePermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|
||||
fun Context.hasCameraPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
|
||||
fun Context.hasRecordAudioPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
|
||||
|
||||
fun Context.isAndroidFour() = Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH
|
||||
|
||||
fun Context.isKitkatPlus() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
|
||||
|
||||
fun Context.isLollipopPlus() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
|
||||
|
||||
fun Context.isMarshmallowPlus() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
|
||||
|
||||
fun Context.isNougatPlus() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
|
||||
|
||||
fun Context.updateTextColors(viewGroup: ViewGroup, tmpTextColor: Int = 0, tmpAccentColor: Int = 0) {
|
||||
|
@ -52,33 +42,26 @@ fun Context.updateTextColors(viewGroup: ViewGroup, tmpTextColor: Int = 0, tmpAcc
|
|||
(0 until cnt)
|
||||
.map { viewGroup.getChildAt(it) }
|
||||
.forEach {
|
||||
if (it is MyTextView) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is MyAppCompatSpinner) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is MySwitchCompat) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is MyCompatRadioButton) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is MyAppCompatCheckbox) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is MyEditText) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is MyFloatingActionButton) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is MySeekBar) {
|
||||
it.setColors(textColor, accentColor, backgroundColor)
|
||||
} else if (it is ViewGroup) {
|
||||
updateTextColors(it, textColor, accentColor)
|
||||
when (it) {
|
||||
is MyTextView -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is MyAppCompatSpinner -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is MySwitchCompat -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is MyCompatRadioButton -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is MyAppCompatCheckbox -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is MyEditText -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is MyFloatingActionButton -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is MySeekBar -> it.setColors(textColor, accentColor, backgroundColor)
|
||||
is ViewGroup -> updateTextColors(it, textColor, accentColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getLinkTextColor(): Int {
|
||||
return if (baseConfig.primaryColor == resources.getColor(R.color.color_primary))
|
||||
return if (baseConfig.primaryColor == resources.getColor(R.color.color_primary)) {
|
||||
baseConfig.primaryColor
|
||||
else
|
||||
} else {
|
||||
baseConfig.textColor
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.setupDialogStuff(view: View, dialog: AlertDialog, titleId: Int = 0) {
|
||||
|
@ -148,3 +131,16 @@ fun Context.getLatestMediaId(uri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT
|
|||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
fun Context.hasPermission(permId: Int) = ContextCompat.checkSelfPermission(this, getPermissionString(permId)) == PackageManager.PERMISSION_GRANTED
|
||||
|
||||
fun Context.getPermissionString(id: Int) = when (id) {
|
||||
PERMISSION_READ_STORAGE -> Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
PERMISSION_WRITE_STORAGE -> Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||
PERMISSION_CAMERA -> Manifest.permission.CAMERA
|
||||
PERMISSION_RECORD_AUDIO -> Manifest.permission.RECORD_AUDIO
|
||||
PERMISSION_READ_CONTACTS -> Manifest.permission.READ_CONTACTS
|
||||
PERMISSION_WRITE_CALENDAR -> Manifest.permission.WRITE_CALENDAR
|
||||
else -> ""
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue