Skip to content

Commit

Permalink
clean up Notification Functions and fix DeviceType Display when acces…
Browse files Browse the repository at this point in the history
…sing TrackingFragment via Notification
  • Loading branch information
imbissbudenaesthetik authored and Sn0wfreezeDev committed Aug 8, 2024
1 parent 8c64ecf commit d195454
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import de.seemoo.at_tracking_detection.R
import de.seemoo.at_tracking_detection.database.models.device.BaseDevice
import de.seemoo.at_tracking_detection.database.models.device.DeviceManager
import de.seemoo.at_tracking_detection.database.models.device.DeviceType
import de.seemoo.at_tracking_detection.ui.MainActivity
import de.seemoo.at_tracking_detection.ui.TrackingNotificationActivity
import de.seemoo.at_tracking_detection.util.SharedPrefs
import de.seemoo.at_tracking_detection.util.risk.RiskLevelEvaluator
Expand Down Expand Up @@ -72,8 +71,9 @@ class NotificationBuilder @Inject constructor(
// }


private fun packBundle(deviceAddress: String, notificationId: Int): Bundle = Bundle().apply {
private fun packBundle(deviceAddress: String, deviceTypeString: String, notificationId: Int): Bundle = Bundle().apply {
putString("deviceAddress", deviceAddress)
putString("deviceTypeAsString", deviceTypeString)
putInt("notificationId", notificationId)
}

Expand All @@ -94,72 +94,21 @@ class NotificationBuilder @Inject constructor(
)
}

fun buildTrackingNotification(
deviceAddress: String,
notificationId: Int
): Notification {
Timber.d("Notification with id $notificationId for device $deviceAddress has been build!")
val bundle: Bundle = packBundle(deviceAddress, notificationId)
val minutesAtLeastTracked = RiskLevelEvaluator.getMinutesAtLeastTrackedBeforeAlarm()
val notifyText = context.resources.getQuantityString(
R.plurals.notification_text_base,
minutesAtLeastTracked.toInt(),
minutesAtLeastTracked
)

var notification = NotificationCompat.Builder(context, NotificationConstants.CHANNEL_ID)
.setContentTitle(context.getString(R.string.notification_title_base))
.setContentText(notifyText)
.setPriority(getNotificationPriority())
.setContentIntent(pendingNotificationIntent(bundle, notificationId))
.setCategory(getNotificationCategory())
.setSmallIcon(R.drawable.ic_warning)
.setStyle(NotificationCompat.BigTextStyle().bigText(notifyText))
.addAction(
R.drawable.ic_warning,
context.getString(R.string.notification_false_alarm),
buildPendingIntent(
bundle,
NotificationConstants.FALSE_ALARM_ACTION,
NotificationConstants.FALSE_ALARM_CODE
)
)

val deviceRepository = ATTrackingDetectionApplication.getCurrentApp().deviceRepository
val device = deviceRepository.getDevice(deviceAddress)

if (device?.deviceType != null && device.deviceType.canBeIgnored()) {
notification = notification.addAction(
R.drawable.ic_warning,
context.getString(R.string.notification_ignore_device),
buildPendingIntent(
bundle,
NotificationConstants.IGNORE_DEVICE_ACTION,
NotificationConstants.IGNORE_DEVICE_CODE
)
)
}

notification = notification.setDeleteIntent(
buildPendingIntent(
bundle,
NotificationConstants.DISMISSED_ACTION,
NotificationConstants.DISMISSED_CODE
)
).setAutoCancel(true)

return notification.build()

}

fun buildTrackingNotification(
baseDevice: BaseDevice,
notificationId: Int
): Notification {
Timber.d("Notification with id $notificationId for device ${baseDevice.address} has been build!")

val deviceAddress = baseDevice.address
val deviceTypeString: String = baseDevice.deviceType?.let {
DeviceManager.deviceTypeToString(
it
)
}
?: "UNKNOWN"

val bundle: Bundle = packBundle(deviceAddress, notificationId)
val bundle: Bundle = packBundle(deviceAddress, deviceTypeString, notificationId)
val device = baseDevice.device
val notificationText: String
val notificationTitle: String
Expand Down Expand Up @@ -223,12 +172,13 @@ class NotificationBuilder @Inject constructor(

fun buildObserveTrackerNotification(
deviceAddress: String,
deviceTypeString: String,
notificationId: Int,
observationDuration: Long,
observationPositive: Boolean
): Notification {
Timber.d("Notification with id $notificationId for device $deviceAddress has been build!")
val bundle: Bundle = packBundle(deviceAddress, notificationId)
val bundle: Bundle = packBundle(deviceAddress, deviceTypeString, notificationId)

val notifyText = if (observationPositive) {
context.resources.getQuantityString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ class NotificationService @Inject constructor(
private val notificationBuilder: NotificationBuilder,
private val notificationViewModel: NotificationViewModel
) {
@SuppressLint("MissingPermission")
suspend fun sendTrackingNotification(deviceAddress: String) {
val notificationId = generateUniqueNotificationId()
notificationViewModel.insert(deviceAddress)
with(notificationManagerCompat) {
if (this.areNotificationsEnabled()) {
notify(
TRACKING_NOTIFICATION_TAG,
notificationId,
notificationBuilder.buildTrackingNotification(deviceAddress, notificationId)
)
}
}
}

@SuppressLint("MissingPermission")
suspend fun sendTrackingNotification(baseDevice: BaseDevice) {
val notificationId = generateUniqueNotificationId()
Expand All @@ -58,14 +43,14 @@ class NotificationService @Inject constructor(
}

@SuppressLint("MissingPermission")
fun sendObserveTrackerNotification(deviceAddress: String, observationDuration: Long, observationPositive: Boolean) {
fun sendObserveTrackerNotification(deviceAddress: String, deviceTypeString: String, observationDuration: Long, observationPositive: Boolean) {
val notificationId = generateUniqueNotificationId()
with(notificationManagerCompat) {
if (this.areNotificationsEnabled()) {
notify(
OBSERVE_TRACKER_NOTIFICATION_TAG,
notificationId,
notificationBuilder.buildObserveTrackerNotification(deviceAddress, notificationId, observationDuration, observationPositive)
notificationBuilder.buildObserveTrackerNotification(deviceAddress, deviceTypeString, notificationId, observationDuration, observationPositive)
)
}
}
Expand All @@ -85,23 +70,6 @@ class NotificationService @Inject constructor(
}
}

/*
@SuppressLint("MissingPermission")
suspend fun sendObserveTrackerNotification(baseDevice: BaseDevice) {
val notificationId = notificationViewModel.insert(deviceAddress = baseDevice.address)
with(notificationManagerCompat) {
if (this.areNotificationsEnabled()) {
notify(
OBSERVE_TRACKER_NOTIFICATION_TAG,
notificationId,
notificationBuilder.buildTrackingNotification(baseDevice, notificationId)
)
}
}
}
*/


@SuppressLint("MissingPermission")
fun sendBLEErrorNotification() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TrackingNotificationActivity : AppCompatActivity() {
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!")

Expand All @@ -32,6 +33,7 @@ class TrackingNotificationActivity : AppCompatActivity() {
} else {
val args = TrackingFragmentArgs(
deviceAddress = deviceAddress,
deviceTypeAsString = deviceTypeAsString,
notificationId = notificationId
).toBundle()
navController.setGraph(R.navigation.tracking_navigation, args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import de.seemoo.at_tracking_detection.ATTrackingDetectionApplication
import de.seemoo.at_tracking_detection.R
import de.seemoo.at_tracking_detection.database.models.device.BaseDevice
import de.seemoo.at_tracking_detection.database.models.device.DeviceManager
import de.seemoo.at_tracking_detection.database.models.device.DeviceType
import de.seemoo.at_tracking_detection.databinding.FragmentDebugBinding
import de.seemoo.at_tracking_detection.notifications.NotificationService
import de.seemoo.at_tracking_detection.statistics.api.Api
Expand All @@ -36,6 +37,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import timber.log.Timber
import java.time.LocalDateTime
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -89,7 +91,20 @@ class DebugFragment : Fragment() {
scanLeDevice()
}
view.findViewById<Button>(R.id.button2)?.setOnClickListener {
debugViewModel.viewModelScope.launch { notificationService.sendTrackingNotification("Some device address") }
val testBaseDevice = BaseDevice(
address = "00:11:22:33:44:55",
ignore = false,
connectable = true,
payloadData = null,
firstDiscovery = LocalDateTime.now(),
lastSeen = LocalDateTime.now(),
deviceType = DeviceType.AIRTAG
).apply {
notificationSent = true
lastNotificationSent = LocalDateTime.now()
}

debugViewModel.viewModelScope.launch { notificationService.sendTrackingNotification(testBaseDevice) }
}
view.findViewById<ListView>(R.id.bluetoothList)
.setOnItemClickListener { _, _, position, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fun setDeviceName (textView: TextView, wrappedScanResult: ScanResultWrapper) {
if (deviceFromDb?.name != null) {
textView.text = deviceFromDb.getDeviceNameWithID()
} else {
// TODO: this can be optimized
val device = BaseDevice(wrappedScanResult.scanResult).device
textView.text = device.deviceContext.defaultDeviceName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import de.seemoo.at_tracking_detection.ATTrackingDetectionApplication
import de.seemoo.at_tracking_detection.database.models.device.DeviceManager
import de.seemoo.at_tracking_detection.detection.ScanBluetoothWorker
import timber.log.Timber

Expand All @@ -16,8 +17,8 @@ class ObserveTrackerWorker(

override suspend fun doWork(): Result {
Timber.d("ObserveTrackerWorker doWork() called")
val deviceRepository = ATTrackingDetectionApplication.getCurrentApp()?.deviceRepository ?: return Result.failure()
val notificationService = ATTrackingDetectionApplication.getCurrentApp()?.notificationService ?: return Result.failure()
val deviceRepository = ATTrackingDetectionApplication.getCurrentApp().deviceRepository
val notificationService = ATTrackingDetectionApplication.getCurrentApp().notificationService

// Call ScanBluetoothWorker to scan for devices
val workRequest = OneTimeWorkRequest.Builder(ScanBluetoothWorker::class.java)
Expand All @@ -40,7 +41,15 @@ class ObserveTrackerWorker(
Timber.d("Observation for device ${device.address} is over... Sending Notification!")
device.nextObservationNotification = null
device.currentObservationDuration = null
notificationService.sendObserveTrackerNotification(device.address, currentObservationDuration, observationPositive)

val deviceTypeString: String = device.deviceType?.let {
DeviceManager.deviceTypeToString(
it
)
}
?: "UNKNOWN"

notificationService.sendObserveTrackerNotification(device.address, deviceTypeString, currentObservationDuration, observationPositive)

// Update device
deviceRepository.update(device)
Expand Down

0 comments on commit d195454

Please sign in to comment.