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);
}
}
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();
}
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;
}
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);
}
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));
}
}
});
}
Aggregations