feat: share file after save logs #106
3 changed files with 41 additions and 13 deletions
|
@ -1,6 +1,7 @@
|
|||
package im.angry.openeuicc.ui
|
||||
|
||||
import android.icu.text.SimpleDateFormat
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
|
@ -8,8 +9,10 @@ import android.view.View
|
|||
import android.widget.ScrollView
|
||||
import android.widget.TextView
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.ActivityResultCallback
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ShareCompat
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
import im.angry.openeuicc.common.R
|
||||
|
@ -20,23 +23,24 @@ import kotlinx.coroutines.withContext
|
|||
import java.io.FileOutputStream
|
||||
import java.util.Date
|
||||
|
||||
class LogsActivity : AppCompatActivity() {
|
||||
class LogsActivity : AppCompatActivity(), ActivityResultCallback<Uri?> {
|
||||
private lateinit var swipeRefresh: SwipeRefreshLayout
|
||||
private lateinit var scrollView: ScrollView
|
||||
private lateinit var logText: TextView
|
||||
private lateinit var logStr: String
|
||||
private var sharable = false
|
||||
|
||||
private val saveLogs =
|
||||
registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { uri ->
|
||||
if (uri == null) return@registerForActivityResult
|
||||
if (!this::logStr.isInitialized) return@registerForActivityResult
|
||||
contentResolver.openFileDescriptor(uri, "w")?.use {
|
||||
FileOutputStream(it.fileDescriptor).use { os ->
|
||||
os.write(logStr.encodeToByteArray())
|
||||
}
|
||||
}
|
||||
private val fileName: String
|
||||
get() {
|
||||
val time = SimpleDateFormat.getDateTimeInstance().format(Date())
|
||||
return getString(R.string.logs_filename_template, time)
|
||||
}
|
||||
|
||||
private val saveLogs = registerForActivityResult(
|
||||
ActivityResultContracts.CreateDocument("text/plain"),
|
||||
this,
|
||||
)
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
enableEdgeToEdge()
|
||||
super.onCreate(savedInstanceState)
|
||||
|
@ -76,9 +80,13 @@ class LogsActivity : AppCompatActivity() {
|
|||
true
|
||||
}
|
||||
R.id.save -> {
|
||||
saveLogs.launch(getString(R.string.logs_filename_template,
|
||||
SimpleDateFormat.getDateTimeInstance().format(Date())
|
||||
))
|
||||
sharable = false
|
||||
saveLogs.launch(fileName)
|
||||
true
|
||||
}
|
||||
R.id.share -> {
|
||||
sharable = true
|
||||
saveLogs.launch(fileName)
|
||||
true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
|
@ -100,4 +108,20 @@ class LogsActivity : AppCompatActivity() {
|
|||
scrollView.fullScroll(View.FOCUS_DOWN)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
|
||||
override fun onActivityResult(uri: Uri?) {
|
||||
if (uri == null) return
|
||||
if (!this::logStr.isInitialized) return
|
||||
contentResolver.openFileDescriptor(uri, "w")?.use {
|
||||
val fd = FileOutputStream(it.fileDescriptor)
|
||||
fd.write(logStr.encodeToByteArray())
|
||||
fd.close()
|
||||
}
|
||||
if (sharable) ShareCompat.IntentBuilder(this)
|
||||
.setType("image/plain")
|
||||
.setChooserTitle(fileName)
|
||||
.addStream(uri)
|
||||
.startChooser()
|
||||
}
|
||||
}
|
|
@ -6,4 +6,7 @@
|
|||
android:icon="@drawable/ic_save_as_black"
|
||||
android:title="@string/logs_save"
|
||||
app:showAsAction="always" />
|
||||
<item
|
||||
android:id="@+id/share"
|
||||
android:title="@string/logs_share" />
|
||||
</menu>
|
|
@ -132,6 +132,7 @@
|
|||
<string name="no">No</string>
|
||||
|
||||
<string name="logs_save">Save</string>
|
||||
<string name="logs_share">Share</string>
|
||||
<string name="logs_filename_template">Logs at %s</string>
|
||||
|
||||
<string name="developer_options_steps">You are %d steps away from being a developer.</string>
|
||||
|
|
Loading…
Add table
Reference in a new issue