Browse Source

ui: Implement log content sharing

Peter Cai 1 year ago
parent
commit
ec334d104a

+ 20 - 4
app-common/src/main/java/im/angry/openeuicc/util/UiUtils.kt

@@ -1,12 +1,14 @@
 package im.angry.openeuicc.util
 
 import android.content.Context
+import android.content.Intent
 import android.content.res.Resources
 import android.graphics.Rect
 import android.view.View
 import android.view.ViewGroup
 import androidx.activity.result.ActivityResultCaller
 import androidx.activity.result.contract.ActivityResultContracts
+import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
 import androidx.core.view.ViewCompat
 import androidx.core.view.WindowInsetsCompat
@@ -83,17 +85,31 @@ fun <T : ActivityResultCaller> T.setupLogSaving(
         registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { uri ->
             if (uri == null) return@registerForActivityResult
 
-            val contentResolver = when (this@setupLogSaving) {
-                is Context -> contentResolver
-                is Fragment -> requireContext().contentResolver
+            val context = when (this@setupLogSaving) {
+                is Context -> this@setupLogSaving
+                is Fragment -> requireContext()
                 else -> throw IllegalArgumentException("Must be either Context or Fragment!")
             }
 
-            contentResolver.openFileDescriptor(uri, "w")?.use {
+            context.contentResolver.openFileDescriptor(uri, "w")?.use {
                 FileOutputStream(it.fileDescriptor).use { os ->
                     os.write(getLogText().encodeToByteArray())
                 }
             }
+
+            AlertDialog.Builder(context).apply {
+                setMessage(R.string.logs_saved_message)
+                setNegativeButton(R.string.no) { _, _ -> }
+                setPositiveButton(R.string.yes) { _, _ ->
+                    val intent = Intent().apply {
+                        action = Intent.ACTION_SEND
+                        type = "text/plain"
+                        putExtra(Intent.EXTRA_STREAM, uri)
+                    }
+
+                    context.startActivity(Intent.createChooser(intent, null))
+                }
+            }.show()
         }
 
     return {

+ 2 - 0
app-common/src/main/res/values/strings.xml

@@ -94,6 +94,8 @@
     <string name="download_wizard_diagnostics_save">Save</string>
     <string name="download_wizard_diagnostics_file_template">Diagnostics at %s</string>
 
+    <string name="logs_saved_message">Logs have been saved to the selected path. Would you like to share the log through another app?</string>
+
     <string name="profile_rename_new_name">New nickname</string>
 
     <string name="profile_delete_confirm">Are you sure you want to delete the profile %s? This operation is irreversible.</string>