From 8916fd35ab29c9200f2a05ff233363d117315b8c Mon Sep 17 00:00:00 2001 From: Trevor Date: Wed, 20 Aug 2025 10:34:14 -0500 Subject: [PATCH] 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. --- .../activities/PolycentricBackupActivity.kt | 76 +++++++++++++++---- app/src/main/res/values/strings.xml | 6 ++ 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/futo/platformplayer/activities/PolycentricBackupActivity.kt b/app/src/main/java/com/futo/platformplayer/activities/PolycentricBackupActivity.kt index 616bb5e3..d2de73be 100644 --- a/app/src/main/java/com/futo/platformplayer/activities/PolycentricBackupActivity.kt +++ b/app/src/main/java/com/futo/platformplayer/activities/PolycentricBackupActivity.kt @@ -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"; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 760b5bea..ef23aa4c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -647,6 +647,12 @@ Export to File Save profile to file for sharing Import from File + Export Profile + Choose export option + Save to Device + Share Profile + Profile saved to Downloads + Failed to save profile com.futo.platformplayer.fileprovider Share Text Copied Text