From 94f1617031318be7f0e140a52a9a55eca1682c5b Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 22 Jul 2024 15:47:08 +0200 Subject: [PATCH] Use brandingUtil for MaterialAlertDialogs Signed-off-by: alperozturk --- .../AccountPickerDialogFragment.java | 7 +- .../AccountSwitcherDialog.java | 2 +- .../edit/category/CategoryDialogFragment.java | 10 ++- .../edit/title/EditTitleDialogFragment.java | 10 ++- .../exception/ExceptionDialogFragment.java | 10 ++- .../owncloud/notes/main/MainActivity.java | 70 +++++++++++-------- .../ManageAccountsActivity.java | 2 +- 7 files changed, 67 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/accountpicker/AccountPickerDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/accountpicker/AccountPickerDialogFragment.java index 75fb39e5..9f9a11ee 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/accountpicker/AccountPickerDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/accountpicker/AccountPickerDialogFragment.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; +import it.niedermann.owncloud.notes.NotesApplication; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.branding.BrandedDialogFragment; import it.niedermann.owncloud.notes.databinding.DialogChooseAccountBinding; @@ -80,11 +81,11 @@ public class AccountPickerDialogFragment extends BrandedDialogFragment { @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - final var dialogBuilder = new MaterialAlertDialogBuilder(requireActivity()) + final MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(requireActivity()) .setTitle(R.string.simple_move) .setNegativeButton(android.R.string.cancel, null); - if (targetAccounts.size() > 0) { + if (!targetAccounts.isEmpty()) { final var binding = DialogChooseAccountBinding.inflate(LayoutInflater.from(requireContext())); final var adapter = new AccountChooserAdapter(targetAccounts, (account -> { accountPickerListener.onAccountPicked(account); @@ -96,6 +97,8 @@ public class AccountPickerDialogFragment extends BrandedDialogFragment { dialogBuilder.setMessage(getString(R.string.no_other_accounts)); } + NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), dialogBuilder); + return dialogBuilder.create(); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/accountswitcher/AccountSwitcherDialog.java b/app/src/main/java/it/niedermann/owncloud/notes/accountswitcher/AccountSwitcherDialog.java index 323b18ee..3c9e4e20 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/accountswitcher/AccountSwitcherDialog.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/accountswitcher/AccountSwitcherDialog.java @@ -109,7 +109,7 @@ public class AccountSwitcherDialog extends BrandedDialogFragment { dismiss(); }); - MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext()) + final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext()) .setView(binding.getRoot()); NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), builder); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java index 500a5bcf..ebc0c73b 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java @@ -24,6 +24,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder; import java.util.List; +import it.niedermann.owncloud.notes.NotesApplication; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.branding.BrandedDialogFragment; import it.niedermann.owncloud.notes.branding.BrandingUtil; @@ -155,13 +156,16 @@ public class CategoryDialogFragment extends BrandedDialogFragment { } }); - return new MaterialAlertDialogBuilder(requireContext()) + final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.change_category_title) .setView(dialogView) .setCancelable(true) .setPositiveButton(R.string.action_edit_save, (dialog, which) -> listener.onCategoryChosen(editCategory.getText().toString())) - .setNegativeButton(R.string.simple_cancel, null) - .create(); + .setNegativeButton(R.string.simple_cancel, null); + + NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), alertDialogBuilder); + + return alertDialogBuilder.create(); } @Override diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java index 55b3bddb..c657912c 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java @@ -17,6 +17,7 @@ import androidx.fragment.app.DialogFragment; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import it.niedermann.owncloud.notes.NotesApplication; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.branding.BrandedDialogFragment; import it.niedermann.owncloud.notes.branding.BrandingUtil; @@ -59,13 +60,16 @@ public class EditTitleDialogFragment extends BrandedDialogFragment { binding.title.setText(oldTitle); } - return new MaterialAlertDialogBuilder(requireContext()) + final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.change_note_title) .setView(dialogView) .setCancelable(true) .setPositiveButton(R.string.action_edit_save, (dialog, which) -> listener.onTitleEdited(binding.title.getText().toString())) - .setNegativeButton(R.string.simple_cancel, null) - .create(); + .setNegativeButton(R.string.simple_cancel, null); + + NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), alertDialogBuilder); + + return alertDialogBuilder.create(); } @Override diff --git a/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java index 4a6d1e87..4f5e15aa 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import it.niedermann.android.util.ClipboardUtil; import it.niedermann.nextcloud.exception.ExceptionUtil; import it.niedermann.owncloud.notes.BuildConfig; +import it.niedermann.owncloud.notes.NotesApplication; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.databinding.DialogExceptionBinding; import it.niedermann.owncloud.notes.exception.tips.TipsAdapter; @@ -69,12 +70,15 @@ public class ExceptionDialogFragment extends AppCompatDialogFragment { adapter.setThrowables(throwables); - return new MaterialAlertDialogBuilder(requireActivity()) + final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(requireActivity()) .setView(binding.getRoot()) .setTitle(R.string.error_dialog_title) .setPositiveButton(android.R.string.copy, (a, b) -> ClipboardUtil.copyToClipboard(requireContext(), getString(R.string.simple_exception), "```\n" + debugInfos + "\n```")) - .setNegativeButton(R.string.simple_close, null) - .create(); + .setNegativeButton(R.string.simple_close, null); + + NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), alertDialogBuilder); + + return alertDialogBuilder.create(); } public static DialogFragment newInstance(ArrayList exceptions) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java index f0664154..5e2e60c0 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java @@ -71,6 +71,7 @@ import java.util.stream.Collectors; import it.niedermann.android.util.ColorUtil; import it.niedermann.owncloud.notes.LockedActivity; +import it.niedermann.owncloud.notes.NotesApplication; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.accountpicker.AccountPickerListener; import it.niedermann.owncloud.notes.accountswitcher.AccountSwitcherDialog; @@ -179,37 +180,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A runOnUiThread(() -> mainViewModel.postCurrentAccount(account)); } catch (NextcloudFilesAppAccountNotFoundException e) { // Verbose log output for https://github.com/nextcloud/notes-android/issues/1256 - runOnUiThread(() -> new MaterialAlertDialogBuilder(this) - .setTitle(NextcloudFilesAppAccountNotFoundException.class.getSimpleName()) - .setMessage(R.string.backup) - .setPositiveButton(R.string.simple_backup, (a, b) -> executor.submit(() -> { - final var modifiedNotes = new LinkedList(); - for (final var account : mainViewModel.getAccounts()) { - modifiedNotes.addAll(mainViewModel.getLocalModifiedNotes(account.getId())); - } - if (modifiedNotes.size() == 1) { - final var note = modifiedNotes.get(0); - ShareUtil.openShareDialog(this, note.getTitle(), note.getContent()); - } else { - ShareUtil.openShareDialog(this, - getResources().getQuantityString(R.plurals.share_multiple, modifiedNotes.size(), modifiedNotes.size()), - mainViewModel.collectNoteContents(modifiedNotes.stream().map(Note::getId).collect(Collectors.toList()))); - } - })) - .setNegativeButton(R.string.simple_error, (a, b) -> { - final var ssoPreferences = AccountImporter.getSharedPreferences(getApplicationContext()); - final var ssoPreferencesString = new StringBuilder() - .append("Current SSO account: ").append(ssoPreferences.getString("PREF_CURRENT_ACCOUNT_STRING", null)).append("\n") - .append("\n") - .append("SSO SharedPreferences: ").append("\n"); - for (final var entry : ssoPreferences.getAll().entrySet()) { - ssoPreferencesString.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n"); - } - ssoPreferencesString.append("\n") - .append("Available accounts in DB: ").append(TextUtils.join(", ", mainViewModel.getAccounts().stream().map(Account::getAccountName).collect(Collectors.toList()))); - runOnUiThread(() -> ExceptionDialogFragment.newInstance(new RuntimeException(e.getMessage(), new RuntimeException(ssoPreferencesString.toString(), e))).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName())); - }) - .show()); + runOnUiThread(() -> showExceptionAlertDialog(e)); } catch (NoCurrentAccountSelectedException e) { runOnUiThread(() -> ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName())); } @@ -354,6 +325,43 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A }); } + private void showExceptionAlertDialog(NextcloudFilesAppAccountNotFoundException e) { + final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(this) + .setTitle(NextcloudFilesAppAccountNotFoundException.class.getSimpleName()) + .setMessage(R.string.backup) + .setPositiveButton(R.string.simple_backup, (a, b) -> executor.submit(() -> { + final var modifiedNotes = new LinkedList(); + for (final var account : mainViewModel.getAccounts()) { + modifiedNotes.addAll(mainViewModel.getLocalModifiedNotes(account.getId())); + } + if (modifiedNotes.size() == 1) { + final var note = modifiedNotes.get(0); + ShareUtil.openShareDialog(this, note.getTitle(), note.getContent()); + } else { + ShareUtil.openShareDialog(this, + getResources().getQuantityString(R.plurals.share_multiple, modifiedNotes.size(), modifiedNotes.size()), + mainViewModel.collectNoteContents(modifiedNotes.stream().map(Note::getId).collect(Collectors.toList()))); + } + })) + .setNegativeButton(R.string.simple_error, (a, b) -> { + final var ssoPreferences = AccountImporter.getSharedPreferences(getApplicationContext()); + final var ssoPreferencesString = new StringBuilder() + .append("Current SSO account: ").append(ssoPreferences.getString("PREF_CURRENT_ACCOUNT_STRING", null)).append("\n") + .append("\n") + .append("SSO SharedPreferences: ").append("\n"); + for (final var entry : ssoPreferences.getAll().entrySet()) { + ssoPreferencesString.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n"); + } + ssoPreferencesString.append("\n") + .append("Available accounts in DB: ").append(TextUtils.join(", ", mainViewModel.getAccounts().stream().map(Account::getAccountName).collect(Collectors.toList()))); + runOnUiThread(() -> ExceptionDialogFragment.newInstance(new RuntimeException(e.getMessage(), new RuntimeException(ssoPreferencesString.toString(), e))).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName())); + }); + + NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(this, alertDialogBuilder); + + alertDialogBuilder.show(); + } + @Override protected void onResume() { final var accountLiveData = mainViewModel.getCurrentAccount(); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/manageaccounts/ManageAccountsActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/manageaccounts/ManageAccountsActivity.java index 1dd1905a..30e8199f 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/manageaccounts/ManageAccountsActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/manageaccounts/ManageAccountsActivity.java @@ -147,7 +147,7 @@ public class ManageAccountsActivity extends LockedActivity implements IManageAcc binding.inputWrapper.setHint(title); - MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(this) + final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(this) .setTitle(title) .setMessage(message) .setView(binding.getRoot())