Skip to content

Commit

Permalink
add requesting permissions and checking device settings
Browse files Browse the repository at this point in the history
  • Loading branch information
solygambas committed Apr 18, 2022
1 parent 34d225d commit 1d50532
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
5 changes: 3 additions & 2 deletions 19-treasure-hunt/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.treasureHunt">

<!-- TODO: Step 1 add in permissions for fine location and background-->

<!-- Step 1 add in permissions for fine location and background-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class HuntMainActivity : AppCompatActivity() {
private lateinit var geofencingClient: GeofencingClient
private lateinit var viewModel: GeofenceViewModel

// TODO: Step 2 add in variable to check if device is running Q or later
// Step 2 add in variable to check if device is running Q or later
private val runningQOrLater = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q

// A PendingIntent for the Broadcast Receiver that handles geofence transitions.
// TODO: Step 8 add in a pending intent
Expand Down Expand Up @@ -90,8 +91,11 @@ class HuntMainActivity : AppCompatActivity() {
*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// TODO: Step 7 add code to check that the user turned on their device location and ask
// Step 7 add code to check that the user turned on their device location and ask
// again if they did not
if (requestCode == REQUEST_TURN_DEVICE_LOCATION_ON) {
checkDeviceLocationSettingsAndStartGeofence()
}
}

/*
Expand Down Expand Up @@ -119,7 +123,26 @@ class HuntMainActivity : AppCompatActivity() {
permissions: Array<String>,
grantResults: IntArray
) {
// TODO: Step 5 add code to handle the result of the user's permission
// Step 5 add code to handle the result of the user's permission
Log.d(TAG, "onRequestPermissionResult")
if (grantResults.isEmpty() ||
grantResults[LOCATION_PERMISSION_INDEX] == PackageManager.PERMISSION_DENIED ||
(requestCode == REQUEST_FOREGROUND_AND_BACKGROUND_PERMISSION_RESULT_CODE &&
grantResults[BACKGROUND_LOCATION_PERMISSION_INDEX] == PackageManager.PERMISSION_DENIED)) {
Snackbar.make(
binding.activityMapsMain,
R.string.permission_denied_explanation,
Snackbar.LENGTH_INDEFINITE
).setAction(R.string.settings) {
startActivity(Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
data = Uri.fromParts("package", BuildConfig.APPLICATION_ID, null)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
}.show()
} else {
checkDeviceLocationSettingsAndStartGeofence()
}
}

/**
Expand Down Expand Up @@ -149,7 +172,36 @@ class HuntMainActivity : AppCompatActivity() {
* the opportunity to turn on location services within our app.
*/
private fun checkDeviceLocationSettingsAndStartGeofence(resolve:Boolean = true) {
// TODO: Step 6 add code to check that the device's location is on
// Step 6 add code to check that the device's location is on
val locationRequest = LocationRequest.create().apply {
priority = LocationRequest.PRIORITY_LOW_POWER
}
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
val settingsClient = LocationServices.getSettingsClient(this)
val locationSettingsResponseTask = settingsClient.checkLocationSettings(builder.build())
locationSettingsResponseTask.addOnFailureListener { exception ->
if (exception is ResolvableApiException && resolve) {
try {
exception.startResolutionForResult(this@HuntMainActivity,
REQUEST_TURN_DEVICE_LOCATION_ON)
} catch (sendEx: IntentSender.SendIntentException) {
Log.d(TAG, "Error getting location settings resolution: " + sendEx.message)
}
} else {
Snackbar.make(
binding.activityMapsMain,
R.string.location_required_error,
Snackbar.LENGTH_INDEFINITE
).setAction(android.R.string.ok) {
checkDeviceLocationSettingsAndStartGeofence()
}.show()
}
}
locationSettingsResponseTask.addOnCompleteListener {
if (it.isSuccessful) {
addGeofenceForClue()
}
}
}

/*
Expand All @@ -158,17 +210,45 @@ class HuntMainActivity : AppCompatActivity() {
*/
@TargetApi(29)
private fun foregroundAndBackgroundLocationPermissionApproved(): Boolean {
// TODO: Step 3 replace this with code to check that the foreground and background
// Step 3 replace this with code to check that the foreground and background
// permissions were approved
return false
val foregroundLocationApproved = (
PackageManager.PERMISSION_GRANTED ==
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION))
val backgroundPermissionApproved =
if (runningQOrLater) {
PackageManager.PERMISSION_GRANTED ==
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
true
}
return foregroundLocationApproved && backgroundPermissionApproved
}

/*
* Requests ACCESS_FINE_LOCATION and (on Android 10+ (Q) ACCESS_BACKGROUND_LOCATION.
*/
@TargetApi(29 )
private fun requestForegroundAndBackgroundLocationPermissions() {
// TODO: Step 4 add code to request foreground and background permissions
// Step 4 add code to request foreground and background permissions
if (foregroundAndBackgroundLocationPermissionApproved())
return
var permissionsArray = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
var resultCode = when {
runningQOrLater -> {
permissionsArray += Manifest.permission.ACCESS_BACKGROUND_LOCATION
REQUEST_FOREGROUND_AND_BACKGROUND_PERMISSION_RESULT_CODE
}
else -> REQUEST_FOREGROUND_ONLY_PERMISSIONS_REQUEST_CODE
}
Log.d(TAG, "Request foreground only location permission")
ActivityCompat.requestPermissions(
this@HuntMainActivity,
permissionsArray,
resultCode
)
}

/*
Expand Down

0 comments on commit 1d50532

Please sign in to comment.