X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FImportExportActivity.kt;h=8e4fff48177ea464e21a27dd99d47386b83bb6d5;hb=b75067d9fa0ab792f623e14e39628448eb8f6145;hp=cdd1bf0d28bb8e7a04fecfb3ea3063edaa3bfc15;hpb=97ff4463a390d0b789f19d1c45bf9fc47428c86c;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/ImportExportActivity.kt b/app/src/main/java/com/stoutner/privacybrowser/activities/ImportExportActivity.kt index cdd1bf0d..8e4fff48 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/ImportExportActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/ImportExportActivity.kt @@ -36,8 +36,8 @@ import android.widget.LinearLayout 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 @@ -49,6 +49,9 @@ import com.google.android.material.textfield.TextInputLayout 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 @@ -117,7 +120,7 @@ class ImportExportActivity : AppCompatActivity() { } } - 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. @@ -186,8 +189,6 @@ class ImportExportActivity : AppCompatActivity() { // Find out if OpenKeychain is installed. openKeychainInstalled = try { - // The newer method can be used once the minimum API >= 33. - @Suppress("DEPRECATION") packageManager.getPackageInfo("org.sufficientlysecure.keychain", 0).versionName.isNotEmpty() } catch (exception: PackageManager.NameNotFoundException) { false @@ -392,7 +393,7 @@ class ImportExportActivity : AppCompatActivity() { 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. @@ -457,9 +458,9 @@ class ImportExportActivity : AppCompatActivity() { } 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, ImportExportDatabaseHelper.SCHEMA_VERSION)) + 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, ImportExportDatabaseHelper.SCHEMA_VERSION)) + browseForExportActivityResultLauncher.launch(getString(R.string.privacy_browser_settings_pbs_aes, BuildConfig.VERSION_NAME, IMPORT_EXPORT_SCHEMA_VERSION)) } } @@ -494,7 +495,7 @@ class ImportExportActivity : AppCompatActivity() { } // Restart Privacy Browser if successful. - if (importStatus == ImportExportDatabaseHelper.IMPORT_SUCCESSFUL) + if (importStatus == IMPORT_SUCCESSFUL) restartPrivacyBrowser() } @@ -600,7 +601,7 @@ class ImportExportActivity : AppCompatActivity() { temporaryUnencryptedImportFile.delete() // Restart Privacy Browser if successful. - if (importStatus == ImportExportDatabaseHelper.IMPORT_SUCCESSFUL) + if (importStatus == IMPORT_SUCCESSFUL) restartPrivacyBrowser() } catch (exception: Exception) { // Update the import status. @@ -658,7 +659,7 @@ class ImportExportActivity : AppCompatActivity() { openKeychainDecryptActivityResultLauncher.launch(openKeychainDecryptIntent) // Update the import status. - importStatus = ImportExportDatabaseHelper.IMPORT_SUCCESSFUL + importStatus = IMPORT_SUCCESSFUL } catch (exception: Exception) { // Update the import status. importStatus = exception.toString() @@ -666,11 +667,9 @@ class ImportExportActivity : AppCompatActivity() { } } - // 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) { @@ -690,11 +689,10 @@ class ImportExportActivity : AppCompatActivity() { 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() @@ -810,7 +808,7 @@ class ImportExportActivity : AppCompatActivity() { 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() @@ -830,7 +828,7 @@ class ImportExportActivity : AppCompatActivity() { // Set the temporary pre-encrypted export file. temporaryPreEncryptedExportFile = File(fileProviderDirectory.toString() + "/" + - getString(R.string.privacy_browser_settings_pbs, BuildConfig.VERSION_NAME, ImportExportDatabaseHelper.SCHEMA_VERSION)) + 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()) @@ -849,7 +847,7 @@ class ImportExportActivity : AppCompatActivity() { 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. @@ -898,4 +896,4 @@ class ImportExportActivity : AppCompatActivity() { // 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 +}