/*
- * Copyright 2018-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2018-2023 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
*
import android.widget.RadioButton
import android.widget.Spinner
import android.widget.TextView
-import androidx.activity.result.contract.ActivityResultContracts
+import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.cardview.widget.CardView
import com.stoutner.privacybrowser.R
import com.stoutner.privacybrowser.BuildConfig
+import com.stoutner.privacybrowser.helpers.EXPORT_SUCCESSFUL
+import com.stoutner.privacybrowser.helpers.IMPORT_EXPORT_SCHEMA_VERSION
+import com.stoutner.privacybrowser.helpers.IMPORT_SUCCESSFUL
import com.stoutner.privacybrowser.helpers.ImportExportDatabaseHelper
import java.io.File
}
}
- private val browseForExportActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { fileUri: Uri? ->
+ private val browseForExportActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument("*/*")) { fileUri: Uri? ->
// Only do something if the user didn't press back from the file picker.
if (fileUri != null) {
// Get the file name string from the URI.
savedInstanceState.putString(IMPORT_EXPORT_BUTTON_TEXT, importExportButton.text.toString())
}
- fun onClickRadioButton(@Suppress("UNUSED_PARAMETER") view: View) {
+ fun onClickRadioButton(view: View) {
// Check to see if import or export was selected.
if (view.id == R.id.import_radiobutton) { // The import radio button is selected.
// Check to see if OpenPGP encryption is selected.
} else { // Export is selected
// Open the file picker with the export name according to the encryption type.
if (encryptionSpinner.selectedItemPosition == NO_ENCRYPTION) // No encryption is selected.
- browseForExportActivityResultLauncher.launch(getString(R.string.privacy_browser_settings_pbs, BuildConfig.VERSION_NAME))
+ browseForExportActivityResultLauncher.launch(getString(R.string.privacy_browser_settings_pbs, BuildConfig.VERSION_NAME, IMPORT_EXPORT_SCHEMA_VERSION))
else // Password encryption is selected.
- browseForExportActivityResultLauncher.launch(getString(R.string.privacy_browser_settings_pbs_aes, BuildConfig.VERSION_NAME))
+ browseForExportActivityResultLauncher.launch(getString(R.string.privacy_browser_settings_pbs_aes, BuildConfig.VERSION_NAME, IMPORT_EXPORT_SCHEMA_VERSION))
}
}
}
// Restart Privacy Browser if successful.
- if (importStatus == ImportExportDatabaseHelper.IMPORT_SUCCESSFUL)
+ if (importStatus == IMPORT_SUCCESSFUL)
restartPrivacyBrowser()
}
temporaryUnencryptedImportFile.delete()
// Restart Privacy Browser if successful.
- if (importStatus == ImportExportDatabaseHelper.IMPORT_SUCCESSFUL)
+ if (importStatus == IMPORT_SUCCESSFUL)
restartPrivacyBrowser()
} catch (exception: Exception) {
// Update the import status.
openKeychainDecryptActivityResultLauncher.launch(openKeychainDecryptIntent)
// Update the import status.
- importStatus = ImportExportDatabaseHelper.IMPORT_SUCCESSFUL
+ importStatus = IMPORT_SUCCESSFUL
} catch (exception: Exception) {
// Update the import status.
importStatus = exception.toString()
}
}
- // Respond to the import status.
- if (importStatus != ImportExportDatabaseHelper.IMPORT_SUCCESSFUL) {
- // Display a snack bar with the import error.
+ // Display a snack bar with the import error if it was unsuccessful.
+ if (importStatus != IMPORT_SUCCESSFUL)
Snackbar.make(fileNameEditText, getString(R.string.import_failed, importStatus), Snackbar.LENGTH_INDEFINITE).show()
- }
} else { // Export is selected.
// Export according to the encryption type.
when (encryptionSpinner.selectedItemPosition) {
exportFileOutputStream.close()
// Display an export disposition snackbar.
- if (noEncryptionExportStatus == ImportExportDatabaseHelper.EXPORT_SUCCESSFUL) {
+ if (noEncryptionExportStatus == EXPORT_SUCCESSFUL)
Snackbar.make(fileNameEditText, getString(R.string.export_successful), Snackbar.LENGTH_SHORT).show()
- } else {
+ else
Snackbar.make(fileNameEditText, getString(R.string.export_failed, noEncryptionExportStatus), Snackbar.LENGTH_INDEFINITE).show()
- }
} catch (fileNotFoundException: FileNotFoundException) {
// Display a snackbar with the exception.
Snackbar.make(fileNameEditText, getString(R.string.export_failed, fileNotFoundException), Snackbar.LENGTH_INDEFINITE).show()
temporaryUnencryptedExportFile.delete()
// Display an export disposition snackbar.
- if (passwordEncryptionExportStatus == ImportExportDatabaseHelper.EXPORT_SUCCESSFUL)
+ if (passwordEncryptionExportStatus == EXPORT_SUCCESSFUL)
Snackbar.make(fileNameEditText, getString(R.string.export_successful), Snackbar.LENGTH_SHORT).show()
else
Snackbar.make(fileNameEditText, getString(R.string.export_failed, passwordEncryptionExportStatus), Snackbar.LENGTH_INDEFINITE).show()
fileProviderDirectory.mkdir()
// Set the temporary pre-encrypted export file.
- temporaryPreEncryptedExportFile = File(fileProviderDirectory.toString() + "/" + getString(R.string.privacy_browser_settings_pbs, BuildConfig.VERSION_NAME))
+ temporaryPreEncryptedExportFile = File(fileProviderDirectory.toString() + "/" +
+ getString(R.string.privacy_browser_settings_pbs, BuildConfig.VERSION_NAME, IMPORT_EXPORT_SCHEMA_VERSION))
// Delete the temporary pre-encrypted export file if it already exists.
if (temporaryPreEncryptedExportFile.exists())
temporaryPreEncryptedExportOutputStream.close()
// Display an export error snackbar if the temporary pre-encrypted export failed.
- if (openpgpEncryptionExportStatus != ImportExportDatabaseHelper.EXPORT_SUCCESSFUL)
+ if (openpgpEncryptionExportStatus != EXPORT_SUCCESSFUL)
Snackbar.make(fileNameEditText, getString(R.string.export_failed, openpgpEncryptionExportStatus), Snackbar.LENGTH_INDEFINITE).show()
// Create an encryption intent for OpenKeychain.
// Restart Privacy Browser after 150 milliseconds to allow enough time for the preferences to be saved.
restartHandler.postDelayed(restartRunnable, 150)
}
-}
\ No newline at end of file
+}