diff --git a/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLogger.kt b/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLogger.kt index 9ad3859..88619c6 100644 --- a/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLogger.kt +++ b/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLogger.kt @@ -37,4 +37,8 @@ internal fun getCategories(): List = internal fun getPersistedLogs(callback: TaskCallback) { appLoggerBLInstance?.getPersistedLogs(callback) +} + +internal fun storeVisibleLogs(visibleLogs: String, callback: TaskCallback) { + appLoggerBLInstance?.storeVisibleLogs(visibleLogs, callback) } \ No newline at end of file diff --git a/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLoggerBL.kt b/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLoggerBL.kt index 98b29d3..24d2bf1 100644 --- a/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLoggerBL.kt +++ b/library/src/enabled/java/com/telefonica/androidlogger/domain/AppLoggerBL.kt @@ -58,6 +58,10 @@ internal open class AppLoggerBL( fileLogger.getReport(callback) } + fun storeVisibleLogs(visibleLogs: String, callback: TaskCallback) { + fileLogger.storeVisibleLogs(visibleLogs, callback) + } + private fun addLog(logEntry: LogEntry) { synchronized(logs) { logs.addLast(logEntry) diff --git a/library/src/enabled/java/com/telefonica/androidlogger/io/AppFileLogger.kt b/library/src/enabled/java/com/telefonica/androidlogger/io/AppFileLogger.kt index 6322953..a4c9a98 100644 --- a/library/src/enabled/java/com/telefonica/androidlogger/io/AppFileLogger.kt +++ b/library/src/enabled/java/com/telefonica/androidlogger/io/AppFileLogger.kt @@ -58,11 +58,23 @@ internal open class AppFileLogger( callback = callback ) - private fun buildLogReportFile(): File { - val outputDir = File(appContext.cacheDir, REPORTS_DIRECTORY).apply { mkdir() } - val outputFile = - File.createTempFile(TEMPORAL_FILE_PREFIX, TEMPORAL_FILE_EXTENSION, outputDir) + fun storeVisibleLogs(visibleLogs: String, callback: TaskCallback) { + executor.submit( + task = { + val outputFile = createTempFile() + outputFile.writeText(visibleLogs) + FileProvider.getUriForFile( + appContext, + appContext.packageName + FILE_PROVIDER_AUTHORITY_SUFFIX, + outputFile + ) + }, + callback = callback + ) + } + private fun buildLogReportFile(): File { + val outputFile = createTempFile() var source: Source? = null var sink: BufferedSink? = null try { @@ -77,6 +89,11 @@ internal open class AppFileLogger( return outputFile } + private fun createTempFile(): File { + val outputDir = File(appContext.cacheDir, REPORTS_DIRECTORY).apply { mkdir() } + return File.createTempFile(TEMPORAL_FILE_PREFIX, TEMPORAL_FILE_EXTENSION, outputDir) + } + private fun buildLogReportStream(): InputStream = getLogFiles() .map { FileInputStream(it) } diff --git a/library/src/enabled/java/com/telefonica/androidlogger/ui/AppLoggerActivity.kt b/library/src/enabled/java/com/telefonica/androidlogger/ui/AppLoggerActivity.kt index f8a6a06..1705bde 100644 --- a/library/src/enabled/java/com/telefonica/androidlogger/ui/AppLoggerActivity.kt +++ b/library/src/enabled/java/com/telefonica/androidlogger/ui/AppLoggerActivity.kt @@ -39,6 +39,7 @@ import com.telefonica.androidlogger.io.executor.TaskCallback import com.telefonica.androidlogger.ui.viewmodel.AppLoggerViewModel import com.telefonica.androidlogger.ui.viewmodel.LogCategoryViewModel import com.telefonica.androidlogger.ui.viewmodel.LogPriorityViewModel +import java.io.File import java.security.InvalidParameterException class AppLoggerActivity : AppCompatActivity() { @@ -286,12 +287,19 @@ class AppLoggerActivity : AppCompatActivity() { } private fun shareVisibleLogs() { - val shareIntent = Intent.createChooser(Intent().apply { - action = Intent.ACTION_SEND - type = "text/plain" - putExtra(Intent.EXTRA_TEXT, adapter.getVisibleInfoAsString()) - }, null) - startActivity(shareIntent) + val visibleInfoAsString: String = adapter.getVisibleInfoAsString() + shareAllLogsCallback = TaskCallback { uri -> + mainThreadHandler.post { + if (uri != null) { + launchShareAllLogsIntent(uri) + } else { + Toast.makeText(this, R.string.share_visible_logs_failure, Toast.LENGTH_SHORT) + .show() + } + } + }.apply { + viewModel.onLogsThatShouldBeStoredIntoAFile(visibleInfoAsString, this) + } } private fun shareAllLogs() { diff --git a/library/src/enabled/java/com/telefonica/androidlogger/ui/viewmodel/AppLoggerViewModels.kt b/library/src/enabled/java/com/telefonica/androidlogger/ui/viewmodel/AppLoggerViewModels.kt index d6e1729..8d04bdb 100644 --- a/library/src/enabled/java/com/telefonica/androidlogger/ui/viewmodel/AppLoggerViewModels.kt +++ b/library/src/enabled/java/com/telefonica/androidlogger/ui/viewmodel/AppLoggerViewModels.kt @@ -11,6 +11,7 @@ import com.telefonica.androidlogger.domain.LogEntry import com.telefonica.androidlogger.domain.getCategories import com.telefonica.androidlogger.domain.getLogs import com.telefonica.androidlogger.domain.getPersistedLogs +import com.telefonica.androidlogger.domain.storeVisibleLogs import com.telefonica.androidlogger.io.executor.TaskCallback import com.telefonica.androidlogger.ui.livedata.filter import com.telefonica.androidlogger.ui.livedata.map @@ -97,6 +98,10 @@ internal class AppLoggerViewModel : ViewModel() { getPersistedLogs(callback) } + fun onLogsThatShouldBeStoredIntoAFile(visibleLogs: String, callback: TaskCallback) { + storeVisibleLogs(visibleLogs, callback) + } + private fun List.filterByLogLevel() = filter { it.priority.toLogPriority() >= minLogLevel diff --git a/library/src/enabled/res/values/strings.xml b/library/src/enabled/res/values/strings.xml index 9482fdb..2cee393 100644 --- a/library/src/enabled/res/values/strings.xml +++ b/library/src/enabled/res/values/strings.xml @@ -14,6 +14,7 @@ Log copied to clipboard! Something went wrong retrieving persisted logs + Something went wrong while sharing visible logs Minimum Log Level Categories