From ee6619550cdb412c7383c465ed46c5d0f65ef6ce Mon Sep 17 00:00:00 2001 From: Fabien Carrion Date: Sun, 1 Jul 2012 19:27:32 -0500 Subject: [PATCH] Take off the layer class all the net edition of bugs --- .../net/osmand/plus/osmedit/OsmBugsLayer.java | 56 ++-------------- .../plus/osmedit/OsmBugsRemoteUtil.java | 66 +++++++++++++++++++ .../net/osmand/plus/osmedit/OsmBugsUtil.java | 10 +++ 3 files changed, 81 insertions(+), 51 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java create mode 100644 OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java index f92853d370..9764dc68e9 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java @@ -7,7 +7,6 @@ import java.io.InputStreamReader; import java.io.Serializable; import java.net.URL; import java.net.URLConnection; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -54,6 +53,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider private final static int startZoom = 8; private final int SEARCH_LIMIT = 100; + private OsmBugsRemoteUtil osmbugsUtil; private OsmandMapTileView view; private Handler handlerToLoop; @@ -83,7 +83,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider public OsmBugsLayer(MapActivity activity){ this.activity = activity; - + this.osmbugsUtil = new OsmBugsRemoteUtil(); } @Override @@ -254,52 +254,6 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider cRightLongitude = 0; } - public boolean createNewBug(double latitude, double longitude, String text, String authorName){ - StringBuilder b = new StringBuilder(); - b.append("http://openstreetbugs.schokokeks.org/api/0.1/addPOIexec?"); //$NON-NLS-1$ - b.append("lat=").append(latitude); //$NON-NLS-1$ - b.append("&lon=").append(longitude); //$NON-NLS-1$ - text = text + " [" + authorName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ - b.append("&text=").append(URLEncoder.encode(text)); //$NON-NLS-1$ - b.append("&name=").append(URLEncoder.encode(authorName)); //$NON-NLS-1$ - return editingPOI(b.toString(), "creating bug"); //$NON-NLS-1$ - } - - public boolean addingComment(long id, String text, String authorName){ - StringBuilder b = new StringBuilder(); - b.append("http://openstreetbugs.schokokeks.org/api/0.1/editPOIexec?"); //$NON-NLS-1$ - b.append("id=").append(id); //$NON-NLS-1$ - text = text + " [" + authorName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ - b.append("&text=").append(URLEncoder.encode(text)); //$NON-NLS-1$ - b.append("&name=").append(URLEncoder.encode(authorName)); //$NON-NLS-1$ - return editingPOI(b.toString(), "adding comment"); //$NON-NLS-1$ - } - - public boolean closingBug(long id){ - StringBuilder b = new StringBuilder(); - b.append("http://openstreetbugs.schokokeks.org/api/0.1/closePOIexec?"); //$NON-NLS-1$ - b.append("id=").append(id); //$NON-NLS-1$ - return editingPOI(b.toString(),"closing bug"); //$NON-NLS-1$ - } - - - private boolean editingPOI(String urlStr, String debugAction){ - try { - log.debug("Action " + debugAction + " " + urlStr); //$NON-NLS-1$ //$NON-NLS-2$ - URL url = new URL(urlStr); - URLConnection connection = url.openConnection(); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - while(reader.readLine() != null){ - } - log.debug("Action " + debugAction + " successfull"); //$NON-NLS-1$ //$NON-NLS-2$ - return true; - } catch (IOException e) { - log.error("Error " +debugAction, e); //$NON-NLS-1$ - } catch (RuntimeException e) { - log.error("Error "+debugAction, e); //$NON-NLS-1$ - } - return false; - } protected List loadingBugs(double topLatitude, double leftLongitude, double bottomLatitude,double rightLongitude){ List bugs = new ArrayList(); @@ -367,7 +321,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider String author = ((EditText)openBug.findViewById(R.id.AuthorName)).getText().toString(); // do not set name as author it is ridiculous in that case ((OsmandApplication) activity.getApplication()).getSettings().USER_OSM_BUG_NAME.set(author); - boolean bug = createNewBug(latitude, longitude, text, author); + boolean bug = osmbugsUtil.createNewBug(latitude, longitude, text, author); if (bug) { AccessibleToast.makeText(activity, activity.getResources().getString(R.string.osb_add_dialog_success), Toast.LENGTH_LONG).show(); clearCache(); @@ -406,7 +360,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider String text = ((EditText)view.findViewById(R.id.BugMessage)).getText().toString(); String author = ((EditText)view.findViewById(R.id.AuthorName)).getText().toString(); ((OsmandApplication) OsmBugsLayer.this.activity.getApplication()).getSettings().USER_OSM_BUG_NAME.set(author); - boolean added = addingComment(bug.getId(), text, author); + boolean added = osmbugsUtil.addingComment(bug.getId(), text, author); if (added) { AccessibleToast.makeText(activity, activity.getResources().getString(R.string.osb_comment_dialog_success), Toast.LENGTH_LONG).show(); clearCache(); @@ -438,7 +392,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider @Override public void onClick(DialogInterface dialog, int which) { OpenStreetBug bug = (OpenStreetBug) args.getSerializable(KEY_BUG); - boolean closed = closingBug(bug.getId()); + boolean closed = osmbugsUtil.closingBug(bug.getId()); if (closed) { AccessibleToast.makeText(activity, activity.getString(R.string.osb_close_dialog_success), Toast.LENGTH_LONG).show(); clearCache(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java new file mode 100644 index 0000000000..7a05683ca5 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java @@ -0,0 +1,66 @@ +package net.osmand.plus.osmedit; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; + +import net.osmand.LogUtil; + +import org.apache.commons.logging.Log; + +public class OsmBugsRemoteUtil implements OsmBugsUtil { + + private static final Log log = LogUtil.getLog(OsmBugsRemoteUtil.class); + + private final static String SITE_API = "http://openstreetbugs.schokokeks.org/api/0.1/"; //$NON-NLS-1$ + + public boolean createNewBug(double latitude, double longitude, String text, String authorName){ + StringBuilder b = new StringBuilder(); + b.append(SITE_API).append("addPOIexec?"); //$NON-NLS-1$ + b.append("lat=").append(latitude); //$NON-NLS-1$ + b.append("&lon=").append(longitude); //$NON-NLS-1$ + text = text + " [" + authorName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ + b.append("&text=").append(URLEncoder.encode(text)); //$NON-NLS-1$ + b.append("&name=").append(URLEncoder.encode(authorName)); //$NON-NLS-1$ + return editingPOI(b.toString(), "creating bug"); //$NON-NLS-1$ + } + + public boolean addingComment(long id, String text, String authorName){ + StringBuilder b = new StringBuilder(); + b.append(SITE_API).append("editPOIexec?"); //$NON-NLS-1$ + b.append("id=").append(id); //$NON-NLS-1$ + text = text + " [" + authorName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ + b.append("&text=").append(URLEncoder.encode(text)); //$NON-NLS-1$ + b.append("&name=").append(URLEncoder.encode(authorName)); //$NON-NLS-1$ + return editingPOI(b.toString(), "adding comment"); //$NON-NLS-1$ + } + + public boolean closingBug(long id){ + StringBuilder b = new StringBuilder(); + b.append(SITE_API).append("closePOIexec?"); //$NON-NLS-1$ + b.append("id=").append(id); //$NON-NLS-1$ + return editingPOI(b.toString(),"closing bug"); //$NON-NLS-1$ + } + + private boolean editingPOI(String urlStr, String debugAction){ + try { + log.debug("Action " + debugAction + " " + urlStr); //$NON-NLS-1$ //$NON-NLS-2$ + URL url = new URL(urlStr); + URLConnection connection = url.openConnection(); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + while(reader.readLine() != null){ + } + log.debug("Action " + debugAction + " successfull"); //$NON-NLS-1$ //$NON-NLS-2$ + return true; + } catch (IOException e) { + log.error("Error " +debugAction, e); //$NON-NLS-1$ + } catch (RuntimeException e) { + log.error("Error "+debugAction, e); //$NON-NLS-1$ + } + return false; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java new file mode 100644 index 0000000000..467e0e4f1a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsUtil.java @@ -0,0 +1,10 @@ +package net.osmand.plus.osmedit; + +public interface OsmBugsUtil { + + public boolean createNewBug(double latitude, double longitude, String text, String authorName); + + public boolean addingComment(long id, String text, String authorName); + + public boolean closingBug(long id); +}