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.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
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";
protected OsmandApplication app;
protected SelectedGPXAdapter adapter;
protected Activity activity;
protected TrackActivity activity;
private boolean updateEnable;
@Override
public void onAttach(Activity activity) {
this.activity = activity;
this.activity = (TrackActivity) activity;
super.onAttach(activity);
final Collator collator = Collator.getInstance();
@ -76,7 +78,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
app = (OsmandApplication) activity.getApplication();
}
public Activity getMyActivity() {
public TrackActivity getMyActivity() {
return activity;
}
@ -100,8 +102,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
}, 2000);
}
public boolean isArgumentTrue(String arg) {
Bundle args = getArguments();
public static boolean isArgumentTrue(@Nullable Bundle args, @NonNull String arg) {
return args != null && args.getBoolean(arg);
}
@ -123,14 +124,14 @@ public class SelectedGPXFragment extends OsmAndListFragment {
}
protected List<GpxDisplayGroup> filterGroups(GpxDisplayItemType type) {
List<GpxDisplayGroup> result = ((TrackActivity) getActivity()).getResult();
protected static List<GpxDisplayGroup> filterGroups(@NonNull GpxDisplayItemType type,
@NonNull TrackActivity trackActivity,
@Nullable Bundle args) {
List<GpxDisplayGroup> result = trackActivity.getResult();
List<GpxDisplayGroup> groups = new ArrayList<GpxSelectionHelper.GpxDisplayGroup>();
for (GpxDisplayGroup group : result) {
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();
while (item.hasNext()) {
GpxDisplayItem it2 = item.next();
@ -158,7 +159,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
protected void updateContent() {
adapter.clear();
List<GpxSelectionHelper.GpxDisplayGroup> groups = filterGroups(filterType());
List<GpxSelectionHelper.GpxDisplayGroup> groups = filterGroups(filterType(), getMyActivity(), getArguments());
adapter.setNotifyOnChange(false);
for(GpxDisplayItem i: flatten(groups)) {
adapter.add(i);
@ -204,7 +205,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
protected void saveAsFavorites(final GpxDisplayItemType gpxDisplayItemType) {
AlertDialog.Builder b = new AlertDialog.Builder(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) {
return;
}
@ -267,7 +268,8 @@ public class SelectedGPXFragment extends OsmAndListFragment {
}
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);
@ -279,13 +281,13 @@ public class SelectedGPXFragment extends OsmAndListFragment {
AlertDialog.Builder bld = new AlertDialog.Builder(getMyActivity());
final List<Double> distanceSplit = new ArrayList<Double>();
final TIntArrayList timeSplit = new TIntArrayList();
if(groups.size() == 0) {
if (groups.size() == 0) {
sp.setVisibility(View.GONE);
view.findViewById(R.id.GpxSpinnerRow).setVisibility(View.GONE);
} else {
sp.setVisibility(View.VISIBLE);
int[] checkedItem = new int[] { !groups.get(0).isSplitDistance() && !groups.get(0).isSplitTime() ? 0 : -1 };
int[] checkedItem = new int[]{!groups.get(0).isSplitDistance() && !groups.get(0).isSplitTime() ? 0 : -1};
List<String> options = new ArrayList<String>();
@ -293,15 +295,15 @@ public class SelectedGPXFragment extends OsmAndListFragment {
distanceSplit.add(-1d);
timeSplit.add(-1);
addOptionSplit(30, true, options, distanceSplit, timeSplit, checkedItem, groups); // 100 feet, 50 yards, 50
// m
// m
addOptionSplit(60, true, options, distanceSplit, timeSplit, checkedItem, groups); // 200 feet, 100 yards,
// 100 m
// 100 m
addOptionSplit(150, true, options, distanceSplit, timeSplit, checkedItem, groups); // 500 feet, 200 yards,
// 200 m
// 200 m
addOptionSplit(300, true, options, distanceSplit, timeSplit, checkedItem, groups); // 1000 feet, 500 yards,
// 500 m
// 500 m
addOptionSplit(600, true, options, distanceSplit, timeSplit, checkedItem, groups); // 2000 feet, 1000 yards,
// 1km
// 1km
addOptionSplit(1500, true, options, distanceSplit, timeSplit, checkedItem, groups); // 1mi, 2km
addOptionSplit(3000, true, options, distanceSplit, timeSplit, checkedItem, groups); // 2mi, 5km
addOptionSplit(8000, true, options, distanceSplit, timeSplit, checkedItem, groups); // 5mi, 10km
@ -335,7 +337,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
public void onClick(DialogInterface dialog, int which) {
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(getGpx(), vis.isChecked(), false);
int clr = list.get(colorSpinner.getSelectedItemPosition());
if(vis.isChecked() && clr != 0 && sf.getModifiableGpxFile() != null) {
if (vis.isChecked() && clr != 0 && sf.getModifiableGpxFile() != null) {
sf.getModifiableGpxFile().setColor(clr);
sf.processPoints();
}
@ -343,7 +345,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
updateSplit(groups, distanceSplit, timeSplit, sp.getSelectedItemPosition(), vis.isChecked() ? sf
: null);
}
if(vis.isChecked() && sf.getGpxFile() != null) {
if (vis.isChecked() && sf.getGpxFile() != null) {
if (groups.size() > 0 && groups.get(0).getModifiableList().size() > 0) {
GpxDisplayItem item = groups.get(0).getModifiableList().get(0);
app.getSettings().setMapLocationToShow(item.locationStart.lat, item.locationStart.lon,
@ -370,41 +372,14 @@ public class SelectedGPXFragment extends OsmAndListFragment {
}
private void updateSplit(final List<GpxDisplayGroup> groups, final List<Double> distanceSplit,
final TIntArrayList timeSplit, final int which, final SelectedGpxFile sf) {
new AsyncTask<Void, Void, Void>() {
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 updateSplit(List<GpxDisplayGroup> groups, List<Double> distanceSplit,
TIntArrayList timeSplit, int which, SelectedGpxFile sf) {
new SplitTrackAsyncTask(sf, this, getMyActivity(), groups, distanceSplit, timeSplit, which)
.execute((Void) null);
}
private void addOptionSplit(int value, boolean distance, List<String> options, List<Double> distanceSplit,
TIntArrayList timeSplit, int[] checkedItem, List<GpxDisplayGroup> model) {
TIntArrayList timeSplit, int[] checkedItem, List<GpxDisplayGroup> model) {
if (distance) {
double dvalue = OsmAndFormatter.calculateRoundedDist(value, app);
options.add(OsmAndFormatter.getFormattedDistance((float) dvalue, app));
@ -443,12 +418,12 @@ public class SelectedGPXFragment extends OsmAndListFragment {
LayoutInflater inflater = getMyActivity().getLayoutInflater();
row = inflater.inflate(R.layout.gpx_item_list_item, parent, false);
}
GpxDisplayItem child = getItem(position);
GpxDisplayItem child = getItem(position);
TextView label = (TextView) row.findViewById(R.id.name);
TextView description = (TextView) row.findViewById(R.id.description);
TextView additional = (TextView) row.findViewById(R.id.additional);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
if(child.splitMetric >= 0 && child.splitName != null) {
if (child.splitMetric >= 0 && child.splitName != null) {
additional.setVisibility(View.VISIBLE);
icon.setVisibility(View.INVISIBLE);
additional.setText(child.splitName);
@ -461,13 +436,13 @@ public class SelectedGPXFragment extends OsmAndListFragment {
icon.setImageDrawable(app.getIconsCache().getContentIcon(R.drawable.ic_action_markers_dark));
} else {
int groupColor = child.group.getColor();
if(child.locationStart != null) {
if (child.locationStart != null) {
groupColor = child.locationStart.getColor(groupColor);
}
if(groupColor == 0) {
if (groupColor == 0) {
groupColor = getMyActivity().getResources().getColor(R.color.gpx_track);
}
icon.setImageDrawable(FavoriteImageDrawable.getOrCreate(getMyActivity(), groupColor, false));
icon.setImageDrawable(FavoriteImageDrawable.getOrCreate(getMyActivity(), groupColor, false));
}
}
row.setTag(child);
@ -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;
}
}
}