Fixed #2233. Async task provided with reference on TrackActivity. It results in short term memory leak but that reference is necessary.

This commit is contained in:
GaidamakUA 2016-02-12 13:05:31 +02:00
parent 1d908a4a28
commit b55a594bd1

View file

@ -8,6 +8,8 @@ import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.Html; import android.text.Html;
@ -62,13 +64,13 @@ public class SelectedGPXFragment extends OsmAndListFragment {
public static final String ARG_TO_HIDE_CONFIG_BTN = "ARG_TO_HIDE_CONFIG_BTN"; public static final String ARG_TO_HIDE_CONFIG_BTN = "ARG_TO_HIDE_CONFIG_BTN";
protected OsmandApplication app; protected OsmandApplication app;
protected SelectedGPXAdapter adapter; protected SelectedGPXAdapter adapter;
protected Activity activity; protected TrackActivity activity;
private boolean updateEnable; private boolean updateEnable;
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
this.activity = activity; this.activity = (TrackActivity) activity;
super.onAttach(activity); super.onAttach(activity);
final Collator collator = Collator.getInstance(); final Collator collator = Collator.getInstance();
@ -76,7 +78,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
app = (OsmandApplication) activity.getApplication(); app = (OsmandApplication) activity.getApplication();
} }
public Activity getMyActivity() { public TrackActivity getMyActivity() {
return activity; return activity;
} }
@ -100,8 +102,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
}, 2000); }, 2000);
} }
public boolean isArgumentTrue(String arg) { public static boolean isArgumentTrue(@Nullable Bundle args, @NonNull String arg) {
Bundle args = getArguments();
return args != null && args.getBoolean(arg); return args != null && args.getBoolean(arg);
} }
@ -123,14 +124,14 @@ public class SelectedGPXFragment extends OsmAndListFragment {
} }
protected static List<GpxDisplayGroup> filterGroups(@NonNull GpxDisplayItemType type,
@NonNull TrackActivity trackActivity,
protected List<GpxDisplayGroup> filterGroups(GpxDisplayItemType type) { @Nullable Bundle args) {
List<GpxDisplayGroup> result = ((TrackActivity) getActivity()).getResult(); List<GpxDisplayGroup> result = trackActivity.getResult();
List<GpxDisplayGroup> groups = new ArrayList<GpxSelectionHelper.GpxDisplayGroup>(); List<GpxDisplayGroup> groups = new ArrayList<GpxSelectionHelper.GpxDisplayGroup>();
for (GpxDisplayGroup group : result) { for (GpxDisplayGroup group : result) {
boolean add = group.getType() == type || type == null; boolean add = group.getType() == type || type == null;
if (isArgumentTrue(ARG_TO_FILTER_SHORT_TRACKS)) { if (isArgumentTrue(args, ARG_TO_FILTER_SHORT_TRACKS)) {
Iterator<GpxDisplayItem> item = group.getModifiableList().iterator(); Iterator<GpxDisplayItem> item = group.getModifiableList().iterator();
while (item.hasNext()) { while (item.hasNext()) {
GpxDisplayItem it2 = item.next(); GpxDisplayItem it2 = item.next();
@ -158,7 +159,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
protected void updateContent() { protected void updateContent() {
adapter.clear(); adapter.clear();
List<GpxSelectionHelper.GpxDisplayGroup> groups = filterGroups(filterType()); List<GpxSelectionHelper.GpxDisplayGroup> groups = filterGroups(filterType(), getMyActivity(), getArguments());
adapter.setNotifyOnChange(false); adapter.setNotifyOnChange(false);
for(GpxDisplayItem i: flatten(groups)) { for(GpxDisplayItem i: flatten(groups)) {
adapter.add(i); adapter.add(i);
@ -204,7 +205,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
protected void saveAsFavorites(final GpxDisplayItemType gpxDisplayItemType) { protected void saveAsFavorites(final GpxDisplayItemType gpxDisplayItemType) {
AlertDialog.Builder b = new AlertDialog.Builder(getMyActivity()); AlertDialog.Builder b = new AlertDialog.Builder(getMyActivity());
final EditText editText = new EditText(getMyActivity()); final EditText editText = new EditText(getMyActivity());
final List<GpxDisplayGroup> gs = filterGroups(gpxDisplayItemType); final List<GpxDisplayGroup> gs = filterGroups(gpxDisplayItemType, getMyActivity(), getArguments());
if (gs.size() == 0) { if (gs.size() == 0) {
return; return;
} }
@ -267,7 +268,8 @@ public class SelectedGPXFragment extends OsmAndListFragment {
} }
protected void selectSplitDistance() { protected void selectSplitDistance() {
final List<GpxDisplayGroup> groups = filterGroups(GpxDisplayItemType.TRACK_SEGMENT); final List<GpxDisplayGroup> groups = filterGroups(GpxDisplayItemType.TRACK_SEGMENT,
getMyActivity(), getArguments());
View view = getMyActivity().getLayoutInflater().inflate(R.layout.selected_track_edit, null); View view = getMyActivity().getLayoutInflater().inflate(R.layout.selected_track_edit, null);
@ -370,37 +372,10 @@ public class SelectedGPXFragment extends OsmAndListFragment {
} }
private void updateSplit(final List<GpxDisplayGroup> groups, final List<Double> distanceSplit, private void updateSplit(List<GpxDisplayGroup> groups, List<Double> distanceSplit,
final TIntArrayList timeSplit, final int which, final SelectedGpxFile sf) { TIntArrayList timeSplit, int which, SelectedGpxFile sf) {
new AsyncTask<Void, Void, Void>() { new SplitTrackAsyncTask(sf, this, getMyActivity(), groups, distanceSplit, timeSplit, which)
.execute((Void) null);
protected void onPostExecute(Void result) {
if(sf != null) {
sf.setDisplayGroups(filterGroups(null));
}
updateContent();
(getActivity()).setProgressBarIndeterminateVisibility(false);
}
protected void onPreExecute() {
(getActivity()).setProgressBarIndeterminateVisibility(true);
}
@Override
protected Void doInBackground(Void... params) {
for (GpxDisplayGroup model : groups) {
if (which == 0) {
model.noSplit(app);
} else if (distanceSplit.get(which) > 0) {
model.splitByDistance(app, distanceSplit.get(which));
} else if (timeSplit.get(which) > 0) {
model.splitByTime(app, timeSplit.get(which));
}
}
return null;
}
}.execute((Void)null);
} }
private void addOptionSplit(int value, boolean distance, List<String> options, List<Double> distanceSplit, private void addOptionSplit(int value, boolean distance, List<String> options, List<Double> distanceSplit,
@ -531,4 +506,63 @@ public class SelectedGPXFragment extends OsmAndListFragment {
// } // }
*/ */
} }
public static class SplitTrackAsyncTask extends AsyncTask<Void, Void, Void> {
@Nullable private final SelectedGpxFile mSelectedGpxFile;
@NonNull private final SelectedGPXFragment mFragment;
@NonNull private final TrackActivity mActivity;
private final List<GpxDisplayGroup> groups;
private final List<Double> distanceSplit;
private final TIntArrayList timeSplit;
private final int which;
public SplitTrackAsyncTask(@Nullable SelectedGpxFile selectedGpxFile,
SelectedGPXFragment fragment,
TrackActivity activity,
List<GpxDisplayGroup> groups,
List<Double> distanceSplit,
TIntArrayList timeSplit,
int which) {
mSelectedGpxFile = selectedGpxFile;
mFragment = fragment;
mActivity = activity;
this.groups = groups;
this.distanceSplit = distanceSplit;
this.timeSplit = timeSplit;
this.which = which;
}
protected void onPostExecute(Void result) {
if (mSelectedGpxFile != null) {
mSelectedGpxFile.setDisplayGroups(filterGroups(null, mActivity, mFragment.getArguments()));
}
if (mFragment.isVisible()) {
mFragment.updateContent();
}
if (!mActivity.isFinishing()) {
mActivity.setProgressBarIndeterminateVisibility(false);
}
}
protected void onPreExecute() {
mActivity.setProgressBarIndeterminateVisibility(true);
}
@Override
protected Void doInBackground(Void... params) {
for (GpxDisplayGroup model : groups) {
OsmandApplication application = mActivity.getMyApplication();
if (which == 0) {
model.noSplit(application);
} else if (distanceSplit.get(which) > 0) {
model.splitByDistance(application, distanceSplit.get(which));
} else if (timeSplit.get(which) > 0) {
model.splitByTime(application, timeSplit.get(which));
}
}
return null;
}
}
} }