diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ProgressDialogFragment.java b/OsmAnd/src/net/osmand/plus/dialogs/ProgressDialogFragment.java new file mode 100644 index 0000000000..da7e311004 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/dialogs/ProgressDialogFragment.java @@ -0,0 +1,80 @@ +package net.osmand.plus.dialogs; + +import android.app.Dialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; + +import net.osmand.plus.ProgressImplementation; + +/** + * Created by GaidamakUA on 12/7/15. + */ +public class ProgressDialogFragment extends DialogFragment { + public static final String TAG = "progress"; + private static final String TITLE_ID = "title_id"; + private static final String MESSAGE_ID = "message_id"; + private static final String STYLE = "style"; + private int mMax; + private int accumulatedDelta; + private DialogInterface.OnDismissListener onDismissListener; + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Bundle args = getArguments(); + int titleId = args.getInt(TITLE_ID); + int messageId = args.getInt(MESSAGE_ID); + int style = args.getInt(STYLE); + final ProgressDialog dialog = ProgressImplementation.createProgressDialog(getActivity(), + getString(titleId), getString(messageId), + style).getDialog(); + + dialog.setIndeterminate(false); + dialog.setMax(mMax); + dialog.setProgress(0); + return dialog; + } + + public void setMax(final int max) { + mMax = max; + if (getDialog() != null) { + getProgressDialog().setMax(mMax); + } + } + + public void incrementProgressBy(int delta) { + if (getDialog() == null) { + accumulatedDelta += delta; + } else { + getProgressDialog().incrementProgressBy(delta + accumulatedDelta); + } + } + + private ProgressDialog getProgressDialog() { + return (ProgressDialog) getDialog(); + } + + @Override + public void onDismiss(DialogInterface dialog) { + if (onDismissListener != null) { + onDismissListener.onDismiss(dialog); + } + } + + public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) { + this.onDismissListener = onDismissListener; + } + + public static ProgressDialogFragment createInstance(int titleId, int messageId, int style) { + ProgressDialogFragment fragment = new ProgressDialogFragment(); + Bundle args = new Bundle(); + args.putInt(TITLE_ID, titleId); + args.putInt(MESSAGE_ID, messageId); + args.putInt(STYLE, style); + fragment.setArguments(args); + return fragment; + } +} diff --git a/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java index d527522b78..a133db2e89 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java @@ -14,12 +14,12 @@ import android.widget.TextView; import net.osmand.data.PointDescription; import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.ProgressImplementation; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.dashboard.DashBaseFragment; import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.dashboard.tools.DashFragmentData; +import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment; import java.util.ArrayList; @@ -153,9 +153,8 @@ public class DashOsmEditsFragment extends DashBaseFragment public void showProgressDialog(OsmPoint[] points, boolean closeChangeSet) { OsmPoint[] toUpload = points; - ProgressDialog dialog = ProgressImplementation.createProgressDialog(getActivity(), - getString(R.string.uploading), getString(R.string.local_openstreetmap_uploading), - ProgressDialog.STYLE_HORIZONTAL).getDialog(); + ProgressDialogFragment dialog = ProgressDialogFragment.createInstance(R.string.uploading, + R.string.local_openstreetmap_uploading, ProgressDialog.STYLE_HORIZONTAL); OsmEditsUploadListener listener = new OsmEditsUploadListenerHelper(getActivity(), getString(R.string.local_openstreetmap_were_uploaded)) { @Override @@ -174,10 +173,10 @@ public class DashOsmEditsFragment extends DashBaseFragment } } }; + dialog.show(getChildFragmentManager(), ProgressDialogFragment.TAG); UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask(dialog, listener, plugin, toUpload.length, closeChangeSet); uploadTask.execute(toUpload); - dialog.show(); } private void getOsmPoints(ArrayList dataPoints) { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/EditPOIMenuController.java b/OsmAnd/src/net/osmand/plus/osmedit/EditPOIMenuController.java index c5759a5ca5..222d591fc2 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/EditPOIMenuController.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/EditPOIMenuController.java @@ -8,9 +8,9 @@ import net.osmand.access.AccessibleAlertBuilder; import net.osmand.data.PointDescription; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.ProgressImplementation; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.plus.osmedit.OsmPoint.Action; import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment; @@ -26,7 +26,7 @@ public class EditPOIMenuController extends MenuController { private String pointTypeStr; private ProgressDialogPoiUploader poiUploader; - public EditPOIMenuController(OsmandApplication app, MapActivity mapActivity, PointDescription pointDescription, OsmPoint osmPoint) { + public EditPOIMenuController(OsmandApplication app, final MapActivity mapActivity, PointDescription pointDescription, OsmPoint osmPoint) { super(new EditPOIMenuBuilder(app, osmPoint), pointDescription, mapActivity); this.osmPoint = osmPoint; plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class); @@ -34,11 +34,10 @@ public class EditPOIMenuController extends MenuController { poiUploader = new ProgressDialogPoiUploader() { @Override public void showProgressDialog(OsmPoint[] points, boolean closeChangeSet) { - ProgressDialog dialog = ProgressImplementation.createProgressDialog( - getMapActivity(), - getMapActivity().getString(R.string.uploading), - getMapActivity().getString(R.string.local_openstreetmap_uploading), - ProgressDialog.STYLE_HORIZONTAL).getDialog(); + ProgressDialogFragment dialog = ProgressDialogFragment.createInstance( + R.string.uploading, + R.string.local_openstreetmap_uploading, + ProgressDialog.STYLE_HORIZONTAL); OsmEditsUploadListener listener = new OsmEditsUploadListenerHelper(getMapActivity(), getMapActivity().getString(R.string.local_openstreetmap_were_uploaded)) { @Override @@ -52,11 +51,10 @@ public class EditPOIMenuController extends MenuController { } } }; + dialog.show(mapActivity.getSupportFragmentManager(), ProgressDialogFragment.TAG); UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask( dialog, listener, plugin, points.length, closeChangeSet); uploadTask.execute(points); - - dialog.show(); } }; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 9070c7c03d..60c2c816ac 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -27,19 +27,20 @@ import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; + import net.osmand.access.AccessibleToast; import net.osmand.data.PointDescription; import net.osmand.osm.edit.Node; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; -import net.osmand.plus.ProgressImplementation; import net.osmand.plus.R; import net.osmand.plus.activities.ActionBarProgressActivity; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.OsmAndListFragment; import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.dialogs.DirectionsDialogs; +import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; import net.osmand.plus.osmedit.OsmPoint.Action; @@ -527,11 +528,10 @@ public class OsmEditsFragment extends OsmAndListFragment } public void showProgressDialog(OsmPoint[] points, boolean closeChangeSet) { - ProgressDialog dialog = ProgressImplementation.createProgressDialog( - getActivity(), - getString(R.string.uploading), - getString(R.string.local_openstreetmap_uploading), - ProgressDialog.STYLE_HORIZONTAL).getDialog(); + ProgressDialogFragment dialog = ProgressDialogFragment.createInstance( + R.string.uploading, + R.string.local_openstreetmap_uploading, + ProgressDialog.STYLE_HORIZONTAL); OsmEditsUploadListener listener = new OsmEditsUploadListenerHelper(getActivity(), getString(R.string.local_openstreetmap_were_uploaded)) { @Override @@ -545,11 +545,10 @@ public class OsmEditsFragment extends OsmAndListFragment listAdapter.notifyDataSetChanged(); } }; + dialog.show(getActivity().getSupportFragmentManager(), ProgressDialogFragment.TAG); UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask( dialog, listener, plugin, points.length, closeChangeSet); uploadTask.execute(points); - - dialog.show(); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListenerHelper.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListenerHelper.java index 025805d85c..35b3321d65 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListenerHelper.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListenerHelper.java @@ -27,8 +27,8 @@ import net.osmand.access.AccessibleToast; import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.ProgressImplementation; import net.osmand.plus.R; +import net.osmand.plus.dialogs.ProgressDialogFragment; import java.text.MessageFormat; import java.util.ArrayList; @@ -85,16 +85,14 @@ public class OsmEditsUploadListenerHelper implements OsmEditsUploadListener { activity.getResources().getString(R.string.local_openstreetmap_were_uploaded)); Resources resources = activity.getResources(); - ProgressDialog dialog = ProgressImplementation.createProgressDialog( - activity, - resources.getString(R.string.uploading), - resources.getString(R.string.local_openstreetmap_uploading), - ProgressDialog.STYLE_HORIZONTAL).getDialog(); + ProgressDialogFragment dialog = ProgressDialogFragment.createInstance( + R.string.uploading, + R.string.local_openstreetmap_uploading, + ProgressDialog.STYLE_HORIZONTAL); + dialog.show(activity.getSupportFragmentManager(), ProgressDialogFragment.TAG); UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask( dialog, helper, plugin, toUpload.length, false); uploadTask.execute(toUpload); - - dialog.show(); } public static final class UploadingErrorDialogFragment extends DialogFragment { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java index e0c140f873..a13034f197 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java @@ -1,11 +1,11 @@ package net.osmand.plus.osmedit; -import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.AsyncTask; import net.osmand.osm.edit.EntityInfo; import net.osmand.osm.edit.Node; +import net.osmand.plus.dialogs.ProgressDialogFragment; import java.util.HashMap; import java.util.Map; @@ -14,7 +14,7 @@ import java.util.Map; */ public class UploadOpenstreetmapPointAsyncTask extends AsyncTask> { - private ProgressDialog progress; + private ProgressDialogFragment progress; private OpenstreetmapRemoteUtil remotepoi; private OsmBugsRemoteUtil remotebug; private int listSize = 0; @@ -23,7 +23,7 @@ public class UploadOpenstreetmapPointAsyncTask private OsmEditingPlugin plugin; private final boolean closeChangeSet; - public UploadOpenstreetmapPointAsyncTask(ProgressDialog progress, + public UploadOpenstreetmapPointAsyncTask(ProgressDialogFragment progress, OsmEditsUploadListener listener, OsmEditingPlugin plugin, int listSize, @@ -81,20 +81,18 @@ public class UploadOpenstreetmapPointAsyncTask protected void onPreExecute() { interruptUploading = false; - progress.setOnCancelListener(new DialogInterface.OnCancelListener() { + progress.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override - public void onCancel(DialogInterface dialog) { + public void onDismiss(DialogInterface dialog) { UploadOpenstreetmapPointAsyncTask.this.setInterruptUploading(true); } }); - progress.setIndeterminate(false); progress.setMax(listSize); - progress.setProgress(0); } @Override protected void onPostExecute(Map loadErrorsMap) { - if (progress.isShowing()) { + if (progress != null) { progress.dismiss(); } listener.uploadEnded(loadErrorsMap); @@ -108,9 +106,10 @@ public class UploadOpenstreetmapPointAsyncTask protected void onProgressUpdate(OsmPoint... points) { for (OsmPoint p : points) { listener.uploadUpdated(p); - progress.incrementProgressBy(1); + if (progress != null) { + progress.incrementProgressBy(1); + } } } - }