use of io.jawg.osmcontributor.flickr.rest.FlickrAddTagClient in project osm-contributor by jawg.
the class PhotoActivity method setPhotoTag.
private void setPhotoTag(final String photoId) throws UnsupportedEncodingException {
// Create the machine tag for Flickr to associate picture to this Poi
Poi currentPoi = application.getOsmTemplateComponent().getPoiManager().queryForId(poiId);
StringBuilder flickrOsmTagBuilder = new StringBuilder(FLICKR_DEFAULT_TAG);
if (currentPoi.getBackendId() != null && currentPoi.getBackendId().length() > 0) {
flickrOsmTagBuilder.append(",osm:");
flickrOsmTagBuilder.append((currentPoi.getWay()) ? "way" : "node");
flickrOsmTagBuilder.append("=");
flickrOsmTagBuilder.append(currentPoi.getBackendId());
} else {
// TODO Handle pictures added on new POIs which haven't an OSM ID yet
// Such POIs might be sent to OSM first, and when an ID is defined, update the Flickr picture
}
String flickrOsmTag = URLEncoder.encode(flickrOsmTagBuilder.toString(), UTF_8);
OAuthRequest oauthRequest = flickrOAuth.getOAuthRequest();
if (oauthRequest == null) {
oauthRequest = new OAuthRequest(application.getFlickr().getApiKey(), application.getFlickr().getSharedSecret());
oauthRequest.setOAuthToken(configManager.getFlickrToken());
oauthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
flickrOAuth.setOAuthRequest(oauthRequest);
}
oauthRequest.setRequestUrl(FLICKR_API_SERVICES);
oauthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, oauthRequest.getOAuthToken()).put("method", FLICKR_METHOD_ADDTAGS).put("photo_id", photoId).put("tags", flickrOsmTag).toMap());
oauthRequest.signRequest(Verb.POST);
// Filter params for Authorization header
Map<String, String> oauthParams = new TreeMap<String, String>();
for (Map.Entry<String, String> entry : oauthRequest.getParams().entrySet()) {
if (entry.getKey().startsWith(PARAM_OAUTH_PREFIX)) {
oauthParams.put(entry.getKey(), entry.getValue());
}
}
flickrAddTagClient = FlickrPhotoUtils.getAdapter(oauthParams).create(FlickrAddTagClient.class);
flickrAddTagClient.addTags(new StringBuilder("method=").append(FLICKR_METHOD_ADDTAGS).append("&photo_id=").append(photoId).append("&tags=").append(flickrOsmTag).toString()).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
String responseAsString = response.body();
Log.d(TAG, responseAsString);
progressDialog.dismiss();
photoFile.delete();
Toast.makeText(PhotoActivity.this, R.string.picture_sent_success, Toast.LENGTH_LONG).show();
if (ImageAdapter.getPhotoUrlsCachedThumbs(poiId) == null || ImageAdapter.getPhotoUrlsCachedThumbs(poiId).isEmpty()) {
finish();
}
} else {
onFailure(call, new Exception());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
if (nbTry < MAX_RETRY_UPLOAD) {
nbTry++;
try {
setPhotoTag(photoId);
} catch (UnsupportedEncodingException e) {
Timber.e(e, e.getMessage());
}
} else {
nbTry = 0;
Toast.makeText(PhotoActivity.this, R.string.poi_association_failure, Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
Aggregations