From b764d155d1bbcef9c2c56ef81695fe02b25e4dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 1 Nov 2019 12:35:30 +0100 Subject: [PATCH] Share Session Logs on Android --- android/app/src/main/AndroidManifest.xml | 11 ++++++++++ .../com/metallic/chiaki/common/LogManager.kt | 4 +++- .../chiaki/settings/SettingsLogsAdapter.kt | 7 +++++-- .../chiaki/settings/SettingsLogsFragment.kt | 20 +++++++++++++++++++ android/app/src/main/res/xml/filepaths.xml | 6 ++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 android/app/src/main/res/xml/filepaths.xml diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index ddfd9f9..14b0466 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -15,6 +15,17 @@ android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning"> + + + + diff --git a/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt b/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt index 3907451..01ed0cf 100644 --- a/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt +++ b/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt @@ -24,6 +24,8 @@ import java.text.SimpleDateFormat import java.util.* import java.util.regex.Pattern +val fileProviderAuthority = "com.metallic.chiaki.fileprovider" +private const val baseDirName = "session_logs" // must be in sync with filepaths.xml private val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS", Locale.US) private const val filePrefix = "chiaki_session_" private const val filePostfix = ".log" @@ -46,7 +48,7 @@ class LogFile private constructor(val logManager: LogManager, val filename: Stri class LogManager(context: Context) { - val baseDir = File(context.filesDir, "session_logs").also { + val baseDir = File(context.filesDir, baseDirName).also { it.mkdirs() } diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt index 41cd916..171d329 100644 --- a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt @@ -30,8 +30,10 @@ import java.util.* class SettingsLogsAdapter: RecyclerView.Adapter() { - val dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT) - val timeFormat = SimpleDateFormat("HH:mm:ss:SSS", Locale.getDefault()) + var shareCallback: ((LogFile) -> Unit)? = null + + private val dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT) + private val timeFormat = SimpleDateFormat("HH:mm:ss:SSS", Locale.getDefault()) class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) @@ -52,5 +54,6 @@ class SettingsLogsAdapter: RecyclerView.Adapter( val logFile = logFiles[position] view.nameTextView.text = "${dateFormat.format(logFile.date)} ${timeFormat.format(logFile.date)}" view.summaryTextView.text = logFile.filename + view.shareButton.setOnClickListener { shareCallback?.let { it(logFile) } } } } \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt index 40feabe..b1edbba 100644 --- a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt @@ -17,20 +17,25 @@ package com.metallic.chiaki.settings +import android.content.ClipData +import android.content.Intent import android.content.res.Resources import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AppCompatDialogFragment +import androidx.core.content.FileProvider import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.metallic.chiaki.R +import com.metallic.chiaki.common.LogFile import com.metallic.chiaki.common.LogManager import com.metallic.chiaki.common.ext.viewModelFactory +import com.metallic.chiaki.common.fileProviderAuthority import kotlinx.android.synthetic.main.fragment_settings_logs.* class SettingsLogsFragment: AppCompatDialogFragment(), TitleFragment @@ -51,6 +56,7 @@ class SettingsLogsFragment: AppCompatDialogFragment(), TitleFragment val adapter = SettingsLogsAdapter() logsRecyclerView.layoutManager = LinearLayoutManager(context) logsRecyclerView.adapter = adapter + adapter.shareCallback = this::shareLogFile viewModel.sessionLogs.observe(this, Observer { adapter.logFiles = it emptyInfoGroup.visibility = if(it.isEmpty()) View.VISIBLE else View.GONE @@ -69,4 +75,18 @@ class SettingsLogsFragment: AppCompatDialogFragment(), TitleFragment } override fun getTitle(resources: Resources): String = resources.getString(R.string.preferences_logs_title) + + private fun shareLogFile(file: LogFile) + { + val activity = activity ?: return + val uri = FileProvider.getUriForFile(activity, fileProviderAuthority, file.file) + Intent(Intent.ACTION_SEND).also { + it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + it.type = "text/plain" + it.putExtra(Intent.EXTRA_STREAM, uri) + it.clipData = ClipData.newRawUri("", uri) + startActivity(Intent.createChooser(it, resources.getString(R.string.action_share_log))) + } + + } } \ No newline at end of file diff --git a/android/app/src/main/res/xml/filepaths.xml b/android/app/src/main/res/xml/filepaths.xml new file mode 100644 index 0000000..f3315d2 --- /dev/null +++ b/android/app/src/main/res/xml/filepaths.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file