Add save to device option for Polycentric profile export

- Add dialog with two options: Share or Save to Device
- Implement saveToDevice method using MediaStore API to save to Downloads folder
- Add necessary string resources for the new UI elements
- Use UIDialogs.showDialog with Action and ActionStyle for proper dialog handling
- Add ContentValues and MediaStore imports for file saving functionality

Users can now choose to either share the exported profile file or save it directly to their device's Downloads folder for later use.
This commit is contained in:
Trevor
2025-08-20 10:34:14 -05:00
parent cfb030f59b
commit 8916fd35ab
2 changed files with 68 additions and 14 deletions
@@ -44,6 +44,8 @@ import userpackage.Protocol.ExportBundle
import userpackage.Protocol.URLInfo
import java.io.File
import java.io.FileWriter
import android.content.ContentValues
import android.provider.MediaStore
class PolycentricBackupActivity : AppCompatActivity() {
private lateinit var _buttonShare: BigButton;
@@ -270,27 +272,73 @@ class PolycentricBackupActivity : AppCompatActivity() {
writer.write(_exportBundle)
}
val uri = FileProvider.getUriForFile(
this,
"${packageName}.fileprovider",
file
)
val uri = FileProvider.getUriForFile(
this,
"${packageName}.fileprovider",
file
)
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_STREAM, uri)
putExtra(Intent.EXTRA_SUBJECT, "Polycentric Profile Export")
putExtra(Intent.EXTRA_TEXT, "Polycentric profile export file")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(Intent.createChooser(shareIntent, "Export Profile to File"))
// Show dialog with options
UIDialogs.showDialog(
this,
R.drawable.ic_download,
getString(R.string.export_profile),
getString(R.string.choose_export_option),
null,
0,
UIDialogs.Action(getString(R.string.share), {
// Share the file
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_STREAM, uri)
putExtra(Intent.EXTRA_SUBJECT, "Polycentric Profile Export")
putExtra(Intent.EXTRA_TEXT, "Polycentric profile export file")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_profile)))
}, UIDialogs.ActionStyle.PRIMARY),
UIDialogs.Action(getString(R.string.save_to_device), {
// Save to device
saveToDevice(fileName)
}, UIDialogs.ActionStyle.NONE)
)
} catch (e: Exception) {
Logger.e(TAG, "Failed to export to file", e)
UIDialogs.toast(this, "Failed to export profile to file")
}
}
private fun saveToDevice(fileName: String) {
try {
// Use MediaStore API to save to Downloads folder
val contentValues = ContentValues().apply {
put(MediaStore.Downloads.DISPLAY_NAME, fileName)
put(MediaStore.Downloads.MIME_TYPE, "text/plain")
put(MediaStore.Downloads.IS_PENDING, 1)
}
val resolver = contentResolver
val uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
uri?.let { fileUri ->
resolver.openOutputStream(fileUri)?.use { outputStream ->
outputStream.write(_exportBundle.toByteArray())
}
contentValues.clear()
contentValues.put(MediaStore.Downloads.IS_PENDING, 0)
resolver.update(fileUri, contentValues, null, null)
UIDialogs.toast(this, getString(R.string.profile_saved_to_downloads))
} ?: run {
UIDialogs.toast(this, getString(R.string.failed_to_save_profile))
}
} catch (e: Exception) {
Logger.e(TAG, "Failed to save to device", e)
UIDialogs.toast(this, getString(R.string.failed_to_save_profile))
}
}
companion object {
private const val TAG = "PolycentricBackupActivity";
}
+6
View File
@@ -647,6 +647,12 @@
<string name="export_to_file">Export to File</string>
<string name="save_profile_to_file_for_sharing">Save profile to file for sharing</string>
<string name="import_from_file">Import from File</string>
<string name="export_profile">Export Profile</string>
<string name="choose_export_option">Choose export option</string>
<string name="save_to_device">Save to Device</string>
<string name="share_profile">Share Profile</string>
<string name="profile_saved_to_downloads">Profile saved to Downloads</string>
<string name="failed_to_save_profile">Failed to save profile</string>
<string name="authority">com.futo.platformplayer.fileprovider</string>
<string name="share_text">Share Text</string>
<string name="copied_text">Copied Text</string>