Skip to content

Commit

Permalink
display location disclosure only if permission is not granted
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Feb 23, 2024
1 parent af66406 commit 5d52bc8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 56 deletions.
61 changes: 47 additions & 14 deletions app/src/main/kotlin/io/treehouses/remote/InitialActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.treehouses.remote

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
Expand All @@ -14,7 +15,10 @@ import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.preference.PreferenceManager
Expand Down Expand Up @@ -141,10 +145,14 @@ class InitialActivity : BaseInitialActivity() {

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 99) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this@InitialActivity, "Permissions Granted", Toast.LENGTH_SHORT).show()
} //TODO re-request
when (requestCode) {
REQUEST_LOCATION_PERMISSION_FOR_COMMUNITY -> {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
dataSharing()
} else {
Toast.makeText(this, "Permission denied. Cannot proceed to community features.", Toast.LENGTH_SHORT).show()
}
}
}
}

Expand Down Expand Up @@ -172,27 +180,48 @@ class InitialActivity : BaseInitialActivity() {
FeedbackDialogFragment().show(supportFragmentManager.beginTransaction(), "feedbackDialogFragment")
}
R.id.action_community -> {
preferences = PreferenceManager.getDefaultSharedPreferences(this)
val v = layoutInflater.inflate(R.layout.alert_log_map, null)
if (!preferences?.getBoolean("send_log", false)!!) {
val builder = DialogUtils.createAlertDialog(this@InitialActivity, "Sharing is Caring.", "The community map is only available with data sharing. " +
"Please enable data sharing to access this feature.", v).setCancelable(false)
DialogUtils.createAdvancedDialog(builder, Pair("Enable Data Sharing", "Cancel"), {
preferences!!.edit().putBoolean("send_log", true).apply()
goToCommunity()
}, {MainApplication.showLogDialog = false })
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
showLocationPermissionDisclosureForCommunity()
} else {
dataSharing()
}
else { goToCommunity() }
}
}
return super.onOptionsItemSelected(item)
}

private fun dataSharing() {
preferences = PreferenceManager.getDefaultSharedPreferences(this)
val v = layoutInflater.inflate(R.layout.alert_log_map, null)
if (!preferences?.getBoolean("send_log", false)!!) {
val builder = DialogUtils.createAlertDialog(this@InitialActivity, "Sharing is Caring.", "The community map is only available with data sharing. " +
"Please enable data sharing to access this feature.", v).setCancelable(false)
DialogUtils.createAdvancedDialog(builder, Pair("Enable Data Sharing", "Cancel"), {
preferences!!.edit().putBoolean("send_log", true).apply()
goToCommunity()
}, {MainApplication.showLogDialog = false })
} else {
goToCommunity()
}
}

private fun goToCommunity() {
openCallFragment(CommunityFragment())
title = getString(R.string.action_community)
}

private fun showLocationPermissionDisclosureForCommunity() {
AlertDialog.Builder(this)
.setTitle("Location & GPS Usage")
.setMessage("This app needs to collect 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 Location.")
.setPositiveButton("Yes") { _, _ ->
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), REQUEST_LOCATION_PERMISSION_FOR_COMMUNITY)
}.setNegativeButton("No", null).show()
}

fun changeAppBar() {
mActionBarDrawerToggle = ActionBarDrawerToggle(this, bind.drawerLayout, findViewById(R.id.toolbar), 0, 0)
mActionBarDrawerToggle.toolbarNavigationClickListener = View.OnClickListener {
Expand All @@ -213,4 +242,8 @@ class InitialActivity : BaseInitialActivity() {
mActionBarDrawerToggle.isDrawerIndicatorEnabled = true
bind.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
}

companion object {
private const val REQUEST_LOCATION_PERMISSION_FOR_COMMUNITY = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,18 @@ import androidx.core.content.ContextCompat
import io.treehouses.remote.R

abstract class PermissionActivity : AppCompatActivity() {
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
val result = ContextCompat.checkSelfPermission(this, strPermission)
return result == PackageManager.PERMISSION_GRANTED
}

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

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,
Manifest.permission.POST_NOTIFICATIONS
), 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?")
Expand All @@ -70,23 +38,68 @@ abstract class PermissionActivity : AppCompatActivity() {
}

fun requestPermission() {
if (!areLocationPermissionsGranted()) {
showProminentDisclosure()
showLocationPermissionDisclosure()
}

private fun showLocationPermissionDisclosure() {
val builder = AlertDialog.Builder(ContextThemeWrapper(this, R.style.CustomAlertDialogStyle))
builder.setTitle("Location & GPS Usage")
.setMessage("This app needs to collect 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 Location.")
.setPositiveButton("Yes") { _, _ -> proceedWithLocationPermission() }
.setNegativeButton("No") { _, _ -> proceedWithoutLocationPermission() }
.show()
}

private fun proceedWithLocationPermission() {
val permissionsToRequest = mutableListOf(
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
)

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU && !checkPermission(Manifest.permission.POST_NOTIFICATIONS)) {
permissionsToRequest.add(Manifest.permission.POST_NOTIFICATIONS)
}

if (permissionsToRequest.any { !checkPermission(it) }) {
ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(), PERMISSION_REQUEST_WIFI)
} else {
statusCheck()
}
}

private fun proceedWithoutLocationPermission() {
val permissionsToRequest = mutableListOf(
Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
)

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU && !checkPermission(Manifest.permission.POST_NOTIFICATIONS)) {
permissionsToRequest.add(Manifest.permission.POST_NOTIFICATIONS)
}

if (permissionsToRequest.any { !checkPermission(it) }) {
ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(), PERMISSION_REQUEST_WIFI)
}
}

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) {
statusCheck()
if (grantResults.isNotEmpty() && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
statusCheck()
}
}
}
}

companion object {
private const val PERMISSION_REQUEST_WIFI = 111
}
}
}

0 comments on commit 5d52bc8

Please sign in to comment.