diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/EditPOIMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/EditPOIMenuController.java index d5efe12e90..de620ace80 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/EditPOIMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/EditPOIMenuController.java @@ -1,8 +1,7 @@ package net.osmand.plus.mapcontextmenu.controllers; -import android.app.ProgressDialog; -import android.content.DialogInterface; -import android.graphics.drawable.Drawable; +import java.util.Map; + import net.osmand.access.AccessibleAlertBuilder; import net.osmand.data.PointDescription; import net.osmand.plus.OsmandApplication; @@ -14,6 +13,7 @@ import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.plus.mapcontextmenu.builders.EditPOIMenuBuilder; import net.osmand.plus.osmedit.OpenstreetmapPoint; import net.osmand.plus.osmedit.OpenstreetmapRemoteUtil; +import net.osmand.plus.osmedit.OsmBugsLayer; import net.osmand.plus.osmedit.OsmBugsRemoteUtil; import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.plus.osmedit.OsmEditsUploadListener; @@ -25,8 +25,9 @@ import net.osmand.plus.osmedit.UploadOpenstreetmapPointAsyncTask; import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment; import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.ProgressDialogPoiUploader; import net.osmand.util.Algorithms; - -import java.util.Map; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.graphics.drawable.Drawable; public class EditPOIMenuController extends MenuController { @@ -54,6 +55,11 @@ public class EditPOIMenuController extends MenuController { public void uploadEnded(Map loadErrorsMap) { super.uploadEnded(loadErrorsMap); getMapActivity().getContextMenu().close(); + OsmBugsLayer l = getMapActivity().getMapView().getLayerByClass(OsmBugsLayer.class); + if(l != null) { + l.clearCache(); + getMapActivity().refreshMap(); + } } }; OpenstreetmapRemoteUtil remotepoi = new OpenstreetmapRemoteUtil(getMapActivity()); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java index b018a29e44..c1505c7e9f 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java @@ -24,6 +24,7 @@ import net.osmand.plus.R; import net.osmand.plus.activities.DialogProvider; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.osmedit.OsmBugsUtil.Action; +import net.osmand.plus.osmedit.OsmBugsUtil.OsmBugResult; import net.osmand.plus.views.ContextMenuLayer.IContextMenuProvider; import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapTileView; @@ -44,6 +45,7 @@ import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.util.Xml; import android.view.View; +import android.widget.Button; import android.widget.EditText; import android.widget.Toast; @@ -313,82 +315,55 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider - private void createNewBugAsync(final double latitude, final double longitude, final String text) { - AsyncTask task = new AsyncTask() { + + private void asyncActionTask(final OpenStreetNote bug, final String text, final Action action) { + AsyncTask task = new AsyncTask() { private OsmBugsUtil osmbugsUtil; - @Override - protected String doInBackground(Void... params) { - osmbugsUtil = getOsmbugsUtil(null); - return osmbugsUtil.createNewBug(latitude, longitude, text); + protected OsmBugResult doInBackground(Void... params) { + osmbugsUtil = getOsmbugsUtil(bug); + if(action == Action.REOPEN) { + return osmbugsUtil.reopenBug(bug.getLatitude(), bug.getLongitude(), bug.getId(), text); + } else if(action == Action.MODIFY) { + return osmbugsUtil.addingComment(bug.getLatitude(), bug.getLongitude(), bug.getId(), text); + } else if(action == Action.CLOSE) { + return osmbugsUtil.closingBug(bug.getLatitude(), bug.getLongitude(), bug.getId(), text); + } else if(action == Action.CREATE) { + return osmbugsUtil.createNewBug(bug.getLatitude(), bug.getLongitude(), text); + } + return null; } - - protected void onPostExecute(String result) { - if (result == null) { + protected void onPostExecute(OsmBugResult obj) { + if (obj != null && obj.warning == null) { if(local == osmbugsUtil) { AccessibleToast.makeText(activity, R.string.osm_changes_added_to_local_edits, Toast.LENGTH_LONG).show(); - List points = plugin.getDBBug().getOsmbugsPoints(); - if(points.size() > 0) { - OsmPoint point = points.get(points.size() - 1); - activity.getContextMenu().showOrUpdate(new LatLon(latitude, longitude), plugin.getOsmEditsLayer(activity).getObjectName(point), point); + if(obj.local != null) { + PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_OSM_BUG, obj.local.getText()); + activity.getContextMenu().show(new LatLon(obj.local.getLatitude(), obj.local.getLongitude()), pd, obj.local); } } else { - AccessibleToast.makeText(activity, R.string.osn_add_dialog_success, Toast.LENGTH_LONG).show(); - activity.getContextMenu().close(); - } - refreshMap(); - } else { - AccessibleToast.makeText(activity, activity.getResources().getString(R.string.osn_add_dialog_error) + "\n" + result, - Toast.LENGTH_LONG).show(); - openBug(latitude, longitude, text); - } - } - }; - executeTaskInBackground(task); - } - - private void addingCommentAsync(final OpenStreetNote bug, final String text) { - AsyncTask task = new AsyncTask() { - private OsmBugsUtil osmbugsUtil; - @Override - protected String doInBackground(Void... params) { - osmbugsUtil = getOsmbugsUtil(bug); - return osmbugsUtil.addingComment(bug.getLatitude(), bug.getLongitude(), bug.getId(), text); - } - protected void onPostExecute(String warn) { - if (warn == null) { - if(local == osmbugsUtil) { - AccessibleToast.makeText(activity, R.string.osm_changes_added_to_local_edits, Toast.LENGTH_LONG).show(); - } else { - AccessibleToast.makeText(activity, R.string.osb_comment_dialog_success, Toast.LENGTH_LONG).show(); + if(action == Action.REOPEN) { + AccessibleToast.makeText(activity, R.string.osn_add_dialog_success, Toast.LENGTH_LONG).show(); + } else if(action == Action.MODIFY) { + AccessibleToast.makeText(activity, R.string.osb_comment_dialog_success, Toast.LENGTH_LONG).show(); + } else if(action == Action.CLOSE) { + AccessibleToast.makeText(activity, R.string.osn_close_dialog_success, Toast.LENGTH_LONG).show(); + } else if(action == Action.CREATE) { + AccessibleToast.makeText(activity, R.string.osn_add_dialog_success, Toast.LENGTH_LONG).show(); + } + } clearCache(); } else { - AccessibleToast.makeText(activity, activity.getResources().getString(R.string.osb_comment_dialog_error) + "\n" + warn, Toast.LENGTH_LONG).show(); - } - } - }; - executeTaskInBackground(task); - } - - private void reopeningtAsync(final OpenStreetNote bug, final String text) { - AsyncTask task = new AsyncTask() { - private OsmBugsUtil osmbugsUtil; - @Override - protected String doInBackground(Void... params) { - osmbugsUtil = getOsmbugsUtil(bug); - return osmbugsUtil.reopenBug(bug.getLatitude(), bug.getLongitude(), bug.getId(), text); - } - protected void onPostExecute(String warn) { - if (warn == null) { - if(local == osmbugsUtil) { - AccessibleToast.makeText(activity, R.string.osm_changes_added_to_local_edits, Toast.LENGTH_LONG).show(); - } else { - AccessibleToast.makeText(activity, R.string.osb_comment_dialog_success, Toast.LENGTH_LONG).show(); + int r = R.string.osb_comment_dialog_error; + if(action == Action.REOPEN) { + r = R.string.osn_add_dialog_error; + } else if(action == Action.CLOSE) { + r = R.string.osn_close_dialog_error; + } else if(action == Action.CREATE) { + r = R.string.osn_add_dialog_error; } - clearCache(); - } else { - AccessibleToast.makeText(activity, activity.getResources().getString(R.string.osb_comment_dialog_error) + "\n" + warn, Toast.LENGTH_LONG).show(); + AccessibleToast.makeText(activity, activity.getResources().getString(r) + "\n" + obj, Toast.LENGTH_LONG).show(); } } }; @@ -428,10 +403,9 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider activity.showDialog(DIALOG_BUG); } - private Dialog createBugDialog(final Bundle args) { + private void prepareBugDialog(Bundle args, Dialog dialog) { final OpenStreetNote bug = (OpenStreetNote) args.getSerializable(KEY_BUG); final Action action = OsmBugsUtil.Action.valueOf((String) args.getSerializable(KEY_ACTION)); - AlertDialog.Builder builder = new AlertDialog.Builder(activity); int title ; if(action == Action.CLOSE) { title = R.string.osn_close_dialog_title; @@ -441,14 +415,13 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider title = R.string.osn_reopen_dialog_title; } else { title = R.string.osn_add_dialog_title; - } - builder.setTitle(title); - final View view = activity.getLayoutInflater().inflate(R.layout.open_bug, null); - builder.setView(view); - - if(action == Action.CREATE && bug.comments.size() > 0) { - ((EditText)view.findViewById(R.id.messageEditText)).setText(bug.comments.get(0)); - } + } + AlertDialog dlg = (AlertDialog) dialog; + dlg.setTitle(title); + final View view = dlg.findViewById(R.id.layout); + Button btn = dlg.getButton(DialogInterface.BUTTON_POSITIVE); + btn.setText(title); + OsmBugsUtil util = getOsmbugsUtil(bug); final boolean offline = util instanceof OsmBugsLocalUtil; if(offline) { @@ -460,11 +433,13 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider ((EditText)view.findViewById(R.id.userNameEditText)).setText(getUserName()); ((EditText)view.findViewById(R.id.passwordEditText)).setText(((OsmandApplication) activity.getApplication()).getSettings().USER_PASSWORD.get()); } + if(action == Action.CREATE && bug.comments.size() > 0) { + ((EditText)view.findViewById(R.id.messageEditText)).setText(bug.comments.get(0)); + } AndroidUtils.softKeyboardDelayed((EditText) view.findViewById(R.id.messageEditText)); - builder.setNegativeButton(R.string.shared_string_cancel, null); - builder.setPositiveButton(title, new DialogInterface.OnClickListener() { + btn.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(DialogInterface dialog, int which) { + public void onClick(View dialog) { if (bug != null) { String text = offline ? getMessageText(view) : getTextAndUpdateUserPwd(view); // server validation will handle it @@ -472,20 +447,24 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider // AccessibleToast.makeText(activity, activity.getString(R.string.osb_author_or_password_not_specified), // Toast.LENGTH_SHORT).show(); // } - if(action == Action.CLOSE) { - closingAsync(bug, text); - } else if(action == Action.MODIFY) { - addingCommentAsync(bug, text); - } else if(action == Action.REOPEN) { - reopeningtAsync(bug, text); - } else { - createNewBugAsync(bug.getLatitude(), bug.getLongitude(), text); - } - + asyncActionTask(bug, text, action); activity.getContextMenu().close(); } } }); + } + + private Dialog createBugDialog(final Bundle args) { + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + + builder.setTitle(""); + final View view = activity.getLayoutInflater().inflate(R.layout.open_bug, null); + view.setId(R.id.layout); + builder.setView(view); + + + builder.setNegativeButton(R.string.shared_string_cancel, null); + builder.setPositiveButton(R.string.shared_string_commit, null); return builder.create(); } @@ -513,33 +492,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider } - private void closingAsync(final OpenStreetNote bug, final String text) { - AsyncTask task = new AsyncTask() { - private OsmBugsUtil osmbugsUtil; - - @Override - protected String doInBackground(Void... params) { - osmbugsUtil = getOsmbugsUtil(bug); - return osmbugsUtil.closingBug(bug.getLatitude(), bug.getLongitude(), bug.getId(), text); - } - - protected void onPostExecute(String closed) { - if (closed == null) { - if(local == osmbugsUtil) { - AccessibleToast.makeText(activity, R.string.osm_changes_added_to_local_edits, Toast.LENGTH_LONG).show(); - } else { - AccessibleToast.makeText(activity, R.string.osn_close_dialog_success, Toast.LENGTH_LONG).show(); - } - clearCache(); - refreshMap(); - } else { - AccessibleToast.makeText(activity, activity.getString(R.string.osn_close_dialog_error) + "\n" + closed, - Toast.LENGTH_LONG).show(); - } - } - }; - executeTaskInBackground(task); - } + @Override public String getObjectDescription(Object o) { @@ -597,11 +550,14 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider public void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DIALOG_BUG: -// ((EditText)dialog.findViewById(R.id.messageEditText)).setText(""); + prepareBugDialog(dialogBundle, dialog); + ((EditText)dialog.findViewById(R.id.messageEditText)).setText(""); break; } } + + public static class OpenStreetNote implements Serializable { private boolean local; private static final long serialVersionUID = -7848941747811172615L; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLocalUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLocalUtil.java index 4c105b93f8..8bcfe4debd 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLocalUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLocalUtil.java @@ -18,25 +18,32 @@ public class OsmBugsLocalUtil implements OsmBugsUtil { } @Override - public String createNewBug(double latitude, double longitude, String text){ + public OsmBugResult createNewBug(double latitude, double longitude, String text){ OsmNotesPoint p = new OsmNotesPoint(); p.setId(Math.min(-2, db.getMinID() -1)); p.setText(text); p.setLatitude(latitude); p.setLongitude(longitude); p.setAction(OsmPoint.Action.CREATE); - return db.addOsmbugs(p) ? null : ""; + return wrap(p, db.addOsmbugs(p)); } + private OsmBugResult wrap(OsmNotesPoint p, boolean success) { + OsmBugResult s = new OsmBugResult(); + s.local = p; + s.warning = success ? null : ""; + return null; + } + @Override - public String reopenBug(double latitude, double longitude, long id, String text){ + public OsmBugResult reopenBug(double latitude, double longitude, long id, String text){ OsmNotesPoint p = new OsmNotesPoint(); p.setId(Math.min(-2, db.getMinID() -1)); p.setText(text); p.setLatitude(latitude); p.setLongitude(longitude); p.setAction(OsmPoint.Action.REOPEN); - return db.addOsmbugs(p) ? null : ""; + return wrap(p, db.addOsmbugs(p)); } public List getOsmbugsPoints() { @@ -44,25 +51,25 @@ public class OsmBugsLocalUtil implements OsmBugsUtil { } @Override - public String addingComment(double latitude, double longitude, long id, String text){ + public OsmBugResult addingComment(double latitude, double longitude, long id, String text){ OsmNotesPoint p = new OsmNotesPoint(); p.setId(id); p.setText(text); p.setLatitude(latitude); p.setLongitude(longitude); p.setAction(OsmPoint.Action.MODIFY); - return db.addOsmbugs(p) ? null : ""; + return wrap(p, db.addOsmbugs(p)); } @Override - public String closingBug(double latitude, double longitude, long id, String text){ + public OsmBugResult closingBug(double latitude, double longitude, long id, String text){ OsmNotesPoint p = new OsmNotesPoint(); p.setId(id); p.setText(text); p.setLatitude(latitude); p.setLongitude(longitude); p.setAction(OsmPoint.Action.DELETE); - return db.addOsmbugs(p) ? null : ""; + return wrap(p, db.addOsmbugs(p)); } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java index 50e059c3ce..f7d5708cd8 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java @@ -40,7 +40,7 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { } @Override - public String createNewBug(double latitude, double longitude, String text) { + public OsmBugResult createNewBug(double latitude, double longitude, String text) { StringBuilder b = new StringBuilder(); b.append(getNotesApi()).append("?"); //$NON-NLS-1$ b.append("lat=").append(latitude); //$NON-NLS-1$ @@ -50,7 +50,7 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { } @Override - public String addingComment(double latitude, double longitude, long id, String text) { + public OsmBugResult addingComment(double latitude, double longitude, long id, String text) { StringBuilder b = new StringBuilder(); b.append(getNotesApi()).append("/"); b.append(id); //$NON-NLS-1$ @@ -59,7 +59,7 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { } @Override - public String reopenBug(double latitude, double longitude, long id, String text){ + public OsmBugResult reopenBug(double latitude, double longitude, long id, String text){ StringBuilder b = new StringBuilder(); b.append(getNotesApi()).append("?"); //$NON-NLS-1$ b.append(id); //$NON-NLS-1$ @@ -69,7 +69,7 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { @Override - public String closingBug(double latitude, double longitude, long id, String text) { + public OsmBugResult closingBug(double latitude, double longitude, long id, String text) { StringBuilder b = new StringBuilder(); b.append(getNotesApi()).append("/"); b.append(id); //$NON-NLS-1$ @@ -77,7 +77,8 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { return editingPOI(b.toString(), "POST", "close bug"); //$NON-NLS-1$ } - private String editingPOI(String url, String requestMethod, String userOperation) { + private OsmBugResult editingPOI(String url, String requestMethod, String userOperation) { + OsmBugResult r = new OsmBugResult(); try { HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url); log.info("Editing poi " + url); @@ -111,21 +112,21 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { log.info("Response : " + responseBody); //$NON-NLS-1$ connection.disconnect(); if (!ok) { - return msg + "\n" + responseBody; + r.warning = msg + "\n" + responseBody; } } catch (NullPointerException e) { // that's tricky case why NPE is thrown to fix that problem httpClient could be used String msg = app.getString(R.string.auth_failed); log.error(msg, e); - return app.getString(R.string.auth_failed) + ""; + r.warning = app.getString(R.string.auth_failed) + ""; } catch (MalformedURLException e) { log.error(userOperation + " " + app.getString(R.string.failed_op), e); //$NON-NLS-1$ - return e.getMessage() + ""; + r.warning = e.getMessage() + ""; } catch (IOException e) { log.error(userOperation + " " + app.getString(R.string.failed_op), e); //$NON-NLS-1$ - return e.getMessage() + " link unavailable"; + r.warning = e.getMessage() + " link unavailable"; } - return null; + return r; } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java index 544476ef7f..5f93697702 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java @@ -3,12 +3,17 @@ package net.osmand.plus.osmedit; public interface OsmBugsUtil { public static enum Action {CREATE, MODIFY, CLOSE, REOPEN}; - - public String createNewBug(double latitude, double longitude, String text); - - public String addingComment(double latitude, double longitude, long id, String text); - public String reopenBug(double latitude, double longitude, long id, String text); + public static class OsmBugResult { + OsmNotesPoint local; + String warning; + } - public String closingBug(double latitude, double longitude, long id, String text); + public OsmBugResult createNewBug(double latitude, double longitude, String text); + + public OsmBugResult addingComment(double latitude, double longitude, long id, String text); + + public OsmBugResult reopenBug(double latitude, double longitude, long id, String text); + + public OsmBugResult closingBug(double latitude, double longitude, long id, String text); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java index 7a8e779ce3..dd19d6987a 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java @@ -66,11 +66,13 @@ public class UploadOpenstreetmapPointAsyncTask OsmNotesPoint p = (OsmNotesPoint) point; String errorMessage = null; if (p.getAction() == OsmPoint.Action.CREATE) { - errorMessage = remotebug.createNewBug(p.getLatitude(), p.getLongitude(), p.getText()); + errorMessage = remotebug.createNewBug(p.getLatitude(), p.getLongitude(), p.getText()).warning; } else if (p.getAction() == OsmPoint.Action.MODIFY) { - errorMessage = remotebug.addingComment(p.getLatitude(), p.getLongitude(), p.getId(), p.getText()); + errorMessage = remotebug.addingComment(p.getLatitude(), p.getLongitude(), p.getId(), p.getText()).warning; + } else if (p.getAction() == OsmPoint.Action.REOPEN) { + errorMessage = remotebug.reopenBug(p.getLatitude(), p.getLongitude(), p.getId(), p.getText()).warning; } else if (p.getAction() == OsmPoint.Action.DELETE) { - errorMessage = remotebug.closingBug(p.getLatitude(), p.getLongitude(), p.getId(), p.getText()); + errorMessage = remotebug.closingBug(p.getLatitude(), p.getLongitude(), p.getId(), p.getText()).warning; } if (errorMessage == null) { plugin.getDBBug().deleteAllBugModifications(p);