From 1b7a75d72a9d07cb7a1ab1d6a34384fdad86ec69 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Tue, 29 Sep 2015 16:02:21 +0300 Subject: [PATCH] Now loading of map categories happening after activity start. Fragment saves its state now. --- .../osmand/plus/activities/TabActivity.java | 8 +- .../plus/download/BaseDownloadActivity.java | 6 +- .../plus/download/DownloadActivity.java | 73 +++++++++++-------- .../plus/download/DownloadIndexFragment.java | 25 ++++--- .../plus/download/DownloadIndexesThread.java | 2 +- .../NewLocalIndexesFragment.java | 31 +++++++- .../SubcategoriesFragment.java | 4 - 7 files changed, 94 insertions(+), 55 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/TabActivity.java b/OsmAnd/src/net/osmand/plus/activities/TabActivity.java index 5ac931fb5d..f806a96433 100644 --- a/OsmAnd/src/net/osmand/plus/activities/TabActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/TabActivity.java @@ -1,12 +1,12 @@ package net.osmand.plus.activities; -import java.util.List; - import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; +import java.util.List; + /** * Created by Denis * on 26.01.2015. @@ -34,7 +34,7 @@ public class TabActivity extends ActionBarProgressActivity { pager.setAdapter(new OsmandFragmentPagerAdapter(getSupportFragmentManager(), items)); } - public static class OsmandFragmentPagerAdapter extends FragmentPagerAdapter { + public static class OsmandFragmentPagerAdapter extends FragmentStatePagerAdapter { private List mTabs; diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index 8a08b95ab8..10339c58c7 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -21,9 +21,11 @@ import net.osmand.plus.download.newimplementation.IndexItemCategoryWithSubcat; import java.lang.ref.WeakReference; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * Created by Denis @@ -33,7 +35,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { protected DownloadActivityType type = DownloadActivityType.NORMAL_FILE; protected OsmandSettings settings; public static DownloadIndexesThread downloadListIndexThread; - protected List> fragList = new ArrayList<>(); + protected Set> fragSet = new HashSet<>(); protected List downloadQueue = new ArrayList<>(); public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10; @@ -217,7 +219,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { @Override public void onAttachFragment(Fragment fragment) { - fragList.add(new WeakReference(fragment)); + fragSet.add(new WeakReference(fragment)); } public void makeSureUserCancelDownload() { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index f607aff73d..b1f6285b32 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -75,6 +75,7 @@ public class DownloadActivity extends BaseDownloadActivity { public static final String UPDATES_TAB = "updates"; public static final String SINGLE_TAB = "SINGLE_TAB"; private List downloadTypes = new ArrayList(); + private List cats; @Override protected void onCreate(Bundle savedInstanceState) { @@ -164,13 +165,13 @@ public class DownloadActivity extends BaseDownloadActivity { public void onClick(View v) { getEntriesToDownload().clear(); updateDownloadButton(); - for (WeakReference ref : fragList) { + for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f instanceof OsmAndListFragment) { - if (!f.isDetached() && ((OsmAndListFragment) f).getListAdapter() instanceof ArrayAdapter) { + if (f.isAdded() && ((OsmAndListFragment) f).getListAdapter() instanceof ArrayAdapter) { ((ArrayAdapter) ((OsmAndListFragment) f).getListAdapter()).notifyDataSetChanged(); } - } else if (!f.isDetached() && f instanceof OsmandExpandableListFragment && + } else if (f.isAdded() && f instanceof OsmandExpandableListFragment && ((OsmandExpandableListFragment) f).getAdapter() instanceof BaseExpandableListAdapter) { ((BaseExpandableListAdapter) ((OsmandExpandableListFragment) f).getAdapter()).notifyDataSetChanged(); } @@ -283,10 +284,10 @@ public class DownloadActivity extends BaseDownloadActivity { @Override public void updateDownloadList(List list) { - for (WeakReference ref : fragList) { + for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f instanceof UpdatesIndexFragment) { - if (!f.isDetached()) { + if (f.isAdded()) { ((UpdatesIndexFragment) f).updateItemsList(list); } } @@ -295,10 +296,10 @@ public class DownloadActivity extends BaseDownloadActivity { @Override public void categorizationFinished(List filtered, List cats) { - for (WeakReference ref : fragList) { + for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f instanceof DownloadIndexFragment) { - if (!f.isDetached()) { + if (f.isAdded()) { ((DownloadIndexFragment) f).categorizationFinished(filtered, cats); } } @@ -308,22 +309,28 @@ public class DownloadActivity extends BaseDownloadActivity { @Override public void onCategorizationFinished(List filtered, List cats) { - for (WeakReference ref : fragList) { + boolean isPushed = false; + for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f instanceof NewLocalIndexesFragment) { - if (!f.isDetached()) { + if (f.isAdded()) { + isPushed = true; ((NewLocalIndexesFragment) f).onCategorizationFinished(filtered, cats); } } } + if (!isPushed) { + this.cats = cats; + } } public void downloadListUpdated() { - for (WeakReference ref : fragList) { + for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f instanceof DownloadIndexFragment) { - if (!f.isDetached()) { - ((DownloadIndexAdapter) ((DownloadIndexFragment) f).getExpandableListAdapter()).notifyDataSetInvalidated(); + if (f.isAdded()) { + ((DownloadIndexAdapter) ((DownloadIndexFragment) f).getExpandableListAdapter()) + .notifyDataSetInvalidated(); } } } @@ -331,15 +338,16 @@ public class DownloadActivity extends BaseDownloadActivity { @Override public void downloadedIndexes() { - for (WeakReference ref : fragList) { + for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f instanceof LocalIndexesFragment) { - if (!f.isDetached()) { + if (f.isAdded()) { ((LocalIndexesFragment) f).reloadData(); } } else if (f instanceof DownloadIndexFragment) { - if (!f.isDetached()) { - DownloadIndexAdapter adapter = ((DownloadIndexAdapter) ((DownloadIndexFragment) f).getExpandableListAdapter()); + if (f.isAdded()) { + DownloadIndexAdapter adapter = ((DownloadIndexAdapter) + ((DownloadIndexFragment) f).getExpandableListAdapter()); if (adapter != null) { adapter.setLoadedFiles(getIndexActivatedFileNames(), getIndexFileNames()); @@ -446,22 +454,23 @@ public class DownloadActivity extends BaseDownloadActivity { findViewById(R.id.wikiButton).setVisibility(wikipediaItems.size() == 0 ? View.GONE : View.VISIBLE); } - for (WeakReference ref : fragList) { + for (WeakReference ref : fragSet) { Fragment f = ref.get(); - if (!f.isDetached()) { - if (f instanceof OsmandExpandableListFragment) { - ExpandableListAdapter ad = ((OsmandExpandableListFragment) f).getExpandableListView() - .getExpandableListAdapter(); - if (ad instanceof OsmandBaseExpandableListAdapter) { - ((OsmandBaseExpandableListAdapter) ad).notifyDataSetChanged(); - } - } else if (f instanceof ListFragment) { - ListAdapter la = ((ListFragment) f).getListAdapter(); - if (la instanceof BaseAdapter) { - ((BaseAdapter) la).notifyDataSetChanged(); + if (f != null) + if (f.isAdded()) { + if (f instanceof OsmandExpandableListFragment) { + ExpandableListAdapter ad = ((OsmandExpandableListFragment) f).getExpandableListView() + .getExpandableListAdapter(); + if (ad instanceof OsmandBaseExpandableListAdapter) { + ((OsmandBaseExpandableListAdapter) ad).notifyDataSetChanged(); + } + } else if (f instanceof ListFragment) { + ListAdapter la = ((ListFragment) f).getListAdapter(); + if (la instanceof BaseAdapter) { + ((BaseAdapter) la).notifyDataSetChanged(); + } } } - } } // if (scroll) { // getExpandableListView().scrollTo(x, y); @@ -533,6 +542,12 @@ public class DownloadActivity extends BaseDownloadActivity { return downloadListIndexThread != null ? downloadListIndexThread.getIndexFileNames() : null; } + public List getCats() { + List toReturn = cats; + cats = null; + return toReturn; + } + public void showDialogToDownloadMaps(Collection maps) { int count = 0; int sz = 0; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java index ebb9ebceb4..0802692936 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java @@ -1,17 +1,6 @@ package net.osmand.plus.download; -import java.io.File; -import java.io.FilenameFilter; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import net.osmand.access.AccessibleToast; -import net.osmand.plus.R; -import net.osmand.plus.activities.OsmandExpandableListFragment; -import net.osmand.plus.base.BasicProgressAsyncTask; import android.os.Bundle; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBar; @@ -31,9 +20,21 @@ import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.Toast; +import net.osmand.access.AccessibleToast; +import net.osmand.plus.R; +import net.osmand.plus.activities.OsmandExpandableListFragment; +import net.osmand.plus.base.BasicProgressAsyncTask; + +import java.io.File; +import java.io.FilenameFilter; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + public class DownloadIndexFragment extends OsmandExpandableListFragment { - + /** menus **/ private static boolean SHOW_ONLY_RELOAD = true; public static final int MORE_ID = 10; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index bf24764d7d..b9bf648950 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -430,7 +430,7 @@ public class DownloadIndexesThread { AccessibleToast.makeText(uiActivity, R.string.basemap_was_selected_to_download, Toast.LENGTH_LONG).show(); if (uiActivity instanceof DownloadActivity) { - ((DownloadActivity) uiActivity).updateDownloadButton(); + uiActivity.updateDownloadButton(); } } } diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java index c3fc75c01f..3ffdf5ae58 100644 --- a/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java @@ -30,12 +30,14 @@ import org.apache.commons.logging.Log; import java.io.File; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.List; import java.util.Locale; public class NewLocalIndexesFragment extends OsmAndListFragment { private static final Log LOG = PlatformUtil.getLog(NewLocalIndexesFragment.class); + public static final String CATEGORIES = "categories"; private static final MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); public static final int RELOAD_ID = 0; @@ -55,6 +57,16 @@ public class NewLocalIndexesFragment extends OsmAndListFragment { mAdapter = new CategoriesAdapter(getActivity(), getMyApplication()); listView.setAdapter(mAdapter); + List cats = ((DownloadActivity) getActivity()).getCats(); + if (cats != null) { + onCategorizationFinished(null, cats); + } + if (savedInstanceState != null) { + List savedCats = + savedInstanceState.getParcelableArrayList(CATEGORIES); + onCategorizationFinished(null, savedCats); + } + View header = inflater.inflate(R.layout.local_index_fragment_header, listView, false); initMemoryConsumedCard(header); DownloadsUiInitHelper.initFreeVersionBanner(header, getMyApplication().getSettings(), @@ -102,11 +114,22 @@ public class NewLocalIndexesFragment extends OsmAndListFragment { return super.onOptionsItemSelected(item); } + @Override + public void onSaveInstanceState(Bundle outState) { + LOG.debug("onSaveInstanceState()"); + ArrayList items = new ArrayList<>(mAdapter.getCount()); + for (int i = 0; i < mAdapter.getCount(); i++) { + items.add(mAdapter.getItem(i)); + } + outState.putParcelableArrayList(CATEGORIES, items); + super.onSaveInstanceState(outState); + } + @Override public void onListItemClick(ListView l, View v, int position, long id) { FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); fragmentTransaction.addToBackStack(null); - MapsInCategoryFragment.createInstance(mAdapter.getItem(position-1)) + MapsInCategoryFragment.createInstance(mAdapter.getItem(position - 1)) .show(fragmentTransaction, MapsInCategoryFragment.TAG); } @@ -115,8 +138,10 @@ public class NewLocalIndexesFragment extends OsmAndListFragment { } public void onCategorizationFinished(List filtered, List cats) { - mAdapter.clear(); - mAdapter.addAll(cats); + if (mAdapter != null) { + mAdapter.clear(); + mAdapter.addAll(cats); + } } private static class CategoriesAdapter extends ArrayAdapter { diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java index 84a326305d..59909a433d 100644 --- a/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java @@ -15,7 +15,6 @@ import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; -import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; @@ -24,10 +23,7 @@ import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.IndexItem; import net.osmand.plus.helpers.HasName; -import org.apache.commons.logging.Log; - public class SubcategoriesFragment extends Fragment { - private static final Log LOG = PlatformUtil.getLog(SubcategoriesFragment.class); private static final String CATEGORY = "category"; @Nullable