Skip to content

Commit

Permalink
refactor: cleanup ServiceNotificationHelper.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed Mar 19, 2023
1 parent b7a4233 commit 1fba805
Showing 1 changed file with 88 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.DrawableRes
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import me.timschneeberger.rootlessjamesdsp.BuildConfig
Expand Down Expand Up @@ -35,7 +37,9 @@ object ServiceNotificationHelper: KoinComponent {
context,
0,
Intent(context, EngineLauncherActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
Expand Down Expand Up @@ -95,33 +99,39 @@ object ServiceNotificationHelper: KoinComponent {
})
}

private fun createServiceNotification(context: Context, title: String, message: String): Notification {
return NotificationCompat.Builder(context, Notifications.CHANNEL_SERVICE_STATUS)
.setShowWhen(false)
.setOnlyAlertOnce(true)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_tune_vertical_variant_24dp)
.setContentIntent(
PendingIntent.getActivity(
context,
0,
Intent(context, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
private fun createServiceNotification(
context: Context,
title: String,
message: String
) = NotificationCompat.Builder(context, Notifications.CHANNEL_SERVICE_STATUS)
.setShowWhen(false)
.setOnlyAlertOnce(true)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_tune_vertical_variant_24dp)
.setContentIntent(
PendingIntent.getActivity(
context,
0,
Intent(context, MainActivity::class.java).apply {
addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_CLEAR_TOP
or Intent.FLAG_ACTIVITY_NEW_TASK
)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
.setOngoing(true)
.apply {
if(BuildConfig.ROOTLESS)
addAction(createStopAction(context))
}
.build()
}
)
.setOngoing(true)
.apply {
if(BuildConfig.ROOTLESS)
addAction(createStopAction(context))
}
.build()

fun pushSessionLossNotification(context: Context, mediaProjectionStartIntent: Intent?) {
fun pushSessionLossNotification(context: Context, mediaProjectionStartIntent: Intent?) =
NotificationCompat.Builder(context, Notifications.CHANNEL_SERVICE_SESSION_LOSS)
.setContentTitle(context.getString(R.string.session_control_loss_notification_title))
.setContentText(context.getString(R.string.session_control_loss_notification))
Expand All @@ -133,7 +143,9 @@ object ServiceNotificationHelper: KoinComponent {
context,
0,
Intent(context, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_CLEAR_TOP
or Intent.FLAG_ACTIVITY_NEW_TASK)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
Expand All @@ -143,21 +155,20 @@ object ServiceNotificationHelper: KoinComponent {
SystemServices.get<NotificationManager>(context)
.notify(Notifications.ID_SERVICE_SESSION_LOSS, it)
}
}

@RequiresApi(Build.VERSION_CODES.Q)
fun pushAppIssueNotification(context: Context, mediaProjectionStartIntent: Intent?, appUid: Int) {
fun pushAppIssueNotification(context: Context, projectionIntent: Intent?, appUid: Int) {
NotificationCompat.Builder(context, Notifications.CHANNEL_SERVICE_APP_COMPAT)
.setContentTitle(context.getString(R.string.session_app_compat_notification_title))
.setContentText(context.getString(R.string.session_app_compat_notification))
.setSmallIcon(R.drawable.ic_baseline_warning_24dp)
.addAction(createAppTroubleshootAction(context, mediaProjectionStartIntent, appUid))
.addAction(createAppTroubleshootAction(context, projectionIntent, appUid))
.setAutoCancel(true)
.setContentIntent(
PendingIntent.getActivity(
context,
0,
createAppTroubleshootIntent(context, mediaProjectionStartIntent, appUid, directLaunch = false),
createAppTroubleshootIntent(context, projectionIntent, appUid, false),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
Expand All @@ -168,81 +179,75 @@ object ServiceNotificationHelper: KoinComponent {
}
}

private fun createStopAction(context: Context): NotificationCompat.Action {
private fun createAction(
context: Context,
@StringRes textRes: Int,
@DrawableRes iconRes: Int,
intent: Intent
): NotificationCompat.Action {
return NotificationCompat.Action(
IconCompat.createWithResource(context, R.drawable.ic_close_24dp),
context.getString(R.string.action_stop),
IconCompat.createWithResource(context, iconRes),
context.getString(textRes),
PendingIntent.getService(
context,
0,
createStopIntent(context),
intent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
)
}

private fun createRetryAction(context: Context, mediaProjectionStartIntent: Intent?): NotificationCompat.Action? {
mediaProjectionStartIntent ?: return null
private fun createStopAction(context: Context) =
createAction(
context,
R.string.action_stop,
R.drawable.ic_close_24dp,
if (SdkCheck.isQ && BuildConfig.ROOTLESS) {
with(Intent(context, RootlessAudioProcessorService::class.java)) {
action = RootlessAudioProcessorService.ACTION_STOP
this
}
}
else throw IllegalStateException()
)

return NotificationCompat.Action(
IconCompat.createWithResource(context, R.drawable.ic_baseline_refresh_24dp),
context.getString(R.string.action_retry),
PendingIntent.getService(
private fun createRetryAction(context: Context, mediaProjectionStartIntent: Intent?) =
mediaProjectionStartIntent?.let {
createAction(
context,
0,
createStartIntent(context, mediaProjectionStartIntent),
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
R.string.action_retry,
R.drawable.ic_baseline_refresh_24dp,
it
)
)
}
}

@RequiresApi(Build.VERSION_CODES.Q)
private fun createAppTroubleshootAction(context: Context, mediaProjectionStartIntent: Intent?, appUid: Int): NotificationCompat.Action? {
mediaProjectionStartIntent ?: return null

return NotificationCompat.Action(
IconCompat.createWithResource(context, R.drawable.ic_twotone_chevron_right_24dp),
context.getString(R.string.action_fix),
PendingIntent.getService(
private fun createAppTroubleshootAction(context: Context, projectionIntent: Intent?, uid: Int) =
projectionIntent?.let {
createAction(
context,
0,
createAppTroubleshootIntent(context, mediaProjectionStartIntent, appUid, directLaunch = false),
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
R.string.action_fix,
R.drawable.ic_twotone_chevron_right_24dp,
createAppTroubleshootIntent(context, it, uid, directLaunch = false)
)
)
}
}


@RequiresApi(Build.VERSION_CODES.Q)
fun createAppTroubleshootIntent(ctx: Context, mediaProjectionData: Intent?, appUid: Int, directLaunch: Boolean): Intent {
fun createAppTroubleshootIntent(
ctx: Context,
mediaProjectionData: Intent?,
appUid: Int,
directLaunch: Boolean
): Intent {
return Intent(ctx, AppCompatibilityActivity::class.java).apply {
action = RootlessAudioProcessorService.ACTION_START
putExtra(RootlessAudioProcessorService.EXTRA_MEDIA_PROJECTION_DATA, mediaProjectionData)
putExtra(RootlessAudioProcessorService.EXTRA_APP_UID, appUid)
putExtra(RootlessAudioProcessorService.EXTRA_APP_COMPAT_INTERNAL_CALL, directLaunch)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
}
}

fun createStopIntent(ctx: Context): Intent {
return if (SdkCheck.isQ && BuildConfig.ROOTLESS) {
with(Intent(ctx, RootlessAudioProcessorService::class.java)) {
action = RootlessAudioProcessorService.ACTION_STOP
this
}
}
else
throw IllegalStateException()
}

fun createStartIntent(ctx: Context, mediaProjectionData: Intent? = null): Intent {
return if (SdkCheck.isQ && BuildConfig.ROOTLESS) {
with(Intent(ctx, RootlessAudioProcessorService::class.java)) {
action = RootlessAudioProcessorService.ACTION_START
putExtra(RootlessAudioProcessorService.EXTRA_MEDIA_PROJECTION_DATA, mediaProjectionData)
this
}
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_CLEAR_TOP
or Intent.FLAG_ACTIVITY_NEW_TASK)
}
else
throw IllegalStateException()
}
}

0 comments on commit 1fba805

Please sign in to comment.