Search in sources :

Example 1 with GraphResponse

use of com.facebook.GraphResponse 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)

Example 2 with GraphResponse

use of com.facebook.GraphResponse in project facebook-android-sdk by facebook.

the class DeviceShareDialogFragment method startShare.

private void startShare() {
    Bundle parameters = getGraphParametersForShareContent();
    if (parameters == null || parameters.size() == 0) {
        this.finishActivityWithError(new FacebookRequestError(0, "", "Failed to get share content"));
    }
    String accessToken = Validate.hasAppID() + "|" + Validate.hasClientToken();
    parameters.putString(GraphRequest.ACCESS_TOKEN_PARAM, accessToken);
    parameters.putString(DeviceRequestsHelper.DEVICE_INFO_PARAM, DeviceRequestsHelper.getDeviceInfo());
    GraphRequest graphRequest = new GraphRequest(null, DEVICE_SHARE_ENDPOINT, parameters, HttpMethod.POST, new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            FacebookRequestError error = response.getError();
            if (error != null) {
                finishActivityWithError(error);
                return;
            }
            JSONObject jsonObject = response.getJSONObject();
            RequestState requestState = new RequestState();
            try {
                requestState.setUserCode(jsonObject.getString("user_code"));
                requestState.setExpiresIn(jsonObject.getLong("expires_in"));
            } catch (JSONException ex) {
                finishActivityWithError(new FacebookRequestError(0, "", "Malformed server response"));
                return;
            }
            setCurrentRequestState(requestState);
        }
    });
    graphRequest.executeAsync();
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle) JSONException(org.json.JSONException) FacebookRequestError(com.facebook.FacebookRequestError)

Example 3 with GraphResponse

use of com.facebook.GraphResponse in project facebook-android-sdk by facebook.

the class AppEventQueue method buildRequestForSession.

private static GraphRequest buildRequestForSession(final AccessTokenAppIdPair accessTokenAppId, final SessionEventsState appEvents, final boolean limitEventUsage, final FlushStatistics flushState) {
    String applicationId = accessTokenAppId.getApplicationId();
    FetchedAppSettings fetchedAppSettings = FetchedAppSettingsManager.queryAppSettings(applicationId, false);
    final GraphRequest postRequest = GraphRequest.newPostRequest(null, String.format("%s/activities", applicationId), null, null);
    Bundle requestParameters = postRequest.getParameters();
    if (requestParameters == null) {
        requestParameters = new Bundle();
    }
    requestParameters.putString("access_token", accessTokenAppId.getAccessTokenString());
    String pushNotificationsRegistrationId = AppEventsLogger.getPushNotificationsRegistrationId();
    if (pushNotificationsRegistrationId != null) {
        requestParameters.putString("device_token", pushNotificationsRegistrationId);
    }
    postRequest.setParameters(requestParameters);
    boolean supportsImplicitLogging = false;
    if (fetchedAppSettings != null) {
        supportsImplicitLogging = fetchedAppSettings.supportsImplicitLogging();
    }
    int numEvents = appEvents.populateRequest(postRequest, FacebookSdk.getApplicationContext(), supportsImplicitLogging, limitEventUsage);
    if (numEvents == 0) {
        return null;
    }
    flushState.numEvents += numEvents;
    postRequest.setCallback(new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            handleResponse(accessTokenAppId, postRequest, response, appEvents, flushState);
        }
    });
    return postRequest;
}
Also used : FetchedAppSettings(com.facebook.internal.FetchedAppSettings) GraphRequest(com.facebook.GraphRequest) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle)

Example 4 with GraphResponse

use of com.facebook.GraphResponse in project facebook-android-sdk by facebook.

the class AppLinkData method fetchDeferredAppLinkFromServer.

