cloudstream/app/src/main/java/com/lagradost/cloudstream3/AcraApplication.kt

90 lines
2.9 KiB
Kotlin
Raw Normal View History

2021-07-26 18:29:04 +00:00
package com.lagradost.cloudstream3
import android.app.Application
import android.content.Context
import android.widget.Toast
2021-07-26 18:29:04 +00:00
import com.google.auto.service.AutoService
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.network.post
import com.lagradost.cloudstream3.utils.Coroutines.runOnMainThread
2021-07-26 18:29:04 +00:00
import org.acra.ReportField
import org.acra.config.CoreConfiguration
import org.acra.data.CrashReportData
import org.acra.data.StringFormat
import org.acra.ktx.initAcra
import org.acra.sender.ReportSender
import org.acra.sender.ReportSenderFactory
import java.lang.ref.WeakReference
2021-07-26 18:29:04 +00:00
import kotlin.concurrent.thread
class CustomReportSender : ReportSender {
// Sends all your crashes to google forms
override fun send(context: Context, errorContent: CrashReportData) {
println("Sending report")
val url =
"https://docs.google.com/forms/u/0/d/e/1FAIpQLSeFmyBChi6HF3IkhTVWPiDXJtxt8W0Hf4Agljm_0-0_QuEYFg/formResponse"
val data = mapOf(
"entry.134906550" to errorContent.toJSON()
)
thread { // to not run it on main thread
normalSafeApiCall {
val post = post(url, data = data)
2021-07-26 18:29:04 +00:00
println("Report response: $post")
}
}
runOnMainThread { // to run it on main looper
normalSafeApiCall {
Toast.makeText(context, R.string.acra_report_toast, Toast.LENGTH_SHORT).show()
}
2021-07-26 18:29:04 +00:00
}
}
}
@AutoService(ReportSenderFactory::class)
class CustomSenderFactory : ReportSenderFactory {
override fun create(context: Context, config: CoreConfiguration): ReportSender {
return CustomReportSender()
}
override fun enabled(config: CoreConfiguration): Boolean {
return true
}
}
class AcraApplication : Application() {
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
context = base
2021-07-26 18:29:04 +00:00
initAcra {
//core configuration:
buildConfigClass = BuildConfig::class.java
reportFormat = StringFormat.JSON
reportContent = arrayOf(
ReportField.BUILD_CONFIG, ReportField.USER_CRASH_DATE,
ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL,
ReportField.STACK_TRACE
)
// removed this due to bug when starting the app, moved it to when it actually crashes
2021-07-26 18:29:04 +00:00
//each plugin you chose above can be configured in a block like this:
/*toast {
2021-07-26 18:29:04 +00:00
text = getString(R.string.acra_report_toast)
//opening this block automatically enables the plugin.
}*/
2021-07-26 18:29:04 +00:00
}
}
companion object {
private var _context: WeakReference<Context>? = null
var context
get() = _context?.get()
private set(value) {
_context = WeakReference(value)
}
}
2021-07-26 18:29:04 +00:00
}