Fix bug reporter progress.

This commit is contained in:
Onuray Sahin 2021-01-05 14:44:02 +03:00
parent 23623b8895
commit fa311f4ce2

View File

@ -102,7 +102,7 @@ class BugReporter @Inject constructor(
var screenshot: Bitmap? = null
private set
private val coroutineScope = CoroutineScope(SupervisorJob())
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
private val LOGCAT_CMD_ERROR = arrayOf("logcat", // /< Run 'logcat' command
"-d", // /< Dump the log rather than continue outputting it
@ -169,13 +169,10 @@ class BugReporter @Inject constructor(
// enumerate files to delete
val mBugReportFiles: MutableList<File> = ArrayList()
coroutineScope.executeAsyncTask(
onPreExecute = {
// NOOP
},
doInBackground = { publishProgress: suspend (progress: Int) -> Unit ->
var bugDescription = theBugDescription
coroutineScope.launch {
var serverError: String? = null
withContext(Dispatchers.IO) {
var bugDescription = theBugDescription
val crashCallStack = getCrashDescription(context)
if (null != crashCallStack) {
@ -351,7 +348,11 @@ class BugReporter @Inject constructor(
}
Timber.v("## onWrite() : $percentage%")
suspend { publishProgress(percentage) }
try {
listener?.onProgress(percentage)
} catch (e: Exception) {
Timber.e(e, "## onProgress() : failed")
}
}
// build the request
@ -395,12 +396,14 @@ class BugReporter @Inject constructor(
}
// check if the error message
serverError?.let {
try {
val responseJSON = JSONObject(serverError)
val responseJSON = JSONObject(it)
serverError = responseJSON.getString("error")
} catch (e: JSONException) {
Timber.e(e, "doInBackground ; Json conversion failed")
}
}
// should never happen
if (null == serverError) {
@ -412,19 +415,9 @@ class BugReporter @Inject constructor(
}
}
}
}
serverError
},
onProgressUpdate = { progress ->
if (null != listener) {
try {
listener.onProgress(progress)
} catch (e: Exception) {
Timber.e(e, "## onProgress() : failed")
}
}
},
onPostExecute = { reason: String? ->
withContext(Dispatchers.Main) {
mBugReportCall = null
// delete when the bug report has been successfully sent
@ -436,17 +429,17 @@ class BugReporter @Inject constructor(
try {
if (mIsCancelled) {
listener.onUploadCancelled()
} else if (null == reason) {
} else if (null == serverError) {
listener.onUploadSucceed()
} else {
listener.onUploadFailed(reason)
listener.onUploadFailed(serverError)
}
} catch (e: Exception) {
Timber.e(e, "## onPostExecute() : failed")
}
}
}
)
}
}
/**
@ -464,9 +457,9 @@ class BugReporter @Inject constructor(
activity.startActivity(intent)
}
// ==============================================================================================================
// crash report management
// ==============================================================================================================
// ==============================================================================================================
// crash report management
// ==============================================================================================================
/**
* Provides the crash file
@ -536,9 +529,9 @@ class BugReporter @Inject constructor(
return null
}
// ==============================================================================================================
// Screenshot management
// ==============================================================================================================
// ==============================================================================================================
// Screenshot management
// ==============================================================================================================
/**
* Take a screenshot of the display.
@ -605,9 +598,9 @@ class BugReporter @Inject constructor(
}
}
// ==============================================================================================================
// Logcat management
// ==============================================================================================================
// ==============================================================================================================
// Logcat management
// ==============================================================================================================
/**
* Save the logcat
@ -667,9 +660,9 @@ class BugReporter @Inject constructor(
}
}
// ==============================================================================================================
// File compression management
// ==============================================================================================================
// ==============================================================================================================
// File compression management
// ==============================================================================================================
/**
* GZip a file
@ -703,21 +696,4 @@ class BugReporter @Inject constructor(
return null
}
fun <P, R> CoroutineScope.executeAsyncTask(
onPreExecute: () -> Unit,
doInBackground: suspend (suspend (P) -> Unit) -> R,
onPostExecute: (R) -> Unit,
onProgressUpdate: (P) -> Unit
) = launch {
onPreExecute()
val result = withContext(Dispatchers.IO) {
doInBackground {
withContext(Dispatchers.Main) { onProgressUpdate(it) }
}
}
onPostExecute(result)
}
}