private static void fetchDeferredAppLinkFromServer(Context context, String applicationId, final CompletionHandler completionHandler) {
    JSONObject deferredApplinkParams = new JSONObject();
    try {
        deferredApplinkParams.put("event", DEFERRED_APP_LINK_EVENT);
        Utility.setAppEventAttributionParameters(deferredApplinkParams, AttributionIdentifiers.getAttributionIdentifiers(context), AppEventsLogger.getAnonymousAppDeviceGUID(context), FacebookSdk.getLimitEventAndDataUsage(context));
        Utility.setAppEventExtendedDeviceInfoParameters(deferredApplinkParams, FacebookSdk.getApplicationContext());
        deferredApplinkParams.put("application_package_name", context.getPackageName());
    } catch (JSONException e) {
        throw new FacebookException("An error occurred while preparing deferred app link", e);
    }
    String deferredApplinkUrlPath = String.format(DEFERRED_APP_LINK_PATH, applicationId);
    AppLinkData appLinkData = null;
    try {
        GraphRequest deferredApplinkRequest = GraphRequest.newPostRequest(null, deferredApplinkUrlPath, deferredApplinkParams, null);
        GraphResponse deferredApplinkResponse = deferredApplinkRequest.executeAndWait();
        JSONObject jsonResponse = deferredApplinkResponse.getJSONObject();
        if (jsonResponse != null) {
            final String appLinkArgsJsonString = jsonResponse.optString(DEFERRED_APP_LINK_ARGS_FIELD);
            final long tapTimeUtc = jsonResponse.optLong(DEFERRED_APP_LINK_CLICK_TIME_FIELD, -1);
            final String appLinkClassName = jsonResponse.optString(DEFERRED_APP_LINK_CLASS_FIELD);
            final String appLinkUrl = jsonResponse.optString(DEFERRED_APP_LINK_URL_FIELD);
            if (!TextUtils.isEmpty(appLinkArgsJsonString)) {
                appLinkData = createFromJson(appLinkArgsJsonString);
                if (tapTimeUtc != -1) {
                    try {
                        if (appLinkData.arguments != null) {
                            appLinkData.arguments.put(ARGUMENTS_TAPTIME_KEY, tapTimeUtc);
                        }
                        if (appLinkData.argumentBundle != null) {
                            appLinkData.argumentBundle.putString(ARGUMENTS_TAPTIME_KEY, Long.toString(tapTimeUtc));
                        }
                    } catch (JSONException e) {
                        Log.d(TAG, "Unable to put tap time in AppLinkData.arguments");
                    }
                }
                if (appLinkClassName != null) {
                    try {
                        if (appLinkData.arguments != null) {
                            appLinkData.arguments.put(ARGUMENTS_NATIVE_CLASS_KEY, appLinkClassName);
                        }
                        if (appLinkData.argumentBundle != null) {
                            appLinkData.argumentBundle.putString(ARGUMENTS_NATIVE_CLASS_KEY, appLinkClassName);
                        }
                    } catch (JSONException e) {
                        Log.d(TAG, "Unable to put tap time in AppLinkData.arguments");
                    }
                }
                if (appLinkUrl != null) {
                    try {
                        if (appLinkData.arguments != null) {
                            appLinkData.arguments.put(ARGUMENTS_NATIVE_URL, appLinkUrl);
                        }
                        if (appLinkData.argumentBundle != null) {
                            appLinkData.argumentBundle.putString(ARGUMENTS_NATIVE_URL, appLinkUrl);
                        }
                    } catch (JSONException e) {
                        Log.d(TAG, "Unable to put tap time in AppLinkData.arguments");
                    }
                }
            }
        }
    } catch (Exception e) {
        Utility.logd(TAG, "Unable to fetch deferred applink from server");
    }
    completionHandler.onDeferredAppLinkDataFetched(appLinkData);
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException)

Example 5 with GraphResponse

use of com.facebook.GraphResponse in project facebook-android-sdk by facebook.

the class DeviceAuthDialog method getPollRequest.

private GraphRequest getPollRequest() {
    Bundle parameters = new Bundle();
    parameters.putString("code", currentRequestState.getRequestCode());
    return new GraphRequest(null, DEVICE_LOGIN_STATUS_ENDPOINT, parameters, HttpMethod.POST, new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            // Check if the request was already cancelled
            if (completed.get()) {
                return;
            }
            FacebookRequestError error = response.getError();
            if (error != null) {
                // message text
                switch(error.getSubErrorCode()) {
                    case LOGIN_ERROR_SUBCODE_AUTHORIZATION_PENDING:
                    case LOGIN_ERROR_SUBCODE_EXCESSIVE_POLLING:
                        {
                            // Keep polling. If we got the slow down message just ignore
                            schedulePoll();
                        }
                        break;
                    case LOGIN_ERROR_SUBCODE_CODE_EXPIRED:
                    case LOGIN_ERROR_SUBCODE_AUTHORIZATION_DECLINED:
                        {
                            onCancel();
                        }
                        break;
                    default:
                        {
                            onError(response.getError().getException());
                        }
                        break;
                }
                return;
            }
            try {
                JSONObject resultObject = response.getJSONObject();
                onSuccess(resultObject.getString("access_token"));
            } catch (JSONException ex) {
                onError(new FacebookException(ex));
            }
        }
    });
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException) FacebookRequestError(com.facebook.FacebookRequestError)

Aggregations

GraphRequest (com.facebook.GraphRequest)19 GraphResponse (com.facebook.GraphResponse)19 JSONObject (org.json.JSONObject)16 Bundle (android.os.Bundle)15 JSONException (org.json.JSONException)11 FacebookException (com.facebook.FacebookException)8 FacebookCallback (com.facebook.FacebookCallback)6 FacebookRequestError (com.facebook.FacebookRequestError)6 AccessToken (com.facebook.AccessToken)4 Uri (android.net.Uri)3 Bitmap (android.graphics.Bitmap)2 FacebookGraphResponseException (com.facebook.FacebookGraphResponseException)2 CollectionMapper (com.facebook.internal.CollectionMapper)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Intent (android.content.Intent)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 AppLink (bolts.AppLink)1