You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.0 KiB

package xyz.myachin.saveto.ui
import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.PermissionChecker
import xyz.myachin.saveto.R
class MainActivity : AppCompatActivity(), View.OnClickListener {
companion object {
const val TAG = "MainAct"
const val READ_ES_PERM = Manifest.permission.READ_EXTERNAL_STORAGE
const val REQ_PERM = 10
}
private var btSaveNow: Button? = null
private var tvReqNow: TextView? = null
private var dv: View? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkPerms()
}
private fun checkPerms() {
if (PermissionChecker.checkSelfPermission(
this,
READ_ES_PERM
) != PermissionChecker.PERMISSION_GRANTED
) {
dv = findViewById(R.id.divider)
tvReqNow = findViewById(R.id.tvReqNow)
btSaveNow = findViewById(R.id.btRequestNow)
setPermsVisible()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
REQ_PERM -> {
when (grantResults[0]) {
PermissionChecker.PERMISSION_GRANTED -> {
setPermsGone()
}
PermissionChecker.PERMISSION_DENIED, PermissionChecker.PERMISSION_DENIED_APP_OP -> {
tvReqNow?.text = getString(R.string.tvSetPermsManual)
btSaveNow?.visibility = View.GONE
val btReqMan = findViewById<Button>(R.id.btRequestManual)
btReqMan.visibility = View.VISIBLE
btReqMan.setOnClickListener(this)
}
}
}
}
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.btRequestNow -> {
requestPermissions(arrayOf(READ_ES_PERM), REQ_PERM)
}
R.id.btRequestManual -> {
Intent().apply {
action = Intent.ACTION_AUTO_REVOKE_PERMISSIONS
data = Uri.fromParts("package", applicationContext.packageName, null)
}.also {
startActivity(it)
}
}
}
}
private fun setPermsVisible() {
dv?.visibility = View.VISIBLE
tvReqNow?.visibility = View.VISIBLE
btSaveNow?.visibility = View.VISIBLE
btSaveNow?.setOnClickListener(this)
}
private fun setPermsGone() {
dv?.visibility = View.GONE
tvReqNow?.visibility = View.GONE
btSaveNow?.visibility = View.GONE
}
}