Skip to content

Commit

Permalink
Remove notifications trampoline (#47)
Browse files Browse the repository at this point in the history
* Remove notifications trampoline

* Make notification non-cancellable
  • Loading branch information
YarikSOffice authored Dec 24, 2022
1 parent 5a13545 commit 626c38a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
7 changes: 4 additions & 3 deletions venom/src/main/java/com/github/venom/DeathActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Process
import com.github.venom.service.VenomService

internal class DeathActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
stopService(Intent(this, VenomService::class.java))
@Suppress("DEPRECATION")
val launchMode = intent.getSerializableExtra(LAUNCH_MODE) as LaunchMode
application.registerActivityLifecycleCallbacks(SuicidalLifecycleCallbacks(this, launchMode))
Expand Down Expand Up @@ -133,11 +135,10 @@ internal class DeathActivity : Activity() {
private const val DELAY_TIMEOUT_MILLIS = 1000L // 1s
private const val LAUNCH_MODE = "launch-mode"

fun launch(context: Context, launchMode: LaunchMode) {
val intent = Intent(context, DeathActivity::class.java)
fun createIntent(context: Context, launchMode: LaunchMode): Intent {
return Intent(context, DeathActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(LAUNCH_MODE, launchMode)
context.startActivity(intent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.Action
import androidx.core.content.ContextCompat
import com.github.venom.DeathActivity
import com.github.venom.DeathActivity.LaunchMode
import com.github.venom.DeathActivity.LaunchMode.KILL
import com.github.venom.DeathActivity.LaunchMode.RESTART
import com.github.venom.service.VenomService.Companion.ACTION_CANCEL
import com.github.venom.service.VenomService.Companion.ACTION_KILL
import com.github.venom.service.VenomService.Companion.ACTION_RESTART

private const val ACTIVITY_KILL_CODE = 100
private const val ACTIVITY_RESTART_CODE = 200
private const val SERVICE_CANCEL_CODE = 300
private const val VENOM_NOTIFICATION_CHANNEL_ID = "venom_notification_channel"
private const val VENOM_NOTIFICATION_CHANNEL_NAME = "Venom"

internal class VenomNotificationManager(private val context: Context) {

Expand All @@ -60,30 +68,47 @@ internal class VenomNotificationManager(private val context: Context) {
.setContentText(config.text)
.setSmallIcon(config.iconRes)
.setColor(ContextCompat.getColor(context, config.colorRes))
.addAction(createAction(ACTION_KILL, config.buttonKill))
.addAction(createAction(ACTION_RESTART, config.buttonRestart))
.addAction(createAction(ACTION_CANCEL, config.buttonCancel))
.addAction(createActivityAction(config.buttonKill, KILL, ACTIVITY_KILL_CODE))
.addAction(createActivityAction(config.buttonRestart, RESTART, ACTIVITY_RESTART_CODE))
.addAction(createCancelAction())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true)
.build()
}

private fun createAction(action: String, text: String): Action {
val intent = Intent(context, VenomService::class.java).setAction(action)
private fun createActivityAction(
text: String,
launchMode: LaunchMode,
requestCode: Int
): Action {
val intent = DeathActivity.createIntent(context, launchMode)
val intentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
} else {
PendingIntent.FLAG_ONE_SHOT
}
val pendingIntent = PendingIntent.getService(
val pendingIntent = PendingIntent.getActivity(
context,
0,
requestCode,
intent,
intentFlags
)
return Action(0, text, pendingIntent)
}
companion object {
private const val VENOM_NOTIFICATION_CHANNEL_ID = "venom_notification_channel"
private const val VENOM_NOTIFICATION_CHANNEL_NAME = "Venom"

private fun createCancelAction(): Action {
val intent = Intent(context, VenomService::class.java).setAction(ACTION_CANCEL)
val intentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
} else {
PendingIntent.FLAG_ONE_SHOT
}
val pendingIntent = PendingIntent.getService(
context,
SERVICE_CANCEL_CODE,
intent,
intentFlags
)
return Action(0, config.buttonCancel, pendingIntent)
}
}
17 changes: 0 additions & 17 deletions venom/src/main/java/com/github/venom/service/VenomService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.github.venom.CompositionRoot
import com.github.venom.DeathActivity
import com.github.venom.DeathActivity.LaunchMode.KILL
import com.github.venom.DeathActivity.LaunchMode.RESTART
import com.github.venom.VenomPreferenceManager

internal class VenomService : Service() {
Expand All @@ -51,31 +48,17 @@ internal class VenomService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
ACTION_CANCEL -> cancelAndTerminate()
ACTION_KILL -> launchKill()
ACTION_RESTART -> launchRestart()
}
return START_NOT_STICKY
}

private fun launchRestart() {
DeathActivity.launch(this, RESTART)
stopSelf()
}

private fun launchKill() {
DeathActivity.launch(this, KILL)
stopSelf()
}

private fun cancelAndTerminate() {
prefs.setActive(false)
stopSelf()
}

companion object {
private const val NOTIFICATION_ID = 200
const val ACTION_KILL = "action_kill"
const val ACTION_RESTART = "action_restart"
const val ACTION_CANCEL = "action_cancel"
}
}

0 comments on commit 626c38a

Please sign in to comment.