summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorDavid Göransson <david.goransson90@gmail.com>2023-09-27 11:19:55 +0200
committerDavid Göransson <david.goransson90@gmail.com>2023-10-04 09:19:41 +0200
commite6b30e63580b285d9f826c023a2d7fb6b5cba69c (patch)
tree00345da1884db0298d41a0d76936a51504cb0e10 /android
parent3c54cb0a604226fae03ccb1880ed0ccda1d388ea (diff)
downloadmullvadvpn-e6b30e63580b285d9f826c023a2d7fb6b5cba69c.tar.xz
mullvadvpn-e6b30e63580b285d9f826c023a2d7fb6b5cba69c.zip
Simplify MullvadProblemReport
Diffstat (limited to 'android')
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt45
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemViewModel.kt6
2 files changed, 26 insertions, 25 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt
index f36c9a4975..ca9f4c7e23 100644
--- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt
+++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt
@@ -5,7 +5,7 @@ import java.io.File
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
-const val PROBLEM_REPORT_FILE = "problem_report.txt"
+const val PROBLEM_REPORT_LOGS_FILE = "problem_report.txt"
sealed interface SendProblemReportResult {
data object Success : SendProblemReportResult
@@ -21,30 +21,25 @@ sealed interface SendProblemReportResult {
data class UserReport(val email: String, val message: String)
class MullvadProblemReport(context: Context) {
- private val logDirectory = File(context.filesDir.toURI())
private val cacheDirectory = File(context.cacheDir.toURI())
- private val problemReportPath = File(logDirectory, PROBLEM_REPORT_FILE)
-
- private var hasCollectedReport = false
+ private val logDirectory = File(context.filesDir.toURI())
+ private val logsPath = File(logDirectory, PROBLEM_REPORT_LOGS_FILE)
init {
System.loadLibrary("mullvad_jni")
}
- private suspend fun collectReport() =
+ suspend fun collectLogs(): Boolean =
withContext(Dispatchers.IO) {
- val logDirectoryPath = logDirectory.absolutePath
- val reportPath = problemReportPath.absolutePath
// Delete any old report
- deleteReport()
- hasCollectedReport = collectReport(logDirectoryPath, reportPath)
+ deleteLogs()
+
+ collectReport(logDirectory.absolutePath, logsPath.absolutePath)
}
suspend fun sendReport(userReport: UserReport): SendProblemReportResult {
- if (!hasCollectedReport) {
- collectReport()
- }
- if (!hasCollectedReport) {
+ // If report is not collected then, collect it, if it fails then return error
+ if (!logsExists() && !collectLogs()) {
return SendProblemReportResult.Error.CollectLog
}
@@ -53,13 +48,13 @@ class MullvadProblemReport(context: Context) {
sendProblemReport(
userReport.email,
userReport.message,
- problemReportPath.absolutePath,
+ logsPath.absolutePath,
cacheDirectory.absolutePath
)
}
return if (sentSuccessfully) {
- deleteReport()
+ deleteLogs()
SendProblemReportResult.Success
} else {
SendProblemReportResult.Error.SendReport
@@ -67,24 +62,26 @@ class MullvadProblemReport(context: Context) {
}
suspend fun readLogs(): List<String> {
- if (!hasCollectedReport) {
- collectReport()
+ if (!logsExists()) {
+ collectLogs()
}
- return if (hasCollectedReport) {
- problemReportPath.readLines()
+ return if (logsExists()) {
+ logsPath.readLines()
} else {
listOf("Failed to collect logs for problem report")
}
}
- fun deleteReport() {
- problemReportPath.delete()
- hasCollectedReport = false
+ private fun logsExists() =
+ logsPath.exists()
+
+ fun deleteLogs() {
+ logsPath.delete()
}
// TODO We should remove the external functions from this class and migrate it to the service
- private external fun collectReport(logDirectory: String, reportPath: String): Boolean
+ private external fun collectReport(logDirectory: String, logsPath: String): Boolean
private external fun sendProblemReport(
userEmail: String,
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemViewModel.kt
index c007ca5c8f..75b65eaca6 100644
--- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemViewModel.kt
+++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemViewModel.kt
@@ -73,9 +73,13 @@ class ReportProblemViewModel(private val mullvadProblemReporter: MullvadProblemR
SendProblemReportResult.Success -> SendingReportUiState.Success(email)
}
+ init {
+ viewModelScope.launch { mullvadProblemReporter.collectLogs() }
+ }
+
override fun onCleared() {
super.onCleared()
// Delete any logs if user leaves the screen
- mullvadProblemReporter.deleteReport()
+ mullvadProblemReporter.deleteLogs()
}
}