public static JSONObject toJSONObjectForWeb( final ShareOpenGraphContent shareOpenGraphContent) throws JSONException { ShareOpenGraphAction action = shareOpenGraphContent.getAction(); return OpenGraphJSONUtility.toJSONObject( action, new OpenGraphJSONUtility.PhotoJSONProcessor() { @Override public JSONObject toJSONObject(SharePhoto photo) { Uri photoUri = photo.getImageUrl(); JSONObject photoJSONObject = new JSONObject(); try { photoJSONObject.put( NativeProtocol.IMAGE_URL_KEY, photoUri.toString()); } catch (JSONException e) { throw new FacebookException("Unable to attach images", e); } return photoJSONObject; } }); }
static AccessToken createAccessTokenFromNativeLogin( Bundle bundle, AccessTokenSource source, String applicationId) { Date expires = Utility.getBundleLongAsDate( bundle, NativeProtocol.EXTRA_EXPIRES_SECONDS_SINCE_EPOCH, new Date(0)); ArrayList<String> permissions = bundle.getStringArrayList(NativeProtocol.EXTRA_PERMISSIONS); String token = bundle.getString(NativeProtocol.EXTRA_ACCESS_TOKEN); if (Utility.isNullOrEmpty(token)) { return null; } String userId = bundle.getString(NativeProtocol.EXTRA_USER_ID); return new AccessToken( token, applicationId, userId, permissions, null, source, expires, new Date()); }
@Override boolean tryAuthorize(LoginClient.Request request) { String e2e = LoginClient.getE2E(); Intent intent = NativeProtocol.createProxyAuthIntent( loginClient.getActivity(), request.getApplicationId(), request.getPermissions(), e2e, request.isRerequest(), request.hasPublishPermission(), request.getDefaultAudience()); addLoggingExtra(ServerProtocol.DIALOG_PARAM_E2E, e2e); return tryIntent(intent, LoginClient.getLoginRequestCode()); }
private void handlePassThroughError() { Intent requestIntent = getIntent(); // The error we need to respond with is passed to us as method arguments. Bundle errorResults = NativeProtocol.getMethodArgumentsFromIntent(requestIntent); FacebookException exception = NativeProtocol.getExceptionFromErrorData(errorResults); // Create a result intent that is formed based on the request intent Intent resultIntent = NativeProtocol.createProtocolResultIntent( requestIntent, null, exception); setResult(RESULT_CANCELED, resultIntent); finish(); }
private void cancelPendingAppCall(FacebookDialog.Callback facebookDialogCallback) { if (facebookDialogCallback != null) { Intent pendingIntent = pendingFacebookDialogCall.getRequestIntent(); Intent cancelIntent = new Intent(); cancelIntent.putExtra(NativeProtocol.EXTRA_PROTOCOL_CALL_ID, pendingIntent.getStringExtra(NativeProtocol.EXTRA_PROTOCOL_CALL_ID)); cancelIntent.putExtra(NativeProtocol.EXTRA_PROTOCOL_ACTION, pendingIntent.getStringExtra(NativeProtocol.EXTRA_PROTOCOL_ACTION)); cancelIntent.putExtra(NativeProtocol.EXTRA_PROTOCOL_VERSION, pendingIntent.getIntExtra(NativeProtocol.EXTRA_PROTOCOL_VERSION, 0)); cancelIntent.putExtra(NativeProtocol.STATUS_ERROR_TYPE, NativeProtocol.ERROR_UNKNOWN_ERROR); FacebookDialog.handleActivityResult(activity, pendingFacebookDialogCall, pendingFacebookDialogCall.getRequestCode(), cancelIntent, facebookDialogCallback); } pendingFacebookDialogCall = null; }
/** * Parses the results of a dialog activity and calls the appropriate method on the provided Callback. * * @param context the Context that is handling the activity result * @param appCall an PendingCall containing the call ID and original Intent used to launch the dialog * @param requestCode the request code for the activity result * @param data the result Intent * @param callback a callback to call after parsing the results * @return true if the activity result was handled, false if not */ public static boolean handleActivityResult(Context context, PendingCall appCall, int requestCode, Intent data, Callback callback) { if (requestCode != appCall.getRequestCode()) { return false; } if (attachmentStore != null) { attachmentStore.cleanupAttachmentsForCall(context, appCall.getCallId()); } if (callback != null) { if (NativeProtocol.isErrorResult(data)) { Exception error = NativeProtocol.getErrorFromResult(data); callback.onError(appCall, error, data.getExtras()); } else { callback.onComplete(appCall, data.getExtras()); } } return true; }
static private String getEventName(String action, boolean hasPhotos) { String eventName; if (action.equals(NativeProtocol.ACTION_FEED_DIALOG)) { eventName = hasPhotos ? AnalyticsEvents.EVENT_NATIVE_DIALOG_TYPE_PHOTO_SHARE : AnalyticsEvents.EVENT_NATIVE_DIALOG_TYPE_SHARE; } else if (action.equals(NativeProtocol.ACTION_MESSAGE_DIALOG)) { eventName = hasPhotos ? AnalyticsEvents.EVENT_NATIVE_DIALOG_TYPE_PHOTO_MESSAGE : AnalyticsEvents.EVENT_NATIVE_DIALOG_TYPE_MESSAGE; } else if (action.equals(NativeProtocol.ACTION_OGACTIONPUBLISH_DIALOG)) { eventName = AnalyticsEvents.EVENT_NATIVE_DIALOG_TYPE_OG_SHARE; } else if (action.equals(NativeProtocol.ACTION_OGMESSAGEPUBLISH_DIALOG)) { eventName = AnalyticsEvents.EVENT_NATIVE_DIALOG_TYPE_OG_MESSAGE; } else { throw new FacebookException("An unspecified action was presented"); } return eventName; }
/** * Constructs a FacebookDialog with an Intent that is correctly populated to present the dialog within * the Facebook application. * * @return a FacebookDialog instance */ public FacebookDialog build() { validate(); Bundle extras = new Bundle(); putExtra(extras, NativeProtocol.EXTRA_APPLICATION_ID, applicationId); putExtra(extras, NativeProtocol.EXTRA_APPLICATION_NAME, applicationName); extras = setBundleExtras(extras); String action = getActionForFeatures(getDialogFeatures()); int protocolVersion = getProtocolVersionForNativeDialog(activity, action, getMinVersionForFeatures(getDialogFeatures())); Intent intent = NativeProtocol.createPlatformActivityIntent(activity, action, protocolVersion, extras); if (intent == null) { logDialogActivity(activity, fragment, getEventName(action, extras.containsKey(NativeProtocol.EXTRA_PHOTOS)), AnalyticsEvents.PARAMETER_DIALOG_OUTCOME_VALUE_FAILED); throw new FacebookException( "Unable to create Intent; this likely means the Facebook app is not installed."); } appCall.setRequestIntent(intent); return new FacebookDialog(activity, fragment, appCall, getOnPresentCallback()); }
@Override Bundle setBundleExtras(Bundle extras) { putExtra(extras, NativeProtocol.EXTRA_APPLICATION_ID, applicationId); putExtra(extras, NativeProtocol.EXTRA_APPLICATION_NAME, applicationName); putExtra(extras, NativeProtocol.EXTRA_TITLE, name); putExtra(extras, NativeProtocol.EXTRA_SUBTITLE, caption); putExtra(extras, NativeProtocol.EXTRA_DESCRIPTION, description); putExtra(extras, NativeProtocol.EXTRA_LINK, link); putExtra(extras, NativeProtocol.EXTRA_IMAGE, picture); putExtra(extras, NativeProtocol.EXTRA_PLACE_TAG, place); putExtra(extras, NativeProtocol.EXTRA_TITLE, name); putExtra(extras, NativeProtocol.EXTRA_REF, ref); extras.putBoolean(NativeProtocol.EXTRA_DATA_FAILURES_FATAL, dataErrorsFatal); if (!Utility.isNullOrEmpty(friends)) { extras.putStringArrayList(NativeProtocol.EXTRA_FRIEND_TAGS, friends); } return extras; }
private void updateActionAttachmentUrls(List<String> attachmentUrls, boolean isUserGenerated) { List<JSONObject> attachments = action.getImage(); if (attachments == null) { attachments = new ArrayList<JSONObject>(attachmentUrls.size()); } for (String url : attachmentUrls) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put(NativeProtocol.IMAGE_URL_KEY, url); if (isUserGenerated) { jsonObject.put(NativeProtocol.IMAGE_USER_GENERATED_KEY, true); } } catch (JSONException e) { throw new FacebookException("Unable to attach images", e); } attachments.add(jsonObject); } action.setImage(attachments); }
@Override boolean tryAuthorize(AuthorizationRequest request) { applicationId = request.getApplicationId(); Intent intent = NativeProtocol.createLoginDialog20121101Intent(context, request.getApplicationId(), new ArrayList<String>(request.getPermissions()), request.getDefaultAudience().getNativeProtocolAudience()); if (intent == null) { return false; } callId = intent.getStringExtra(NativeProtocol.EXTRA_PROTOCOL_CALL_ID); addLoggingExtra(EVENT_EXTRAS_APP_CALL_ID, callId); addLoggingExtra(EVENT_EXTRAS_PROTOCOL_VERSION, intent.getIntExtra(NativeProtocol.EXTRA_PROTOCOL_VERSION, 0)); addLoggingExtra(EVENT_EXTRAS_PERMISSIONS, TextUtils.join(",", intent.getStringArrayListExtra(NativeProtocol.EXTRA_PERMISSIONS))); addLoggingExtra(EVENT_EXTRAS_WRITE_PRIVACY, intent.getStringExtra(NativeProtocol.EXTRA_WRITE_PRIVACY)); logEvent(AnalyticsEvents.EVENT_NATIVE_LOGIN_DIALOG_START, AnalyticsEvents.PARAMETER_NATIVE_LOGIN_DIALOG_START_TIME, callId); return tryIntent(intent, request.getRequestCode()); }
@Override Intent handleBuild(Bundle extras) { putExtra(extras, NativeProtocol.EXTRA_APPLICATION_ID, applicationId); putExtra(extras, NativeProtocol.EXTRA_APPLICATION_NAME, applicationName); putExtra(extras, NativeProtocol.EXTRA_TITLE, name); putExtra(extras, NativeProtocol.EXTRA_SUBTITLE, caption); putExtra(extras, NativeProtocol.EXTRA_DESCRIPTION, description); putExtra(extras, NativeProtocol.EXTRA_LINK, link); putExtra(extras, NativeProtocol.EXTRA_IMAGE, picture); putExtra(extras, NativeProtocol.EXTRA_PLACE_TAG, place); putExtra(extras, NativeProtocol.EXTRA_TITLE, name); putExtra(extras, NativeProtocol.EXTRA_REF, ref); extras.putBoolean(NativeProtocol.EXTRA_DATA_FAILURES_FATAL, dataErrorsFatal); if (!Utility.isNullOrEmpty(friends)) { extras.putStringArrayList(NativeProtocol.EXTRA_FRIEND_TAGS, friends); } int protocolVersion = getProtocolVersionForNativeDialog(activity, MIN_NATIVE_SHARE_PROTOCOL_VERSION); Intent intent = NativeProtocol.createPlatformActivityIntent(activity, NativeProtocol.ACTION_FEED_DIALOG, protocolVersion, extras); return intent; }
@Override Intent handleBuild(Bundle extras) { putExtra(extras, NativeProtocol.EXTRA_PREVIEW_PROPERTY_NAME, previewPropertyName); putExtra(extras, NativeProtocol.EXTRA_ACTION_TYPE, actionType); extras.putBoolean(NativeProtocol.EXTRA_DATA_FAILURES_FATAL, dataErrorsFatal); JSONObject jsonAction = action.getInnerJSONObject(); jsonAction = flattenChildrenOfGraphObject(jsonAction); String jsonString = jsonAction.toString(); putExtra(extras, NativeProtocol.EXTRA_ACTION, jsonString); int protocolVersion = getProtocolVersionForNativeDialog(activity, MIN_NATIVE_SHARE_PROTOCOL_VERSION); Intent intent = NativeProtocol.createPlatformActivityIntent(activity, NativeProtocol.ACTION_OGACTIONPUBLISH_DIALOG, protocolVersion, extras); return intent; }
/** * Constructs a FacebookDialog with an Intent that is correctly populated to present the dialog within * the Facebook application. * @return a FacebookDialog instance */ public FacebookDialog build() { validate(); Bundle extras = new Bundle(); putExtra(extras, NativeProtocol.EXTRA_APPLICATION_ID, applicationId); putExtra(extras, NativeProtocol.EXTRA_APPLICATION_NAME, applicationName); Intent intent = handleBuild(extras); if (intent == null) { throw new FacebookException("Unable to create Intent; this likely means the Facebook app is not installed."); } appCall.setRequestIntent(intent); return new FacebookDialog(activity, fragment, appCall, getOnPresentCallback()); }
@Override public void onReceive(Context context, Intent intent) { String appCallId = intent.getStringExtra(NativeProtocol.EXTRA_PROTOCOL_CALL_ID); String action = intent.getStringExtra(NativeProtocol.EXTRA_PROTOCOL_ACTION); if (appCallId != null && action != null) { Bundle extras = intent.getExtras(); if (NativeProtocol.isErrorResult(intent)) { onFailedAppCall(appCallId, action, extras); } else { onSuccessfulAppCall(appCallId, action, extras); } } }
/** * Only to be called after an OG-publish was attempted and something went wrong. The Button * state is reverted and an error is returned to the LikeViews */ private void publishDidError(boolean oldLikeState) { updateLikeState(oldLikeState); Bundle errorBundle = new Bundle(); errorBundle.putString( NativeProtocol.STATUS_ERROR_DESCRIPTION, ERROR_PUBLISH_ERROR); broadcastAction( LikeActionController.this, ACTION_LIKE_ACTION_CONTROLLER_DID_ERROR, errorBundle); }
LikeStatusClient(Context context, String applicationId, String objectId) { super(context, NativeProtocol.MESSAGE_GET_LIKE_STATUS_REQUEST, NativeProtocol.MESSAGE_GET_LIKE_STATUS_REPLY, NativeProtocol.PROTOCOL_VERSION_20141001, applicationId); this.objectId = objectId; }
public static boolean handleActivityResult( int requestCode, int resultCode, Intent data, ResultProcessor resultProcessor) { AppCall appCall = getAppCallFromActivityResult(requestCode, resultCode, data); if (appCall == null) { return false; } NativeAppCallAttachmentStore.cleanupAttachmentsForCall(appCall.getCallId()); if (resultProcessor == null) { return true; } FacebookException exception = NativeProtocol.getExceptionFromErrorData( NativeProtocol.getErrorDataFromResultIntent(data)); if (exception != null) { if (exception instanceof FacebookOperationCanceledException) { resultProcessor.onCancel(appCall); } else { resultProcessor.onError(appCall, exception); } } else { // If here, we did not find an error in the result. Bundle results = NativeProtocol.getSuccessResultsFromIntent(data); resultProcessor.onSuccess(appCall, results); } return true; }
private static AppCall getAppCallFromActivityResult(int requestCode, int resultCode, Intent data) { UUID callId = NativeProtocol.getCallIdFromIntent(data); if (callId == null) { return null; } return AppCall.finishPendingCall(callId, requestCode); }
private LoginClient.Result handleResultOk(LoginClient.Request request, Intent data) { Bundle extras = data.getExtras(); String error = getError(extras); String errorCode = extras.getString("error_code"); String errorMessage = getErrorMessage(extras); String e2e = extras.getString(NativeProtocol.FACEBOOK_PROXY_AUTH_E2E_KEY); if (!Utility.isNullOrEmpty(e2e)) { logWebLoginCompleted(e2e); } if (error == null && errorCode == null && errorMessage == null) { try { AccessToken token = createAccessTokenFromWebBundle(request.getPermissions(), extras, AccessTokenSource.FACEBOOK_APPLICATION_WEB, request.getApplicationId()); return LoginClient.Result.createTokenResult(request, token); } catch (FacebookException ex) { return LoginClient.Result.createErrorResult(request, null, ex.getMessage()); } } else if (ServerProtocol.errorsProxyAuthDisabled.contains(error)) { return null; } else if (ServerProtocol.errorsUserCanceled.contains(error)) { return LoginClient.Result.createCancelResult(request, null); } else { return LoginClient.Result.createErrorResult(request, error, errorMessage, errorCode); } }
void getTokenCompleted(LoginClient.Request request, Bundle result) { if (getTokenClient != null) { getTokenClient.setCompletedListener(null); } getTokenClient = null; loginClient.notifyBackgroundProcessingStop(); if (result != null) { ArrayList<String> currentPermissions = result.getStringArrayList(NativeProtocol.EXTRA_PERMISSIONS); Set<String> permissions = request.getPermissions(); if ((currentPermissions != null) && ((permissions == null) || currentPermissions.containsAll(permissions))) { // We got all the permissions we needed, so we can complete the auth now. complete(request, result); return; } // We didn't get all the permissions we wanted, so update the request with just the // permissions we still need. Set<String> newPermissions = new HashSet<String>(); for (String permission : permissions) { if (!currentPermissions.contains(permission)) { newPermissions.add(permission); } } if (!newPermissions.isEmpty()) { addLoggingExtra( LoginLogger.EVENT_EXTRAS_NEW_PERMISSIONS, TextUtils.join(",", newPermissions) ); } request.setPermissions(newPermissions); } loginClient.tryNextHandler(); }
GetTokenClient(Context context, String applicationId) { super( context, NativeProtocol.MESSAGE_GET_ACCESS_TOKEN_REQUEST, NativeProtocol.MESSAGE_GET_ACCESS_TOKEN_REPLY, NativeProtocol.PROTOCOL_VERSION_20121101, applicationId); }
/** * Creates a JSONObject for an open graph object that is suitable for posting. * @param type the Open Graph object type for the object, or null if it will be specified later * @param title the title of the object, or null if it will be specified later * @param imageUrl the URL of an image associated with the object, or null * @param url the URL associated with the object, or null * @param description the description of the object, or null * @param objectProperties the properties of the open graph object * @param id the id of the object if the post is for update * @return a JSONObject */ public static JSONObject createOpenGraphObjectForPost( String type, String title, String imageUrl, String url, String description, JSONObject objectProperties, String id) { JSONObject openGraphObject = new JSONObject(); try { if (type != null) { openGraphObject.put("type", type); } openGraphObject.put("title", title); if (imageUrl != null) { JSONObject imageUrlObject = new JSONObject(); imageUrlObject.put("url", imageUrl); JSONArray imageUrls = new JSONArray(); imageUrls.put(imageUrlObject); openGraphObject.put("image", imageUrls); } openGraphObject.put("url", url); openGraphObject.put("description", description); openGraphObject.put(NativeProtocol.OPEN_GRAPH_CREATE_OBJECT_KEY, true); if (objectProperties != null) { openGraphObject.put("data", objectProperties); } if (id != null) { openGraphObject.put("id", id); } } catch (JSONException e) { throw new FacebookException("An error occurred while setting up the graph object", e); } return openGraphObject; }
void getTokenCompleted(AuthorizationRequest request, Bundle result) { getTokenClient = null; notifyBackgroundProcessingStop(); if (result != null) { ArrayList<String> currentPermissions = result.getStringArrayList(NativeProtocol.EXTRA_PERMISSIONS); List<String> permissions = request.getPermissions(); if ((currentPermissions != null) && ((permissions == null) || currentPermissions.containsAll(permissions))) { // We got all the permissions we needed, so we can complete the auth now. AccessToken token = AccessToken .createFromNativeLogin(result, AccessTokenSource.FACEBOOK_APPLICATION_SERVICE); Result outcome = Result.createTokenResult(pendingRequest, token); completeAndValidate(outcome); return; } // We didn't get all the permissions we wanted, so update the request with just the permissions // we still need. List<String> newPermissions = new ArrayList<String>(); for (String permission : permissions) { if (!currentPermissions.contains(permission)) { newPermissions.add(permission); } } if (!newPermissions.isEmpty()) { addLoggingExtra(EVENT_EXTRAS_NEW_PERMISSIONS, TextUtils.join(",", newPermissions)); } request.setPermissions(newPermissions); } tryNextHandler(); }