Search in sources :

Example 1 with FlickrAddTagClient

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();
            }
        }
    });
}
Also used : OAuthRequest(io.jawg.osmcontributor.flickr.oauth.OAuthRequest) FlickrAddTagClient(io.jawg.osmcontributor.flickr.rest.FlickrAddTagClient) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Poi(io.jawg.osmcontributor.model.entities.Poi) TreeMap(java.util.TreeMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Aggregations

OAuthRequest (io.jawg.osmcontributor.flickr.oauth.OAuthRequest)1 FlickrAddTagClient (io.jawg.osmcontributor.flickr.rest.FlickrAddTagClient)1 Poi (io.jawg.osmcontributor.model.entities.Poi)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1