From 359b386a6cdcd9a98d7e91c4ac57aa1f1eb6bcf4 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 8 Apr 2024 16:07:37 +0200 Subject: [PATCH] fix 'create backup' button --- .../settings/MainSettingsFragment.java | 74 ++++++++++++++++++- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/fragment/settings/MainSettingsFragment.java b/src/main/java/eu/siacs/conversations/ui/fragment/settings/MainSettingsFragment.java index 045275f95..e9761b1a7 100644 --- a/src/main/java/eu/siacs/conversations/ui/fragment/settings/MainSettingsFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/fragment/settings/MainSettingsFragment.java @@ -1,33 +1,101 @@ package eu.siacs.conversations.ui.fragment.settings; +import android.Manifest; +import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; +import android.widget.Toast; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.common.base.Strings; import eu.siacs.conversations.BuildConfig; import eu.siacs.conversations.R; +import eu.siacs.conversations.persistance.FileBackend; +import eu.siacs.conversations.services.ExportBackupService; public class MainSettingsFragment extends PreferenceFragmentCompat { + private static final String CREATE_BACKUP = "create_backup"; + + private ActivityResultLauncher requestStorageForBackupLauncher = + registerForActivityResult( + new ActivityResultContracts.RequestPermission(), + isGranted -> { + if (isGranted) { + startBackup(); + } else { + Toast.makeText( + requireActivity(), + getString( + R.string.no_storage_permission, + getString(R.string.app_name)), + Toast.LENGTH_LONG) + .show(); + } + }); + @Override public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) { setPreferencesFromResource(R.xml.preferences_main, rootKey); final var about = findPreference("about"); final var connection = findPreference("connection"); - if (about == null || connection == null) { - throw new IllegalStateException("The preference resource file is missing some preferences"); + final var backup = findPreference(CREATE_BACKUP); + if (about == null || connection == null || backup == null) { + throw new IllegalStateException( + "The preference resource file is missing some preferences"); } + backup.setSummary( + getString( + R.string.pref_create_backup_summary, + FileBackend.getBackupDirectory(requireContext()).getAbsolutePath())); + backup.setOnPreferenceClickListener(this::onBackupPreferenceClicked); about.setTitle(getString(R.string.title_activity_about_x, BuildConfig.APP_NAME)); - about.setSummary(String.format("%s %s %s (%s)", BuildConfig.APP_NAME, BuildConfig.VERSION_NAME, im.conversations.webrtc.BuildConfig.WEBRTC_VERSION, Strings.nullToEmpty(Build.DEVICE))); + about.setSummary( + String.format( + "%s %s %s (%s)", + BuildConfig.APP_NAME, + BuildConfig.VERSION_NAME, + im.conversations.webrtc.BuildConfig.WEBRTC_VERSION, + Strings.nullToEmpty(Build.DEVICE))); if (ConnectionSettingsFragment.hideChannelDiscovery()) { connection.setSummary(R.string.pref_connection_summary); } } + private boolean onBackupPreferenceClicked(final Preference preference) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + if (ContextCompat.checkSelfPermission( + requireContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + requestStorageForBackupLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE); + } else { + startBackup(); + } + } else { + startBackup(); + } + return true; + } + + private void startBackup() { + ContextCompat.startForegroundService( + requireContext(), new Intent(requireContext(), ExportBackupService.class)); + final MaterialAlertDialogBuilder builder = + new MaterialAlertDialogBuilder(requireActivity()); + builder.setMessage(R.string.backup_started_message); + builder.setPositiveButton(R.string.ok, null); + builder.create().show(); + } + @Override public void onStart() { super.onStart();