Advertisement
Guest User

old- Bubble.kt for dashbuddy

a guest
May 17th, 2025
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.44 KB | None | 0 0
  1. package cloud.trotter.dashbuddy.ui
  2.  
  3. import android.app.ActivityOptions
  4. import android.app.Notification
  5. import android.app.NotificationChannel
  6. import android.app.NotificationManager
  7. import android.app.PendingIntent
  8. import android.app.Service
  9. import android.content.Context
  10. import android.content.Intent
  11. import android.content.pm.ServiceInfo
  12. import android.os.Build
  13. import android.os.Bundle
  14. import android.os.IBinder
  15. import android.util.Log
  16. import androidx.annotation.RequiresApi
  17. import androidx.core.app.NotificationCompat
  18. import androidx.core.app.Person
  19. import androidx.core.content.pm.ShortcutInfoCompat
  20. import androidx.core.content.pm.ShortcutManagerCompat
  21. import androidx.core.graphics.drawable.IconCompat
  22. import cloud.trotter.dashbuddy.BubbleActivity
  23. import cloud.trotter.dashbuddy.DashBuddyApplication
  24. import cloud.trotter.dashbuddy.R
  25. import java.util.Date
  26.  
  27. class Bubble : Service() {
  28.  
  29.     private lateinit var icon: IconCompat
  30.     private lateinit var person: Person
  31.     private lateinit var shortcut: ShortcutInfoCompat
  32.  
  33.     companion object {
  34.         const val CHANNEL_ID = "bubble_channel"
  35.         const val NOTIFICATION_ID = 1
  36.         private const val TAG = "BubbleService"
  37.     }
  38.  
  39.     override fun onCreate() {
  40.         super.onCreate()
  41.         Log.d(TAG, "onCreate: BubbleService created")
  42.         icon = IconCompat.createWithResource(
  43.             DashBuddyApplication.context, R.drawable.bag_red_idle)
  44.         person = Person.Builder()
  45.             .setName("DashBuddy")
  46.             .setIcon(icon)
  47.             .setImportant(true)
  48.             .build()
  49.         shortcut = ShortcutInfoCompat.Builder(DashBuddyApplication.context, "DashBuddy_Shortcut")
  50.             .setLongLived(true)
  51.             .setIntent(Intent(DashBuddyApplication.context, BubbleActivity::class.java).apply {
  52.                 action = Intent.ACTION_VIEW
  53.             })
  54.             .setShortLabel("DashBuddy")
  55.             .setIcon(icon)
  56.             .setPerson(person)
  57.             .build()
  58.         ShortcutManagerCompat.pushDynamicShortcut(DashBuddyApplication.context, shortcut)
  59.         DashBuddyApplication.bubbleService = this
  60.         createNotificationChannel()
  61.     }
  62.  
  63.     @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
  64.     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  65.         Log.d(TAG, "onStartCommand: BubbleService started")
  66.         val notification = create("Started!")
  67.         post(notification)
  68.         startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
  69.         return START_STICKY
  70.     }
  71.  
  72.     override fun onBind(intent: Intent?): IBinder? {
  73.         return null
  74.     }
  75.  
  76.     private fun createNotificationChannel() {
  77.         val channel = NotificationChannel(
  78.             CHANNEL_ID,
  79.             "Bubble Channel",
  80.             NotificationManager.IMPORTANCE_HIGH
  81.         ).apply {
  82.             description = "Channel for Bubble Notifications"
  83.         }
  84.         val notificationManager =
  85.             getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  86.         notificationManager.createNotificationChannel(channel)
  87.     }
  88.  
  89.     fun post(notification: Notification) {
  90.         DashBuddyApplication.notificationManager.notify(NOTIFICATION_ID, notification)
  91.     }
  92.  
  93.     private fun getActivityOptionsBundle(): Bundle {
  94.         val activityOptions = ActivityOptions.makeBasic()
  95.  
  96.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
  97.             activityOptions.setPendingIntentBackgroundActivityStartMode(ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
  98.         }
  99.         return activityOptions.toBundle()
  100.     }
  101.  
  102.     fun create(message: String): Notification {
  103.         Log.d(TAG, "createNotification: Creating notification")
  104.  
  105.         val builder = NotificationCompat.Builder(DashBuddyApplication.context, CHANNEL_ID)
  106.  
  107.         val target = Intent(DashBuddyApplication.context, BubbleActivity::class.java)
  108. //        target.putExtra("ActivityOptions",getActivityOptionsBundle())
  109.         val bubbleIntent = PendingIntent.getActivity(
  110.             DashBuddyApplication.context, 0, target,
  111.             PendingIntent.FLAG_UPDATE_CURRENT
  112.                     or PendingIntent.FLAG_MUTABLE
  113. //                    or getActivityOptionsBundle()
  114. //                    or PendingIntent.FLAG_ALLOW_BACKGROUND_ACTIVITY_STARTS
  115.         )
  116.  
  117.         val bubbleMetadata = NotificationCompat.BubbleMetadata.Builder(bubbleIntent, icon)
  118.             .setDesiredHeight(400)
  119.             .setSuppressNotification(true)
  120.             .setAutoExpandBubble(true)
  121.             .build()
  122.  
  123.         with(builder) {
  124.             setBubbleMetadata(bubbleMetadata)
  125.             setStyle(
  126.                 NotificationCompat.MessagingStyle(person).addMessage(
  127.                     NotificationCompat.MessagingStyle.Message(
  128.                         message,
  129.                         Date().time,
  130.                         person
  131.                     )
  132.                 )
  133.             )
  134.             setShortcutId(shortcut.id)
  135.             addPerson(person)
  136.         }
  137.  
  138.         with(builder) {
  139.             setContentTitle("DashBuddy")
  140.             setSmallIcon(
  141.                 icon
  142. //                IconCompat.createWithResource(DashBuddyApplication.context, R.drawable.dashly)
  143.             )
  144.             setCategory(NotificationCompat.CATEGORY_MESSAGE)
  145.             setContentIntent(bubbleIntent)
  146.             setShowWhen(true)
  147.         }
  148.  
  149.         return builder. Build()
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement