diff --git a/OsmAnd/res/layout/osm_edit_list_item.xml b/OsmAnd/res/layout/osm_edit_list_item.xml new file mode 100644 index 0000000000..a6c7d94a5a --- /dev/null +++ b/OsmAnd/res/layout/osm_edit_list_item.xml @@ -0,0 +1,32 @@ + + + + + + + + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 4b1768d0a2..559a13205e 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -2222,4 +2222,9 @@ Afghanistan, Albania, Algeria, Andorra, Angola, Anguilla, Antigua and Barbuda, A Please give OsmAnd a score on Google Play Tell us why. Please tell us what would you want to change in this app. + Failed to upload + Delete change + Successfully uploaded {0}/{1} + Try again + Error: {0} diff --git a/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java index ee1876d123..4c2cc7f19a 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/DashOsmEditsFragment.java @@ -1,16 +1,5 @@ package net.osmand.plus.osmedit; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; - -import net.osmand.access.AccessibleToast; -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 android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; @@ -22,13 +11,23 @@ import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; -import android.widget.Toast; + +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 java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * Created by Denis * on 20.01.2015. */ -public class DashOsmEditsFragment extends DashBaseFragment implements OsmEditsUploadListener { +public class DashOsmEditsFragment extends DashBaseFragment { public static final String TAG = "DASH_OSM_EDITS_FRAGMENT"; OsmEditingPlugin plugin; @@ -59,19 +58,19 @@ public class DashOsmEditsFragment extends DashBaseFragment implements OsmEditsUp if (plugin == null) { plugin = OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class); } - setupEditings(); + setupEditings(); } - + private void setupEditings() { View mainView = getView(); - if (plugin == null){ + if (plugin == null) { mainView.setVisibility(View.GONE); return; } ArrayList dataPoints = new ArrayList<>(); getOsmPoints(dataPoints); - if (dataPoints.size() == 0){ + if (dataPoints.size() == 0) { mainView.setVisibility(View.GONE); return; } else { @@ -114,7 +113,7 @@ public class DashOsmEditsFragment extends DashBaseFragment implements OsmEditsUp } } - private void uploadItem(final OsmPoint point){ + private void uploadItem(final OsmPoint point) { AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); b.setMessage(getString(R.string.local_osm_changes_upload_all_confirm, 1)); b.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @@ -129,13 +128,31 @@ public class DashOsmEditsFragment extends DashBaseFragment implements OsmEditsUp private void showProgressDialog(OsmPoint point) { OpenstreetmapRemoteUtil remotepoi = new OpenstreetmapRemoteUtil(getActivity()); - OsmPoint[] toUpload = new OsmPoint[] { point }; + OsmPoint[] toUpload = new OsmPoint[]{point}; OsmBugsRemoteUtil remotebug = new OsmBugsRemoteUtil(getMyApplication()); ProgressDialog dialog = ProgressImplementation.createProgressDialog(getActivity(), getString(R.string.uploading), getString(R.string.local_openstreetmap_uploading), ProgressDialog.STYLE_HORIZONTAL).getDialog(); + OsmEditsUploadListener listener = new OsmEditsUploadListenerHelper(getActivity(), + getString(R.string.local_openstreetmap_were_uploaded)) { + @Override + public void uploadUpdated(OsmPoint point) { + super.uploadUpdated(point); + if (!DashOsmEditsFragment.this.isDetached()) { + onOpenDash(); + } + } + + @Override + public void uploadEnded(Map loadErrorsMap) { + super.uploadEnded(loadErrorsMap); + if (!DashOsmEditsFragment.this.isDetached()) { + onOpenDash(); + } + } + }; UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask(dialog, - DashOsmEditsFragment.this, plugin, remotepoi, remotebug, toUpload.length); + listener, plugin, remotepoi, remotebug, toUpload.length); uploadTask.execute(toUpload); dialog.show(); } @@ -143,9 +160,9 @@ public class DashOsmEditsFragment extends DashBaseFragment implements OsmEditsUp private void getOsmPoints(ArrayList dataPoints) { List l1 = plugin.getDBPOI().getOpenstreetmapPoints(); List l2 = plugin.getDBBug().getOsmbugsPoints(); - if (l1.isEmpty()){ + if (l1.isEmpty()) { int i = 0; - for(OsmPoint point : l2){ + for (OsmPoint point : l2) { if (i > 2) { break; } @@ -154,7 +171,7 @@ public class DashOsmEditsFragment extends DashBaseFragment implements OsmEditsUp } } else if (l2.isEmpty()) { int i = 0; - for(OsmPoint point : l1){ + for (OsmPoint point : l1) { if (i > 2) { break; } @@ -164,30 +181,11 @@ public class DashOsmEditsFragment extends DashBaseFragment implements OsmEditsUp } else { dataPoints.add(l1.get(0)); dataPoints.add(l2.get(0)); - if (l1.size() > 1){ + if (l1.size() > 1) { dataPoints.add(l1.get(1)); - } else if (l2.size() > 1){ + } else if (l2.size() > 1) { dataPoints.add(l2.get(1)); } } } - - @Override - public void uploadUpdated(OsmPoint point) { - if (!this.isDetached()){ - onOpenDash(); - } - } - - @Override - public void uploadEnded(Integer result) { - if (result != null) { - AccessibleToast.makeText(getActivity(), - MessageFormat.format(getString(R.string.local_openstreetmap_were_uploaded), result), Toast.LENGTH_LONG) - .show(); - } - if (!this.isDetached()){ - onOpenDash(); - } - } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapPoint.java b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapPoint.java index f16233db5c..b757b22d41 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapPoint.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapPoint.java @@ -1,11 +1,9 @@ package net.osmand.plus.osmedit; -import java.io.Serializable; - import net.osmand.osm.edit.Node; import net.osmand.osm.edit.OSMSettings.OSMTagKey; -public class OpenstreetmapPoint extends OsmPoint implements Serializable { +public class OpenstreetmapPoint extends OsmPoint { private static final long serialVersionUID = 729654300829771467L; private Node entity; private String comment; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java index 8e8a90ff47..d77e0852db 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java @@ -1,20 +1,8 @@ package net.osmand.plus.osmedit; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.StringWriter; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.text.MessageFormat; -import java.util.LinkedHashMap; -import java.util.Map; +import android.content.Context; +import android.util.Xml; +import android.widget.Toast; import net.osmand.PlatformUtil; import net.osmand.access.AccessibleToast; @@ -37,9 +25,21 @@ import org.apache.commons.logging.Log; import org.xml.sax.SAXException; import org.xmlpull.v1.XmlSerializer; -import android.content.Context; -import android.util.Xml; -import android.widget.Toast; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.StringWriter; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.text.MessageFormat; +import java.util.LinkedHashMap; +import java.util.Map; public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { @@ -335,11 +335,7 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { return entityInfo; } - } catch (IOException e) { - log.error("Loading node failed " + nodeId, e); //$NON-NLS-1$ - AccessibleToast.makeText(ctx, ctx.getResources().getString(R.string.shared_string_io_error), - Toast.LENGTH_LONG).show(); - } catch (SAXException e) { + } catch (IOException | SAXException e) { log.error("Loading node failed " + nodeId, e); //$NON-NLS-1$ AccessibleToast.makeText(ctx, ctx.getResources().getString(R.string.shared_string_io_error), Toast.LENGTH_LONG).show(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java index 13876ebce9..5dd010e487 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java @@ -1,14 +1,5 @@ package net.osmand.plus.osmedit; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLEncoder; - import net.osmand.PlatformUtil; import net.osmand.osm.io.Base64; import net.osmand.osm.io.NetworkUtils; @@ -19,36 +10,42 @@ import net.osmand.plus.Version; import org.apache.commons.logging.Log; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URLEncoder; + public class OsmBugsRemoteUtil implements OsmBugsUtil { private static final Log log = PlatformUtil.getLog(OsmBugsRemoteUtil.class); - - static String getNotesApi() - { + + static String getNotesApi() { final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; - + String RETURN_API; - + if (deviceApiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) { RETURN_API = "https://api.openstreetmap.org/api/0.6/notes"; - } - else { + } else { RETURN_API = "http://api.openstreetmap.org/api/0.6/notes"; } - + return RETURN_API; } private OsmandApplication app; private OsmandSettings settings; - + public OsmBugsRemoteUtil(OsmandApplication app) { this.app = app; settings = app.getSettings(); } @Override - public String createNewBug(double latitude, double longitude, String text, String author){ + public String createNewBug(double latitude, double longitude, String text, String author) { StringBuilder b = new StringBuilder(); b.append(getNotesApi()).append("?"); //$NON-NLS-1$ b.append("lat=").append(latitude); //$NON-NLS-1$ @@ -58,21 +55,21 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { } @Override - public String addingComment(long id, String text, String author){ + public String addingComment(long id, String text, String author) { StringBuilder b = new StringBuilder(); - b.append(getNotesApi()).append("/"); + b.append(getNotesApi()).append("/"); b.append(id); //$NON-NLS-1$ b.append("/comment?text=").append(URLEncoder.encode(text)); //$NON-NLS-1$ return editingPOI(b.toString(), "POST", "adding comment"); //$NON-NLS-1$ } @Override - public String closingBug(long id, String text, String author){ + public String closingBug(long id, String text, String author) { StringBuilder b = new StringBuilder(); - b.append(getNotesApi()).append("/"); + b.append(getNotesApi()).append("/"); b.append(id); //$NON-NLS-1$ b.append("/close?text=").append(URLEncoder.encode(text)); //$NON-NLS-1$ - return editingPOI(b.toString(), "POST", "close bug") ; //$NON-NLS-1$ + return editingPOI(b.toString(), "POST", "close bug"); //$NON-NLS-1$ } private String editingPOI(String url, String requestMethod, String userOperation) { @@ -104,26 +101,24 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { boolean ok = connection.getResponseCode() == HttpURLConnection.HTTP_OK; log.info(msg); //$NON-NLS-1$ // populate return fields. - + StringBuilder responseBody = new StringBuilder(); - if (true) { - responseBody.setLength(0); - InputStream i = connection.getInputStream(); - if (i != null) { - BufferedReader in = new BufferedReader(new InputStreamReader(i, "UTF-8"), 256); //$NON-NLS-1$ - String s; - boolean f = true; - while ((s = in.readLine()) != null) { - if (!f) { - responseBody.append("\n"); //$NON-NLS-1$ - } else { - f = false; - } - responseBody.append(s); + responseBody.setLength(0); + InputStream i = connection.getInputStream(); + if (i != null) { + BufferedReader in = new BufferedReader(new InputStreamReader(i, "UTF-8"), 256); //$NON-NLS-1$ + String s; + boolean f = true; + while ((s = in.readLine()) != null) { + if (!f) { + responseBody.append("\n"); //$NON-NLS-1$ + } else { + f = false; } + responseBody.append(s); } - log.info("Response : " + responseBody); //$NON-NLS-1$ } + log.info("Response : " + responseBody); //$NON-NLS-1$ connection.disconnect(); if (!ok) { return msg + "\n" + responseBody; @@ -138,7 +133,7 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { return e.getMessage() + ""; } catch (IOException e) { log.error(userOperation + " " + app.getString(R.string.failed_op), e); //$NON-NLS-1$ - return e.getMessage() + ""; + return e.getMessage() + " link unavailable"; } return null; } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index a3f2c652f8..07713a50b2 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -1,12 +1,29 @@ package net.osmand.plus.osmedit; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.v4.view.MenuItemCompat; +import android.support.v7.view.ActionMode; +import android.support.v7.widget.PopupMenu; +import android.util.Xml; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +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; @@ -25,37 +42,18 @@ import net.osmand.plus.myplaces.FavoritesActivity; import org.xmlpull.v1.XmlSerializer; -import android.app.AlertDialog; -import android.app.ProgressDialog; -import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; -import android.os.AsyncTask; -import android.os.Bundle; -import android.support.v4.view.MenuItemCompat; -import android.support.v7.app.ActionBarActivity; -import android.support.v7.view.ActionMode; -import android.support.v7.widget.PopupMenu; -import android.util.Xml; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.ArrayAdapter; -import android.widget.CheckBox; -import android.widget.ImageView; -import android.widget.TextView; -import android.widget.Toast; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** * Created by Denis * on 06.03.2015. */ -public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUploadListener { +public class OsmEditsFragment extends OsmAndListFragment { OsmEditingPlugin plugin; private ArrayList dataPoints; private OsmEditsAdapter listAdapter; @@ -100,17 +98,17 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo return view; } - private void selectAll(){ - for(int i =0 ;i < listAdapter.getCount(); i++){ + private void selectAll() { + for (int i = 0; i < listAdapter.getCount(); i++) { OsmPoint point = listAdapter.getItem(i); - if (!osmEditsSelected.contains(point)){ + if (!osmEditsSelected.contains(point)) { osmEditsSelected.add(point); } } listAdapter.notifyDataSetInvalidated(); } - private void deselectAll(){ + private void deselectAll() { osmEditsSelected.clear(); listAdapter.notifyDataSetInvalidated(); } @@ -199,8 +197,8 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo }); } - private void enterSelectionMode(int type){ - switch (type){ + private void enterSelectionMode(int type) { + switch (type) { case MODE_DELETE: enterDeleteMode(); break; @@ -256,10 +254,10 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo refreshSelectAll(); } - private void updateSelectionTitle(ActionMode m){ - if(osmEditsSelected.size() > 0) { + private void updateSelectionTitle(ActionMode m) { + if (osmEditsSelected.size() > 0) { m.setTitle(osmEditsSelected.size() + " " + getMyApplication().getString(R.string.shared_string_selected_lowercase)); - } else{ + } else { m.setTitle(""); } } @@ -282,8 +280,8 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo private void enableSelectionMode(boolean selectionMode) { this.selectionMode = selectionMode; - getView().findViewById(R.id.select_all).setVisibility(selectionMode? View.VISIBLE : View.GONE); - ((FavoritesActivity)getActivity()).setToolbarVisibility(!selectionMode); + getView().findViewById(R.id.select_all).setVisibility(selectionMode ? View.VISIBLE : View.GONE); + ((FavoritesActivity) getActivity()).setToolbarVisibility(!selectionMode); } public OsmandActionBarActivity getActionBarActivity() { @@ -389,7 +387,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo final CheckBox ch = (CheckBox) v.findViewById(R.id.check_local_index); View options = v.findViewById(R.id.options); - if(selectionMode) { + if (selectionMode) { options.setVisibility(View.GONE); ch.setVisibility(View.VISIBLE); ch.setChecked(osmEditsSelected.contains(child)); @@ -482,7 +480,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo return (OsmandApplication) getActivity().getApplication(); } - private void uploadItems(final OsmPoint[] items){ + private void uploadItems(final OsmPoint[] items) { AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); b.setMessage(getString(R.string.local_osm_changes_upload_all_confirm, items.length)); b.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @@ -502,8 +500,10 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo getString(R.string.uploading), getString(R.string.local_openstreetmap_uploading), ProgressDialog.STYLE_HORIZONTAL).getDialog(); - UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask(dialog, this, plugin, remotepoi, - remotebug, toUpload.length); + OsmEditsUploadListener listener = new OsmEditsUploadListenerHelper(getActivity(), + getString(R.string.local_openstreetmap_were_uploaded)); + UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask( + dialog, listener, plugin, remotepoi, remotebug, toUpload.length); uploadTask.execute(toUpload); dialog.show(); @@ -614,21 +614,6 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo } } - @Override - public void uploadUpdated(OsmPoint point) { - listAdapter.delete(point); - } - - @Override - public void uploadEnded(Integer result) { - listAdapter.notifyDataSetChanged(); - if (result != null) { - AccessibleToast.makeText(getActivity(), - MessageFormat.format(getString(R.string.local_openstreetmap_were_uploaded), result), Toast.LENGTH_LONG) - .show(); - } - } - private void showOnMap(OsmPoint osmPoint) { boolean isOsmPoint = osmPoint instanceof OpenstreetmapPoint; String type = osmPoint.getGroup() == OsmPoint.Group.POI ? PointDescription.POINT_TYPE_POI : PointDescription.POINT_TYPE_OSM_BUG; @@ -637,6 +622,4 @@ public class OsmEditsFragment extends OsmAndListFragment implements OsmEditsUplo new PointDescription(type, name), true, osmPoint); //$NON-NLS-1$ MapActivity.launchMapActivityMoveToTop(getActivity()); } - - } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListener.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListener.java index 2edf57a300..f84d5c765e 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListener.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListener.java @@ -1,12 +1,12 @@ package net.osmand.plus.osmedit; +import java.util.Map; + /** * Created by Denis * on 11.03.2015. */ public interface OsmEditsUploadListener { - - public void uploadUpdated(OsmPoint point); - - public void uploadEnded(Integer result); + void uploadUpdated(OsmPoint point); + void uploadEnded(Map loadErrorsMap); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListenerHelper.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListenerHelper.java new file mode 100644 index 0000000000..9210c5e2fe --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsUploadListenerHelper.java @@ -0,0 +1,285 @@ +package net.osmand.plus.osmedit; + +import android.app.Activity; +import android.app.Dialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.content.res.Resources; +import android.os.Bundle; +import android.support.annotation.MainThread; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v7.app.AlertDialog; +import android.text.TextUtils; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +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 java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Map; + +public class OsmEditsUploadListenerHelper implements OsmEditsUploadListener { + public static final String TAG = "OsmEditUploadListenerHe"; + private final FragmentActivity activity; + private final String numberFormat; + + public OsmEditsUploadListenerHelper(FragmentActivity activity, String numberFormat) { + this.activity = activity; + this.numberFormat = numberFormat; + } + + @Override + public void uploadUpdated(OsmPoint point) { + } + + @MainThread + @Override + public void uploadEnded(Map loadErrorsMap) { + int uploaded = 0; + int pointsNum = loadErrorsMap.keySet().size(); + for (OsmPoint point : loadErrorsMap.keySet()) { + if (loadErrorsMap.get(point) == null) { + uploaded++; + } + } + if (uploaded == pointsNum) { + AccessibleToast.makeText(activity, + MessageFormat.format(numberFormat, uploaded), + Toast.LENGTH_LONG) + .show(); + } else if (pointsNum == 1) { + Log.v(TAG, "in if1"); + OsmPoint point = loadErrorsMap.keySet().iterator().next(); + String message = loadErrorsMap.get(point); + DialogFragment dialogFragment = + UploadingErrorDialogFragment.getInstance(message, point); + dialogFragment.show(activity.getSupportFragmentManager(), "error_loading"); + } else { + UploadingMultipleErrorDialogFragment dialogFragment = + UploadingMultipleErrorDialogFragment.createInstance(loadErrorsMap); + dialogFragment.show(activity.getSupportFragmentManager(), "multiple_error_loading"); + + } + } + + private static void showUploadItemsProgressDialog(Fragment fragment, OsmPoint[] toUpload) { + FragmentActivity activity = fragment.getActivity(); + OsmEditingPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class); + OpenstreetmapRemoteUtil remotepoi = new OpenstreetmapRemoteUtil(activity); + OsmBugsRemoteUtil remotebug = new OsmBugsRemoteUtil((OsmandApplication) activity.getApplication()); + + OsmEditsUploadListenerHelper helper = new OsmEditsUploadListenerHelper(activity, + 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(); + UploadOpenstreetmapPointAsyncTask uploadTask = new UploadOpenstreetmapPointAsyncTask( + dialog, helper, plugin, remotepoi, remotebug, toUpload.length); + uploadTask.execute(toUpload); + + dialog.show(); + } + + public static final class UploadingErrorDialogFragment extends DialogFragment { + private static final String ERROR_MESSAGE = "error_message"; + private static final String POINT = "point"; + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Bundle argument = getArguments(); + String errorMessage = argument.getString(ERROR_MESSAGE); + final OsmPoint point = (OsmPoint) argument.getSerializable(POINT); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(getResources().getString(R.string.failed_to_upload)) + .setMessage(MessageFormat.format( + getResources().getString(R.string.error_message_pattern), errorMessage)) + .setPositiveButton(R.string.shared_string_ok, null) + .setNeutralButton(getResources().getString(R.string.delete_change), + new DialogInterface.OnClickListener() { + public void onClick(@Nullable DialogInterface dialog, int id) { + OsmEditingPlugin plugin = + OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class); + if (point.getGroup() == OsmPoint.Group.BUG) { + plugin.getDBBug().deleteAllBugModifications( + (OsmNotesPoint) point); + } else if (point.getGroup() == OsmPoint.Group.POI) { + plugin.getDBPOI().deletePOI((OpenstreetmapPoint) point); + } + } + }); + return builder.create(); + } + + public static UploadingErrorDialogFragment getInstance(String errorMessage, + OsmPoint point) { + UploadingErrorDialogFragment fragment = new UploadingErrorDialogFragment(); + Bundle bundle = new Bundle(); + bundle.putString(ERROR_MESSAGE, errorMessage); + bundle.putSerializable(POINT, point); + fragment.setArguments(bundle); + return fragment; + } + } + + public static final class UploadingMultipleErrorDialogFragment extends DialogFragment { + private static final String HAS_ERROR = "has_error"; + private static final String POINT_NAMES = "point_names"; + private static final String POINTS_WITH_ERRORS = "points_with_errors"; + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Bundle arguments = getArguments(); + String[] pointNames = arguments.getStringArray(POINT_NAMES); + boolean[] hasErrors = arguments.getBooleanArray(HAS_ERROR); + final OsmPoint[] points = (OsmPoint[]) arguments.getSerializable(POINTS_WITH_ERRORS); + int successfulUploads = 0; + for (boolean hasError : hasErrors) { + if (!hasError) { + successfulUploads++; + } + } + PointsWithErrorsAdapter adapter = + PointsWithErrorsAdapter.createInstance(getActivity(), pointNames, hasErrors); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(MessageFormat.format(getResources() + .getString(R.string.successfully_uploaded_pattern), + successfulUploads, hasErrors.length)) + .setAdapter(adapter, null) + .setPositiveButton(R.string.shared_string_ok, null) + .setNeutralButton(getResources().getString(R.string.try_again), + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + OsmEditsUploadListenerHelper + .showUploadItemsProgressDialog( + UploadingMultipleErrorDialogFragment.this, + points); + } + }); + return builder.create(); + } + + public static UploadingMultipleErrorDialogFragment createInstance( + Map loadErrorsMap) { + String[] pointNames = new String[loadErrorsMap.keySet().size()]; + boolean[] hasErrors = new boolean[loadErrorsMap.keySet().size()]; + ArrayList pointsWithErrors = new ArrayList<>(); + int i = 0; + for (OsmPoint point : loadErrorsMap.keySet()) { + pointNames[i] = point.getGroup() == OsmPoint.Group.BUG ? + ((OsmNotesPoint) point).getText() : + ((OpenstreetmapPoint) point).getName(); + pointNames[i] = TextUtils.isEmpty(pointNames[i]) ? + "id:" + point.getId() : pointNames[i]; + hasErrors[i] = loadErrorsMap.get(point) != null; + if (hasErrors[i]) { + pointsWithErrors.add(point); + } + i++; + } + + if (pointNames.length != hasErrors.length) { + throw new IllegalArgumentException("pointNames and hasError arrays " + + "must me of equal length"); + } + UploadingMultipleErrorDialogFragment fragment = + new UploadingMultipleErrorDialogFragment(); + Bundle bundle = new Bundle(); + bundle.putSerializable(POINTS_WITH_ERRORS, + pointsWithErrors.toArray(new OsmPoint[pointsWithErrors.size()])); + bundle.putStringArray(POINT_NAMES, pointNames); + bundle.putBooleanArray(HAS_ERROR, hasErrors); + fragment.setArguments(bundle); + return fragment; + } + } + + private static final class PointWithPotentialError { + String point; + boolean hasError; + + public PointWithPotentialError(String point, boolean hasError) { + this.point = point; + this.hasError = hasError; + } + } + + private static final class PointsWithErrorsAdapter extends ArrayAdapter { + private final int layoutResourceId; + PointWithPotentialError[] data; + Activity context; + + private PointsWithErrorsAdapter(Activity context, int layoutResourceId, + PointWithPotentialError[] objects) { + super(context, layoutResourceId, objects); + data = objects; + this.context = context; + this.layoutResourceId = layoutResourceId; + } + + @Override + public View getView(int position, View convertView, @NonNull ViewGroup parent) { + View row = convertView; + PointHolder holder = null; + + if (row == null) { + LayoutInflater inflater = context.getLayoutInflater(); + row = inflater.inflate(layoutResourceId, parent, false); + + holder = new PointHolder(); + holder.checkedUncheckedImageView = (ImageView) row.findViewById(R.id.iconImageView); + holder.pointNameTextView = (TextView) row.findViewById(R.id.nameTextView); + + row.setTag(holder); + } else { + holder = (PointHolder) row.getTag(); + } + + PointWithPotentialError pointWrapper = data[position]; + holder.pointNameTextView.setText(pointWrapper.point); + IconsCache cache = ((OsmandApplication) context.getApplication()).getIconsCache(); + holder.checkedUncheckedImageView.setImageDrawable(pointWrapper.hasError ? + cache.getContentIcon(R.drawable.ic_action_remove_dark) : + cache.getContentIcon(R.drawable.ic_action_done)); + + return row; + } + + public static PointsWithErrorsAdapter createInstance(Activity activity, + String[] pointNames, + boolean[] hasError) { + PointWithPotentialError[] array = new PointWithPotentialError[pointNames.length]; + for (int i = 0; i < pointNames.length; i++) { + array[i] = new PointWithPotentialError(pointNames[i], hasError[i]); + } + return new PointsWithErrorsAdapter(activity, R.layout.osm_edit_list_item, array); + } + + private static class PointHolder { + TextView pointNameTextView; + ImageView checkedUncheckedImageView; + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmNotesPoint.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmNotesPoint.java index 0904419ff6..da72273750 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmNotesPoint.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmNotesPoint.java @@ -1,8 +1,6 @@ package net.osmand.plus.osmedit; -import java.io.Serializable; - -public class OsmNotesPoint extends OsmPoint implements Serializable { +public class OsmNotesPoint extends OsmPoint { private static final long serialVersionUID = 729654300829771468L; private long id; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmPoint.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmPoint.java index 6b83d5e669..958d69e059 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmPoint.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmPoint.java @@ -1,9 +1,10 @@ package net.osmand.plus.osmedit; +import java.io.Serializable; import java.util.HashMap; import java.util.Map; -public abstract class OsmPoint { +public abstract class OsmPoint implements Serializable { public static enum Group {BUG, POI}; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java index be3756f9bd..f513892f31 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java @@ -3,47 +3,44 @@ package net.osmand.plus.osmedit; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.AsyncTask; -import android.support.v4.app.Fragment; import net.osmand.osm.edit.EntityInfo; import net.osmand.osm.edit.Node; +import java.util.HashMap; +import java.util.Map; + /** * Created by Denis * on 11.03.2015. */ -public class UploadOpenstreetmapPointAsyncTask extends AsyncTask { - +public class UploadOpenstreetmapPointAsyncTask + extends AsyncTask> { private ProgressDialog progress; - private OpenstreetmapRemoteUtil remotepoi; - private OsmBugsRemoteUtil remotebug; - - private int listSize = 0; - private boolean interruptUploading = false; - - private Fragment ctx; - + private OsmEditsUploadListener listener; private OsmEditingPlugin plugin; - public UploadOpenstreetmapPointAsyncTask(ProgressDialog progress,Fragment ctx, - OsmEditingPlugin plugin, - OpenstreetmapRemoteUtil remotepoi, OsmBugsRemoteUtil remotebug, + public UploadOpenstreetmapPointAsyncTask(ProgressDialog progress, + OsmEditsUploadListener listener, + OsmEditingPlugin plugin, + OpenstreetmapRemoteUtil remotepoi, + OsmBugsRemoteUtil remotebug, int listSize) { this.progress = progress; this.plugin = plugin; this.remotepoi = remotepoi; this.remotebug = remotebug; this.listSize = listSize; - this.ctx = ctx; + this.listener = listener; } @Override - protected Integer doInBackground(OsmPoint... points) { - int uploaded = 0; + protected Map doInBackground(OsmPoint... points) { + Map loadErrorsMap = new HashMap<>(); for (OsmPoint point : points) { if (interruptUploading) @@ -57,31 +54,29 @@ public class UploadOpenstreetmapPointAsyncTask extends AsyncTask loadErrorsMap) { progress.dismiss(); - if (ctx instanceof OsmEditsUploadListener){ - ((OsmEditsUploadListener)ctx).uploadEnded(result); - } + listener.uploadEnded(loadErrorsMap); } public void setInterruptUploading(boolean b) { @@ -113,10 +106,8 @@ public class UploadOpenstreetmapPointAsyncTask extends AsyncTask