-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathUploadToOpenStreetMapTask.java
More file actions
213 lines (183 loc) · 6.55 KB
/
UploadToOpenStreetMapTask.java
File metadata and controls
213 lines (183 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package net.osmtracker.osm;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.util.Log;
import androidx.preference.PreferenceManager;
import net.osmtracker.OSMTracker;
import net.osmtracker.R;
import net.osmtracker.db.DataHelper;
import net.osmtracker.db.model.Track;
import net.osmtracker.util.DialogUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import de.westnordost.osmapi.OsmConnection;
import de.westnordost.osmapi.common.errors.OsmAuthorizationException;
import de.westnordost.osmapi.common.errors.OsmBadUserInputException;
import de.westnordost.osmapi.traces.GpsTraceDetails;
import de.westnordost.osmapi.traces.GpsTracesApi;
/**
* Uploads a GPX file to OpenStreetMap
*
* @author Nicolas Guillaumin
*/
public class UploadToOpenStreetMapTask extends AsyncTask<Void, Void, Void> {
private static final String TAG = UploadToOpenStreetMapTask.class.getSimpleName();
/** Upload progress dialog */
private ProgressDialog dialog;
private final Activity activity;
private final String accessToken;
private final long trackId;
/** File to export */
private final File gpxFile;
/** Filename to use when uploading */
private final String filename;
/** Track description */
private final String description;
/** Track tags */
private final String tags;
/** Track visibility */
private final Track.OSMVisibility visibility;
/**
* Error message, or text of the response returned by OSM
* if the request completed
*/
private String errorMsg;
/**
* Either the HTTP result code, or -1 for an internal error
*/
private int resultCode = -1;
private final int authorizationErrorResultCode = -2;
private final int anotherErrorResultCode = -3;
private final int okResultCode = 1;
public UploadToOpenStreetMapTask(Activity activity, String accessToken, long trackId,
File gpxFile, String filename, String description, String tags,
Track.OSMVisibility visibility) {
this.activity = activity;
this.accessToken = accessToken;
this.trackId = trackId;
this.filename = filename;
this.gpxFile = gpxFile;
this.description = (description == null) ? "test" : description;
this.tags = (tags == null) ? "test" : tags;
this.visibility = (visibility == null) ? Track.OSMVisibility.Private : visibility;
}
@Override
protected void onPreExecute() {
try {
// Display progress dialog
dialog = new ProgressDialog(activity);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setIndeterminate(true);
dialog.setTitle(
activity.getResources().getString(R.string.osm_upload_sending)
.replace("{0}", Long.toString(trackId)));
dialog.setCancelable(false);
dialog.show();
} catch (Exception e) {
Log.e(TAG, "onPreExecute() failed", e);
errorMsg = e.getLocalizedMessage();
cancel(true);
}
}
@Override
protected void onPostExecute(Void result) {
switch (resultCode) {
case -1:
dialog.dismiss();
// Internal error, the request didn't start at all
DialogUtils.showErrorDialog(activity,
activity.getResources().getString(R.string.osm_upload_error)
+ ": " + errorMsg);
break;
case okResultCode:
dialog.dismiss();
// Success ! Update database and close activity
DataHelper.setTrackUploadDate(trackId, System.currentTimeMillis(), activity.getContentResolver());
new AlertDialog.Builder(activity)
.setIcon(android.R.drawable.ic_dialog_info)
.setMessage(R.string.osm_upload_sucess)
.setCancelable(true)
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
activity.finish();
}
}).create().show();
break;
case authorizationErrorResultCode:
dialog.dismiss();
// Authorization issue. Provide a way to clear credentials
new AlertDialog.Builder(activity)
.setTitle(android.R.string.dialog_alert_title)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.osm_upload_unauthorized)
.setCancelable(true)
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Editor editor = PreferenceManager.getDefaultSharedPreferences(activity).edit();
editor.remove(OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN);
editor.commit();
dialog.dismiss();
}
}).create().show();
default:
// Another error. Display OSM response
dialog.dismiss();
// Internal error, the request didn't start at all
DialogUtils.showErrorDialog(activity,
activity.getResources().getString(R.string.osm_upload_error)
+ ": " + errorMsg);
}
}
protected GpsTracesApi createGpsTracesApi(OsmConnection connection) {
return new GpsTracesApi(connection);
}
@Override
protected Void doInBackground(Void... params) {
OsmConnection osm = new OsmConnection(OpenStreetMapConstants.Api.OSM_API_URL_PATH,
OpenStreetMapConstants.OAuth2.USER_AGENT, accessToken);
List<String> tags = new ArrayList<>();
tags.add(this.tags);
try (InputStream is = new FileInputStream(gpxFile)) {
long gpxAPI = createGpsTracesApi(osm).create(filename, getVisibilityForOsmapi(visibility),
description, tags, is);
Log.v(TAG, "Gpx file uploaded. GPX id: " + gpxAPI);
resultCode = okResultCode;
} catch (IOException | IllegalArgumentException | OsmBadUserInputException e) {
Log.d(TAG, e.getMessage());
resultCode = -1; //internal error.
} catch (OsmAuthorizationException oae) {
Log.d(TAG, "OsmAuthorizationException");
resultCode = authorizationErrorResultCode;
} catch (Exception e) {
Log.e(TAG, e.getMessage());
resultCode = anotherErrorResultCode;
}
return null;
}
private GpsTraceDetails.Visibility getVisibilityForOsmapi(Track.OSMVisibility visibility) {
switch (visibility) {
case Private: return GpsTraceDetails.Visibility.PRIVATE;
case Public: return GpsTraceDetails.Visibility.PUBLIC;
case Trackable: return GpsTraceDetails.Visibility.TRACKABLE;
case Identifiable: return GpsTraceDetails.Visibility.IDENTIFIABLE;
}
return null;
}
}