use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest 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();
}
}
});
}
use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest in project osm-contributor by jawg.
the class PhotoActivity method setPhotoLocation.
private void setPhotoLocation(final String photoId) {
if (flickrPhotoClient == null) {
flickrPhotoClient = FlickrPhotoUtils.getAdapter().create(FlickrPhotoClient.class);
}
OAuthRequest oauthRequest = new OAuthRequest(configManager.getFlickrApiKey(), configManager.getFlickrApiKeySecret());
oauthRequest.setRequestUrl(FLICKR_API_SERVICES);
oauthRequest.setOAuthToken(configManager.getFlickrToken());
oauthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
oauthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, configManager.getFlickrToken()).put("method", "flickr.photos.geo.setLocation").put("photo_id", photoId).put("lat", String.valueOf(latitude)).put("lon", String.valueOf(longitude)).toMap());
oauthRequest.signRequest(Verb.GET);
flickrPhotoClient.setProperties(oauthRequest.getParams()).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
try {
setPhotoTag(photoId);
} catch (UnsupportedEncodingException e) {
onFailure(call, new RuntimeException(e));
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(PhotoActivity.this, R.string.picture_sent_failure, Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
});
}
use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest in project osm-contributor by jawg.
the class PhotoActivity method onUploadFinishedEvent.
public void onUploadFinishedEvent(final String photoId) {
// Retrieve picture URL
if (flickrPhotoClient == null) {
flickrPhotoClient = FlickrPhotoUtils.getAdapter().create(FlickrPhotoClient.class);
}
OAuthRequest oauthRequest = new OAuthRequest(configManager.getFlickrApiKey(), configManager.getFlickrApiKeySecret());
oauthRequest.setRequestUrl(FLICKR_API_SERVICES);
oauthRequest.setOAuthToken(configManager.getFlickrToken());
oauthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
oauthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, configManager.getFlickrToken()).put("method", "flickr.photos.getInfo").put("photo_id", photoId).toMap());
oauthRequest.signRequest(Verb.GET);
flickrPhotoClient.setProperties(oauthRequest.getParams()).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
defineImageOnPoi(ResponseConverter.convertImageUrl(response.body()));
setPhotoLocation(photoId);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(PhotoActivity.this, R.string.flickr_communication_failure, Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
});
}
use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest in project osm-contributor by jawg.
the class PhotoActivity method uploadPhoto.
private void uploadPhoto() {
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_UPLOAD);
oAuthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, oAuthRequest.getOAuthToken()).toMap());
oAuthRequest.signRequest(Verb.POST);
progressDialog.show();
if (flickrUploadClient == null) {
flickrUploadClient = FlickrUploadUtils.getRestAdapter(oAuthRequest.getParams()).create(FlickrUploadClient.class);
}
RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(Uri.fromFile(photoFile))), photoFile);
flickrUploadClient.upload(MultipartBody.Part.createFormData("photo", photoFile.getName(), requestFile)).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
onUploadFinishedEvent(ResponseConverter.convertImageId(response.body()));
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
if (nbTry < MAX_RETRY_UPLOAD) {
nbTry++;
uploadPhoto();
} else {
nbTry = 0;
Toast.makeText(PhotoActivity.this, R.string.flickr_communication_failure, Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
Aggregations