From 2e0e725d478ad12dbf487ccff19a86d90ed45c78 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Sat, 14 Dec 2024 11:27:57 +0100 Subject: [PATCH] Don't allow deleting a preset if it is the sole active one Fixes https://github.com/MarcusWolschon/osmeditor4android/issues/2747 --- .../android/presets/PresetEditorTest.java | 1 + .../android/prefs/PresetEditorActivity.java | 53 ++++++++++++------- src/main/res/values/strings.xml | 2 + 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/androidTest/java/de/blau/android/presets/PresetEditorTest.java b/src/androidTest/java/de/blau/android/presets/PresetEditorTest.java index 95e4ec4464..467254bc5c 100644 --- a/src/androidTest/java/de/blau/android/presets/PresetEditorTest.java +++ b/src/androidTest/java/de/blau/android/presets/PresetEditorTest.java @@ -175,6 +175,7 @@ public void downloadPreset() { menu = entry.getParent().getParent().findObject(By.res(device.getCurrentPackageName() + ":id/listItemMenu")); menu.click(); TestUtils.clickText(device, false, main.getString(R.string.delete), true); + TestUtils.clickText(device, false, main.getString(R.string.yes), true); TestUtils.clickHome(device, true); App.resetPresets(); } diff --git a/src/main/java/de/blau/android/prefs/PresetEditorActivity.java b/src/main/java/de/blau/android/prefs/PresetEditorActivity.java index 61da9d7219..88462a57df 100644 --- a/src/main/java/de/blau/android/prefs/PresetEditorActivity.java +++ b/src/main/java/de/blau/android/prefs/PresetEditorActivity.java @@ -1,5 +1,7 @@ package de.blau.android.prefs; +import static de.blau.android.contract.Constants.LOG_TAG_LEN; + import java.io.File; import java.net.MalformedURLException; import java.net.URL; @@ -54,8 +56,8 @@ /** Provides an activity to edit the preset list. Downloads preset data when necessary. */ public class PresetEditorActivity extends URLListEditActivity { - private static final String DEBUG_TAG = PresetEditorActivity.class.getSimpleName().substring(0, - Math.min(23, PresetEditorActivity.class.getSimpleName().length())); + private static final int TAG_LEN = Math.min(LOG_TAG_LEN, PresetEditorActivity.class.getSimpleName().length()); + private static final String DEBUG_TAG = PresetEditorActivity.class.getSimpleName().substring(0, TAG_LEN); private AdvancedPrefDatabase db; @@ -171,9 +173,7 @@ protected void onLoadList(List items) { @Override protected void onItemClicked(ListEditItem item) { - if (item.active && db.getActivePresets().length == 1) { // at least one item needs to be selected - updateAdapter(); - ScreenMessage.barWarning(this, R.string.toast_min_one_preset); + if (!activePresetEnsured(item)) { return; } item.active = !item.active; @@ -207,8 +207,29 @@ protected void onItemEdited(ListEditItem item) { @Override protected void onItemDeleted(ListEditItem item) { - db.deletePreset(item.id); - App.resetPresets(); + if (!activePresetEnsured(item)) { + return; + } + new AlertDialog.Builder(this).setTitle(R.string.delete).setMessage(R.string.preset_management_delete) + .setPositiveButton(R.string.Yes, (dialog, which) -> { + db.deletePreset(item.id); + App.resetPresets(); + }).setNegativeButton(R.string.cancel, null).show(); + } + + /** + * Check that we have at least one active preset + * + * @param item the current item + * @return true if there will be at least one active item after item is de-activated or deleted + */ + private boolean activePresetEnsured(@NonNull ListEditItem item) { + if (item.active && db.getActivePresets().length == 1) { // at least one item needs to be selected + updateAdapter(); + ScreenMessage.barWarning(this, R.string.toast_min_one_preset); + return false; + } + return true; } @Override @@ -276,12 +297,8 @@ protected void onPreExecute() { protected Integer doInBackground(Void args) { Uri uri = Uri.parse(item.value); final String scheme = uri.getScheme(); - int loadResult; - if (Schemes.FILE.equals(scheme) || Schemes.CONTENT.equals(scheme)) { - loadResult = PresetLoader.load(activity, uri, presetDir, Preset.PRESETXML); - } else { - loadResult = PresetLoader.download(item.value, presetDir, Preset.PRESETXML); - } + int loadResult = Schemes.FILE.equals(scheme) || Schemes.CONTENT.equals(scheme) ? PresetLoader.load(activity, uri, presetDir, Preset.PRESETXML) + : PresetLoader.download(item.value, presetDir, Preset.PRESETXML); if (loadResult == PresetLoader.DOWNLOADED_PRESET_ERROR) { return RESULT_TOTAL_FAILURE; @@ -381,11 +398,11 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { final PresetEditorActivity activity = (PresetEditorActivity) getActivity(); - if (item != null) { + final boolean itemExists = item != null; + if (itemExists) { editName.setText(item.name); editValue.setText(item.value); useTranslations.setChecked(item.boolean0); - } else if (activity.isAddingViaIntent()) { String tmpName = activity.getIntent().getExtras().getString(EXTRA_NAME); String tmpValue = activity.getIntent().getExtras().getString(EXTRA_VALUE); @@ -393,13 +410,13 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { editValue.setText(tmpValue == null ? "" : tmpValue); useTranslations.setChecked(true); } - if (item != null && item.value3 != null) { + if (itemExists && item.value3 != null) { version.setText(item.value3); } else { versionLabel.setVisibility(View.GONE); version.setVisibility(View.GONE); } - if (item != null && LISTITEM_ID_DEFAULT.equals(item.id)) { + if (itemExists && LISTITEM_ID_DEFAULT.equals(item.id)) { // name and value are not editable editName.setInputType(InputType.TYPE_NULL); editName.setBackground(null); @@ -447,7 +464,7 @@ public boolean read(FragmentActivity currentActivity, Uri fileUri) { // save or display toast, exception for localhost is needed for testing if (validPresetURL || presetURL.startsWith(Schemes.FILE) || presetURL.startsWith(Schemes.CONTENT) - || (url != null && "localhost".equals(url.getHost())) || (item != null && item.id.equals(LISTITEM_ID_DEFAULT))) { + || (url != null && "localhost".equals(url.getHost())) || (itemExists && item.id.equals(LISTITEM_ID_DEFAULT))) { if (item == null) { // new item activity.finishCreateItem(new ListEditItem(name, presetURL, null, null, useTranslationsEnabled, null)); diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index f225f12f12..9c27b7d991 100755 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -1556,6 +1556,8 @@ The preset file is invalid. The preset will not work. Preset file downloaded. However, some missing icons were not. Preset downloader + + Really delete Preset? Add preset This preset already exists