diff --git a/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationBuilder.kt b/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationBuilder.kt index d370f3ec..54080630 100644 --- a/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationBuilder.kt +++ b/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationBuilder.kt @@ -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 @@ -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) } @@ -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 @@ -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( diff --git a/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationService.kt b/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationService.kt index a30eeb67..bd99e250 100644 --- a/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationService.kt +++ b/app/src/main/java/de/seemoo/at_tracking_detection/notifications/NotificationService.kt @@ -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() @@ -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) ) } } @@ -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() { diff --git a/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt b/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt index 4a4577fc..5abc3a83 100644 --- a/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt +++ b/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt @@ -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!") @@ -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) diff --git a/app/src/main/java/de/seemoo/at_tracking_detection/ui/debug/DebugFragment.kt b/app/src/main/java/de/seemoo/at_tracking_detection/ui/debug/DebugFragment.kt index 1341f56e..0385ed1a 100644 --- a/app/src/main/java/de/seemoo/at_tracking_detection/ui/debug/DebugFragment.kt +++ b/app/src/main/java/de/seemoo/at_tracking_detection/ui/debug/DebugFragment.kt @@ -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 @@ -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 @@ -89,7 +91,20 @@ class DebugFragment : Fragment() { scanLeDevice() } view.findViewById