From 3adb2e1192aaa70d167c473e1ea296fcffe0d3e7 Mon Sep 17 00:00:00 2001 From: chumva Date: Fri, 28 Jul 2023 18:06:20 +0300 Subject: [PATCH] Fix https://github.com/osmandapp/OsmAnd-Issues/issues/2122 --- .../main/java/net/osmand/util/Algorithms.java | 16 +++---- .../plus/configmap/tracks/TracksFragment.java | 2 +- ...veAsNewTrackBottomSheetDialogFragment.java | 5 ++- .../myplaces/tracks/TrackFoldersHelper.java | 44 ++++++++----------- .../dialogs/MoveGpxFileBottomSheet.java | 15 ++++++- .../dialogs/TracksSelectionFragment.java | 2 +- .../track/fragments/TrackMenuFragment.java | 3 +- 7 files changed, 46 insertions(+), 41 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java index 2ea48a143b3..45fa3c66e6c 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java @@ -284,15 +284,15 @@ public static List collectDirs(File parentDir, List dirs) { } public static List collectDirs(File parentDir, List dirs, File exclDir) { - File[] listFiles = parentDir.listFiles(); - if (listFiles != null) { - Arrays.sort(listFiles); - for (File f : listFiles) { - if (f.isDirectory()) { - if (!f.equals(exclDir)) { - dirs.add(f); + File[] files = parentDir.listFiles(); + if (files != null) { + Arrays.sort(files); + for (File file : files) { + if (file.isDirectory()) { + if (!file.equals(exclDir)) { + dirs.add(file); } - collectDirs(f, dirs); + collectDirs(file, dirs, exclDir); } } } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksFragment.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksFragment.java index 308f0509bbc..d7b70bf4eee 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksFragment.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksFragment.java @@ -553,7 +553,7 @@ private void openTrackOptions(@NonNull FragmentActivity activity, @NonNull View .setIcon(getContentIcon(R.drawable.ic_action_folder_stroke)) .setOnClickListener(v -> { FragmentManager manager = activity.getSupportFragmentManager(); - MoveGpxFileBottomSheet.showInstance(manager, file, this, false, false); + MoveGpxFileBottomSheet.showInstance(manager, file, file.getParentFile(), this, false, false); }) .create()); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/SaveAsNewTrackBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/SaveAsNewTrackBottomSheetDialogFragment.java index 9c1e357d6cc..44f6fb9cc01 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/SaveAsNewTrackBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/SaveAsNewTrackBottomSheetDialogFragment.java @@ -119,8 +119,9 @@ public void afterTextChanged(Editable s) { FragmentActivity activity = getActivity(); if (activity != null) { File dest = getFile(folderPath, destFileName); - MoveGpxFileBottomSheet.showInstance(activity.getSupportFragmentManager(), - dest, SaveAsNewTrackBottomSheetDialogFragment.this, usedOnMap, true); + FragmentManager manager = activity.getSupportFragmentManager(); + MoveGpxFileBottomSheet.showInstance(manager, dest, null, + SaveAsNewTrackBottomSheetDialogFragment.this, usedOnMap, true); } }); BaseBottomSheetItem selectFolderItem = new BaseBottomSheetItem.Builder() diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/TrackFoldersHelper.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/TrackFoldersHelper.java index fd0208aaf14..d465b9dedea 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/TrackFoldersHelper.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/TrackFoldersHelper.java @@ -175,7 +175,7 @@ public void showItemOptionsMenu(@NonNull TrackItem trackItem, @NonNull View view .setIcon(getContentIcon(R.drawable.ic_action_folder_stroke)) .setOnClickListener(v -> { FragmentManager manager = activity.getSupportFragmentManager(); - MoveGpxFileBottomSheet.showInstance(manager, file, fragment, false, false); + MoveGpxFileBottomSheet.showInstance(manager, file, file.getParentFile(), fragment, false, false); }) .create()); @@ -222,12 +222,13 @@ public void showItemOptionsMenu(@NonNull TrackItem trackItem, @NonNull View view PopUpMenu.show(displayData); } - public void showItemsOptionsMenu(@NonNull Set trackItems, @NonNull Set tracksGroups, - @NonNull View view, @NonNull BaseTrackFolderFragment fragment) { - List items = new ArrayList<>(); - Set selectedTrackItems = getSelectedTrackItems(trackItems, tracksGroups); + public void showItemsOptionsMenu(@NonNull View view, @NonNull TrackFolder trackFolder, + @NonNull Set items, @NonNull Set groups, + @NonNull BaseTrackFolderFragment fragment) { + List menuItems = new ArrayList<>(); + Set selectedTrackItems = getSelectedTrackItems(items, groups); - items.add(new PopUpMenuItem.Builder(app) + menuItems.add(new PopUpMenuItem.Builder(app) .setTitleId(R.string.shared_string_show_on_map) .setIcon(getContentIcon(R.drawable.ic_show_on_map)) .setOnClickListener(v -> { @@ -236,7 +237,7 @@ public void showItemsOptionsMenu(@NonNull Set trackItems, @NonNull Se }) .create() ); - items.add(new PopUpMenuItem.Builder(app) + menuItems.add(new PopUpMenuItem.Builder(app) .setTitleId(R.string.shared_string_share) .setIcon(getContentIcon(R.drawable.ic_action_gshare_dark)) .setOnClickListener(v -> { @@ -245,35 +246,26 @@ public void showItemsOptionsMenu(@NonNull Set trackItems, @NonNull Se }) .create() ); - PluginsHelper.onOptionsMenuActivity(activity, fragment, selectedTrackItems, items); + PluginsHelper.onOptionsMenuActivity(activity, fragment, selectedTrackItems, menuItems); String move = app.getString(R.string.shared_string_move); - items.add(new PopUpMenuItem.Builder(app) + menuItems.add(new PopUpMenuItem.Builder(app) .setTitle(move) .setIcon(getContentIcon(R.drawable.ic_action_folder_move)) .setOnClickListener(v -> { - if (trackItems.isEmpty() && tracksGroups.isEmpty()) { + if (items.isEmpty() && groups.isEmpty()) { showEmptyItemsToast(move); } else { - File srcFile = null; - if (!Algorithms.isEmpty(trackItems)) { - srcFile = trackItems.iterator().next().getFile(); - } - if (srcFile == null && !Algorithms.isEmpty(tracksGroups)) { - TracksGroup group = tracksGroups.iterator().next(); - if (group instanceof TrackFolder) { - srcFile = ((TrackFolder) group).getDirFile(); - } - } + File excludedDir = trackFolder.getDirFile(); FragmentManager manager = activity.getSupportFragmentManager(); - MoveGpxFileBottomSheet.showInstance(manager, srcFile, fragment, false, false); + MoveGpxFileBottomSheet.showInstance(manager, null, excludedDir, fragment, false, false); } }) .showTopDivider(true) .create() ); String changeAppearance = app.getString(R.string.change_appearance); - items.add(new PopUpMenuItem.Builder(app) + menuItems.add(new PopUpMenuItem.Builder(app) .setTitle(changeAppearance) .setIcon(getContentIcon(R.drawable.ic_action_appearance)) .setOnClickListener(v -> { @@ -286,14 +278,14 @@ public void showItemsOptionsMenu(@NonNull Set trackItems, @NonNull Se .create() ); String delete = app.getString(R.string.shared_string_delete); - items.add(new PopUpMenuItem.Builder(app) + menuItems.add(new PopUpMenuItem.Builder(app) .setTitle(delete) .setIcon(getContentIcon(R.drawable.ic_action_delete_outlined)) .setOnClickListener(v -> { - if (trackItems.isEmpty() && tracksGroups.isEmpty()) { + if (items.isEmpty() && groups.isEmpty()) { showEmptyItemsToast(delete); } else { - showDeleteConfirmationDialog(trackItems, tracksGroups, fragment); + showDeleteConfirmationDialog(items, groups, fragment); } }) .showTopDivider(true) @@ -302,7 +294,7 @@ public void showItemsOptionsMenu(@NonNull Set trackItems, @NonNull Se PopUpMenuDisplayData displayData = new PopUpMenuDisplayData(); displayData.anchorView = view; - displayData.menuItems = items; + displayData.menuItems = menuItems; displayData.nightMode = fragment.isNightMode(); PopUpMenu.show(displayData); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/MoveGpxFileBottomSheet.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/MoveGpxFileBottomSheet.java index 282ca85f7f8..4bdb337cc81 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/MoveGpxFileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/MoveGpxFileBottomSheet.java @@ -34,11 +34,14 @@ public class MoveGpxFileBottomSheet extends MenuBottomSheetDialogFragment implem private static final String TAG = MoveGpxFileBottomSheet.class.getSimpleName(); private static final String SRC_FILE_KEY = "file_path_key"; + private static final String EXCLUDED_DIR_KEY = "excluded_dir_key"; private static final String SHOW_ALL_FOLDERS_KEY = "show_all_folders_key"; private OsmandApplication app; @Nullable private File srcFile; + @Nullable + private File excludedDir; private boolean showAllFolders; @Override @@ -50,6 +53,9 @@ public void onCreate(Bundle savedInstanceState) { if (savedInstanceState.containsKey(SRC_FILE_KEY)) { srcFile = AndroidUtils.getSerializable(savedInstanceState, SRC_FILE_KEY, File.class); } + if (savedInstanceState.containsKey(EXCLUDED_DIR_KEY)) { + excludedDir = AndroidUtils.getSerializable(savedInstanceState, EXCLUDED_DIR_KEY, File.class); + } showAllFolders = savedInstanceState.getBoolean(SHOW_ALL_FOLDERS_KEY); } } @@ -89,7 +95,7 @@ public void createMenuItems(Bundle savedInstanceState) { File rootDir = app.getAppPath(GPX_INDEX_DIR); File fileDir = srcFile != null ? srcFile.getParentFile() : null; - collectDirs(rootDir, dirs, showAllFolders ? null : fileDir); + collectDirs(rootDir, dirs, excludedDir); if (showAllFolders || !Algorithms.objectEquals(fileDir, rootDir)) { dirs.add(0, rootDir); } @@ -132,6 +138,9 @@ public void onSaveInstanceState(@NonNull Bundle outState) { if (srcFile != null) { outState.putSerializable(SRC_FILE_KEY, srcFile); } + if (excludedDir != null) { + outState.putSerializable(EXCLUDED_DIR_KEY, excludedDir); + } outState.putBoolean(SHOW_ALL_FOLDERS_KEY, showAllFolders); } @@ -164,10 +173,12 @@ public List collectFiles(File parentDir) { } public static void showInstance(@NonNull FragmentManager manager, @Nullable File srcFile, - @Nullable Fragment target, boolean usedOnMap, boolean showAllFolders) { + @Nullable File excludedDir, @Nullable Fragment target, + boolean usedOnMap, boolean showAllFolders) { if (AndroidUtils.isFragmentCanBeAdded(manager, TAG)) { MoveGpxFileBottomSheet fragment = new MoveGpxFileBottomSheet(); fragment.srcFile = srcFile; + fragment.excludedDir = excludedDir; fragment.showAllFolders = showAllFolders; fragment.setUsedOnMap(usedOnMap); fragment.setTargetFragment(target, 0); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/TracksSelectionFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/TracksSelectionFragment.java index b0ff33bc78a..c70cbea8e6d 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/TracksSelectionFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/TracksSelectionFragment.java @@ -165,7 +165,7 @@ private void setupMenuButton(@NonNull LayoutInflater inflater, @NonNull ViewGrou if (foldersHelper != null) { Set trackItems = itemsSelectionHelper.getSelectedItems(); Set tracksGroups = groupsSelectionHelper.getSelectedItems(); - foldersHelper.showItemsOptionsMenu(trackItems, tracksGroups, v, this); + foldersHelper.showItemsOptionsMenu(v, rootFolder, trackItems, tracksGroups, this); } }); button.setContentDescription(getString(R.string.shared_string_more)); diff --git a/OsmAnd/src/net/osmand/plus/track/fragments/TrackMenuFragment.java b/OsmAnd/src/net/osmand/plus/track/fragments/TrackMenuFragment.java index fd5e94d5ac0..cf5d32fc1af 100644 --- a/OsmAnd/src/net/osmand/plus/track/fragments/TrackMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/fragments/TrackMenuFragment.java @@ -1130,7 +1130,8 @@ public void onCardButtonPressed(@NonNull BaseCard card, int buttonIndex) { } else if (buttonIndex == RENAME_BUTTON_INDEX) { FileUtils.renameFile(mapActivity, new File(gpxFile.path), this, true); } else if (buttonIndex == CHANGE_FOLDER_BUTTON_INDEX) { - MoveGpxFileBottomSheet.showInstance(fragmentManager, new File(gpxFile.path), this, true, false); + File file = new File(gpxFile.path); + MoveGpxFileBottomSheet.showInstance(fragmentManager, file, file.getParentFile(), this, true, false); } else if (buttonIndex == GPS_FILTER_BUTTON_INDEX) { GpsFilterFragment.showInstance(fragmentManager, selectedGpxFile, this); } else if (buttonIndex == ALTITUDE_CORRECTION_BUTTON_INDEX) {