Created
July 30, 2019 17:06
-
-
Save FilipeLipan/7db08fbb28296847d842f6e74966f89e to your computer and use it in GitHub Desktop.
This file contains 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 br.com.botpag.ui.payment | |
import android.Manifest | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import androidx.core.app.ActivityCompat | |
import androidx.core.content.ContextCompat | |
import androidx.fragment.app.FragmentActivity | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleObserver | |
import androidx.lifecycle.OnLifecycleEvent | |
import android.content.DialogInterface | |
import android.util.Log | |
import androidx.appcompat.app.AlertDialog | |
import br.com.botpag.R | |
import android.net.Uri | |
import android.provider.Settings | |
class PaymentPermissionHandler(var activity: FragmentActivity?,val onSuccess: () -> Unit) : LifecycleObserver { | |
companion object { | |
val REQUEST_ID_MULTIPLE_PERMISSIONS: Int = 1456 | |
val TAG: String = "PaymentPermission" | |
} | |
init { | |
activity?.lifecycle?.addObserver(this) | |
} | |
fun checkAndRequestPermissions(): Boolean { | |
activity?.let{ | |
val phonestate = ContextCompat.checkSelfPermission(it, Manifest.permission.READ_PHONE_STATE) | |
val location = ContextCompat.checkSelfPermission(it, Manifest.permission.ACCESS_FINE_LOCATION) | |
val locationCoarse = ContextCompat.checkSelfPermission(it, Manifest.permission.ACCESS_COARSE_LOCATION) | |
val write = ContextCompat.checkSelfPermission(it, Manifest.permission.WRITE_EXTERNAL_STORAGE) | |
val read= ContextCompat.checkSelfPermission(it, Manifest.permission.READ_EXTERNAL_STORAGE) | |
val listPermissionsNeeded = arrayListOf<String>() | |
if (phonestate != PackageManager.PERMISSION_GRANTED) { | |
listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE) | |
} | |
if (location != PackageManager.PERMISSION_GRANTED) { | |
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION) | |
} | |
if (locationCoarse != PackageManager.PERMISSION_GRANTED) { | |
listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION) | |
} | |
if (write != PackageManager.PERMISSION_GRANTED) { | |
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE) | |
} | |
if (read != PackageManager.PERMISSION_GRANTED) { | |
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE) | |
} | |
if (listPermissionsNeeded.isNotEmpty()) { | |
requestPermissions(listPermissionsNeeded) | |
return false | |
} | |
} | |
return true | |
} | |
private fun requestPermissions(listofPermissionsNeeded: ArrayList<String>) { | |
activity?.let { | |
ActivityCompat.requestPermissions( | |
it, | |
listofPermissionsNeeded.toTypedArray(), | |
REQUEST_ID_MULTIPLE_PERMISSIONS) | |
} | |
} | |
fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
when (requestCode) { | |
REQUEST_ID_MULTIPLE_PERMISSIONS -> { | |
val perms = mutableMapOf<String, Int>() | |
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED) | |
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED) | |
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED) | |
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED) | |
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED) | |
// Fill with actual results from user | |
if (grantResults.size > 0) { | |
for (i in permissions.indices) | |
perms.put(permissions[i], grantResults[i]) | |
// Check for both permissions | |
if (perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED | |
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED | |
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED | |
&& perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED | |
&& perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { | |
Log.d(TAG, "permissions success") | |
onSuccess() | |
} else { | |
Log.d(TAG, "Some permissions are not granted ask again ") | |
activity?.let { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(it, Manifest.permission.READ_PHONE_STATE) | |
|| ActivityCompat.shouldShowRequestPermissionRationale(it,Manifest.permission.ACCESS_FINE_LOCATION) | |
|| ActivityCompat.shouldShowRequestPermissionRationale(it,Manifest.permission.ACCESS_COARSE_LOCATION) | |
|| ActivityCompat.shouldShowRequestPermissionRationale(it,Manifest.permission.WRITE_EXTERNAL_STORAGE) | |
|| ActivityCompat.shouldShowRequestPermissionRationale(it,Manifest.permission.READ_EXTERNAL_STORAGE)) { | |
showDialogOK(it.getString(R.string.payment_permission_request_description), | |
DialogInterface.OnClickListener { dialog, which -> | |
when (which) { | |
DialogInterface.BUTTON_POSITIVE -> checkAndRequestPermissions() | |
DialogInterface.BUTTON_NEGATIVE -> | |
dialog.dismiss() | |
} | |
}) | |
} else { | |
explain(it.getString(R.string.payment_permissions_settings_message)) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
private fun showDialogOK(message: String, okListener: DialogInterface.OnClickListener) { | |
activity?.let { | |
AlertDialog.Builder(it) | |
.setMessage(message) | |
.setPositiveButton(it.getString(R.string.ok), okListener) | |
.setNegativeButton(it.getString(R.string.cancel), okListener) | |
.create() | |
.show() | |
} | |
} | |
private fun explain(msg: String) { | |
activity?.let { | |
val dialog = AlertDialog.Builder(it) | |
dialog.setMessage(msg) | |
.setPositiveButton(it.getString(R.string.ok)) { paramDialogInterface, paramInt -> | |
startInstalledAppDetailsActivity() | |
} | |
.setNegativeButton(it.getString(R.string.cancel)) { paramDialogInterface, paramInt -> | |
// dialog.create().dismiss() | |
paramDialogInterface.dismiss() | |
} | |
dialog.show() | |
} | |
} | |
private fun startInstalledAppDetailsActivity() { | |
activity?.let { | |
val i = Intent() | |
i.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS | |
i.addCategory(Intent.CATEGORY_DEFAULT) | |
i.data = Uri.parse("package:" + it.packageName) | |
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | |
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) | |
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) | |
it.startActivity(i) | |
} | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | |
fun onDestroy(){ | |
activity = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment