1+ package com.codeskraps.deepcuts
2+
3+ import android.app.Notification
4+ import android.app.NotificationChannel
5+ import android.app.NotificationManager
6+ import android.app.PendingIntent
7+ import android.app.Service
8+ import android.content.Intent
9+ import android.os.IBinder
10+ import android.util.Log
11+ import androidx.core.app.NotificationCompat
12+ import com.codeskraps.deepcuts.util.BackgroundStatus
13+ import com.codeskraps.deepcuts.util.Constants
14+ import com.codeskraps.deepcuts.webview.components.MediaWebView
15+ import dagger.hilt.android.AndroidEntryPoint
16+ import javax.inject.Inject
17+
18+
19+ @AndroidEntryPoint
20+ class ForegroundService : Service () {
21+ companion object {
22+ private val TAG = ForegroundService ::class .java.simpleName
23+ private const val NOTIF_ID = 1
24+ private const val CHANNEL_ID = " ForegroundServiceChannel"
25+ private const val DELETE_EXTRA = " deleteExtra"
26+ private const val HOME_EXTRA = " homeExtra"
27+ private const val REFRESH_EXTRA = " refreshExtra"
28+ }
29+
30+ @Inject
31+ lateinit var backgroundStatus: BackgroundStatus
32+
33+ @Inject
34+ lateinit var mediaWebView: MediaWebView
35+
36+ override fun onBind (intent : Intent ? ): IBinder ? = null
37+
38+ override fun onStartCommand (intent : Intent ? , flags : Int , startId : Int ): Int {
39+ Log .v(TAG , " onStartCommand" )
40+
41+ mediaWebView.setUrlListener { url ->
42+ (getSystemService(NOTIFICATION_SERVICE ) as NotificationManager ).run {
43+ notify(NOTIF_ID , createNotification(url))
44+ }
45+ }
46+
47+ var input = " "
48+
49+ intent?.extras?.let {
50+ if (it.getBoolean(DELETE_EXTRA , false )) {
51+ stopSelf()
52+ return START_NOT_STICKY
53+
54+ } else if (it.getBoolean(HOME_EXTRA , false )) {
55+ mediaWebView.loadUrl(Constants .home)
56+ return START_NOT_STICKY
57+
58+ } else if (it.getBoolean(REFRESH_EXTRA , false )) {
59+ mediaWebView.reload()
60+ return START_NOT_STICKY
61+
62+ } else {
63+ input = it.getString(Constants .inputExtra) ? : input
64+ }
65+ }
66+
67+ createNotificationChannel()
68+ startForeground(NOTIF_ID , createNotification(input))
69+ backgroundStatus.setValue(true )
70+
71+ return START_NOT_STICKY
72+ }
73+
74+ override fun onDestroy () {
75+ super .onDestroy()
76+ backgroundStatus.setValue(false )
77+ mediaWebView.setUrlListener(null )
78+ }
79+
80+ private fun createNotificationChannel () {
81+ val serviceChannel = NotificationChannel (
82+ CHANNEL_ID ,
83+ " Foreground MediaWebView Channel" ,
84+ NotificationManager .IMPORTANCE_DEFAULT
85+ )
86+ getSystemService(NotificationManager ::class .java).run {
87+ createNotificationChannel(serviceChannel)
88+ }
89+ }
90+
91+ private fun createNotification (contentText : String ): Notification =
92+ NotificationCompat .Builder (this , CHANNEL_ID )
93+ .setContentTitle(getString(R .string.app_name))
94+ .setContentText(contentText)
95+ .setSmallIcon(R .drawable.ic_notification)
96+ .setContentIntent(contentPendingIntent())
97+ .setDeleteIntent(deletePendingIntent())
98+ .setPriority(NotificationCompat .PRIORITY_DEFAULT )
99+ .setVisibility(NotificationCompat .VISIBILITY_PUBLIC )
100+ .addAction(R .drawable.ic_home, " Home" , homePendingIntent())
101+ .addAction(R .drawable.ic_refresh, " Refresh" , refreshPendingIntent())
102+ .build()
103+
104+ private fun contentPendingIntent (): PendingIntent = PendingIntent .getActivity(
105+ this ,
106+ 2 ,
107+ Intent (this , MainActivity ::class .java).apply {
108+ flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK
109+ },
110+ PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_IMMUTABLE
111+ )
112+
113+ private fun deletePendingIntent (): PendingIntent = PendingIntent .getService(
114+ this ,
115+ 3 ,
116+ Intent (this , ForegroundService ::class .java).apply {
117+ putExtra(DELETE_EXTRA , true )
118+ },
119+ PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_IMMUTABLE
120+ )
121+
122+ private fun homePendingIntent (): PendingIntent = PendingIntent .getService(
123+ this ,
124+ 4 ,
125+ Intent (this , ForegroundService ::class .java).apply {
126+ putExtra(HOME_EXTRA , true )
127+ },
128+ PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_IMMUTABLE
129+ )
130+
131+ private fun refreshPendingIntent (): PendingIntent = PendingIntent .getService(
132+ this ,
133+ 5 ,
134+ Intent (this , ForegroundService ::class .java).apply {
135+ putExtra(REFRESH_EXTRA , true )
136+ },
137+ PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_IMMUTABLE
138+ )
139+ }
0 commit comments