Skip to content

Commit

Permalink
Merge pull request #330 from thgoebel/refactor-data-classes
Browse files Browse the repository at this point in the history
Refactor data classes
  • Loading branch information
simondankelmann authored Jan 6, 2025
2 parents ac41e63 + ad60971 commit fb474a1
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.simon.dankelmann.bluetoothlespam.Enums

import de.simon.dankelmann.bluetoothlespam.R


enum class AdvertisementSetType {
ADVERTISEMENT_TYPE_UNDEFINED,
ADVERTISEMENT_TYPE_FAST_PAIRING_DEVICE,
Expand All @@ -16,4 +19,29 @@ enum class AdvertisementSetType {
ADVERTISEMENT_TYPE_EASY_SETUP_BUDS,
ADVERTISEMENT_TYPE_LOVESPOUSE_PLAY,
ADVERTISEMENT_TYPE_LOVESPOUSE_STOP
}
}

fun AdvertisementSetType.stringResId(): Int {
return when (this) {
AdvertisementSetType.ADVERTISEMENT_TYPE_UNDEFINED -> R.string.ad_set_type_undefined

AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_DEVICE -> R.string.ad_set_type_fast_pairing_device
AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_NON_PRODUCTION -> R.string.ad_set_type_fast_pairing_non_production
AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_PHONE_SETUP -> R.string.ad_set_type_fast_pairing_phone_setup
AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_DEBUG -> R.string.ad_set_type_fast_pairing_debug

AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_NEW_AIRTAG -> R.string.ad_set_type_continuity_new_airtag
AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_NEW_DEVICE -> R.string.ad_set_type_continuity_new_device
AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_NOT_YOUR_DEVICE -> R.string.ad_set_type_continuity_not_your_device
AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_ACTION_MODALS -> R.string.ad_set_type_continuity_action_modals
AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_IOS_17_CRASH -> R.string.ad_set_type_continuity_ios17_crash

AdvertisementSetType.ADVERTISEMENT_TYPE_SWIFT_PAIRING -> R.string.ad_set_type_swift_pairing

AdvertisementSetType.ADVERTISEMENT_TYPE_EASY_SETUP_WATCH -> R.string.ad_set_type_easy_setup_watch
AdvertisementSetType.ADVERTISEMENT_TYPE_EASY_SETUP_BUDS -> R.string.ad_set_type_easy_setup_buds

AdvertisementSetType.ADVERTISEMENT_TYPE_LOVESPOUSE_PLAY -> R.string.ad_set_type_lovespouse_play
AdvertisementSetType.ADVERTISEMENT_TYPE_LOVESPOUSE_STOP -> R.string.ad_set_type_lovespouse_stop
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.simon.dankelmann.bluetoothlespam.Enums

import de.simon.dankelmann.bluetoothlespam.R

enum class AdvertisementTarget {
ADVERTISEMENT_TARGET_KITCHEN_SINK,
ADVERTISEMENT_TARGET_UNDEFINED,
Expand All @@ -8,4 +10,16 @@ enum class AdvertisementTarget {
ADVERTISEMENT_TARGET_WINDOWS,
ADVERTISEMENT_TARGET_SAMSUNG,
ADVERTISEMENT_TARGET_LOVESPOUSE
}
}

fun AdvertisementTarget.getDrawableId(): Int {
return when (this) {
AdvertisementTarget.ADVERTISEMENT_TARGET_SAMSUNG -> R.drawable.samsung
AdvertisementTarget.ADVERTISEMENT_TARGET_ANDROID -> R.drawable.ic_android
AdvertisementTarget.ADVERTISEMENT_TARGET_IOS -> R.drawable.apple
AdvertisementTarget.ADVERTISEMENT_TARGET_UNDEFINED -> R.drawable.bluetooth
AdvertisementTarget.ADVERTISEMENT_TARGET_WINDOWS -> R.drawable.microsoft
AdvertisementTarget.ADVERTISEMENT_TARGET_KITCHEN_SINK -> R.drawable.shuffle
AdvertisementTarget.ADVERTISEMENT_TARGET_LOVESPOUSE -> R.drawable.heart
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.simon.dankelmann.bluetoothlespam.Enums

import de.simon.dankelmann.bluetoothlespam.R


enum class SpamPackageType {
UNKNOWN,
FAST_PAIRING,
Expand All @@ -13,4 +16,22 @@ enum class SpamPackageType {
EASY_SETUP_BUDS,
LOVESPOUSE_PLAY,
LOVESPOUSE_STOP
}

fun SpamPackageType.stringRes(): Int {
return when (this) {
SpamPackageType.UNKNOWN -> R.string.spam_unknown

SpamPackageType.FAST_PAIRING -> R.string.ad_set_type_fast_pairing
SpamPackageType.CONTINUITY_NEW_AIRTAG -> R.string.ad_set_type_continuity_new_airtag
SpamPackageType.CONTINUITY_NEW_DEVICE -> R.string.ad_set_type_continuity_new_device
SpamPackageType.CONTINUITY_NOT_YOUR_DEVICE -> R.string.ad_set_type_continuity_not_your_device
SpamPackageType.CONTINUITY_ACTION_MODAL -> R.string.ad_set_type_continuity_action_modals
SpamPackageType.CONTINUITY_IOS_17_CRASH -> R.string.ad_set_type_continuity_ios17_crash
SpamPackageType.SWIFT_PAIRING -> R.string.ad_set_type_swift_pairing
SpamPackageType.EASY_SETUP_WATCH -> R.string.ad_set_type_easy_setup_watch
SpamPackageType.EASY_SETUP_BUDS -> R.string.ad_set_type_easy_setup_buds
SpamPackageType.LOVESPOUSE_PLAY -> R.string.ad_set_type_lovespouse_play
SpamPackageType.LOVESPOUSE_STOP -> R.string.ad_set_type_lovespouse_stop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import androidx.core.content.ContextCompat
import androidx.navigation.NavDeepLinkBuilder
import de.simon.dankelmann.bluetoothlespam.AppContext.AppContext
import de.simon.dankelmann.bluetoothlespam.Enums.AdvertisementError
import de.simon.dankelmann.bluetoothlespam.Enums.AdvertisementSetType
import de.simon.dankelmann.bluetoothlespam.Enums.AdvertisementState
import de.simon.dankelmann.bluetoothlespam.Enums.AdvertisementTarget
import de.simon.dankelmann.bluetoothlespam.Enums.getDrawableId
import de.simon.dankelmann.bluetoothlespam.Enums.stringResId
import de.simon.dankelmann.bluetoothlespam.Interfaces.Callbacks.IAdvertisementServiceCallback
import de.simon.dankelmann.bluetoothlespam.Interfaces.Callbacks.IAdvertisementSetQueueHandlerCallback
import de.simon.dankelmann.bluetoothlespam.MainActivity
Expand Down Expand Up @@ -80,7 +80,6 @@ class AdvertisementForegroundService: IAdvertisementServiceCallback, IAdvertisem
super.onDestroy()
}


private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && AppContext.getActivity() != null) {
val notificationManager = AppContext.getActivity().getSystemService(NOTIFICATION_SERVICE) as NotificationManager
Expand All @@ -101,68 +100,28 @@ class AdvertisementForegroundService: IAdvertisementServiceCallback, IAdvertisem
//.setArguments(bundle)
.createPendingIntent()


// Custom Layout
val notificationView = RemoteViews(packageName, R.layout.advertisement_foreground_service_notification)

var title = ""
var subtitle = ""
var targetImageId = R.drawable.bluetooth

if (advertisementSet != null) {
title = advertisementSet.title
subtitle = when (advertisementSet.type) {
AdvertisementSetType.ADVERTISEMENT_TYPE_UNDEFINED -> "Undefined Type"

AdvertisementSetType.ADVERTISEMENT_TYPE_EASY_SETUP_BUDS -> "Easy Setup Buds"
AdvertisementSetType.ADVERTISEMENT_TYPE_EASY_SETUP_WATCH -> "Easy Setup Watch"

AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_DEVICE -> "Fast Pairing"
AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_NON_PRODUCTION -> "Fast Pairing"
AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_PHONE_SETUP -> "Fast Pairing"
AdvertisementSetType.ADVERTISEMENT_TYPE_FAST_PAIRING_DEBUG -> "Fast Pairing"

AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_NEW_DEVICE -> "New Apple Device"
AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_NEW_AIRTAG -> "New Airtag"
AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_NOT_YOUR_DEVICE -> "Not your Device"

AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_ACTION_MODALS -> "Apple Action"
AdvertisementSetType.ADVERTISEMENT_TYPE_CONTINUITY_IOS_17_CRASH -> "Apple iOs 17 Crash"

AdvertisementSetType.ADVERTISEMENT_TYPE_SWIFT_PAIRING -> "Swift Pairing"

AdvertisementSetType.ADVERTISEMENT_TYPE_LOVESPOUSE_PLAY -> "Lovespouse Play"
AdvertisementSetType.ADVERTISEMENT_TYPE_LOVESPOUSE_STOP -> "Lovespouse Stop"
}

targetImageId = when (advertisementSet.target) {
AdvertisementTarget.ADVERTISEMENT_TARGET_SAMSUNG -> R.drawable.samsung
AdvertisementTarget.ADVERTISEMENT_TARGET_ANDROID -> R.drawable.ic_android
AdvertisementTarget.ADVERTISEMENT_TARGET_IOS -> R.drawable.apple
AdvertisementTarget.ADVERTISEMENT_TARGET_UNDEFINED -> R.drawable.bluetooth
AdvertisementTarget.ADVERTISEMENT_TARGET_WINDOWS -> R.drawable.microsoft
AdvertisementTarget.ADVERTISEMENT_TARGET_KITCHEN_SINK -> R.drawable.shuffle
AdvertisementTarget.ADVERTISEMENT_TARGET_LOVESPOUSE -> R.drawable.heart
}
}

val toggleImageSrc = when(AppContext.getAdvertisementSetQueueHandler().isActive()){
val toggleImageSrc = when (AppContext.getAdvertisementSetQueueHandler().isActive()) {
true -> R.drawable.pause
false -> R.drawable.play_arrow
}

// Views for Custom Layout
notificationView.setTextViewText(
R.id.advertisementForegroundServiceNotificationTitleTextView,
title
)
notificationView.setTextViewText(
R.id.advertisementForegroundServiceNotificationSubTitleTextView,
subtitle
advertisementSet?.title ?: ""
)
advertisementSet?.type?.stringResId()?.let { resId ->
notificationView.setTextViewText(
R.id.advertisementForegroundServiceNotificationSubTitleTextView,
getString(resId)
)
}
notificationView.setImageViewResource(
R.id.advertisementForegroundServiceNotificationTargetImageView,
targetImageId
advertisementSet?.target?.getDrawableId() ?: R.drawable.bluetooth
)

notificationView.setImageViewResource(
Expand All @@ -185,14 +144,12 @@ class AdvertisementForegroundService: IAdvertisementServiceCallback, IAdvertisem
targetIconColor,
targetIconColor
)

notificationView.setColorInt(
R.id.advertisementForegroundServiceNotificationToggleImageView,
"setColorFilter",
buttonActiveColor,
buttonActiveColor
)

notificationView.setColorInt(
R.id.advertisementForegroundServiceNotificationStopImageView,
"setColorFilter",
Expand All @@ -205,13 +162,11 @@ class AdvertisementForegroundService: IAdvertisementServiceCallback, IAdvertisem
"setColorFilter",
targetIconColor
)

notificationView.setInt(
R.id.advertisementForegroundServiceNotificationStopImageView,
"setColorFilter",
buttonActiveColor
)

notificationView.setInt(
R.id.advertisementForegroundServiceNotificationToggleImageView,
"setColorFilter",
Expand All @@ -220,31 +175,13 @@ class AdvertisementForegroundService: IAdvertisementServiceCallback, IAdvertisem
}

if (advertisementSet != null) {
var titleColor = when (advertisementSet.advertisementState) {
AdvertisementState.ADVERTISEMENT_STATE_UNDEFINED -> resources.getColor(
R.color.color_title,
AppContext.getContext().theme
)

AdvertisementState.ADVERTISEMENT_STATE_STARTED -> resources.getColor(
R.color.color_title,
AppContext.getContext().theme
)

AdvertisementState.ADVERTISEMENT_STATE_SUCCEEDED -> resources.getColor(
R.color.color_title,
AppContext.getContext().theme
)

AdvertisementState.ADVERTISEMENT_STATE_FAILED -> resources.getColor(
R.color.log_error,
AppContext.getContext().theme
)
val titleColorRes = when (advertisementSet.advertisementState) {
AdvertisementState.ADVERTISEMENT_STATE_FAILED -> R.color.log_error
else -> R.color.color_title
}

notificationView.setTextColor(
R.id.advertisementForegroundServiceNotificationTitleTextView,
titleColor
resources.getColor(titleColorRes, AppContext.getContext().theme)
)
}

Expand Down Expand Up @@ -275,18 +212,14 @@ class AdvertisementForegroundService: IAdvertisementServiceCallback, IAdvertisem
pendingStopSwitchIntent
)

var contentText = "Bluetooth LE Spam"
if (advertisementSet != null) {
contentText = advertisementSet.title
}
val appName = getString(R.string.app_name)
val contentText = advertisementSet?.title ?: appName

return NotificationCompat.Builder(AppContext.getActivity(), _channelId)
.setContentTitle("Bluetooth LE Spam")
.setContentTitle(appName)
.setContentText(contentText)
.setSmallIcon(R.drawable.bluetooth)
.setContentIntent(pendingIntentTargeted)
//.setColor(resources.getColor(R.color.blue_normal, AppContext.getContext().theme))
//.setColorized(true)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setChannelId(_channelId)
.setOngoing(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.core.content.ContextCompat
import androidx.navigation.NavDeepLinkBuilder
import de.simon.dankelmann.bluetoothlespam.AppContext.AppContext
import de.simon.dankelmann.bluetoothlespam.Enums.SpamPackageType
import de.simon.dankelmann.bluetoothlespam.Enums.stringRes
import de.simon.dankelmann.bluetoothlespam.Interfaces.Callbacks.IBluetoothLeScanCallback
import de.simon.dankelmann.bluetoothlespam.MainActivity
import de.simon.dankelmann.bluetoothlespam.Models.FlipperDeviceScanResult
Expand Down Expand Up @@ -53,7 +54,14 @@ class BluetoothLeScanForegroundService: IBluetoothLeScanCallback, Service() {
super.onCreate()

createNotificationChannel()
startForeground(2, createNotification("Detecting Spam", "Searching Flippers and others", false))
startForeground(
2,
createNotification(
getString(R.string.spam_detecting_title),
getString(R.string.spam_detecting_text),
false
)
)
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand Down Expand Up @@ -92,20 +100,19 @@ class BluetoothLeScanForegroundService: IBluetoothLeScanCallback, Service() {
}
}

private fun createNotification(title:String, subTitle: String, alertOnlyOnce:Boolean): Notification {
private fun createNotification(
title: String,
subTitle: String,
alertOnlyOnce: Boolean
): Notification {

val pendingIntentTargeted = NavDeepLinkBuilder(this)
.setComponentName(MainActivity::class.java)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.nav_spam_detector)
//.setArguments(bundle)
.createPendingIntent()


// Custom Layout
val notificationView = RemoteViews(packageName, R.layout.bluetooth_le_scan_foreground_service_notification)

// Views for Custom Layout
notificationView.setTextViewText(
R.id.bluetoothLeScanningForegroundNotificationTitle,
title
Expand All @@ -115,16 +122,11 @@ class BluetoothLeScanForegroundService: IBluetoothLeScanCallback, Service() {
subTitle
)


//var contentText = "Bluetooth LE Spam"

return NotificationCompat.Builder(AppContext.getActivity(), _channelId)
.setContentTitle("Bluetooth LE Spam")
.setContentTitle(getString(R.string.app_name))
.setContentText(subTitle)
.setSmallIcon(R.drawable.bluetooth)
.setContentIntent(pendingIntentTargeted)
//.setColor(resources.getColor(R.color.blue_normal, AppContext.getContext().theme))
//.setColorized(true)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setChannelId(_channelId)
.setOngoing(true)
Expand Down Expand Up @@ -160,27 +162,22 @@ class BluetoothLeScanForegroundService: IBluetoothLeScanCallback, Service() {
}
}

override fun onSpamResultPackageDetected(spamPackageScanResult: SpamPackageScanResult, alreadyKnown: Boolean) {
val spamPackageTypeText = when(spamPackageScanResult.spamPackageType){
SpamPackageType.UNKNOWN -> "Unknown Spam"
SpamPackageType.FAST_PAIRING -> "Fast Pairing"
SpamPackageType.CONTINUITY_NEW_AIRTAG -> "Continuity Airtag"
SpamPackageType.CONTINUITY_NEW_DEVICE -> "Continuity new Device"
SpamPackageType.CONTINUITY_NOT_YOUR_DEVICE -> "Continuity not your Device"
SpamPackageType.CONTINUITY_ACTION_MODAL -> "Continuity Action Modal"
SpamPackageType.CONTINUITY_IOS_17_CRASH -> "Continuity iOS 17 Crash"
SpamPackageType.SWIFT_PAIRING -> "Swift Pairing"
SpamPackageType.EASY_SETUP_WATCH -> "Easy Setup Watch"
SpamPackageType.EASY_SETUP_BUDS -> "Easy Setup Buds"
SpamPackageType.LOVESPOUSE_PLAY -> "Lovespouse Play"
SpamPackageType.LOVESPOUSE_STOP -> "Lovespouse Stop"
}
updateNotification("Spam Detected", spamPackageTypeText + " | " + spamPackageScanResult.address, !notifyOnNewSpam, 2)
override fun onSpamResultPackageDetected(
spamPackageScanResult: SpamPackageScanResult,
alreadyKnown: Boolean
) {
val spamPackageTypeText = getString(spamPackageScanResult.spamPackageType.stringRes())
updateNotification(
getString(R.string.spam_detected_title),
spamPackageTypeText + " | " + spamPackageScanResult.address,
!notifyOnNewSpam,
2
)
notifyOnNewSpam = false
}

override fun onSpamResultPackageListUpdated() {
if(AppContext.getBluetoothLeScanService().getSpamPackageScanResultList().isEmpty()){
if (AppContext.getBluetoothLeScanService().getSpamPackageScanResultList().isEmpty()) {
notifyOnNewSpam = true
}
}
Expand Down
Loading

0 comments on commit fb474a1

Please sign in to comment.