Skip to content

Commit

Permalink
fix new Device not loading correctly when accessing a Device through …
Browse files Browse the repository at this point in the history
…a Notification while another device is already opened
  • Loading branch information
imbissbudenaesthetik committed Dec 4, 2024
1 parent aac4975 commit aa88ff9
Showing 1 changed file with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.seemoo.at_tracking_detection.ui

import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -23,31 +24,7 @@ class TrackingNotificationActivity : AppCompatActivity() {
val navHostFragment = supportFragmentManager.findFragmentById(R.id.tracking_host_fragment) as NavHostFragment
navController = navHostFragment.navController

val deviceAddress = intent.getStringExtra("deviceAddress")
val deviceTypeAsString = intent.getStringExtra("deviceTypeAsString") ?: "UNKNOWN"
val notificationId = intent.getIntExtra("notificationId", -1)
Timber.d("Tracking Activity with device $deviceAddress and notification $notificationId started!")

if (deviceAddress == null) {
Timber.e("Device address is needed! Going home...")
this.onSupportNavigateUp()
} else {
// Workaround: Somehow not possible to use getString with deviceAddress as an Argument
var getTitle = getString(R.string.title_devices_tracking)
getTitle = getTitle.replace("{deviceAddress}", deviceAddress.toString())
supportActionBar?.title = getTitle

val args = TrackingFragmentArgs(
deviceAddress = deviceAddress,
deviceTypeAsString = deviceTypeAsString,
notificationId = notificationId
).toBundle()
navController.setGraph(R.navigation.main_navigation)
val navOptions = NavOptions.Builder()
// .setPopUpTo(R.id.navigation_dashboard, true)
.build()
navController.navigate(R.id.trackingFragment, args, navOptions)
}
navigateToTrackingFragment()

onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Expand Down Expand Up @@ -86,4 +63,39 @@ class TrackingNotificationActivity : AppCompatActivity() {
}
}
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)

navigateToTrackingFragment()
}

private fun navigateToTrackingFragment() {
val deviceAddress = intent.getStringExtra("deviceAddress")
val deviceTypeAsString = intent.getStringExtra("deviceTypeAsString") ?: "UNKNOWN"
val notificationId = intent.getIntExtra("notificationId", -1)
Timber.d("Tracking Activity with device $deviceAddress and notification $notificationId started!")

if (deviceAddress == null) {
Timber.e("Device address is needed! Going home...")
this.onSupportNavigateUp()
} else {
// Workaround: Somehow not possible to use getString with deviceAddress as an Argument
var getTitle = getString(R.string.title_devices_tracking)
getTitle = getTitle.replace("{deviceAddress}", deviceAddress.toString())
supportActionBar?.title = getTitle

val args = TrackingFragmentArgs(
deviceAddress = deviceAddress,
deviceTypeAsString = deviceTypeAsString,
notificationId = notificationId
).toBundle()
navController.setGraph(R.navigation.main_navigation)
val navOptions = NavOptions.Builder()
// .setPopUpTo(R.id.navigation_dashboard, true)
.build()
navController.navigate(R.id.trackingFragment, args, navOptions)
}
}
}

0 comments on commit aa88ff9

Please sign in to comment.