diff --git a/OsmAnd/src/net/osmand/FileUtils.java b/OsmAnd/src/net/osmand/FileUtils.java index 1f9a755be5..fed5346212 100644 --- a/OsmAnd/src/net/osmand/FileUtils.java +++ b/OsmAnd/src/net/osmand/FileUtils.java @@ -1,27 +1,24 @@ package net.osmand; -import android.app.Activity; -import android.content.DialogInterface; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.View; -import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; +import net.osmand.GPXUtilities.GPXFile; +import net.osmand.GPXUtilities.Metadata; import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.SQLiteTileSource; +import net.osmand.plus.dialogs.RenameFileBottomSheet; import net.osmand.util.Algorithms; import java.io.File; import java.io.IOException; -import java.lang.ref.WeakReference; import java.util.regex.Pattern; public class FileUtils { @@ -29,70 +26,11 @@ public class FileUtils { public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]"); public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); - public static void renameFile(Activity a, final File f, final RenameCallback callback) { - final WeakReference weakActivity = new WeakReference<>(a); - AlertDialog.Builder b = new AlertDialog.Builder(a); - if (f.exists()) { - int xt = f.getName().lastIndexOf('.'); - final String ext = xt == -1 ? "" : f.getName().substring(xt); - final String originalName = xt == -1 ? f.getName() : f.getName().substring(0, xt); - final EditText editText = new EditText(a); - editText.setText(originalName); - editText.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - Editable text = editText.getText(); - if (text.length() >= 1) { - Activity activity = weakActivity.get(); - if (ILLEGAL_FILE_NAME_CHARACTERS.matcher(text).find() && activity != null) { - editText.setError(activity.getString(R.string.file_name_containes_illegal_char)); - } - } - } - }); - b.setTitle(R.string.shared_string_rename); - int leftPadding = AndroidUtils.dpToPx(a, 24f); - int topPadding = AndroidUtils.dpToPx(a, 4f); - b.setView(editText, leftPadding, topPadding, leftPadding, topPadding); - // Behaviour will be overwritten later; - b.setPositiveButton(R.string.shared_string_save, null); - b.setNegativeButton(R.string.shared_string_cancel, null); - final AlertDialog alertDialog = b.create(); - alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialog) { - alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - Activity activity = weakActivity.get(); - if (activity != null) { - OsmandApplication app = (OsmandApplication) activity.getApplication(); - if (ext.equals(SQLiteTileSource.EXT)) { - if (renameSQLiteFile(app, f, editText.getText().toString() + ext, - callback) != null) { - alertDialog.dismiss(); - } - } else { - if (renameGpxFile(app, f, editText.getText().toString() + ext, - false, callback) != null) { - alertDialog.dismiss(); - } - } - } - } - }); - } - }); - alertDialog.show(); + public static void renameFile(@NonNull FragmentActivity activity, @NonNull File file, + @Nullable Fragment target, boolean usedOnMap) { + if (file.exists()) { + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + RenameFileBottomSheet.showInstance(fragmentManager, target, file, usedOnMap); } } @@ -106,7 +44,7 @@ public class FileUtils { dest.getParentFile().mkdirs(); } if (source.renameTo(dest)) { - final String[] suffixes = new String[]{"-journal", "-wal", "-shm"}; + final String[] suffixes = new String[] {"-journal", "-wal", "-shm"}; for (String s : suffixes) { File file = new File(ctx.getDatabasePath(source + s).toString()); if (file.exists()) { @@ -140,6 +78,26 @@ public class FileUtils { return res; } + public static File renameFile(@NonNull OsmandApplication app, @NonNull File source, + @NonNull String newName, boolean dirAllowed, RenameCallback callback) { + File dest = checkRenamePossibility(app, source, newName, dirAllowed); + if (dest == null) { + return null; + } + if (!dest.getParentFile().exists()) { + dest.getParentFile().mkdirs(); + } + File res = source.renameTo(dest) ? dest : null; + if (res != null) { + if (callback != null) { + callback.renamedTo(res); + } + } else { + Toast.makeText(app, R.string.file_can_not_be_renamed, Toast.LENGTH_LONG).show(); + } + return res; + } + public static File renameGpxFile(@NonNull OsmandApplication app, @NonNull File src, @NonNull File dest) { if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); diff --git a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java index 839823c369..e249fc2770 100644 --- a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.content.res.ColorStateList; import android.os.AsyncTask; import android.os.Bundle; +import android.os.PersistableBundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.MenuItem; @@ -45,6 +46,7 @@ import net.osmand.plus.settings.backend.OsmAndAppCustomization; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.track.TrackDisplayHelper; import net.osmand.plus.views.AddGpxPointBottomSheetHelper.NewGpxPoint; +import net.osmand.util.Algorithms; import java.io.File; import java.lang.ref.WeakReference; @@ -83,10 +85,35 @@ public class TrackActivity extends TabActivity { return; } displayHelper = new TrackDisplayHelper(app); - if (intent.hasExtra(TRACK_FILE_NAME)) { + if (savedInstanceState != null) { + String path = savedInstanceState.getString(TRACK_FILE_NAME); + if (!Algorithms.isEmpty(path)) { + displayHelper.setFile(new File(path)); + } + } else if (intent.hasExtra(TRACK_FILE_NAME)) { displayHelper.setFile(new File(intent.getStringExtra(TRACK_FILE_NAME))); } + setupActionBar(); + if (intent.hasExtra(OPEN_POINTS_TAB) + || (savedInstanceState != null && savedInstanceState.getBoolean(OPEN_POINTS_TAB, false))) { + openPointsTab = true; + } + if (intent.hasExtra(OPEN_TRACKS_LIST)) { + openTracksList = true; + } + setContentView(R.layout.track_content); + } + + @Override + public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) { + File file = getFile(); + outState.putString(TRACK_FILE_NAME, file != null ? file.getAbsolutePath() : null); + outState.putBoolean(CURRENT_RECORDING, file == null); + super.onSaveInstanceState(outState, outPersistentState); + } + + public void setupActionBar() { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { if (getFile() != null) { @@ -97,14 +124,6 @@ public class TrackActivity extends TabActivity { } actionBar.setElevation(AndroidUtils.dpToPx(app, 4f)); } - if (intent.hasExtra(OPEN_POINTS_TAB) - || (savedInstanceState != null && savedInstanceState.getBoolean(OPEN_POINTS_TAB, false))) { - openPointsTab = true; - } - if (intent.hasExtra(OPEN_TRACKS_LIST)) { - openTracksList = true; - } - setContentView(R.layout.track_content); } public TrackDisplayHelper getDisplayHelper() { diff --git a/OsmAnd/src/net/osmand/plus/dialogs/RenameFileBottomSheet.java b/OsmAnd/src/net/osmand/plus/dialogs/RenameFileBottomSheet.java new file mode 100644 index 0000000000..c6057b56de --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/dialogs/RenameFileBottomSheet.java @@ -0,0 +1,168 @@ +package net.osmand.plus.dialogs; + +import android.content.res.ColorStateList; +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; + +import com.google.android.material.textfield.TextInputEditText; +import com.google.android.material.textfield.TextInputLayout; + +import net.osmand.AndroidUtils; +import net.osmand.FileUtils.RenameCallback; +import net.osmand.IndexConstants; +import net.osmand.PlatformUtil; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.SQLiteTileSource; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.util.Algorithms; + +import org.apache.commons.logging.Log; + +import java.io.File; + +import static net.osmand.FileUtils.ILLEGAL_FILE_NAME_CHARACTERS; +import static net.osmand.FileUtils.renameFile; +import static net.osmand.FileUtils.renameGpxFile; +import static net.osmand.FileUtils.renameSQLiteFile; + +public class RenameFileBottomSheet extends MenuBottomSheetDialogFragment { + + private static final Log LOG = PlatformUtil.getLog(RenameFileBottomSheet.class); + private static final String TAG = RenameFileBottomSheet.class.getName(); + private static final String SOURCE_FILE_NAME_KEY = "source_file_name_key"; + private static final String SELECTED_FILE_NAME_KEY = "selected_file_name_key"; + + private OsmandApplication app; + + private TextInputEditText editText; + private TextInputLayout nameTextBox; + + private File file; + private String selectedFileName; + + @Override + public void createMenuItems(Bundle savedInstanceState) { + app = requiredMyApplication(); + if (savedInstanceState != null) { + String path = savedInstanceState.getString(SOURCE_FILE_NAME_KEY); + if (!Algorithms.isEmpty(path)) { + file = new File(path); + } + selectedFileName = savedInstanceState.getString(SELECTED_FILE_NAME_KEY); + } + items.add(new TitleItem(getString(R.string.shared_string_rename))); + + View view = UiUtilities.getInflater(app, nightMode).inflate(R.layout.track_name_edit_text, null); + nameTextBox = view.findViewById(R.id.name_text_box); + nameTextBox.setBoxBackgroundColorResource(nightMode ? R.color.list_background_color_dark : R.color.activity_background_color_light); + nameTextBox.setHint(AndroidUtils.addColon(app, R.string.shared_string_name)); + ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat + .getColor(app, nightMode ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light)); + nameTextBox.setDefaultHintTextColor(colorStateList); + + editText = view.findViewById(R.id.name_edit_text); + editText.setText(selectedFileName != null ? selectedFileName : Algorithms.getFileNameWithoutExtension(file)); + editText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + updateFileName(s.toString()); + } + }); + + BaseBottomSheetItem editFolderName = new BaseBottomSheetItem.Builder() + .setCustomView(view) + .create(); + items.add(editFolderName); + } + + private void updateFileName(String name) { + if (!Algorithms.isEmpty(name) && ILLEGAL_FILE_NAME_CHARACTERS.matcher(name).find()) { + nameTextBox.setError(getString(R.string.file_name_containes_illegal_char)); + } else { + selectedFileName = name; + nameTextBox.setError(null); + } + updateBottomButtons(); + } + + @Override + protected boolean isRightBottomButtonEnabled() { + return nameTextBox.getError() == null; + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putString(SOURCE_FILE_NAME_KEY, file.getAbsolutePath()); + outState.putString(SELECTED_FILE_NAME_KEY, selectedFileName); + super.onSaveInstanceState(outState); + } + + @Override + protected void onRightBottomButtonClick() { + FragmentActivity activity = getActivity(); + if (activity != null) { + AndroidUtils.hideSoftKeyboard(activity, editText); + } + File dest; + int index = file.getName().lastIndexOf('.'); + String ext = index == -1 ? "" : file.getName().substring(index); + if (SQLiteTileSource.EXT.equals(ext)) { + dest = renameSQLiteFile(app, file, selectedFileName + ext, null); + } else if (IndexConstants.GPX_FILE_EXT.equals(ext)) { + dest = renameGpxFile(app, file, selectedFileName + ext, false, null); + } else { + dest = renameFile(app, file, selectedFileName + ext, false, null); + } + if (dest != null) { + Fragment fragment = getTargetFragment(); + if (fragment instanceof RenameCallback) { + RenameCallback listener = (RenameCallback) fragment; + listener.renamedTo(dest); + } + dismiss(); + } + } + + @Override + protected int getDismissButtonTextId() { + return R.string.shared_string_cancel; + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_save; + } + + public static void showInstance(@NonNull FragmentManager fragmentManager, @Nullable Fragment target, + @NonNull File file, boolean usedOnMap) { + if (file.exists() && !fragmentManager.isStateSaved() + && fragmentManager.findFragmentByTag(RenameFileBottomSheet.TAG) == null) { + RenameFileBottomSheet fragment = new RenameFileBottomSheet(); + fragment.file = file; + fragment.setUsedOnMap(usedOnMap); + fragment.setTargetFragment(target, 0); + fragment.show(fragmentManager, RenameFileBottomSheet.TAG); + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 9160160973..3081c0680b 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -29,6 +29,7 @@ import androidx.appcompat.view.ActionMode; import androidx.appcompat.widget.PopupMenu; import androidx.core.content.ContextCompat; import androidx.core.view.MenuItemCompat; +import androidx.fragment.app.FragmentActivity; import net.osmand.AndroidUtils; import net.osmand.Collator; @@ -74,7 +75,7 @@ import java.util.Map; import java.util.Set; public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents, - OnMapSourceUpdateListener { + OnMapSourceUpdateListener, RenameCallback { private LoadLocalIndexTask asyncLoader; private Map filesToUpdate = new HashMap<>(); private LocalIndexesAdapter listAdapter; @@ -141,13 +142,11 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement private boolean performBasicOperation(int resId, final LocalIndexInfo info) { if (resId == R.string.shared_string_rename) { - FileUtils.renameFile(getActivity(), new File(info.getPathToData()), new RenameCallback() { - - @Override - public void renamedTo(File file) { - getDownloadActivity().reloadLocalIndexes(); - } - }); + FragmentActivity activity = getActivity(); + if (activity != null) { + File file = new File(info.getPathToData()); + FileUtils.renameFile(activity, file, this, false); + } } else if (resId == R.string.clear_tile_data) { AlertDialog.Builder confirm = new AlertDialog.Builder(getActivity()); confirm.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @@ -188,7 +187,19 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement @Override public void onMapSourceUpdated() { - getDownloadActivity().reloadLocalIndexes(); + reloadLocalIndexes(); + } + + @Override + public void renamedTo(File file) { + reloadLocalIndexes(); + } + + private void reloadLocalIndexes() { + DownloadActivity activity = getDownloadActivity(); + if (activity != null) { + activity.reloadLocalIndexes(); + } } public class LoadLocalIndexTask extends AsyncTask> @@ -666,7 +677,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement public void localOptionsMenu(final int itemId) { if (itemId == R.string.shared_string_refresh) { - getDownloadActivity().reloadLocalIndexes(); + reloadLocalIndexes(); } else if (itemId == R.string.shared_string_delete) { openSelectionMode(itemId, R.drawable.ic_action_delete_dark, new DialogInterface.OnClickListener() { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 9e4ac656a4..d17e9d8b67 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -42,6 +42,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.view.ActionMode; import androidx.appcompat.widget.SearchView; import androidx.core.content.ContextCompat; +import androidx.fragment.app.FragmentActivity; import net.osmand.AndroidUtils; import net.osmand.Collator; @@ -84,8 +85,8 @@ import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener; import net.osmand.plus.settings.backend.OsmandSettings; -import net.osmand.plus.widgets.popup.PopUpMenuItem; import net.osmand.plus.widgets.popup.PopUpMenuHelper; +import net.osmand.plus.widgets.popup.PopUpMenuItem; import java.io.File; import java.text.DateFormat; @@ -105,6 +106,7 @@ import java.util.regex.Pattern; import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; +import static net.osmand.plus.myplaces.FavoritesActivity.OPEN_GPX_REQUEST; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import static net.osmand.util.Algorithms.capitalizeFirstLetter; import static net.osmand.util.Algorithms.collectDirs; @@ -113,7 +115,7 @@ import static net.osmand.util.Algorithms.objectEquals; import static net.osmand.util.Algorithms.removeAllFiles; public class AvailableGPXFragment extends OsmandExpandableListFragment implements - FavoritesFragmentStateHolder, OsmAuthorizationListener { + FavoritesFragmentStateHolder, OsmAuthorizationListener, RenameCallback { public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); public static final int SEARCH_ID = -1; @@ -426,7 +428,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, f.getAbsolutePath()); } newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - a.startActivity(newIntent); + a.startActivityForResult(newIntent, OPEN_GPX_REQUEST); } public void reloadTracks() { @@ -434,6 +436,10 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity()); } + public void resetTracksLoader() { + asyncLoader = null; + } + @Override public void onCreateOptionsMenu(Menu menu, @NonNull MenuInflater inflater) { menu.clear(); @@ -879,8 +885,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement File dest = new File(destFolder, info.fileName); if (info.file.renameTo(dest)) { app.getGpxDbHelper().rename(info.file, dest); - asyncLoader = new LoadGpxTask(); - asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity()); + reloadTracks(); } else { Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show(); } @@ -902,8 +907,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement } else { if (info.file.renameTo(dest)) { app.getGpxDbHelper().rename(info.file, dest); - asyncLoader = new LoadGpxTask(); - asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity()); + reloadTracks(); } else { Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show(); } @@ -938,6 +942,11 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement app.startActivity(intent); } + @Override + public void renamedTo(File file) { + reloadTracks(); + } + public class LoadGpxTask extends AsyncTask> { private List result; @@ -1543,13 +1552,10 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - FileUtils.renameFile(getActivity(), gpxInfo.file, new RenameCallback() { - @Override - public void renamedTo(File file) { - asyncLoader = new LoadGpxTask(); - asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity()); - } - }); + FragmentActivity activity = getActivity(); + if (activity != null) { + FileUtils.renameFile(activity, gpxInfo.file, AvailableGPXFragment.this, false); + } } }) .create() @@ -1711,20 +1717,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement GpxInfo item = allGpxAdapter.getChild(groupPosition, childPosition); if (!selectionMode) { - Intent newIntent = new Intent(getActivity(), getMyApplication().getAppCustomization().getTrackActivity()); - // causes wrong position caching: newIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); - if (item.currentlyRecordingTrack) { - newIntent.putExtra(TrackActivity.CURRENT_RECORDING, true); - } else { - newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, item.file.getAbsolutePath()); - } - newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - startActivity(newIntent); - // item.setExpanded(!item.isExpanded()); - // if (item.isExpanded()) { - // descriptionLoader = new LoadLocalIndexDescriptionTask(); - // descriptionLoader.execute(item); - // } + openTrack(getActivity(), item.file); } else { if (!selectedItems.contains(item)) { selectedItems.add(item); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index 43e1e73cc8..c4e5cc8cee 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -42,6 +42,8 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; +import static net.osmand.plus.myplaces.TrackSegmentFragment.TRACK_DELETED_KEY; + /** * */ @@ -50,6 +52,7 @@ public class FavoritesActivity extends TabActivity { private static final int OPEN_GPX_DOCUMENT_REQUEST = 1006; private static final int IMPORT_FAVOURITES_REQUEST = 1007; + protected static final int OPEN_GPX_REQUEST = 1008; public static final String TAB_ID = "selected_tab_id"; @@ -152,6 +155,13 @@ public class FavoritesActivity extends TabActivity { if (data != null && data.getData() != null) { importHelper.handleGpxOrFavouritesImport(data.getData()); } + } else if (requestCode == OPEN_GPX_REQUEST && resultCode == Activity.RESULT_OK) { + if (data != null && data.getBooleanExtra(TRACK_DELETED_KEY, false)) { + AvailableGPXFragment gpxFragment = getGpxFragment(); + if (gpxFragment != null) { + gpxFragment.resetTracksLoader(); + } + } } else { super.onActivityResult(requestCode, resultCode, data); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java index 4541cf810a..4a66e09d99 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java @@ -1,6 +1,8 @@ package net.osmand.plus.myplaces; +import android.app.Activity; import android.content.DialogInterface; +import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.AsyncTask; @@ -23,6 +25,8 @@ import androidx.fragment.app.FragmentManager; import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import net.osmand.AndroidUtils; +import net.osmand.FileUtils; +import net.osmand.FileUtils.RenameCallback; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.TrkSegment; @@ -53,7 +57,10 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -public class TrackSegmentFragment extends OsmAndListFragment implements TrackBitmapDrawerListener, SegmentActionsListener { +public class TrackSegmentFragment extends OsmAndListFragment implements TrackBitmapDrawerListener, + SegmentActionsListener, RenameCallback { + + public static final String TRACK_DELETED_KEY = "track_deleted_key"; private OsmandApplication app; private TrackDisplayHelper displayHelper; @@ -134,8 +141,40 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit } }); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); - } - if (gpxFile.showCurrentTrack) { + MenuItem renameItem = menu.add(R.string.shared_string_rename) + .setIcon(app.getUIUtilities().getIcon((R.drawable.ic_action_edit_dark))) + .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + GPXFile gpx = displayHelper.getGpx(); + FragmentActivity activity = getActivity(); + if (activity != null && gpx != null) { + FileUtils.renameFile(activity, new File(gpx.path), TrackSegmentFragment.this, false); + } + return true; + } + }); + renameItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); + MenuItem deleteItem = menu.add(R.string.shared_string_delete) + .setIcon(app.getUIUtilities().getIcon((R.drawable.ic_action_delete_dark))) + .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + GPXFile gpx = displayHelper.getGpx(); + FragmentActivity activity = getActivity(); + if (activity != null && gpx != null) { + if (FileUtils.removeGpxFile(app, new File((gpx.path)))) { + Intent intent = new Intent(); + intent.putExtra(TRACK_DELETED_KEY, true); + activity.setResult(Activity.RESULT_OK, intent); + activity.onBackPressed(); + } + } + return true; + } + }); + deleteItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); + } else if (gpxFile.showCurrentTrack) { MenuItem item = menu.add(R.string.shared_string_refresh).setIcon(R.drawable.ic_action_refresh_dark) .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override @@ -415,4 +454,14 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit } }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } + + @Override + public void renamedTo(File file) { + displayHelper.setFile(file); + TrackActivity activity = getTrackActivity(); + if (activity != null) { + activity.setupActionBar(); + activity.loadGpx(); + } + } } \ No newline at end of file