Skip to content

Commit

Permalink
Make download error reportable
Browse files Browse the repository at this point in the history
  • Loading branch information
Christine Emrich committed Sep 16, 2020
1 parent c8429e3 commit 1eb6eb5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import androidx.appcompat.app.AppCompatDelegate;

import org.acra.ACRA;
import org.acra.BuildConfig;
import org.acra.ReportField;
import org.acra.annotation.AcraCore;
Expand All @@ -20,6 +21,7 @@
import de.christinecoenen.code.zapp.persistence.Database;
import de.christinecoenen.code.zapp.repositories.ChannelRepository;
import de.christinecoenen.code.zapp.utils.system.NotificationHelper;
import timber.log.Timber;

@AcraCore(buildConfigClass = BuildConfig.class,
reportFormat = StringFormat.KEY_VALUE_LIST,
Expand Down Expand Up @@ -76,6 +78,14 @@ public IPlaybackPositionRepository getPlaybackPositionRepository() {
return playbackPositionRepository;
}

public void reportError(Throwable throwable) {
if (ACRA.isInitialised()) {
ACRA.getErrorReporter().handleException(throwable);
}

Timber.e(throwable);
}

@Override
public void onCreate() {
super.onCreate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import com.tonyodev.fetch2.*
import com.tonyodev.fetch2.util.DEFAULT_NOTIFICATION_TIMEOUT_AFTER
import com.tonyodev.fetch2.util.DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET
import com.tonyodev.fetch2.util.onDownloadNotificationActionTriggered
import de.christinecoenen.code.zapp.app.ZappApplication
import de.christinecoenen.code.zapp.app.mediathek.controller.DownloadReceiver
import de.christinecoenen.code.zapp.app.mediathek.model.PersistedMediathekShow
import de.christinecoenen.code.zapp.app.mediathek.repository.MediathekRepository
import de.christinecoenen.code.zapp.utils.system.NotificationHelper

const val ACTION_TYPE_REPORT_ERROR = 42

abstract class ZappNotificationManager(context: Context, private val mediathekRepository: MediathekRepository) : FetchNotificationManager {

private val application: ZappApplication = context.applicationContext as ZappApplication
private val context: Context = context.applicationContext
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val downloadNotificationsMap = mutableMapOf<Int, DownloadNotification>()
Expand All @@ -32,7 +36,19 @@ abstract class ZappNotificationManager(context: Context, private val mediathekRe
get() = object : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {
// handled by fetch
onDownloadNotificationActionTriggered(context, intent, this@ZappNotificationManager)

// handled by zapp - may be error report intent
if (intent != null) {
val actionType = intent.getIntExtra(EXTRA_ACTION_TYPE, ACTION_TYPE_INVALID)
val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, NOTIFICATION_ID_INVALID)

if (actionType == ACTION_TYPE_REPORT_ERROR && notificationId != NOTIFICATION_ID_INVALID) {
val error = errorMap[notificationId]
application.reportError(error!!.throwable)
}
}
}

}
Expand Down Expand Up @@ -143,6 +159,10 @@ abstract class ZappNotificationManager(context: Context, private val mediathekRe
downloadNotification.isQueued -> {
notificationBuilder.setTimeoutAfter(getNotificationTimeOutMillis())
}
downloadNotification.isFailed -> {
notificationBuilder.setTimeoutAfter(getNotificationTimeOutMillis())
.addAction(R.drawable.fetch_notification_cancel, "Report", getReportPendingIntent(downloadNotification))
}
else -> {
notificationBuilder.setTimeoutAfter(DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET)
}
Expand Down Expand Up @@ -327,6 +347,22 @@ abstract class ZappNotificationManager(context: Context, private val mediathekRe
}
}

private fun getReportPendingIntent(downloadNotification: DownloadNotification): PendingIntent {
synchronized(downloadNotificationsMap) {
val intent = Intent(notificationManagerAction)
intent.putExtra(EXTRA_NAMESPACE, downloadNotification.namespace)
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadNotification.notificationId)
intent.putExtra(EXTRA_NOTIFICATION_ID, downloadNotification.notificationId)
intent.putExtra(EXTRA_GROUP_ACTION, false)
intent.putExtra(EXTRA_NOTIFICATION_GROUP_ID, downloadNotification.groupId)

val action = ACTION_TYPE_REPORT_ERROR

intent.putExtra(EXTRA_ACTION_TYPE, action)
return PendingIntent.getBroadcast(context, downloadNotification.notificationId + action, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}

private fun getSubSubtitleText(downloadNotification: DownloadNotification): String? {
return when {
downloadNotification.isFailed -> errorMap[downloadNotification.notificationId]?.toString()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<string name="fragment_mediathek_download_delete">Delete download</string>

<!-- error messages -->
<string name="error_app_crash">We are very sorry, but Zapp just crashed.\nWould you like to send a crash report to the developer to help fix this error?</string>
<string name="error_app_crash">We are very sorry, but an error occurred.\nWould you like to send an error report to the developer to help fix this problem?</string>
<string name="error_app_crash_mail_subject">Zapp Crash Report</string>
<string name="error_app_crash_mail_body">Thank you very much for this report!\n\nFollowing information will help fix this error:\n</string>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<string name="fragment_mediathek_confirm_delete_dialog_text">Die Video-Datei wird gelöscht und kann später nochmals herunter geladen werden.</string>

<!-- error messages -->
<string name="error_app_crash">Entschuldigung, Zapp ist abgestürzt.\nMöchtest zu einen Absturzbericht per Mail an den Entwickler senden, um diesen Fehler zukünftig zu beheben?</string>
<string name="error_app_crash">Entschuldigung, es gab einen Fehler.\nMöchtest du per Mail einen Fehlerbericht an den Entwickler senden, um diesen Fehler zukünftig zu beheben?</string>
<string name="error_app_crash_mail_subject">Zapp Crash Report</string>
<string name="error_app_crash_mail_body">Vielen Dank für die Rückmeldung!\n\nFolgende Informationen helfen den Entwicklern von Zapp, den Fehler zu beheben:\n</string>

Expand Down

0 comments on commit 1eb6eb5

Please sign in to comment.