Skip to content

Commit

Permalink
fix: better handling of back button in the MapActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
growse committed Oct 2, 2024
1 parent 01beabd commit bb71eff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
1 change: 1 addition & 0 deletions project/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:enableOnBackInvokedCallback="true"
android:theme="@style/Theme.Owntracks"
tools:ignore="UnusedAttribute"
tools:replace="android:allowBackup">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.owntracks.android.ui.map

import android.Manifest.permission.POST_NOTIFICATIONS
import android.annotation.SuppressLint
import android.content.ActivityNotFoundException
import android.content.ComponentName
Expand All @@ -16,19 +17,24 @@ import android.os.Bundle
import android.os.IBinder
import android.provider.Settings
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
import android.text.TextUtils.replace
import android.util.TypedValue
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.annotation.RequiresPermission
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.appcompat.widget.TooltipCompat
import androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.content.ContextCompat.startActivity
import androidx.core.view.setPadding
import androidx.core.widget.ImageViewCompat
import androidx.databinding.BindingAdapter
Expand Down Expand Up @@ -93,7 +99,7 @@ class MapActivity :
private var orientationSensor: Sensor? = null
private lateinit var binding: UiMapBinding

private lateinit var locationServicesAlertDialog: AlertDialog
private lateinit var backPressedCallback: OnBackPressedCallback

@Inject lateinit var notificationsStash: NotificationsStash

Expand Down Expand Up @@ -238,7 +244,21 @@ class MapActivity :
}
.also { listener -> labels.forEach { it.withListener(listener) } }
}

backPressedCallback =
onBackPressedDispatcher.addCallback(this, false) {
Timber.w("ARSE")
when (bottomSheetBehavior?.state) {
BottomSheetBehavior.STATE_COLLAPSED -> {
setBottomSheetHidden()
}
BottomSheetBehavior.STATE_EXPANDED -> {
setBottomSheetCollapsed()
}
else -> {
Timber.w("HMM")
}
}
}
setBottomSheetHidden()

viewModel.currentContact.observe(this) { contact: Contact? ->
Expand Down Expand Up @@ -274,31 +294,6 @@ class MapActivity :
NotificationManagerCompat.from(this).cancel(BACKGROUND_LOCATION_RESTRICTION_NOTIFICATION_TAG, 0)

notifyOnWorkManagerInitFailure(this)

onBackPressedDispatcher.addCallback(this) {
if (bottomSheetBehavior == null) {
finish()
} else {
when (bottomSheetBehavior?.state) {
BottomSheetBehavior.STATE_HIDDEN -> finish()
BottomSheetBehavior.STATE_COLLAPSED -> {
setBottomSheetHidden()
}
BottomSheetBehavior.STATE_DRAGGING -> {
// Noop
}
BottomSheetBehavior.STATE_EXPANDED -> {
setBottomSheetCollapsed()
}
BottomSheetBehavior.STATE_HALF_EXPANDED -> {
setBottomSheetCollapsed()
}
BottomSheetBehavior.STATE_SETTLING -> {
// Noop
}
}
}
}
}

private fun navigateToCurrentContact() {
Expand Down Expand Up @@ -385,6 +380,7 @@ class MapActivity :
}

/** User has granted notification permission. Notify everything that's in the NotificationStash */
@RequiresPermission(POST_NOTIFICATIONS)
private fun notificationPermissionGranted() {
Timber.d("Notification permission granted. Showing all notifications in stash")
notificationsStash.showAll(NotificationManagerCompat.from(this))
Expand Down Expand Up @@ -672,6 +668,7 @@ class MapActivity :
orientationSensor?.let {
sensorManager?.registerListener(viewModel.orientationSensorEventListener, it, SENSOR_DELAY_UI)
}
backPressedCallback.isEnabled = true
}

// BOTTOM SHEET CALLBACKS
Expand All @@ -683,13 +680,15 @@ class MapActivity :
bottomSheetBehavior!!.state = BottomSheetBehavior.STATE_COLLAPSED
binding.mapFragment.setPadding(0)
sensorManager?.unregisterListener(viewModel.orientationSensorEventListener)
backPressedCallback.isEnabled = true
}

private fun setBottomSheetHidden() {
bottomSheetBehavior!!.state = BottomSheetBehavior.STATE_HIDDEN
binding.mapFragment.setPadding(0)
menu?.run { close() }
sensorManager?.unregisterListener(viewModel.orientationSensorEventListener)
backPressedCallback.isEnabled = false
}

override fun onStart() {
Expand Down
6 changes: 4 additions & 2 deletions project/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
agp = "8.6.1"
androidx-activity = "1.9.2"
androidx-concurrent = "1.2.0"
androidx-core = "1.13.1"
androidx-core-testing = "2.2.0"
Expand Down Expand Up @@ -55,7 +56,7 @@ triplet = "3.11.0"
android-application = { id = "com.android.application", version.ref = "agp" }
hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-jvm= { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
triplet = { id = "com.github.triplet.play", version.ref = "triplet" }
ktfmt = { id = "com.ncorti.ktfmt.gradle", version.ref = "ktfmt" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
Expand Down Expand Up @@ -89,6 +90,7 @@ jackson-datatype-jsr310 = { module = "com.fasterxml.jackson.datatype:jackson-dat
# AndroidX
androidx-preference = { module = "androidx.preference:preference", version.ref = "androidx-preference" }
androidx-work-runtime = { module = "androidx.work:work-runtime-ktx", version.ref = "androidx-work" }
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "androidx-activity" }
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
androidx-core-testing = { module = "androidx.arch.core:core-testing", version.ref = "androidx-core-testing" }
Expand Down Expand Up @@ -150,7 +152,7 @@ kotlin = ["kotlin-coroutines-android", "kotlin-coroutines-jdk8", "kotlin-reflect
jaxb-annotation-processors = ["jaxb-api", "jaxb-core", "jaxb-impl"]
kapt-hilt = ["hilt-compiler", "hilt-androidx"]
hilt = ["hilt-android", "hilt-work"]
androidx = ["androidx-core-ktx", "androidx-preference", "androidx-work-runtime", "androidx-fragment-ktx", "androidx-concurrent-futures"]
androidx = ["androidx-core-ktx", "androidx-preference", "androidx-work-runtime", "androidx-fragment-ktx", "androidx-concurrent-futures", "androidx-activity-ktx"]
androidx-room = ["androidx-room-runtime", "androidx-room-ktx"]
androidx-test = ["androidx-test-junit", "androidx-test-core-ktx", "androidx-test-rules", "androidx-test-runner"]
androidx-test-util = ["androidx-test-services", "androidx-test-orchestrator"]
Expand Down

0 comments on commit bb71eff

Please sign in to comment.