Now loading of map categories happening after activity start. Fragment saves its state now.

This commit is contained in:
GaidamakUA 2015-09-29 16:02:21 +03:00
parent 07faee07c4
commit 1b7a75d72a
7 changed files with 94 additions and 55 deletions

View file

@ -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<TabItem> mTabs;

View file

@ -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<WeakReference<Fragment>> fragList = new ArrayList<>();
protected Set<WeakReference<Fragment>> fragSet = new HashSet<>();
protected List<IndexItem> 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>(fragment));
fragSet.add(new WeakReference<Fragment>(fragment));
}
public void makeSureUserCancelDownload() {

View file

@ -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<DownloadActivityType> downloadTypes = new ArrayList<DownloadActivityType>();
private List<IndexItemCategoryWithSubcat> 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<Fragment> ref : fragList) {
for (WeakReference<Fragment> 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<IndexItem> list) {
for (WeakReference<Fragment> ref : fragList) {
for (WeakReference<Fragment> 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<IndexItem> filtered, List<IndexItemCategory> cats) {
for (WeakReference<Fragment> ref : fragList) {
for (WeakReference<Fragment> 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<IndexItem> filtered,
List<IndexItemCategoryWithSubcat> cats) {
for (WeakReference<Fragment> ref : fragList) {
boolean isPushed = false;
for (WeakReference<Fragment> 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<Fragment> ref : fragList) {
for (WeakReference<Fragment> 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<Fragment> ref : fragList) {
for (WeakReference<Fragment> 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<Fragment> ref : fragList) {
for (WeakReference<Fragment> 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<IndexItemCategoryWithSubcat> getCats() {
List<IndexItemCategoryWithSubcat> toReturn = cats;
cats = null;
return toReturn;
}
public void showDialogToDownloadMaps(Collection<String> maps) {
int count = 0;
int sz = 0;

View file

@ -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;

View file

@ -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();
}
}
}

View file

@ -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,<b>#.##</b>} 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<IndexItemCategoryWithSubcat> cats = ((DownloadActivity) getActivity()).getCats();
if (cats != null) {
onCategorizationFinished(null, cats);
}
if (savedInstanceState != null) {
List<IndexItemCategoryWithSubcat> 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<IndexItemCategoryWithSubcat> 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<IndexItem> filtered, List<IndexItemCategoryWithSubcat> cats) {
mAdapter.clear();
mAdapter.addAll(cats);
if (mAdapter != null) {
mAdapter.clear();
mAdapter.addAll(cats);
}
}
private static class CategoriesAdapter extends ArrayAdapter<IndexItemCategoryWithSubcat> {

View file

@ -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