Skip to content

Commit

Permalink
add prominent disclosure before for location
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Feb 12, 2024
1 parent 215883b commit c26289d
Showing 1 changed file with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.treehouses.remote.bases

import android.Manifest
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
Expand All @@ -15,46 +14,63 @@ import androidx.core.content.ContextCompat
import io.treehouses.remote.R

abstract class PermissionActivity : AppCompatActivity() {
private fun checkPermission(strPermission: String?): Boolean {
val result = ContextCompat.checkSelfPermission(this, strPermission!!)
return result == PackageManager.PERMISSION_GRANTED
private fun areLocationPermissionsGranted(): Boolean {
return checkPermission(Manifest.permission.ACCESS_FINE_LOCATION) && checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
}

private fun checkPermission(strPermission: String): Boolean {
return ContextCompat.checkSelfPermission(this, strPermission) == PackageManager.PERMISSION_GRANTED
}

private fun statusCheck() {
val manager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps()
if (areLocationPermissionsGranted()) {
val manager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps()
}
} else {
showProminentDisclosure()
}
}

private fun showProminentDisclosure() {
AlertDialog.Builder(this)
.setTitle("Permission & GPS Usage")
.setMessage("This app collects location data in the background to estimate the radius " +
"from the nearest town, determining community users' general locations. " +
"This helps in targeting support and organizing events by understanding user " +
"distribution. To continue, you must enable GPS.")
.setPositiveButton("Accept") { _, _ -> requestAllPermissions() }
.setNegativeButton("Deny") { dialog, _ -> dialog.cancel() }
.show()
}

private fun requestAllPermissions() {
ActivityCompat.requestPermissions(this, arrayOf(
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
), PERMISSION_REQUEST_WIFI)
}

private fun buildAlertMessageNoGps() {
val builder = AlertDialog.Builder(ContextThemeWrapper(this, R.style.CustomAlertDialogStyle))
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes") { _: DialogInterface?, _: Int -> startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) }
.setNegativeButton("No") { dialog: DialogInterface, _: Int -> dialog.cancel() }
.setCancelable(false)
.setPositiveButton("Yes") { _, _ -> startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) }
.setNegativeButton("No") { dialog, _ -> dialog.cancel() }
val alert = builder.create()
alert.window!!.setBackgroundDrawableResource(android.R.color.transparent)
alert.window?.setBackgroundDrawableResource(android.R.color.transparent)
alert.show()
}

fun requestPermission() {
if (!checkPermission(Manifest.permission.ACCESS_FINE_LOCATION) ||
!checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION) ||
!checkPermission(Manifest.permission.CHANGE_WIFI_STATE) ||
!checkPermission(Manifest.permission.BLUETOOTH) ||
!checkPermission(Manifest.permission.BLUETOOTH_ADMIN) ||
!checkPermission(Manifest.permission.BLUETOOTH_CONNECT) ||
!checkPermission(Manifest.permission.BLUETOOTH_CONNECT)) {
ActivityCompat.requestPermissions(this, arrayOf(
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN,
), PERMISSION_REQUEST_WIFI)
if (!areLocationPermissionsGranted()) {
showProminentDisclosure()
} else {
statusCheck()
}
Expand All @@ -63,8 +79,7 @@ abstract class PermissionActivity : AppCompatActivity() {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_WIFI) {
if (grantResults.isNotEmpty()
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
statusCheck()
}
}
Expand All @@ -73,4 +88,4 @@ abstract class PermissionActivity : AppCompatActivity() {
companion object {
private const val PERMISSION_REQUEST_WIFI = 111
}
}
}

0 comments on commit c26289d

Please sign in to comment.