feat: share logs as file #105
5 changed files with 40 additions and 3 deletions
|
@ -46,5 +46,15 @@
|
||||||
android:name="im.angry.openeuicc.service.EuiccChannelManagerService"
|
android:name="im.angry.openeuicc.service.EuiccChannelManagerService"
|
||||||
android:foregroundServiceType="shortService"
|
android:foregroundServiceType="shortService"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
|
|
||||||
|
<provider
|
||||||
|
android:name="androidx.core.content.FileProvider"
|
||||||
|
android:authorities="${applicationId}.provider"
|
||||||
|
android:exported="false"
|
||||||
|
android:grantUriPermissions="true">
|
||||||
|
<meta-data
|
||||||
|
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||||
|
android:resource="@xml/file_provider_paths" />
|
||||||
|
</provider>
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
|
@ -10,6 +10,8 @@ import android.widget.TextView
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.app.ShareCompat
|
||||||
|
import androidx.core.content.FileProvider
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||||
import im.angry.openeuicc.common.R
|
import im.angry.openeuicc.common.R
|
||||||
|
@ -17,15 +19,22 @@ import im.angry.openeuicc.util.*
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
|
|
||||||
class LogsActivity : AppCompatActivity() {
|
class LogsActivity : AppCompatActivity() {
|
||||||
private lateinit var swipeRefresh: SwipeRefreshLayout
|
private lateinit var swipeRefresh: SwipeRefreshLayout
|
||||||
private lateinit var scrollView: ScrollView
|
private lateinit var scrollView: ScrollView
|
||||||
private lateinit var logText: TextView
|
private lateinit var logText: TextView
|
||||||
private lateinit var logStr: String
|
private lateinit var logStr: String
|
||||||
|
|
||||||
|
private val fileName by lazy {
|
||||||
|
val now = SimpleDateFormat.getDateTimeInstance().format(Date())
|
||||||
|
getString(R.string.logs_filename_template, now)
|
||||||
|
}
|
||||||
|
|
||||||
private val saveLogs =
|
private val saveLogs =
|
||||||
registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { uri ->
|
registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { uri ->
|
||||||
if (uri == null) return@registerForActivityResult
|
if (uri == null) return@registerForActivityResult
|
||||||
|
@ -76,9 +85,19 @@ class LogsActivity : AppCompatActivity() {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
R.id.save -> {
|
R.id.save -> {
|
||||||
saveLogs.launch(getString(R.string.logs_filename_template,
|
saveLogs.launch(fileName)
|
||||||
SimpleDateFormat.getDateTimeInstance().format(Date())
|
true
|
||||||
))
|
}
|
||||||
|
R.id.share -> {
|
||||||
|
val authority = "$packageName.provider"
|
||||||
|
val displayName = "$fileName.txt"
|
||||||
|
val file = File.createTempFile("logs", ".txt", cacheDir)
|
||||||
|
.apply { writeText(logStr) }
|
||||||
|
ShareCompat.IntentBuilder(this)
|
||||||
|
.setType("image/plain")
|
||||||
|
.setChooserTitle(fileName)
|
||||||
|
.addStream(FileProvider.getUriForFile(this, authority, file, displayName))
|
||||||
|
.startChooser()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
else -> super.onOptionsItemSelected(item)
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
|
|
@ -6,4 +6,7 @@
|
||||||
android:icon="@drawable/ic_save_as_black"
|
android:icon="@drawable/ic_save_as_black"
|
||||||
android:title="@string/logs_save"
|
android:title="@string/logs_save"
|
||||||
app:showAsAction="always" />
|
app:showAsAction="always" />
|
||||||
|
<item
|
||||||
|
android:id="@+id/share"
|
||||||
|
android:title="@string/logs_share" />
|
||||||
</menu>
|
</menu>
|
|
@ -130,6 +130,7 @@
|
||||||
<string name="no">No</string>
|
<string name="no">No</string>
|
||||||
|
|
||||||
<string name="logs_save">Save</string>
|
<string name="logs_save">Save</string>
|
||||||
|
<string name="logs_share">Share</string>
|
||||||
<string name="logs_filename_template">Logs at %s</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>
|
<string name="developer_options_steps">You are %d steps away from being a developer.</string>
|
||||||
|
|
4
app-common/src/main/res/xml/file_provider_paths.xml
Normal file
4
app-common/src/main/res/xml/file_provider_paths.xml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<paths>
|
||||||
|
<cache-path name="logs" path="/" />
|
||||||
|
</paths>
|
Loading…
Add table
Reference in a new issue