Add PendingIntent to ErrorUtil.createNotification

This commit is contained in:
Stypox 2021-12-01 10:13:28 +01:00
parent 81f740d409
commit 09d137f740
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 26 additions and 5 deletions

View File

@ -2,9 +2,11 @@ package org.schabi.newpipe.error
import android.app.Activity import android.app.Activity
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.os.Build
import android.view.View import android.view.View
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@ -28,10 +30,7 @@ class ErrorUtil {
*/ */
@JvmStatic @JvmStatic
fun openActivity(context: Context, errorInfo: ErrorInfo) { fun openActivity(context: Context, errorInfo: ErrorInfo) {
val intent = Intent(context, ErrorActivity::class.java) context.startActivity(getErrorActivityIntent(context, errorInfo))
intent.putExtra(ErrorActivity.ERROR_INFO, errorInfo)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
} }
@JvmStatic @JvmStatic
@ -68,18 +67,39 @@ class ErrorUtil {
openActivity(context, errorInfo) openActivity(context, errorInfo)
} }
var pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntentFlags = pendingIntentFlags or PendingIntent.FLAG_IMMUTABLE
}
val notificationBuilder: NotificationCompat.Builder = val notificationBuilder: NotificationCompat.Builder =
NotificationCompat.Builder( NotificationCompat.Builder(
context, context,
context.getString(R.string.error_report_channel_id) context.getString(R.string.error_report_channel_id)
) )
.setSmallIcon(R.drawable.ic_bug_report) .setSmallIcon(R.drawable.ic_bug_report)
.setContentTitle(context.getString(R.string.error_report_title)) .setContentTitle(context.getString(R.string.error_report_notification_title))
.setContentText(context.getString(errorInfo.messageStringId)) .setContentText(context.getString(errorInfo.messageStringId))
.setAutoCancel(true)
.setContentIntent(
PendingIntent.getActivity(
context,
0,
getErrorActivityIntent(context, errorInfo),
pendingIntentFlags
)
)
notificationManager!!.notify(ERROR_REPORT_NOTIFICATION_ID, notificationBuilder.build()) notificationManager!!.notify(ERROR_REPORT_NOTIFICATION_ID, notificationBuilder.build())
} }
private fun getErrorActivityIntent(context: Context, errorInfo: ErrorInfo): Intent {
val intent = Intent(context, ErrorActivity::class.java)
intent.putExtra(ErrorActivity.ERROR_INFO, errorInfo)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
return intent
}
private fun showSnackbar(context: Context, rootView: View?, errorInfo: ErrorInfo) { private fun showSnackbar(context: Context, rootView: View?, errorInfo: ErrorInfo) {
if (rootView == null) { if (rootView == null) {
// fallback to showing a notification if no root view is available // fallback to showing a notification if no root view is available

View File

@ -246,6 +246,7 @@
<string name="restore_defaults_confirmation">Do you want to restore defaults?</string> <string name="restore_defaults_confirmation">Do you want to restore defaults?</string>
<string name="permission_display_over_apps">Give permission to display over other apps</string> <string name="permission_display_over_apps">Give permission to display over other apps</string>
<!-- error activity --> <!-- error activity -->
<string name="error_report_notification_title">NewPipe encountered an error, tap to report</string>
<string name="sorry_string">Sorry, that should not have happened.</string> <string name="sorry_string">Sorry, that should not have happened.</string>
<string name="guru_meditation" translatable="false">Guru Meditation.</string> <string name="guru_meditation" translatable="false">Guru Meditation.</string>
<string name="error_report_button_text">Report this error via e-mail</string> <string name="error_report_button_text">Report this error via e-mail</string>