use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class AppEventsLogger method updateUserProperties.
public static void updateUserProperties(final Bundle parameters, final String applicationID, final GraphRequest.Callback callback) {
final String userID = getUserID();
if (userID == null || userID.isEmpty()) {
Logger.log(LoggingBehavior.APP_EVENTS, TAG, "AppEventsLogger userID cannot be null or empty");
return;
}
getAnalyticsExecutor().execute(new Runnable() {
@Override
public void run() {
Bundle userPropertiesParams = new Bundle();
userPropertiesParams.putString("user_unique_id", userID);
userPropertiesParams.putBundle("custom_data", parameters);
// This call must be run on the background thread
AttributionIdentifiers identifiers = AttributionIdentifiers.getAttributionIdentifiers(FacebookSdk.getApplicationContext());
if (identifiers != null && identifiers.getAndroidAdvertiserId() != null) {
userPropertiesParams.putString("advertiser_id", identifiers.getAndroidAdvertiserId());
}
Bundle data = new Bundle();
try {
JSONObject userData = BundleJSONConverter.convertToJSON(userPropertiesParams);
JSONArray dataArray = new JSONArray();
dataArray.put(userData);
data.putString("data", dataArray.toString());
} catch (JSONException ex) {
throw new FacebookException("Failed to construct request", ex);
}
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), String.format(Locale.US, "%s/user_properties", applicationID), data, HttpMethod.POST, callback);
request.setSkipClientToken(true);
request.executeAsync();
}
});
}
use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class FetchedAppSettingsManager method getAppSettingsQueryResponse.
// Note that this method makes a synchronous Graph API call, so should not be called from the
// main thread.
private static JSONObject getAppSettingsQueryResponse(String applicationId) {
Bundle appSettingsParams = new Bundle();
appSettingsParams.putString(APPLICATION_FIELDS, TextUtils.join(",", APP_SETTING_FIELDS));
GraphRequest request = GraphRequest.newGraphPathRequest(null, applicationId, null);
request.setSkipClientToken(true);
request.setParameters(appSettingsParams);
return request.executeAndWait().getJSONObject();
}
use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class ShareGraphRequest method createOpenGraphObject.
/**
* Create an User Owned Open Graph object
*
* Use this method to create an open graph object, which can then be posted utilizing the same
* GraphRequest methods as other GraphRequests.
*
* @param openGraphObject The open graph object to create. Only SharePhotos with the imageUrl
* set are accepted through this helper method.
* @return GraphRequest for creating the given openGraphObject
* @throws FacebookException thrown in the case of a JSONException or in the case of invalid
* format for SharePhoto (missing imageUrl)
*/
public static GraphRequest createOpenGraphObject(final ShareOpenGraphObject openGraphObject) throws FacebookException {
String type = openGraphObject.getString("type");
if (type == null) {
type = openGraphObject.getString("og:type");
}
if (type == null) {
throw new FacebookException("Open graph object type cannot be null");
}
try {
JSONObject stagedObject = (JSONObject) OpenGraphJSONUtility.toJSONValue(openGraphObject, 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 (Exception e) {
throw new FacebookException("Unable to attach images", e);
}
return photoJSONObject;
}
});
String ogType = type;
Bundle parameters = new Bundle();
parameters.putString("object", stagedObject.toString());
String graphPath = String.format(Locale.ROOT, "%s/%s", "me", "objects/" + ogType);
return new GraphRequest(AccessToken.getCurrentAccessToken(), graphPath, parameters, HttpMethod.POST);
} catch (JSONException e) {
throw new FacebookException(e.getMessage());
}
}
use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class FacebookAppLinkResolver method getAppLinkFromUrlsInBackground.
/**
* Asynchronously resolves App Link data for multiple URLs
*
* @param uris A list of Uri objects to resolve into App Links
* @return A Task that, when successful, will return a Map of Uri->AppLink for each Uri that was
* successfully resolved into an App Link. Uris that could not be resolved into App Links will
* not be present in the Map. In the case of general server errors, the task will be completed
* with the corresponding error.
*/
public Task<Map<Uri, AppLink>> getAppLinkFromUrlsInBackground(List<Uri> uris) {
final Map<Uri, AppLink> appLinkResults = new HashMap<Uri, AppLink>();
final HashSet<Uri> urisToRequest = new HashSet<Uri>();
StringBuilder graphRequestFields = new StringBuilder();
for (Uri uri : uris) {
AppLink appLink;
synchronized (cachedAppLinks) {
appLink = cachedAppLinks.get(uri);
}
if (appLink != null) {
appLinkResults.put(uri, appLink);
} else {
if (!urisToRequest.isEmpty()) {
graphRequestFields.append(',');
}
graphRequestFields.append(uri.toString());
urisToRequest.add(uri);
}
}
if (urisToRequest.isEmpty()) {
return Task.forResult(appLinkResults);
}
final Task<Map<Uri, AppLink>>.TaskCompletionSource taskCompletionSource = Task.create();
Bundle appLinkRequestParameters = new Bundle();
appLinkRequestParameters.putString("ids", graphRequestFields.toString());
appLinkRequestParameters.putString("fields", String.format("%s.fields(%s,%s)", APP_LINK_KEY, APP_LINK_ANDROID_TARGET_KEY, APP_LINK_WEB_TARGET_KEY));
GraphRequest appLinkRequest = new GraphRequest(// token
AccessToken.getCurrentAccessToken(), /* Access Token */
"", /* Graph path */
appLinkRequestParameters, /* Query parameters */
null, /* HttpMethod */
new GraphRequest.Callback() {
/* Callback */
@Override
public void onCompleted(GraphResponse response) {
FacebookRequestError error = response.getError();
if (error != null) {
taskCompletionSource.setError(error.getException());
return;
}
JSONObject responseJson = response.getJSONObject();
if (responseJson == null) {
taskCompletionSource.setResult(appLinkResults);
return;
}
for (Uri uri : urisToRequest) {
String uriString = uri.toString();
if (!responseJson.has(uriString)) {
continue;
}
JSONObject urlData;
try {
urlData = responseJson.getJSONObject(uri.toString());
JSONObject appLinkData = urlData.getJSONObject(APP_LINK_KEY);
JSONArray rawTargets = appLinkData.getJSONArray(APP_LINK_ANDROID_TARGET_KEY);
int targetsCount = rawTargets.length();
List<AppLink.Target> targets = new ArrayList<AppLink.Target>(targetsCount);
for (int i = 0; i < targetsCount; i++) {
AppLink.Target target = getAndroidTargetFromJson(rawTargets.getJSONObject(i));
if (target != null) {
targets.add(target);
}
}
Uri webFallbackUrl = getWebFallbackUriFromJson(uri, appLinkData);
AppLink appLink = new AppLink(uri, targets, webFallbackUrl);
appLinkResults.put(uri, appLink);
synchronized (cachedAppLinks) {
cachedAppLinks.put(uri, appLink);
}
} catch (JSONException e) {
// The data for this uri was missing or badly formed.
continue;
}
}
taskCompletionSource.setResult(appLinkResults);
}
});
appLinkRequest.executeAsync();
return taskCompletionSource.getTask();
}
use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class PickerFragment method loadData.
/**
* Causes the picker to load data from the service and display it to the user.
*
* @param forceReload if true, data will be loaded even if there is already data being displayed (or loading);
* if false, data will not be re-loaded if it is already displayed (or loading)
* @param selectIds ids to select, if they are present in the loaded data
*/
public void loadData(boolean forceReload, Set<String> selectIds) {
if (!forceReload && loadingStrategy.isDataPresentOrLoading()) {
return;
}
selectionHint = selectIds;
clearResults();
GraphRequest request = getRequestForLoadData();
if (request != null) {
onLoadingData();
loadingStrategy.startLoading(request);
}
}
Aggregations