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,30 +64,30 @@ 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();
collator.setStrength(Collator.SECONDARY); collator.setStrength(Collator.SECONDARY);
app = (OsmandApplication) activity.getApplication(); app = (OsmandApplication) activity.getApplication();
} }
public Activity getMyActivity() { public TrackActivity getMyActivity() {
return activity; return activity;
} }
@Override @Override
public ArrayAdapter<?> getAdapter() { public ArrayAdapter<?> getAdapter() {
return adapter; return adapter;
} }
private void startHandler() { private void startHandler() {
Handler updateCurrentRecordingTrack = new Handler(); Handler updateCurrentRecordingTrack = new Handler();
updateCurrentRecordingTrack.postDelayed(new Runnable() { updateCurrentRecordingTrack.postDelayed(new Runnable() {
@ -99,12 +101,11 @@ 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);
} }
@Override @Override
public void onResume() { public void onResume() {
@ -115,7 +116,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
startHandler(); startHandler();
} }
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
@ -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);
@ -190,21 +191,21 @@ public class SelectedGPXFragment extends OsmAndListFragment {
tv.setTextSize(24); tv.setTextSize(24);
listView.setEmptyView(tv); listView.setEmptyView(tv);
setContent(); setContent();
return view; return view;
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
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,41 +268,42 @@ 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);
final TIntArrayList list = new TIntArrayList(); final TIntArrayList list = new TIntArrayList();
final Spinner colorSpinner = (Spinner) view.findViewById(R.id.ColorSpinner); final Spinner colorSpinner = (Spinner) view.findViewById(R.id.ColorSpinner);
ColorDialogs.setupColorSpinner(getActivity(), getGpx().getColor(0), colorSpinner, list); ColorDialogs.setupColorSpinner(getActivity(), getGpx().getColor(0), colorSpinner, list);
final Spinner sp = (Spinner) view.findViewById(R.id.Spinner); final Spinner sp = (Spinner) view.findViewById(R.id.Spinner);
AlertDialog.Builder bld = new AlertDialog.Builder(getMyActivity()); AlertDialog.Builder bld = new AlertDialog.Builder(getMyActivity());
final List<Double> distanceSplit = new ArrayList<Double>(); final List<Double> distanceSplit = new ArrayList<Double>();
final TIntArrayList timeSplit = new TIntArrayList(); final TIntArrayList timeSplit = new TIntArrayList();
if(groups.size() == 0) { if (groups.size() == 0) {
sp.setVisibility(View.GONE); sp.setVisibility(View.GONE);
view.findViewById(R.id.GpxSpinnerRow).setVisibility(View.GONE); view.findViewById(R.id.GpxSpinnerRow).setVisibility(View.GONE);
} else { } else {
sp.setVisibility(View.VISIBLE); 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>(); List<String> options = new ArrayList<String>();
options.add(app.getString(R.string.none)); options.add(app.getString(R.string.none));
distanceSplit.add(-1d); distanceSplit.add(-1d);
timeSplit.add(-1); timeSplit.add(-1);
addOptionSplit(30, true, options, distanceSplit, timeSplit, checkedItem, groups); // 100 feet, 50 yards, 50 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, 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, 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, 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, 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(1500, true, options, distanceSplit, timeSplit, checkedItem, groups); // 1mi, 2km
addOptionSplit(3000, true, options, distanceSplit, timeSplit, checkedItem, groups); // 2mi, 5km addOptionSplit(3000, true, options, distanceSplit, timeSplit, checkedItem, groups); // 2mi, 5km
addOptionSplit(8000, true, options, distanceSplit, timeSplit, checkedItem, groups); // 5mi, 10km addOptionSplit(8000, true, options, distanceSplit, timeSplit, checkedItem, groups); // 5mi, 10km
@ -323,19 +325,19 @@ public class SelectedGPXFragment extends OsmAndListFragment {
sp.setSelection(checkedItem[0]); sp.setSelection(checkedItem[0]);
} }
} }
final CheckBox vis = (CheckBox) view.findViewById(R.id.Visibility); final CheckBox vis = (CheckBox) view.findViewById(R.id.Visibility);
vis.setChecked(app.getSelectedGpxHelper().getSelectedFileByPath(getGpx().path) != null); vis.setChecked(app.getSelectedGpxHelper().getSelectedFileByPath(getGpx().path) != null);
bld.setView(view); bld.setView(view);
bld.setNegativeButton(R.string.shared_string_cancel, null); bld.setNegativeButton(R.string.shared_string_cancel, null);
bld.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { bld.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(getGpx(), vis.isChecked(), false); SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(getGpx(), vis.isChecked(), false);
int clr = list.get(colorSpinner.getSelectedItemPosition()); 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.getModifiableGpxFile().setColor(clr);
sf.processPoints(); sf.processPoints();
} }
@ -343,7 +345,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
updateSplit(groups, distanceSplit, timeSplit, sp.getSelectedItemPosition(), vis.isChecked() ? sf updateSplit(groups, distanceSplit, timeSplit, sp.getSelectedItemPosition(), vis.isChecked() ? sf
: null); : null);
} }
if(vis.isChecked() && sf.getGpxFile() != null) { if (vis.isChecked() && sf.getGpxFile() != null) {
if (groups.size() > 0 && groups.get(0).getModifiableList().size() > 0) { if (groups.size() > 0 && groups.get(0).getModifiableList().size() > 0) {
GpxDisplayItem item = groups.get(0).getModifiableList().get(0); GpxDisplayItem item = groups.get(0).getModifiableList().get(0);
app.getSettings().setMapLocationToShow(item.locationStart.lat, item.locationStart.lon, 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, 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,
TIntArrayList timeSplit, int[] checkedItem, List<GpxDisplayGroup> model) { TIntArrayList timeSplit, int[] checkedItem, List<GpxDisplayGroup> model) {
if (distance) { if (distance) {
double dvalue = OsmAndFormatter.calculateRoundedDist(value, app); double dvalue = OsmAndFormatter.calculateRoundedDist(value, app);
options.add(OsmAndFormatter.getFormattedDistance((float) dvalue, app)); options.add(OsmAndFormatter.getFormattedDistance((float) dvalue, app));
@ -427,7 +402,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
checkedItem[0] = distanceSplit.size() - 1; checkedItem[0] = distanceSplit.size() - 1;
} }
} }
} }
class SelectedGPXAdapter extends ArrayAdapter<GpxDisplayItem> { class SelectedGPXAdapter extends ArrayAdapter<GpxDisplayItem> {
@ -435,7 +410,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
public SelectedGPXAdapter(List<GpxDisplayItem> items) { public SelectedGPXAdapter(List<GpxDisplayItem> items) {
super(getActivity(), R.layout.gpx_item_list_item, items); super(getActivity(), R.layout.gpx_item_list_item, items);
} }
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView; View row = convertView;
@ -443,12 +418,12 @@ public class SelectedGPXFragment extends OsmAndListFragment {
LayoutInflater inflater = getMyActivity().getLayoutInflater(); LayoutInflater inflater = getMyActivity().getLayoutInflater();
row = inflater.inflate(R.layout.gpx_item_list_item, parent, false); 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 label = (TextView) row.findViewById(R.id.name);
TextView description = (TextView) row.findViewById(R.id.description); TextView description = (TextView) row.findViewById(R.id.description);
TextView additional = (TextView) row.findViewById(R.id.additional); TextView additional = (TextView) row.findViewById(R.id.additional);
ImageView icon = (ImageView) row.findViewById(R.id.icon); 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); additional.setVisibility(View.VISIBLE);
icon.setVisibility(View.INVISIBLE); icon.setVisibility(View.INVISIBLE);
additional.setText(child.splitName); additional.setText(child.splitName);
@ -461,13 +436,13 @@ public class SelectedGPXFragment extends OsmAndListFragment {
icon.setImageDrawable(app.getIconsCache().getContentIcon(R.drawable.ic_action_markers_dark)); icon.setImageDrawable(app.getIconsCache().getContentIcon(R.drawable.ic_action_markers_dark));
} else { } else {
int groupColor = child.group.getColor(); int groupColor = child.group.getColor();
if(child.locationStart != null) { if (child.locationStart != null) {
groupColor = child.locationStart.getColor(groupColor); groupColor = child.locationStart.getColor(groupColor);
} }
if(groupColor == 0) { if (groupColor == 0) {
groupColor = getMyActivity().getResources().getColor(R.color.gpx_track); 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); 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;
}
}
} }