From bbd7c27b9a33d710a6dd2556740dd358e087e394 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Fri, 21 Jun 2019 17:34:37 +0300 Subject: [PATCH 01/10] fix for issue 6971 --- OsmAnd/src/net/osmand/plus/activities/MapActivity.java | 7 +++++++ OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java | 6 ++++++ OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java | 4 +++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 3f56a6fac6..19802aa833 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -640,6 +640,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (mapViewTrackingUtilities.isMapLinkedToLocation()) { prevActivityIntent.putExtra(SearchActivity.SEARCH_NEARBY, true); } + if (prevActivityIntent.getComponent() != null + && prevActivityIntent.getComponent().getClassName().equals("net.osmand.plus.myplaces.FavoritesActivity")) { +// LOG.debug("PrevVisibleDash: " + dashboardOnMap.getPreviousVisibleType().name()); + } this.startActivity(prevActivityIntent); prevActivityIntent = null; return; @@ -1612,6 +1616,9 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (intentExtraActionName != null) { newIntent.putExtra(intentExtraActionName, intentExtraActionValue); } + if (intentExtraActionName == null && intentExtraActionValue != null) { + newIntent.putExtras(intentExtraActionValue); + } activity.startActivity(newIntent); } } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index 1fff265234..289e1a5778 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -44,6 +44,7 @@ public class FavoritesActivity extends TabActivity { public static final String OPEN_FAVOURITES_TAB = "open_favourites_tab"; public static final String OPEN_MY_PLACES_TAB = "open_my_places_tab"; + public static final String OPEN_OSM_EDITS_TAB = "open_osm_edits_tab"; public static final int GPX_TAB = R.string.shared_string_tracks; public static final int FAV_TAB = R.string.shared_string_my_favorites; @@ -72,6 +73,11 @@ public class FavoritesActivity extends TabActivity { setTabs(mTabs); // setupHomeButton(); +// Bundle b = getIntent().getExtras(); +// if (b != null) { +// if () +// } + ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); if (icicle == null) { Intent intent = getIntent(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 33600b93ef..4ecae35a94 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -648,7 +648,9 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo String name = (isOsmPoint ? ((OpenstreetmapPoint) osmPoint).getName() : ((OsmNotesPoint) osmPoint).getText()); getMyApplication().getSettings().setMapLocationToShow(osmPoint.getLatitude(), osmPoint.getLongitude(), 15, new PointDescription(type, name), true, osmPoint); //$NON-NLS-1$ - MapActivity.launchMapActivityMoveToTop(getActivity()); + Bundle b = new Bundle(); + b.putString("favorites_activity", "osm_edits_tab"); + MapActivity.launchMapActivityMoveToTop(getActivity(), null, b); } private void deletePoint(OsmPoint osmPoint) { From b2898233d085040d45a0ddc0f4384c2ccde145d1 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Mon, 24 Jun 2019 16:38:13 +0300 Subject: [PATCH 02/10] Restoring tab and position on return from MapActivity to FavoritesActivity. --- .../osmand/plus/activities/MapActivity.java | 6 +-- .../osmand/plus/audionotes/NotesFragment.java | 39 +++++++++++++++- .../plus/myplaces/FavoritesActivity.java | 45 +++++++++++++++---- .../osmand/plus/osmedit/OsmEditsFragment.java | 39 ++++++++++++++-- 4 files changed, 113 insertions(+), 16 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 19802aa833..7966c4b096 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1601,6 +1601,9 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven Intent intent = ((Activity) activity).getIntent(); if (intent != null) { prevActivityIntent = new Intent(intent); + if (intentExtraActionName == null && intentExtraActionValue != null) { + prevActivityIntent.putExtras(intentExtraActionValue); + } prevActivityIntent.putExtra(INTENT_KEY_PARENT_MAP_ACTIVITY, true); } else { prevActivityIntent = null; @@ -1616,9 +1619,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (intentExtraActionName != null) { newIntent.putExtra(intentExtraActionName, intentExtraActionValue); } - if (intentExtraActionName == null && intentExtraActionValue != null) { - newIntent.putExtras(intentExtraActionValue); - } activity.startActivity(newIntent); } } diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index f009782815..68e0999dc5 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -1,5 +1,9 @@ package net.osmand.plus.audionotes; +import static net.osmand.plus.myplaces.FavoritesActivity.NOTE_TAB; +import static net.osmand.plus.myplaces.FavoritesActivity.SCROLL_POSITION; +import static net.osmand.plus.myplaces.FavoritesActivity.TAB_TO_OPEN; + import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -48,6 +52,7 @@ import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; +import net.osmand.plus.myplaces.FavoritesActivity.ObjectListPosition; import org.apache.commons.logging.Log; import java.io.File; @@ -60,7 +65,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; -public class NotesFragment extends OsmAndListFragment { +public class NotesFragment extends OsmAndListFragment implements ObjectListPosition { public static final Recording SHARE_LOCATION_FILE = new Recording(new File(".")); @@ -141,6 +146,9 @@ public class NotesFragment extends OsmAndListFragment { listAdapter.setListener(createAdapterListener()); listAdapter.setPortrait(portrait); listView.setAdapter(listAdapter); + if (getActivity() != null && getActivity() instanceof FavoritesActivity) { + setListPosition(((FavoritesActivity) getActivity()).getScrollPosition()); + } } @Override @@ -562,10 +570,14 @@ public class NotesFragment extends OsmAndListFragment { } private void showOnMap(Recording recording) { + int position = getObjectListPosition(recording); getMyApplication().getSettings().setMapLocationToShow(recording.getLatitude(), recording.getLongitude(), 15, new PointDescription(recording.getSearchHistoryType(), recording.getName(getActivity(), true)), true, recording); - MapActivity.launchMapActivityMoveToTop(getActivity()); + Bundle b = new Bundle(); + b.putInt(TAB_TO_OPEN, NOTE_TAB); + b.putInt(SCROLL_POSITION, position); + MapActivity.launchMapActivityMoveToTop(getActivity(), null, b); } private void editNote(final Recording recording) { @@ -604,4 +616,27 @@ public class NotesFragment extends OsmAndListFragment { .setNegativeButton(R.string.shared_string_cancel, null) .show(); } + + @Override + public int getObjectListPosition(Object object) { + List list = createItemsList(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i) == object) { + return i; + } + } + return 0; + } + + @Override + public void setListPosition(int position) { + int itemsCount = getListView().getAdapter().getCount(); + if (itemsCount > 0 && itemsCount > position) { + if (position == 1) { + getListView().setSelection(0); + } else { + getListView().setSelection(position); + } + } + } } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index 289e1a5778..009d8006bb 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -44,15 +44,22 @@ public class FavoritesActivity extends TabActivity { public static final String OPEN_FAVOURITES_TAB = "open_favourites_tab"; public static final String OPEN_MY_PLACES_TAB = "open_my_places_tab"; - public static final String OPEN_OSM_EDITS_TAB = "open_osm_edits_tab"; + public static final String TAB_TO_OPEN = "tab_to_open"; + + public static final String SCROLL_POSITION = "scroll_position"; + + public static final int GPX_TAB = R.string.shared_string_tracks; + public static final int FAV_TAB = R.string.shared_string_my_favorites; + public static final int NOTE_TAB = R.string.notes; + public static final int OSM_TAB = R.string.osm_edits; - public static final int GPX_TAB = R.string.shared_string_tracks; - public static final int FAV_TAB = R.string.shared_string_my_favorites; protected List> fragList = new ArrayList<>(); private int tabSize; private ImportHelper importHelper; private String groupNameToShow; + private int scrollPosition = 0; + @Override public void onCreate(Bundle icicle) { OsmandApplication app = (OsmandApplication) getApplication(); @@ -61,6 +68,7 @@ public class FavoritesActivity extends TabActivity { app.logEvent("myplaces_open"); + importHelper = new ImportHelper(this, getMyApplication(), null); //noinspection ConstantConditions @@ -73,15 +81,13 @@ public class FavoritesActivity extends TabActivity { setTabs(mTabs); // setupHomeButton(); -// Bundle b = getIntent().getExtras(); -// if (b != null) { -// if () -// } - ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); if (icicle == null) { Intent intent = getIntent(); if (intent != null) { + if (intent.hasExtra(SCROLL_POSITION)) { + scrollPosition = intent.getIntExtra(SCROLL_POSITION, 0); + } if (intent.hasExtra(OPEN_FAVOURITES_TAB) && intent.getBooleanExtra(OPEN_FAVOURITES_TAB, false)) { if (intent.hasExtra(GROUP_NAME_TO_SHOW)) { groupNameToShow = intent.getStringExtra(GROUP_NAME_TO_SHOW); @@ -90,6 +96,16 @@ public class FavoritesActivity extends TabActivity { } else if (intent.hasExtra(OPEN_MY_PLACES_TAB) && intent.getBooleanExtra(OPEN_MY_PLACES_TAB, false)) { mViewPager.setCurrentItem(1, false); } + + if (intent.hasExtra(TAB_TO_OPEN)) { + for (int n = 0; n < mTabs.size(); n++) { + int tab = intent.getIntExtra(TAB_TO_OPEN, FAV_TAB); + if (mTabs.get(n).mTitle.equals(getString(tab))) { + mViewPager.setCurrentItem(n, false); + break; + } + } + } } } } @@ -250,5 +266,18 @@ public class FavoritesActivity extends TabActivity { e.printStackTrace(); } } + + public interface ObjectListPosition { + + int getObjectListPosition(Object object); + + void setListPosition(int position); + } + + public int getScrollPosition() { + int p = scrollPosition; + scrollPosition = 0; + return p; + } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 4ecae35a94..6f777bbe6b 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -1,5 +1,9 @@ package net.osmand.plus.osmedit; +import static net.osmand.plus.myplaces.FavoritesActivity.OSM_TAB; +import static net.osmand.plus.myplaces.FavoritesActivity.SCROLL_POSITION; +import static net.osmand.plus.myplaces.FavoritesActivity.TAB_TO_OPEN; + import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; @@ -33,7 +37,6 @@ import net.osmand.AndroidUtils; import net.osmand.data.PointDescription; import net.osmand.osm.edit.Entity; import net.osmand.osm.edit.Node; -import net.osmand.osm.edit.Way; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.WptPt; @@ -49,6 +52,7 @@ import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; +import net.osmand.plus.myplaces.FavoritesActivity.ObjectListPosition; import net.osmand.plus.osmedit.ExportOptionsBottomSheetDialogFragment.ExportOptionsFragmentListener; import net.osmand.plus.osmedit.FileTypeBottomSheetDialogFragment.FileTypeFragmentListener; import net.osmand.plus.osmedit.OsmEditOptionsBottomSheetDialogFragment.OsmEditOptionsFragmentListener; @@ -69,7 +73,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialogFragment.ProgressDialogPoiUploader, OpenstreetmapLocalUtil.OnNodeCommittedListener { +public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialogFragment.ProgressDialogPoiUploader, OpenstreetmapLocalUtil.OnNodeCommittedListener, + ObjectListPosition { public static final int EXPORT_TYPE_ALL = 0; public static final int EXPORT_TYPE_POI = 1; @@ -404,6 +409,9 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo public void onResume() { super.onResume(); fetchData(); + if (getActivity() != null && getActivity() instanceof FavoritesActivity) { + setListPosition(((FavoritesActivity) getActivity()).getScrollPosition()); + } } @Override @@ -643,13 +651,15 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo } private void showOnMap(OsmPoint osmPoint) { + int position = getObjectListPosition(osmPoint); boolean isOsmPoint = osmPoint instanceof OpenstreetmapPoint; String type = osmPoint.getGroup() == Group.POI ? PointDescription.POINT_TYPE_POI : PointDescription.POINT_TYPE_OSM_BUG; String name = (isOsmPoint ? ((OpenstreetmapPoint) osmPoint).getName() : ((OsmNotesPoint) osmPoint).getText()); getMyApplication().getSettings().setMapLocationToShow(osmPoint.getLatitude(), osmPoint.getLongitude(), 15, new PointDescription(type, name), true, osmPoint); //$NON-NLS-1$ Bundle b = new Bundle(); - b.putString("favorites_activity", "osm_edits_tab"); + b.putInt(TAB_TO_OPEN, OSM_TAB); + b.putInt(SCROLL_POSITION, position); MapActivity.launchMapActivityMoveToTop(getActivity(), null, b); } @@ -896,4 +906,27 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo } } + + @Override + public int getObjectListPosition(Object object) { + List list = createItemsList(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i) == object) { + return i; + } + } + return 0; + } + + @Override + public void setListPosition(int position) { + int itemsCount = getListView().getAdapter().getCount(); + if (itemsCount > 0 && itemsCount > position) { + if (position == 1) { + getListView().setSelection(0); + } else { + getListView().setSelection(position); + } + } + } } \ No newline at end of file From 975d2b63251746538083422e7996d6af06a08c25 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Mon, 24 Jun 2019 16:42:26 +0300 Subject: [PATCH 03/10] clean code for pr --- OsmAnd/src/net/osmand/plus/activities/MapActivity.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 7966c4b096..c21ee01772 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -640,10 +640,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (mapViewTrackingUtilities.isMapLinkedToLocation()) { prevActivityIntent.putExtra(SearchActivity.SEARCH_NEARBY, true); } - if (prevActivityIntent.getComponent() != null - && prevActivityIntent.getComponent().getClassName().equals("net.osmand.plus.myplaces.FavoritesActivity")) { -// LOG.debug("PrevVisibleDash: " + dashboardOnMap.getPreviousVisibleType().name()); - } this.startActivity(prevActivityIntent); prevActivityIntent = null; return; From e34ed50487176f9918848d10755358714caafd86 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Mon, 24 Jun 2019 18:24:12 +0300 Subject: [PATCH 04/10] refactoring --- .../osmand/plus/activities/MapActivity.java | 11 +++--- .../osmand/plus/audionotes/NotesFragment.java | 36 +++++++++---------- .../audionotes/adapters/NotesAdapter.java | 6 ++-- .../plus/myplaces/FavoritesActivity.java | 16 ++++----- .../osmand/plus/osmedit/OsmEditsAdapter.java | 6 ++-- .../osmand/plus/osmedit/OsmEditsFragment.java | 33 +++++++---------- 6 files changed, 48 insertions(+), 60 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index c21ee01772..12cfa03f1a 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1585,7 +1585,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven return mapLayers; } - public static void launchMapActivityMoveToTop(Context activity, String intentExtraActionName, Bundle intentExtraActionValue) { + public static void launchMapActivityMoveToTop(Context activity, Bundle intentParams) { if (activity instanceof MapActivity) { if (((MapActivity) activity).getDashboard().isVisible()) { ((MapActivity) activity).getDashboard().hideDashboard(); @@ -1597,8 +1597,8 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven Intent intent = ((Activity) activity).getIntent(); if (intent != null) { prevActivityIntent = new Intent(intent); - if (intentExtraActionName == null && intentExtraActionValue != null) { - prevActivityIntent.putExtras(intentExtraActionValue); + if (intentParams != null) { + prevActivityIntent.putExtras(intentParams); } prevActivityIntent.putExtra(INTENT_KEY_PARENT_MAP_ACTIVITY, true); } else { @@ -1612,15 +1612,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven Intent newIntent = new Intent(activity, ((OsmandApplication) activity.getApplicationContext()) .getAppCustomization().getMapActivity()); newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | additionalFlags); - if (intentExtraActionName != null) { - newIntent.putExtra(intentExtraActionName, intentExtraActionValue); - } activity.startActivity(newIntent); } } public static void launchMapActivityMoveToTop(Context activity) { - launchMapActivityMoveToTop(activity, null, null); + launchMapActivityMoveToTop(activity, null); } public static void clearPrevActivityIntent() { diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index 68e0999dc5..a3324e392d 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -52,7 +52,7 @@ import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; -import net.osmand.plus.myplaces.FavoritesActivity.ObjectListPosition; +import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; import org.apache.commons.logging.Log; import java.io.File; @@ -65,7 +65,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; -public class NotesFragment extends OsmAndListFragment implements ObjectListPosition { +public class NotesFragment extends OsmAndListFragment implements FragmentStateHolder { public static final Recording SHARE_LOCATION_FILE = new Recording(new File(".")); @@ -147,7 +147,7 @@ public class NotesFragment extends OsmAndListFragment implements ObjectListPosit listAdapter.setPortrait(portrait); listView.setAdapter(listAdapter); if (getActivity() != null && getActivity() instanceof FavoritesActivity) { - setListPosition(((FavoritesActivity) getActivity()).getScrollPosition()); + restoreState(((FavoritesActivity) getActivity()).getItemPosition()); } } @@ -274,8 +274,8 @@ public class NotesFragment extends OsmAndListFragment implements ObjectListPosit } @Override - public void onItemClick(Recording rec) { - showOnMap(rec); + public void onItemClick(Recording rec, int position) { + showOnMap(rec, position); } @Override @@ -570,14 +570,15 @@ public class NotesFragment extends OsmAndListFragment implements ObjectListPosit } private void showOnMap(Recording recording) { - int position = getObjectListPosition(recording); + showOnMap(recording, -1); + } + + private void showOnMap(Recording recording, int itemPosition) { getMyApplication().getSettings().setMapLocationToShow(recording.getLatitude(), recording.getLongitude(), 15, new PointDescription(recording.getSearchHistoryType(), recording.getName(getActivity(), true)), true, recording); - Bundle b = new Bundle(); - b.putInt(TAB_TO_OPEN, NOTE_TAB); - b.putInt(SCROLL_POSITION, position); - MapActivity.launchMapActivityMoveToTop(getActivity(), null, b); + Bundle b = itemPosition >= 0 ? storeState(NOTE_TAB, itemPosition) : null; + MapActivity.launchMapActivityMoveToTop(getActivity(), b); } private void editNote(final Recording recording) { @@ -618,18 +619,15 @@ public class NotesFragment extends OsmAndListFragment implements ObjectListPosit } @Override - public int getObjectListPosition(Object object) { - List list = createItemsList(); - for (int i = 0; i < list.size(); i++) { - if (list.get(i) == object) { - return i; - } - } - return 0; + public Bundle storeState(int tabId, int itemPosition) { + Bundle b = new Bundle(); + b.putInt(SCROLL_POSITION, itemPosition); + b.putInt(TAB_TO_OPEN, tabId); + return b; } @Override - public void setListPosition(int position) { + public void restoreState(int position) { int itemsCount = getListView().getAdapter().getCount(); if (itemsCount > 0 && itemsCount > position) { if (position == 1) { diff --git a/OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java b/OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java index 99660a24c1..883afbb10c 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java @@ -201,7 +201,7 @@ public class NotesAdapter extends ArrayAdapter { return R.string.shared_string_video; } - private void setupItem(int position, final Recording recording, final ItemViewHolder holder) { + private void setupItem(final int position, final Recording recording, final ItemViewHolder holder) { setupBackground(holder.view); if (recording == NotesFragment.SHARE_LOCATION_FILE) { holder.title.setText(R.string.av_locations); @@ -248,7 +248,7 @@ public class NotesAdapter extends ArrayAdapter { holder.checkBox.performClick(); } else { if (listener != null) { - listener.onItemClick(recording); + listener.onItemClick(recording, position); } } } @@ -368,7 +368,7 @@ public class NotesAdapter extends ArrayAdapter { void onCheckBoxClick(Recording rec, boolean checked); - void onItemClick(Recording rec); + void onItemClick(Recording rec, int position); void onOptionsClick(Recording rec); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index 009d8006bb..21f060e40b 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -58,7 +58,7 @@ public class FavoritesActivity extends TabActivity { private ImportHelper importHelper; private String groupNameToShow; - private int scrollPosition = 0; + private int itemPosition = 0; @Override public void onCreate(Bundle icicle) { @@ -86,7 +86,7 @@ public class FavoritesActivity extends TabActivity { Intent intent = getIntent(); if (intent != null) { if (intent.hasExtra(SCROLL_POSITION)) { - scrollPosition = intent.getIntExtra(SCROLL_POSITION, 0); + itemPosition = intent.getIntExtra(SCROLL_POSITION, 0); } if (intent.hasExtra(OPEN_FAVOURITES_TAB) && intent.getBooleanExtra(OPEN_FAVOURITES_TAB, false)) { if (intent.hasExtra(GROUP_NAME_TO_SHOW)) { @@ -267,16 +267,16 @@ public class FavoritesActivity extends TabActivity { } } - public interface ObjectListPosition { + public interface FragmentStateHolder { - int getObjectListPosition(Object object); + Bundle storeState(int tabId, int itemPosition); - void setListPosition(int position); + void restoreState(int position); } - public int getScrollPosition() { - int p = scrollPosition; - scrollPosition = 0; + public int getItemPosition() { + int p = itemPosition; + itemPosition = 0; return p; } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsAdapter.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsAdapter.java index e8da4526f5..e0aa6d3f32 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsAdapter.java @@ -176,7 +176,7 @@ public class OsmEditsAdapter extends ArrayAdapter { } } - private void bindOsmEditViewHolder(final OsmEditViewHolder holder, final OsmPoint osmEdit, int position) { + private void bindOsmEditViewHolder(final OsmEditViewHolder holder, final OsmPoint osmEdit, final int position) { setupBackground(holder.mainView); holder.titleTextView.setText(getTitle(osmEdit)); holder.descriptionTextView.setText(getDescription(osmEdit)); @@ -219,7 +219,7 @@ public class OsmEditsAdapter extends ArrayAdapter { holder.selectCheckBox.performClick(); } else { if (listener != null) { - listener.onItemShowMap(osmEdit); + listener.onItemShowMap(osmEdit, position); } } @@ -350,7 +350,7 @@ public class OsmEditsAdapter extends ArrayAdapter { void onItemSelect(OsmPoint point, boolean checked); - void onItemShowMap(OsmPoint point); + void onItemShowMap(OsmPoint point, int position); void onOptionsClick(OsmPoint note); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 6f777bbe6b..7719f4e8f7 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -52,7 +52,7 @@ import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; -import net.osmand.plus.myplaces.FavoritesActivity.ObjectListPosition; +import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; import net.osmand.plus.osmedit.ExportOptionsBottomSheetDialogFragment.ExportOptionsFragmentListener; import net.osmand.plus.osmedit.FileTypeBottomSheetDialogFragment.FileTypeFragmentListener; import net.osmand.plus.osmedit.OsmEditOptionsBottomSheetDialogFragment.OsmEditOptionsFragmentListener; @@ -74,7 +74,7 @@ import java.util.List; import java.util.Map; public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialogFragment.ProgressDialogPoiUploader, OpenstreetmapLocalUtil.OnNodeCommittedListener, - ObjectListPosition { + FragmentStateHolder { public static final int EXPORT_TYPE_ALL = 0; public static final int EXPORT_TYPE_POI = 1; @@ -410,7 +410,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo super.onResume(); fetchData(); if (getActivity() != null && getActivity() instanceof FavoritesActivity) { - setListPosition(((FavoritesActivity) getActivity()).getScrollPosition()); + restoreState(((FavoritesActivity) getActivity()).getItemPosition()); } } @@ -462,8 +462,8 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo } @Override - public void onItemShowMap(OsmPoint point) { - showOnMap(point); + public void onItemShowMap(OsmPoint point, int position) { + showOnMap(point, position); } @Override @@ -650,17 +650,13 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo uploadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, points); } - private void showOnMap(OsmPoint osmPoint) { - int position = getObjectListPosition(osmPoint); + private void showOnMap(OsmPoint osmPoint, int itemPosition) { boolean isOsmPoint = osmPoint instanceof OpenstreetmapPoint; String type = osmPoint.getGroup() == Group.POI ? PointDescription.POINT_TYPE_POI : PointDescription.POINT_TYPE_OSM_BUG; String name = (isOsmPoint ? ((OpenstreetmapPoint) osmPoint).getName() : ((OsmNotesPoint) osmPoint).getText()); getMyApplication().getSettings().setMapLocationToShow(osmPoint.getLatitude(), osmPoint.getLongitude(), 15, new PointDescription(type, name), true, osmPoint); //$NON-NLS-1$ - Bundle b = new Bundle(); - b.putInt(TAB_TO_OPEN, OSM_TAB); - b.putInt(SCROLL_POSITION, position); - MapActivity.launchMapActivityMoveToTop(getActivity(), null, b); + MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(OSM_TAB, itemPosition)); } private void deletePoint(OsmPoint osmPoint) { @@ -908,18 +904,15 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo @Override - public int getObjectListPosition(Object object) { - List list = createItemsList(); - for (int i = 0; i < list.size(); i++) { - if (list.get(i) == object) { - return i; - } - } - return 0; + public Bundle storeState(int tabId, int itemPosition) { + Bundle b = new Bundle(); + b.putInt(SCROLL_POSITION, itemPosition); + b.putInt(TAB_TO_OPEN, tabId); + return b; } @Override - public void setListPosition(int position) { + public void restoreState(int position) { int itemsCount = getListView().getAdapter().getCount(); if (itemsCount > 0 && itemsCount > position) { if (position == 1) { From d83f80d65eaf32b130e471bd6d4d6ac6953f6660 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Tue, 25 Jun 2019 10:03:58 +0300 Subject: [PATCH 05/10] refactoring 2 --- .../osmand/plus/audionotes/NotesFragment.java | 38 +++++++++---------- .../plus/myplaces/FavoritesActivity.java | 20 +++++----- .../osmand/plus/osmedit/OsmEditsFragment.java | 38 +++++++++---------- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index a3324e392d..6903e6af57 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -146,9 +146,7 @@ public class NotesFragment extends OsmAndListFragment implements FragmentStateHo listAdapter.setListener(createAdapterListener()); listAdapter.setPortrait(portrait); listView.setAdapter(listAdapter); - if (getActivity() != null && getActivity() instanceof FavoritesActivity) { - restoreState(((FavoritesActivity) getActivity()).getItemPosition()); - } + restoreState(getArguments()); } @Override @@ -577,8 +575,10 @@ public class NotesFragment extends OsmAndListFragment implements FragmentStateHo getMyApplication().getSettings().setMapLocationToShow(recording.getLatitude(), recording.getLongitude(), 15, new PointDescription(recording.getSearchHistoryType(), recording.getName(getActivity(), true)), true, recording); - Bundle b = itemPosition >= 0 ? storeState(NOTE_TAB, itemPosition) : null; - MapActivity.launchMapActivityMoveToTop(getActivity(), b); + Bundle b = new Bundle(); + b.putInt(SCROLL_POSITION, itemPosition); + b.putInt(TAB_TO_OPEN, NOTE_TAB); + MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); } private void editNote(final Recording recording) { @@ -619,22 +619,22 @@ public class NotesFragment extends OsmAndListFragment implements FragmentStateHo } @Override - public Bundle storeState(int tabId, int itemPosition) { - Bundle b = new Bundle(); - b.putInt(SCROLL_POSITION, itemPosition); - b.putInt(TAB_TO_OPEN, tabId); - return b; + public Bundle storeState(Bundle bundle) { + return bundle; } - + @Override - public void restoreState(int position) { - int itemsCount = getListView().getAdapter().getCount(); - if (itemsCount > 0 && itemsCount > position) { - if (position == 1) { - getListView().setSelection(0); - } else { - getListView().setSelection(position); - } + public void restoreState(Bundle bundle) { + if (bundle != null && bundle.containsKey(SCROLL_POSITION)) { + int position= bundle.getInt(SCROLL_POSITION, 0); + int itemsCount = getListView().getAdapter().getCount(); + if (itemsCount > 0 && itemsCount > position) { + if (position == 1) { + getListView().setSelection(0); + } else { + getListView().setSelection(position); + } + } } } } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index 21f060e40b..a11fee95ea 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -102,6 +102,7 @@ public class FavoritesActivity extends TabActivity { int tab = intent.getIntExtra(TAB_TO_OPEN, FAV_TAB); if (mTabs.get(n).mTitle.equals(getString(tab))) { mViewPager.setCurrentItem(n, false); + break; } } @@ -209,6 +210,11 @@ public class FavoritesActivity extends TabActivity { @Override public void onAttachFragment(Fragment fragment) { + if (fragment instanceof FragmentStateHolder && getIntent() != null) { + Bundle b = new Bundle(); + b.putInt(SCROLL_POSITION, getIntent().getIntExtra(SCROLL_POSITION, 0)); + fragment.setArguments(b); + } fragList.add(new WeakReference<>(fragment)); } @@ -268,16 +274,10 @@ public class FavoritesActivity extends TabActivity { } public interface FragmentStateHolder { - - Bundle storeState(int tabId, int itemPosition); - - void restoreState(int position); - } - - public int getItemPosition() { - int p = itemPosition; - itemPosition = 0; - return p; + + Bundle storeState(Bundle bundle); + + void restoreState(Bundle bundle); } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 7719f4e8f7..93cd040ebe 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.osmedit; +import static net.osmand.plus.myplaces.FavoritesActivity.NOTE_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.OSM_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.SCROLL_POSITION; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_TO_OPEN; @@ -409,9 +410,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo public void onResume() { super.onResume(); fetchData(); - if (getActivity() != null && getActivity() instanceof FavoritesActivity) { - restoreState(((FavoritesActivity) getActivity()).getItemPosition()); - } + restoreState(getArguments()); } @Override @@ -656,7 +655,10 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo String name = (isOsmPoint ? ((OpenstreetmapPoint) osmPoint).getName() : ((OsmNotesPoint) osmPoint).getText()); getMyApplication().getSettings().setMapLocationToShow(osmPoint.getLatitude(), osmPoint.getLongitude(), 15, new PointDescription(type, name), true, osmPoint); //$NON-NLS-1$ - MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(OSM_TAB, itemPosition)); + Bundle b = new Bundle(); + b.putInt(SCROLL_POSITION, itemPosition); + b.putInt(TAB_TO_OPEN, OSM_TAB); + MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); } private void deletePoint(OsmPoint osmPoint) { @@ -902,23 +904,21 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo } } - @Override - public Bundle storeState(int tabId, int itemPosition) { - Bundle b = new Bundle(); - b.putInt(SCROLL_POSITION, itemPosition); - b.putInt(TAB_TO_OPEN, tabId); - return b; + public Bundle storeState(Bundle bundle) { + return bundle; } - - @Override - public void restoreState(int position) { - int itemsCount = getListView().getAdapter().getCount(); - if (itemsCount > 0 && itemsCount > position) { - if (position == 1) { - getListView().setSelection(0); - } else { - getListView().setSelection(position); + + public void restoreState(Bundle bundle) { + if (bundle != null && bundle.containsKey(SCROLL_POSITION)) { + int position= bundle.getInt(SCROLL_POSITION, 0); + int itemsCount = getListView().getAdapter().getCount(); + if (itemsCount > 0 && itemsCount > position) { + if (position == 1) { + getListView().setSelection(0); + } else { + getListView().setSelection(position); + } } } } From f128c9402dbf5bb2fda65605ac97e3dbcd3ef082 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Tue, 25 Jun 2019 13:54:40 +0300 Subject: [PATCH 06/10] fixed logic and add to favorites and gpx fragments. --- .../activities/FavoritesTreeFragment.java | 40 ++++++-- .../osmand/plus/activities/MapActivity.java | 2 + .../osmand/plus/audionotes/NotesFragment.java | 14 +-- .../net/osmand/plus/helpers/ImportHelper.java | 4 +- .../builders/FavouritePointMenuBuilder.java | 2 +- .../plus/myplaces/AvailableGPXFragment.java | 18 +++- .../plus/myplaces/FavoritesActivity.java | 92 +++++++++---------- .../osmand/plus/osmedit/OsmEditsFragment.java | 13 ++- 8 files changed, 112 insertions(+), 73 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java index 2d194d42a5..284548f27f 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java @@ -48,6 +48,7 @@ import net.osmand.plus.base.OsmandExpandableListFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.FontCache; import net.osmand.plus.myplaces.FavoritesActivity; +import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -64,7 +65,8 @@ import java.util.Map; import java.util.Set; -public class FavoritesTreeFragment extends OsmandExpandableListFragment { +public class FavoritesTreeFragment extends OsmandExpandableListFragment implements + FragmentStateHolder { public static final int SEARCH_ID = -1; // public static final int EXPORT_ID = 0; // public static final int IMPORT_ID = 1; @@ -204,7 +206,12 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { getGroupExpandedPreference(groupName).set(true); } }); - String groupNameToShow = ((FavoritesActivity) getActivity()).getGroupNameToShow(); + + String groupNameToShow = null; + if (getArguments() != null) { + groupNameToShow = getArguments().getString(FavoritesActivity.GROUP_NAME_TO_SHOW); + } + if (groupNameToShow != null) { int groupPos = favouritesAdapter.getGroupPosition(groupNameToShow); if (groupPos != -1) { @@ -235,6 +242,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { super.onResume(); favouritesAdapter.synchronizeGroups(); initListExpandedState(); + restoreState(getArguments()); } @Override @@ -308,7 +316,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { updateSelectionMode(actionMode); } else { final FavouritePoint point = favouritesAdapter.getChild(groupPosition, childPosition); - showOnMap(point); + showOnMap(point, groupPosition, childPosition); } return true; } @@ -685,7 +693,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { return preference; } - public void showOnMap(final FavouritePoint point) { + public void showOnMap(final FavouritePoint point, int groupPos, int childPos) { getMyApplication().getSettings().FAVORITES_TAB.set(FavoritesActivity.FAV_TAB); final OsmandSettings settings = getMyApplication().getSettings(); @@ -695,7 +703,25 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { new PointDescription(PointDescription.POINT_TYPE_FAVORITE, point.getName()), true, point); //$NON-NLS-1$ - MapActivity.launchMapActivityMoveToTop(getActivity()); + Bundle b = new Bundle(); + b.putInt(FavoritesActivity.GROUP_POSITION, groupPos); + b.putInt(FavoritesActivity.ITEM_POSITION, childPos); + MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); + } + + @Override + public Bundle storeState(Bundle bundle) { + bundle.putInt(FavoritesActivity.TAB_ID, FavoritesActivity.FAV_TAB); + return bundle; + } + + @Override + public void restoreState(Bundle bundle) { + if (bundle != null) { + int group = bundle.getInt(FavoritesActivity.GROUP_POSITION, 0); + int child = bundle.getInt(FavoritesActivity.ITEM_POSITION, 0); + listView.setSelectedChild(group, child, true); + } } class FavouritesAdapter extends OsmandBaseExpandableListAdapter implements Filterable { @@ -865,7 +891,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { @Override - public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, + public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View row = convertView; if (row == null) { @@ -897,7 +923,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { options.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - showOnMap(model); + showOnMap(model, groupPosition, childPosition); } }); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 12cfa03f1a..eab39c3b31 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -159,6 +159,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven OnRequestPermissionsResultCallback, IRouteInformationListener, AMapPointUpdateListener, MapMarkerChangedListener, OnDismissDialogFragmentListener, OnDrawMapListener, OsmAndAppCustomizationListener { public static final String INTENT_KEY_PARENT_MAP_ACTIVITY = "intent_parent_map_activity_key"; + public static final String INTENT_PARAMS = "intent_prarams"; private static final int SHOW_POSITION_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 1; private static final int LONG_KEYPRESS_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 2; @@ -1598,6 +1599,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (intent != null) { prevActivityIntent = new Intent(intent); if (intentParams != null) { + prevActivityIntent.putExtra(INTENT_PARAMS, intentParams); prevActivityIntent.putExtras(intentParams); } prevActivityIntent.putExtra(INTENT_KEY_PARENT_MAP_ACTIVITY, true); diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index 6903e6af57..a3f27e497f 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -1,8 +1,8 @@ package net.osmand.plus.audionotes; -import static net.osmand.plus.myplaces.FavoritesActivity.NOTE_TAB; -import static net.osmand.plus.myplaces.FavoritesActivity.SCROLL_POSITION; -import static net.osmand.plus.myplaces.FavoritesActivity.TAB_TO_OPEN; +import static net.osmand.plus.myplaces.FavoritesActivity.NOTES_TAB; +import static net.osmand.plus.myplaces.FavoritesActivity.ITEM_POSITION; +import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import android.content.Context; import android.content.DialogInterface; @@ -576,8 +576,8 @@ public class NotesFragment extends OsmAndListFragment implements FragmentStateHo new PointDescription(recording.getSearchHistoryType(), recording.getName(getActivity(), true)), true, recording); Bundle b = new Bundle(); - b.putInt(SCROLL_POSITION, itemPosition); - b.putInt(TAB_TO_OPEN, NOTE_TAB); + b.putInt(ITEM_POSITION, itemPosition); + b.putInt(TAB_ID, NOTES_TAB); MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); } @@ -625,8 +625,8 @@ public class NotesFragment extends OsmAndListFragment implements FragmentStateHo @Override public void restoreState(Bundle bundle) { - if (bundle != null && bundle.containsKey(SCROLL_POSITION)) { - int position= bundle.getInt(SCROLL_POSITION, 0); + if (bundle != null && bundle.containsKey(ITEM_POSITION)) { + int position= bundle.getInt(ITEM_POSITION, 0); int itemsCount = getListView().getAdapter().getCount(); if (itemsCount > 0 && itemsCount > position) { if (position == 1) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index f99e4daefd..f0c717288f 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -295,7 +295,7 @@ public class ImportHelper { final Intent newIntent = new Intent(activity, app.getAppCustomization().getFavoritesActivity()); newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - newIntent.putExtra(FavoritesActivity.OPEN_FAVOURITES_TAB, true); + newIntent.putExtra(FavoritesActivity.TAB_ID, FavoritesActivity.FAV_TAB); activity.startActivity(newIntent); } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); @@ -596,7 +596,7 @@ public class ImportHelper { if (forceImportFavourites) { final Intent newIntent = new Intent(activity, app.getAppCustomization().getFavoritesActivity()); newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - newIntent.putExtra(FavoritesActivity.OPEN_MY_PLACES_TAB, true); + newIntent.putExtra(FavoritesActivity.TAB_ID, FavoritesActivity.GPX_TAB); activity.startActivity(newIntent); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java index fca2c3bcfa..2b4881d0a1 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java @@ -258,7 +258,7 @@ public class FavouritePointMenuBuilder extends MenuBuilder { public void onClick(View view) { OsmAndAppCustomization appCustomization = app.getAppCustomization(); final Intent intent = new Intent(context, appCustomization.getFavoritesActivity()); - intent.putExtra(FavoritesActivity.OPEN_FAVOURITES_TAB, true); + intent.putExtra(FavoritesActivity.TAB_ID, FavoritesActivity.FAV_TAB); intent.putExtra(FavoritesActivity.GROUP_NAME_TO_SHOW, group.name); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); context.startActivity(intent); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 9df5649701..37daf80cff 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -79,6 +79,7 @@ import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; +import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.util.Algorithms; @@ -101,7 +102,8 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -public class AvailableGPXFragment extends OsmandExpandableListFragment { +public class AvailableGPXFragment extends OsmandExpandableListFragment implements + FragmentStateHolder { public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); @@ -196,6 +198,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment { updateEnable = true; startHandler(); + restoreState(getArguments()); } @Override @@ -713,7 +716,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment { settings.setMapLocationToShow(loc.lat, loc.lon, settings.getLastKnownMapZoom()); e = false; getMyApplication().getSelectedGpxHelper().setGpxFileToDisplay(info.gpx); - MapActivity.launchMapActivityMoveToTop(getActivity()); + MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(new Bundle())); } } if (e) { @@ -862,6 +865,17 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment { builder.create().show(); } + @Override + public Bundle storeState(Bundle bundle) { + bundle.putInt(FavoritesActivity.TAB_ID, FavoritesActivity.GPX_TAB); + return bundle; + } + + @Override + public void restoreState(Bundle bundle) { + + } + public class LoadGpxTask extends AsyncTask> { private List result; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index a11fee95ea..dfd4a3fe4f 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -24,8 +24,11 @@ import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.FavoritesTreeFragment; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TabActivity; +import net.osmand.plus.audionotes.NotesFragment; import net.osmand.plus.helpers.ImportHelper; +import net.osmand.plus.osmedit.OsmEditsFragment; import net.osmand.plus.views.controls.PagerSlidingTabStrip; import java.lang.ref.WeakReference; @@ -41,25 +44,23 @@ public class FavoritesActivity extends TabActivity { private static final int IMPORT_FAVOURITES_REQUEST = 1007; public static final String GROUP_NAME_TO_SHOW = "group_name_to_show"; - - public static final String OPEN_FAVOURITES_TAB = "open_favourites_tab"; - public static final String OPEN_MY_PLACES_TAB = "open_my_places_tab"; - public static final String TAB_TO_OPEN = "tab_to_open"; - - public static final String SCROLL_POSITION = "scroll_position"; - + + public static final String TAB_ID = "selected_tab_id"; + public static final String ITEM_POSITION = "item_position"; + public static final String GROUP_POSITION = "group_position"; + public static final int GPX_TAB = R.string.shared_string_tracks; public static final int FAV_TAB = R.string.shared_string_my_favorites; - public static final int NOTE_TAB = R.string.notes; + public static final int NOTES_TAB = R.string.notes; public static final int OSM_TAB = R.string.osm_edits; - protected List> fragList = new ArrayList<>(); + protected List> fragList = new ArrayList<>(); private int tabSize; private ImportHelper importHelper; - private String groupNameToShow; - - private int itemPosition = 0; + private List mTabs; + private Bundle intentParams = null; + @Override public void onCreate(Bundle icicle) { OsmandApplication app = (OsmandApplication) getApplication(); @@ -77,44 +78,27 @@ public class FavoritesActivity extends TabActivity { setContentView(R.layout.tab_content); - List mTabs = getTabItems(); + mTabs = getTabItems(); setTabs(mTabs); - // setupHomeButton(); ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); if (icicle == null) { Intent intent = getIntent(); - if (intent != null) { - if (intent.hasExtra(SCROLL_POSITION)) { - itemPosition = intent.getIntExtra(SCROLL_POSITION, 0); - } - if (intent.hasExtra(OPEN_FAVOURITES_TAB) && intent.getBooleanExtra(OPEN_FAVOURITES_TAB, false)) { - if (intent.hasExtra(GROUP_NAME_TO_SHOW)) { - groupNameToShow = intent.getStringExtra(GROUP_NAME_TO_SHOW); - } - mViewPager.setCurrentItem(0, false); - } else if (intent.hasExtra(OPEN_MY_PLACES_TAB) && intent.getBooleanExtra(OPEN_MY_PLACES_TAB, false)) { - mViewPager.setCurrentItem(1, false); - } - - if (intent.hasExtra(TAB_TO_OPEN)) { - for (int n = 0; n < mTabs.size(); n++) { - int tab = intent.getIntExtra(TAB_TO_OPEN, FAV_TAB); - if (mTabs.get(n).mTitle.equals(getString(tab))) { - mViewPager.setCurrentItem(n, false); - - break; - } + if (intent != null && intent.hasExtra(MapActivity.INTENT_PARAMS)) { + intentParams = intent.getBundleExtra(MapActivity.INTENT_PARAMS); + int tabId = intentParams.getInt(TAB_ID, FAV_TAB); + int pagerItem = 0; + for (int n = 0; n < mTabs.size(); n++) { + if (mTabs.get(n).mTitle.equals(getString(tabId))) { + pagerItem = n; + break; } } + mViewPager.setCurrentItem(pagerItem, false); } } } - public String getGroupNameToShow() { - return groupNameToShow; - } - public void addTrack() { Intent intent = getImportGpxIntent(); startActivityForResult(intent, OPEN_GPX_DOCUMENT_REQUEST); @@ -174,8 +158,8 @@ public class FavoritesActivity extends TabActivity { private AvailableGPXFragment getGpxFragment() { AvailableGPXFragment gpxFragment = null; - for (WeakReference f : fragList) { - Fragment frag = f.get(); + for (WeakReference f : fragList) { + FragmentStateHolder frag = f.get(); if (frag instanceof AvailableGPXFragment) { gpxFragment = (AvailableGPXFragment) frag; } @@ -210,18 +194,32 @@ public class FavoritesActivity extends TabActivity { @Override public void onAttachFragment(Fragment fragment) { - if (fragment instanceof FragmentStateHolder && getIntent() != null) { - Bundle b = new Bundle(); - b.putInt(SCROLL_POSITION, getIntent().getIntExtra(SCROLL_POSITION, 0)); - fragment.setArguments(b); + if (fragment instanceof FragmentStateHolder) { + if (intentParams != null && intentParams.getInt(TAB_ID, -1) != -1) { + Bundle b = new Bundle(); + int tabId = intentParams.getInt(TAB_ID, FAV_TAB); + if (tabId == FAV_TAB && fragment instanceof FavoritesTreeFragment) { + b.putString(GROUP_NAME_TO_SHOW, intentParams.getString(GROUP_NAME_TO_SHOW)); + b.putInt(GROUP_POSITION, intentParams.getInt(GROUP_POSITION, 0)); + b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + } else if (tabId == GPX_TAB && fragment instanceof AvailableGPXFragment) { + b.putInt(GROUP_POSITION, intentParams.getInt(GROUP_POSITION, 0)); + b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + } else if (tabId == NOTES_TAB && fragment instanceof NotesFragment) { + b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + } else if (tabId == OSM_TAB && fragment instanceof OsmEditsFragment) { + b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + } + fragment.setArguments(b); + } + fragList.add(new WeakReference<>((FragmentStateHolder) fragment)); } - fragList.add(new WeakReference<>(fragment)); } @Override protected void onResume() { super.onResume(); - List mTabs = getTabItems(); + mTabs = getTabItems(); if(mTabs.size() != tabSize ) { setTabs(mTabs); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 93cd040ebe..415053914e 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -1,9 +1,8 @@ package net.osmand.plus.osmedit; -import static net.osmand.plus.myplaces.FavoritesActivity.NOTE_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.OSM_TAB; -import static net.osmand.plus.myplaces.FavoritesActivity.SCROLL_POSITION; -import static net.osmand.plus.myplaces.FavoritesActivity.TAB_TO_OPEN; +import static net.osmand.plus.myplaces.FavoritesActivity.ITEM_POSITION; +import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import android.app.Dialog; import android.app.ProgressDialog; @@ -656,8 +655,8 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo getMyApplication().getSettings().setMapLocationToShow(osmPoint.getLatitude(), osmPoint.getLongitude(), 15, new PointDescription(type, name), true, osmPoint); //$NON-NLS-1$ Bundle b = new Bundle(); - b.putInt(SCROLL_POSITION, itemPosition); - b.putInt(TAB_TO_OPEN, OSM_TAB); + b.putInt(ITEM_POSITION, itemPosition); + b.putInt(TAB_ID, OSM_TAB); MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); } @@ -910,8 +909,8 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo } public void restoreState(Bundle bundle) { - if (bundle != null && bundle.containsKey(SCROLL_POSITION)) { - int position= bundle.getInt(SCROLL_POSITION, 0); + if (bundle != null && bundle.containsKey(ITEM_POSITION)) { + int position= bundle.getInt(ITEM_POSITION, 0); int itemsCount = getListView().getAdapter().getCount(); if (itemsCount > 0 && itemsCount > position) { if (position == 1) { From 8bddc731a332bc82182e0b0a4e54b5606b60a8a5 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Tue, 25 Jun 2019 13:58:14 +0300 Subject: [PATCH 07/10] refactoring --- OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java | 2 +- OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index a3f27e497f..e7dff238dd 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -577,7 +577,6 @@ public class NotesFragment extends OsmAndListFragment implements FragmentStateHo true, recording); Bundle b = new Bundle(); b.putInt(ITEM_POSITION, itemPosition); - b.putInt(TAB_ID, NOTES_TAB); MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); } @@ -620,6 +619,7 @@ public class NotesFragment extends OsmAndListFragment implements FragmentStateHo @Override public Bundle storeState(Bundle bundle) { + bundle.putInt(TAB_ID, NOTES_TAB); return bundle; } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 415053914e..d13ba85bdd 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -656,7 +656,6 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo new PointDescription(type, name), true, osmPoint); //$NON-NLS-1$ Bundle b = new Bundle(); b.putInt(ITEM_POSITION, itemPosition); - b.putInt(TAB_ID, OSM_TAB); MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); } @@ -905,6 +904,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo @Override public Bundle storeState(Bundle bundle) { + bundle.putInt(TAB_ID, OSM_TAB); return bundle; } From 507d1e9032f80b3a4b5db6475bee11e96cb1f04c Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Tue, 25 Jun 2019 14:04:41 +0300 Subject: [PATCH 08/10] refactoring 2 --- .../osmand/plus/activities/FavoritesTreeFragment.java | 9 ++++++--- .../builders/FavouritePointMenuBuilder.java | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java index 284548f27f..998da0c4b6 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java @@ -92,7 +92,9 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment implemen private View footerView; private FavoritesListener favoritesListener; - + + String groupNameToShow = null; + @Override public void onAttach(Context context) { super.onAttach(context); @@ -207,7 +209,6 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment implemen } }); - String groupNameToShow = null; if (getArguments() != null) { groupNameToShow = getArguments().getString(FavoritesActivity.GROUP_NAME_TO_SHOW); } @@ -242,7 +243,9 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment implemen super.onResume(); favouritesAdapter.synchronizeGroups(); initListExpandedState(); - restoreState(getArguments()); + if (groupNameToShow == null) { + restoreState(getArguments()); + } } @Override diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java index 2b4881d0a1..c7fd86ee6f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java @@ -3,6 +3,7 @@ package net.osmand.plus.mapcontextmenu.builders; import android.content.Context; import android.content.Intent; import android.graphics.Color; +import android.os.Bundle; import android.support.annotation.NonNull; import android.text.TextUtils; import android.view.Gravity; @@ -258,9 +259,10 @@ public class FavouritePointMenuBuilder extends MenuBuilder { public void onClick(View view) { OsmAndAppCustomization appCustomization = app.getAppCustomization(); final Intent intent = new Intent(context, appCustomization.getFavoritesActivity()); - intent.putExtra(FavoritesActivity.TAB_ID, FavoritesActivity.FAV_TAB); - intent.putExtra(FavoritesActivity.GROUP_NAME_TO_SHOW, group.name); - intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + Bundle b = new Bundle(); + b.putInt(FavoritesActivity.TAB_ID, FavoritesActivity.FAV_TAB); + b.putString(FavoritesActivity.GROUP_NAME_TO_SHOW, group.name); + intent.putExtra(MapActivity.INTENT_PARAMS, b); context.startActivity(intent); } }); From 9d0333734fa0d91ce32b213ffbaf315970b22f40 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Tue, 25 Jun 2019 14:28:35 +0300 Subject: [PATCH 09/10] refactoring 2 --- .../activities/FavoritesTreeFragment.java | 14 +++--- .../osmand/plus/audionotes/NotesFragment.java | 5 +-- .../plus/myplaces/AvailableGPXFragment.java | 5 +-- .../plus/myplaces/FavoritesActivity.java | 44 ++++++++++++------- .../osmand/plus/osmedit/OsmEditsFragment.java | 5 +-- 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java index 998da0c4b6..14e5371715 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java @@ -48,7 +48,7 @@ import net.osmand.plus.base.OsmandExpandableListFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.FontCache; import net.osmand.plus.myplaces.FavoritesActivity; -import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; +import net.osmand.plus.myplaces.FavoritesActivity.FavoritesFragmentStateHolder; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -66,7 +66,7 @@ import java.util.Set; public class FavoritesTreeFragment extends OsmandExpandableListFragment implements - FragmentStateHolder { + FavoritesFragmentStateHolder { public static final int SEARCH_ID = -1; // public static final int EXPORT_ID = 0; // public static final int IMPORT_ID = 1; @@ -210,7 +210,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment implemen }); if (getArguments() != null) { - groupNameToShow = getArguments().getString(FavoritesActivity.GROUP_NAME_TO_SHOW); + groupNameToShow = getArguments().getString(GROUP_NAME_TO_SHOW); } if (groupNameToShow != null) { @@ -707,8 +707,8 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment implemen true, point); //$NON-NLS-1$ Bundle b = new Bundle(); - b.putInt(FavoritesActivity.GROUP_POSITION, groupPos); - b.putInt(FavoritesActivity.ITEM_POSITION, childPos); + b.putInt(GROUP_POSITION, groupPos); + b.putInt(ITEM_POSITION, childPos); MapActivity.launchMapActivityMoveToTop(getActivity(), storeState(b)); } @@ -721,8 +721,8 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment implemen @Override public void restoreState(Bundle bundle) { if (bundle != null) { - int group = bundle.getInt(FavoritesActivity.GROUP_POSITION, 0); - int child = bundle.getInt(FavoritesActivity.ITEM_POSITION, 0); + int group = bundle.getInt(GROUP_POSITION, 0); + int child = bundle.getInt(ITEM_POSITION, 0); listView.setSelectedChild(group, child, true); } } diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index e7dff238dd..6b46ed03a2 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -1,7 +1,6 @@ package net.osmand.plus.audionotes; import static net.osmand.plus.myplaces.FavoritesActivity.NOTES_TAB; -import static net.osmand.plus.myplaces.FavoritesActivity.ITEM_POSITION; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import android.content.Context; @@ -52,7 +51,7 @@ import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; -import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; +import net.osmand.plus.myplaces.FavoritesActivity.FavoritesFragmentStateHolder; import org.apache.commons.logging.Log; import java.io.File; @@ -65,7 +64,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; -public class NotesFragment extends OsmAndListFragment implements FragmentStateHolder { +public class NotesFragment extends OsmAndListFragment implements FavoritesFragmentStateHolder { public static final Recording SHARE_LOCATION_FILE = new Recording(new File(".")); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 37daf80cff..9fada49b3c 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -79,7 +79,7 @@ import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; -import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; +import net.osmand.plus.myplaces.FavoritesActivity.FavoritesFragmentStateHolder; import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.util.Algorithms; @@ -103,10 +103,9 @@ import java.util.Set; import java.util.regex.Pattern; public class AvailableGPXFragment extends OsmandExpandableListFragment implements - FragmentStateHolder { + FavoritesFragmentStateHolder { public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); - public static final int SEARCH_ID = -1; // public static final int ACTION_ID = 0; // protected static final int DELETE_ACTION_ID = 1; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index dfd4a3fe4f..6b3c0921de 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -43,18 +43,17 @@ public class FavoritesActivity extends TabActivity { private static final int OPEN_GPX_DOCUMENT_REQUEST = 1006; private static final int IMPORT_FAVOURITES_REQUEST = 1007; - public static final String GROUP_NAME_TO_SHOW = "group_name_to_show"; + public static final String TAB_ID = "selected_tab_id"; - public static final String ITEM_POSITION = "item_position"; - public static final String GROUP_POSITION = "group_position"; + public static final int GPX_TAB = R.string.shared_string_tracks; public static final int FAV_TAB = R.string.shared_string_my_favorites; public static final int NOTES_TAB = R.string.notes; public static final int OSM_TAB = R.string.osm_edits; - protected List> fragList = new ArrayList<>(); + protected List> fragList = new ArrayList<>(); private int tabSize; private ImportHelper importHelper; @@ -158,8 +157,8 @@ public class FavoritesActivity extends TabActivity { private AvailableGPXFragment getGpxFragment() { AvailableGPXFragment gpxFragment = null; - for (WeakReference f : fragList) { - FragmentStateHolder frag = f.get(); + for (WeakReference f : fragList) { + FavoritesFragmentStateHolder frag = f.get(); if (frag instanceof AvailableGPXFragment) { gpxFragment = (AvailableGPXFragment) frag; } @@ -194,25 +193,32 @@ public class FavoritesActivity extends TabActivity { @Override public void onAttachFragment(Fragment fragment) { - if (fragment instanceof FragmentStateHolder) { + if (fragment instanceof FavoritesFragmentStateHolder) { if (intentParams != null && intentParams.getInt(TAB_ID, -1) != -1) { Bundle b = new Bundle(); int tabId = intentParams.getInt(TAB_ID, FAV_TAB); if (tabId == FAV_TAB && fragment instanceof FavoritesTreeFragment) { - b.putString(GROUP_NAME_TO_SHOW, intentParams.getString(GROUP_NAME_TO_SHOW)); - b.putInt(GROUP_POSITION, intentParams.getInt(GROUP_POSITION, 0)); - b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + b.putString(FavoritesFragmentStateHolder.GROUP_NAME_TO_SHOW, + intentParams.getString(FavoritesFragmentStateHolder.GROUP_NAME_TO_SHOW)); + b.putInt(FavoritesFragmentStateHolder.GROUP_POSITION, intentParams.getInt( + FavoritesFragmentStateHolder.GROUP_POSITION, 0)); + b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, + intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); } else if (tabId == GPX_TAB && fragment instanceof AvailableGPXFragment) { - b.putInt(GROUP_POSITION, intentParams.getInt(GROUP_POSITION, 0)); - b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + b.putInt(FavoritesFragmentStateHolder.GROUP_POSITION, + intentParams.getInt(FavoritesFragmentStateHolder.GROUP_POSITION, 0)); + b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, + intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); } else if (tabId == NOTES_TAB && fragment instanceof NotesFragment) { - b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, + intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); } else if (tabId == OSM_TAB && fragment instanceof OsmEditsFragment) { - b.putInt(ITEM_POSITION, intentParams.getInt(ITEM_POSITION, 0)); + b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, + intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); } fragment.setArguments(b); } - fragList.add(new WeakReference<>((FragmentStateHolder) fragment)); + fragList.add(new WeakReference<>((FavoritesFragmentStateHolder) fragment)); } } @@ -271,7 +277,13 @@ public class FavoritesActivity extends TabActivity { } } - public interface FragmentStateHolder { + public interface FavoritesFragmentStateHolder { + + String ITEM_POSITION = "item_position"; + + String GROUP_POSITION = "group_position"; + + String GROUP_NAME_TO_SHOW = "group_name_to_show"; Bundle storeState(Bundle bundle); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index d13ba85bdd..28d7bac75c 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -1,7 +1,6 @@ package net.osmand.plus.osmedit; import static net.osmand.plus.myplaces.FavoritesActivity.OSM_TAB; -import static net.osmand.plus.myplaces.FavoritesActivity.ITEM_POSITION; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import android.app.Dialog; @@ -52,7 +51,7 @@ import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; -import net.osmand.plus.myplaces.FavoritesActivity.FragmentStateHolder; +import net.osmand.plus.myplaces.FavoritesActivity.FavoritesFragmentStateHolder; import net.osmand.plus.osmedit.ExportOptionsBottomSheetDialogFragment.ExportOptionsFragmentListener; import net.osmand.plus.osmedit.FileTypeBottomSheetDialogFragment.FileTypeFragmentListener; import net.osmand.plus.osmedit.OsmEditOptionsBottomSheetDialogFragment.OsmEditOptionsFragmentListener; @@ -74,7 +73,7 @@ import java.util.List; import java.util.Map; public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialogFragment.ProgressDialogPoiUploader, OpenstreetmapLocalUtil.OnNodeCommittedListener, - FragmentStateHolder { + FavoritesFragmentStateHolder { public static final int EXPORT_TYPE_ALL = 0; public static final int EXPORT_TYPE_POI = 1; From 67f1c3f80867572b9424735122264da5ba60cd66 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Wed, 26 Jun 2019 09:59:04 +0300 Subject: [PATCH 10/10] pr refactoring --- .../activities/FavoritesTreeFragment.java | 13 +++++++---- .../osmand/plus/audionotes/NotesFragment.java | 22 ++++++++++--------- .../builders/FavouritePointMenuBuilder.java | 3 ++- .../plus/myplaces/AvailableGPXFragment.java | 1 - .../plus/myplaces/FavoritesActivity.java | 20 ++++------------- .../osmand/plus/osmedit/OsmEditsFragment.java | 20 +++++++++-------- 6 files changed, 38 insertions(+), 41 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java index 14e5371715..3f012d4bce 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java @@ -1,5 +1,8 @@ package net.osmand.plus.activities; +import static net.osmand.plus.myplaces.FavoritesActivity.FAV_TAB; +import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; + import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -720,10 +723,12 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment implemen @Override public void restoreState(Bundle bundle) { - if (bundle != null) { - int group = bundle.getInt(GROUP_POSITION, 0); - int child = bundle.getInt(ITEM_POSITION, 0); - listView.setSelectedChild(group, child, true); + if (bundle != null && bundle.containsKey(TAB_ID) && bundle.containsKey(ITEM_POSITION)) { + if (bundle.getInt(TAB_ID, 0) == FAV_TAB) { + int group = bundle.getInt(GROUP_POSITION, 0); + int child = bundle.getInt(ITEM_POSITION, 0); + listView.setSelectedChild(group, child, true); + } } } diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index 6b46ed03a2..5ffba1f49b 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -624,16 +624,18 @@ public class NotesFragment extends OsmAndListFragment implements FavoritesFragme @Override public void restoreState(Bundle bundle) { - if (bundle != null && bundle.containsKey(ITEM_POSITION)) { - int position= bundle.getInt(ITEM_POSITION, 0); - int itemsCount = getListView().getAdapter().getCount(); - if (itemsCount > 0 && itemsCount > position) { - if (position == 1) { - getListView().setSelection(0); - } else { - getListView().setSelection(position); - } - } + if (bundle != null && bundle.containsKey(TAB_ID) && bundle.containsKey(ITEM_POSITION)) { + if (bundle.getInt(TAB_ID, 0) == NOTES_TAB) { + int position= bundle.getInt(ITEM_POSITION, 0); + int itemsCount = getListView().getAdapter().getCount(); + if (itemsCount > 0 && itemsCount > position) { + if (position == 1) { + getListView().setSelection(0); + } else { + getListView().setSelection(position); + } + } + } } } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java index c7fd86ee6f..20cd85703b 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java @@ -28,6 +28,7 @@ import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.myplaces.FavoritesActivity; +import net.osmand.plus.myplaces.FavoritesActivity.FavoritesFragmentStateHolder; import net.osmand.plus.widgets.TextViewEx; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -261,7 +262,7 @@ public class FavouritePointMenuBuilder extends MenuBuilder { final Intent intent = new Intent(context, appCustomization.getFavoritesActivity()); Bundle b = new Bundle(); b.putInt(FavoritesActivity.TAB_ID, FavoritesActivity.FAV_TAB); - b.putString(FavoritesActivity.GROUP_NAME_TO_SHOW, group.name); + b.putString(FavoritesFragmentStateHolder.GROUP_NAME_TO_SHOW, group.name); intent.putExtra(MapActivity.INTENT_PARAMS, b); context.startActivity(intent); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 9fada49b3c..a2dacad371 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -872,7 +872,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public void restoreState(Bundle bundle) { - } public class LoadGpxTask extends AsyncTask> { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java index 6b3c0921de..227c550c8d 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/FavoritesActivity.java @@ -26,9 +26,7 @@ import net.osmand.plus.R; import net.osmand.plus.activities.FavoritesTreeFragment; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TabActivity; -import net.osmand.plus.audionotes.NotesFragment; import net.osmand.plus.helpers.ImportHelper; -import net.osmand.plus.osmedit.OsmEditsFragment; import net.osmand.plus.views.controls.PagerSlidingTabStrip; import java.lang.ref.WeakReference; @@ -197,25 +195,15 @@ public class FavoritesActivity extends TabActivity { if (intentParams != null && intentParams.getInt(TAB_ID, -1) != -1) { Bundle b = new Bundle(); int tabId = intentParams.getInt(TAB_ID, FAV_TAB); - if (tabId == FAV_TAB && fragment instanceof FavoritesTreeFragment) { + b.putInt(TAB_ID, intentParams.getInt(TAB_ID, FAV_TAB)); + if (tabId == FAV_TAB) { b.putString(FavoritesFragmentStateHolder.GROUP_NAME_TO_SHOW, intentParams.getString(FavoritesFragmentStateHolder.GROUP_NAME_TO_SHOW)); b.putInt(FavoritesFragmentStateHolder.GROUP_POSITION, intentParams.getInt( FavoritesFragmentStateHolder.GROUP_POSITION, 0)); - b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, - intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); - } else if (tabId == GPX_TAB && fragment instanceof AvailableGPXFragment) { - b.putInt(FavoritesFragmentStateHolder.GROUP_POSITION, - intentParams.getInt(FavoritesFragmentStateHolder.GROUP_POSITION, 0)); - b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, - intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); - } else if (tabId == NOTES_TAB && fragment instanceof NotesFragment) { - b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, - intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); - } else if (tabId == OSM_TAB && fragment instanceof OsmEditsFragment) { - b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, - intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); } + b.putInt(FavoritesFragmentStateHolder.ITEM_POSITION, + intentParams.getInt(FavoritesFragmentStateHolder.ITEM_POSITION, 0)); fragment.setArguments(b); } fragList.add(new WeakReference<>((FavoritesFragmentStateHolder) fragment)); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 28d7bac75c..8cf9ee8ceb 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -908,15 +908,17 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo } public void restoreState(Bundle bundle) { - if (bundle != null && bundle.containsKey(ITEM_POSITION)) { - int position= bundle.getInt(ITEM_POSITION, 0); - int itemsCount = getListView().getAdapter().getCount(); - if (itemsCount > 0 && itemsCount > position) { - if (position == 1) { - getListView().setSelection(0); - } else { - getListView().setSelection(position); - } + if (bundle != null && bundle.containsKey(TAB_ID) && bundle.containsKey(ITEM_POSITION)) { + if (bundle.getInt(TAB_ID, 0) == OSM_TAB) { + int position= bundle.getInt(ITEM_POSITION, 0); + int itemsCount = getListView().getAdapter().getCount(); + if (itemsCount > 0 && itemsCount > position) { + if (position == 1) { + getListView().setSelection(0); + } else { + getListView().setSelection(position); + } + } } } }