Search in sources :

Example 1 with Mutable

use of com.facebook.internal.Mutable in project facebook-android-sdk by facebook.

the class ShareApi method stageArrayList.

private void stageArrayList(final ArrayList arrayList, final CollectionMapper.OnMapValueCompleteListener onArrayListStagedListener) {
    final JSONArray stagedObject = new JSONArray();
    final CollectionMapper.Collection<Integer> collection = new CollectionMapper.Collection<Integer>() {

        @Override
        public Iterator<Integer> keyIterator() {
            final int size = arrayList.size();
            final Mutable<Integer> current = new Mutable<Integer>(0);
            return new Iterator<Integer>() {

                @Override
                public boolean hasNext() {
                    return current.value < size;
                }

                @Override
                public Integer next() {
                    return current.value++;
                }

                @Override
                public void remove() {
                }
            };
        }

        @Override
        public Object get(Integer key) {
            return arrayList.get(key);
        }

        @Override
        public void set(Integer key, Object value, CollectionMapper.OnErrorListener onErrorListener) {
            try {
                stagedObject.put(key, value);
            } catch (final JSONException ex) {
                String message = ex.getLocalizedMessage();
                if (message == null) {
                    message = "Error staging object.";
                }
                onErrorListener.onError(new FacebookException(message));
            }
        }
    };
    final CollectionMapper.OnMapperCompleteListener onStagedArrayMapperCompleteListener = new CollectionMapper.OnMapperCompleteListener() {

        @Override
        public void onComplete() {
            onArrayListStagedListener.onComplete(stagedObject);
        }

        @Override
        public void onError(FacebookException exception) {
            onArrayListStagedListener.onError(exception);
        }
    };
    stageCollectionValues(collection, onStagedArrayMapperCompleteListener);
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Mutable(com.facebook.internal.Mutable) FacebookException(com.facebook.FacebookException) CollectionMapper(com.facebook.internal.CollectionMapper) JSONObject(org.json.JSONObject)

Example 2 with Mutable

use of com.facebook.internal.Mutable in project facebook-android-sdk by facebook.

the class ShareApi method sharePhotoContent.

private void sharePhotoContent(final SharePhotoContent photoContent, final FacebookCallback<Sharer.Result> callback) {
    final Mutable<Integer> requestCount = new Mutable<Integer>(0);
    final AccessToken accessToken = AccessToken.getCurrentAccessToken();
    final ArrayList<GraphRequest> requests = new ArrayList<GraphRequest>();
    final ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    final ArrayList<GraphResponse> errorResponses = new ArrayList<GraphResponse>();
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject result = response.getJSONObject();
            if (result != null) {
                results.add(result);
            }
            if (response.getError() != null) {
                errorResponses.add(response);
            }
            requestCount.value -= 1;
            if (requestCount.value == 0) {
                if (!errorResponses.isEmpty()) {
                    ShareInternalUtility.invokeCallbackWithResults(callback, null, errorResponses.get(0));
                } else if (!results.isEmpty()) {
                    final String postId = results.get(0).optString("id");
                    ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
                }
            }
        }
    };
    try {
        for (SharePhoto photo : photoContent.getPhotos()) {
            Bundle params;
            try {
                params = getSharePhotoCommonParameters(photo, photoContent);
            } catch (JSONException e) {
                ShareInternalUtility.invokeCallbackWithException(callback, e);
                return;
            }
            final Bitmap bitmap = photo.getBitmap();
            final Uri photoUri = photo.getImageUrl();
            String caption = photo.getCaption();
            if (caption == null) {
                caption = this.getMessage();
            }
            if (bitmap != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(accessToken, getGraphPath(PHOTOS_EDGE), bitmap, caption, params, requestCallback));
            } else if (photoUri != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(accessToken, getGraphPath(PHOTOS_EDGE), photoUri, caption, params, requestCallback));
            }
        }
        requestCount.value += requests.size();
        for (GraphRequest request : requests) {
            request.executeAsync();
        }
    } catch (final FileNotFoundException ex) {
        ShareInternalUtility.invokeCallbackWithException(callback, ex);
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) Uri(android.net.Uri) Mutable(com.facebook.internal.Mutable) Bitmap(android.graphics.Bitmap) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) AccessToken(com.facebook.AccessToken)

Aggregations

Mutable (com.facebook.internal.Mutable)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 AccessToken (com.facebook.AccessToken)1 FacebookCallback (com.facebook.FacebookCallback)1 FacebookException (com.facebook.FacebookException)1 GraphRequest (com.facebook.GraphRequest)1 GraphResponse (com.facebook.GraphResponse)1 CollectionMapper (com.facebook.internal.CollectionMapper)1 FileNotFoundException (java.io.FileNotFoundException)1 JSONArray (org.json.JSONArray)1