Share Session Logs on Android

This commit is contained in:
Florian Märkl 2019-11-01 12:35:30 +01:00
commit b764d155d1
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 45 additions and 3 deletions

View file

@ -15,6 +15,17 @@
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<provider
android:authorities="com.metallic.chiaki.fileprovider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"
/>
</provider>
<activity android:name=".main.MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>

View file

@ -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()
}

View file

@ -30,8 +30,10 @@ import java.util.*
class SettingsLogsAdapter: RecyclerView.Adapter<SettingsLogsAdapter.ViewHolder>()
{
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<SettingsLogsAdapter.ViewHolder>(
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) } }
}
}

View file

@ -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)))
}
}
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="session_logs"
path="session_logs/" /> <!-- must be in sync with LogManager -->
</paths>