2021-12-01 09:10:59 +01:00
|
|
|
package org.schabi.newpipe.error
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.app.NotificationManager
|
2021-12-01 10:13:28 +01:00
|
|
|
import android.app.PendingIntent
|
2021-12-01 09:10:59 +01:00
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.Color
|
2021-12-01 10:13:28 +01:00
|
|
|
import android.os.Build
|
2021-12-01 09:10:59 +01:00
|
|
|
import android.view.View
|
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
import org.schabi.newpipe.R
|
|
|
|
|
|
|
|
class ErrorUtil {
|
|
|
|
companion object {
|
2021-12-01 09:43:24 +01:00
|
|
|
private const val ERROR_REPORT_NOTIFICATION_ID = 5340681
|
2021-12-01 09:10:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reports a new error by starting a new activity.
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @param errorInfo
|
|
|
|
*/
|
|
|
|
@JvmStatic
|
|
|
|
fun openActivity(context: Context, errorInfo: ErrorInfo) {
|
2021-12-01 10:13:28 +01:00
|
|
|
context.startActivity(getErrorActivityIntent(context, errorInfo))
|
2021-12-01 09:10:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun showSnackbar(context: Context, errorInfo: ErrorInfo) {
|
|
|
|
val rootView = if (context is Activity) context.findViewById<View>(R.id.content) else null
|
|
|
|
showSnackbar(context, rootView, errorInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun showSnackbar(fragment: Fragment, errorInfo: ErrorInfo) {
|
|
|
|
var rootView = fragment.view
|
|
|
|
if (rootView == null && fragment.activity != null) {
|
|
|
|
rootView = fragment.requireActivity().findViewById(R.id.content)
|
|
|
|
}
|
|
|
|
showSnackbar(fragment.requireContext(), rootView, errorInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun showUiErrorSnackbar(context: Context, request: String, throwable: Throwable) {
|
|
|
|
showSnackbar(context, ErrorInfo(throwable, UserAction.UI_ERROR, request))
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun showUiErrorSnackbar(fragment: Fragment, request: String, throwable: Throwable) {
|
|
|
|
showSnackbar(fragment, ErrorInfo(throwable, UserAction.UI_ERROR, request))
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun createNotification(context: Context, errorInfo: ErrorInfo) {
|
|
|
|
val notificationManager =
|
2021-12-01 09:43:24 +01:00
|
|
|
ContextCompat.getSystemService(context, NotificationManager::class.java)
|
2021-12-01 09:10:59 +01:00
|
|
|
if (notificationManager == null) {
|
|
|
|
// this should never happen, but just in case open error activity
|
|
|
|
openActivity(context, errorInfo)
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:13:28 +01:00
|
|
|
var pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
pendingIntentFlags = pendingIntentFlags or PendingIntent.FLAG_IMMUTABLE
|
|
|
|
}
|
|
|
|
|
2021-12-01 09:10:59 +01:00
|
|
|
val notificationBuilder: NotificationCompat.Builder =
|
2021-12-01 09:43:24 +01:00
|
|
|
NotificationCompat.Builder(
|
|
|
|
context,
|
|
|
|
context.getString(R.string.error_report_channel_id)
|
|
|
|
)
|
|
|
|
.setSmallIcon(R.drawable.ic_bug_report)
|
2021-12-01 10:13:28 +01:00
|
|
|
.setContentTitle(context.getString(R.string.error_report_notification_title))
|
2021-12-01 09:43:24 +01:00
|
|
|
.setContentText(context.getString(errorInfo.messageStringId))
|
2021-12-01 10:13:28 +01:00
|
|
|
.setAutoCancel(true)
|
|
|
|
.setContentIntent(
|
|
|
|
PendingIntent.getActivity(
|
|
|
|
context,
|
|
|
|
0,
|
|
|
|
getErrorActivityIntent(context, errorInfo),
|
|
|
|
pendingIntentFlags
|
|
|
|
)
|
|
|
|
)
|
2021-12-01 09:10:59 +01:00
|
|
|
|
|
|
|
notificationManager!!.notify(ERROR_REPORT_NOTIFICATION_ID, notificationBuilder.build())
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:13:28 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-01 09:10:59 +01:00
|
|
|
private fun showSnackbar(context: Context, rootView: View?, errorInfo: ErrorInfo) {
|
|
|
|
if (rootView == null) {
|
|
|
|
// fallback to showing a notification if no root view is available
|
|
|
|
createNotification(context, errorInfo)
|
|
|
|
} else {
|
|
|
|
Snackbar.make(rootView, R.string.error_snackbar_message, Snackbar.LENGTH_LONG)
|
2021-12-01 09:43:24 +01:00
|
|
|
.setActionTextColor(Color.YELLOW)
|
|
|
|
.setAction(context.getString(R.string.error_snackbar_action).uppercase()) {
|
|
|
|
openActivity(context, errorInfo)
|
|
|
|
}.show()
|
2021-12-01 09:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 09:43:24 +01:00
|
|
|
}
|