From b55a594bd140914ff77f58e15a143c43a7656830 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Fri, 12 Feb 2016 13:05:31 +0200 Subject: [PATCH] Fixed #2233. Async task provided with reference on TrackActivity. It results in short term memory leak but that reference is necessary. --- .../plus/myplaces/SelectedGPXFragment.java | 194 ++++++++++-------- 1 file changed, 114 insertions(+), 80 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java index 8e5836fe18..4bac9e1ada 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java @@ -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,30 +64,30 @@ 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(); collator.setStrength(Collator.SECONDARY); app = (OsmandApplication) activity.getApplication(); } - - public Activity getMyActivity() { + + public TrackActivity getMyActivity() { return activity; } - + @Override public ArrayAdapter getAdapter() { return adapter; } - - + + private void startHandler() { Handler updateCurrentRecordingTrack = new Handler(); updateCurrentRecordingTrack.postDelayed(new Runnable() { @@ -99,12 +101,11 @@ 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); } - + @Override public void onResume() { @@ -115,7 +116,7 @@ public class SelectedGPXFragment extends OsmAndListFragment { startHandler(); } } - + @Override public void onPause() { super.onPause(); @@ -123,14 +124,14 @@ public class SelectedGPXFragment extends OsmAndListFragment { } - - - protected List filterGroups(GpxDisplayItemType type) { - List result = ((TrackActivity) getActivity()).getResult(); + protected static List filterGroups(@NonNull GpxDisplayItemType type, + @NonNull TrackActivity trackActivity, + @Nullable Bundle args) { + List result = trackActivity.getResult(); List groups = new ArrayList(); 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 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 groups = filterGroups(filterType()); + List groups = filterGroups(filterType(), getMyActivity(), getArguments()); adapter.setNotifyOnChange(false); for(GpxDisplayItem i: flatten(groups)) { adapter.add(i); @@ -190,21 +191,21 @@ public class SelectedGPXFragment extends OsmAndListFragment { tv.setTextSize(24); listView.setEmptyView(tv); setContent(); - + return view; } - - + + @Override public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } - + protected void saveAsFavorites(final GpxDisplayItemType gpxDisplayItemType) { AlertDialog.Builder b = new AlertDialog.Builder(getMyActivity()); final EditText editText = new EditText(getMyActivity()); - final List gs = filterGroups(gpxDisplayItemType); + final List gs = filterGroups(gpxDisplayItemType, getMyActivity(), getArguments()); if (gs.size() == 0) { return; } @@ -267,41 +268,42 @@ public class SelectedGPXFragment extends OsmAndListFragment { } protected void selectSplitDistance() { - final List groups = filterGroups(GpxDisplayItemType.TRACK_SEGMENT); + final List groups = filterGroups(GpxDisplayItemType.TRACK_SEGMENT, + getMyActivity(), getArguments()); View view = getMyActivity().getLayoutInflater().inflate(R.layout.selected_track_edit, null); - + final TIntArrayList list = new TIntArrayList(); final Spinner colorSpinner = (Spinner) view.findViewById(R.id.ColorSpinner); ColorDialogs.setupColorSpinner(getActivity(), getGpx().getColor(0), colorSpinner, list); - + final Spinner sp = (Spinner) view.findViewById(R.id.Spinner); AlertDialog.Builder bld = new AlertDialog.Builder(getMyActivity()); final List distanceSplit = new ArrayList(); 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 options = new ArrayList(); - + options.add(app.getString(R.string.none)); 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 @@ -323,19 +325,19 @@ public class SelectedGPXFragment extends OsmAndListFragment { sp.setSelection(checkedItem[0]); } } - + final CheckBox vis = (CheckBox) view.findViewById(R.id.Visibility); vis.setChecked(app.getSelectedGpxHelper().getSelectedFileByPath(getGpx().path) != null); - + bld.setView(view); bld.setNegativeButton(R.string.shared_string_cancel, null); bld.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { - + @Override 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 groups, final List distanceSplit, - final TIntArrayList timeSplit, final int which, final SelectedGpxFile sf) { - new AsyncTask() { - - 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 groups, List 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 options, List distanceSplit, - TIntArrayList timeSplit, int[] checkedItem, List model) { + TIntArrayList timeSplit, int[] checkedItem, List model) { if (distance) { double dvalue = OsmAndFormatter.calculateRoundedDist(value, app); options.add(OsmAndFormatter.getFormattedDistance((float) dvalue, app)); @@ -427,7 +402,7 @@ public class SelectedGPXFragment extends OsmAndListFragment { checkedItem[0] = distanceSplit.size() - 1; } } - + } class SelectedGPXAdapter extends ArrayAdapter { @@ -435,7 +410,7 @@ public class SelectedGPXFragment extends OsmAndListFragment { public SelectedGPXAdapter(List items) { super(getActivity(), R.layout.gpx_item_list_item, items); } - + @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; @@ -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 { + @Nullable private final SelectedGpxFile mSelectedGpxFile; + @NonNull private final SelectedGPXFragment mFragment; + @NonNull private final TrackActivity mActivity; + + private final List groups; + private final List distanceSplit; + private final TIntArrayList timeSplit; + private final int which; + + public SplitTrackAsyncTask(@Nullable SelectedGpxFile selectedGpxFile, + SelectedGPXFragment fragment, + TrackActivity activity, + List groups, + List 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; + } + } }