use of com.facebook.model.GraphObject in project facebook-api-android-maven by avianey.
the class AppLinkData method fetchDeferredAppLinkFromServer.
private static void fetchDeferredAppLinkFromServer(Context context, String applicationId, final CompletionHandler completionHandler) {
GraphObject deferredApplinkParams = GraphObject.Factory.create();
deferredApplinkParams.setProperty("event", DEFERRED_APP_LINK_EVENT);
Utility.setAppEventAttributionParameters(deferredApplinkParams, AttributionIdentifiers.getAttributionIdentifiers(context), Utility.getHashedDeviceAndAppID(context, applicationId), Settings.getLimitEventAndDataUsage(context));
deferredApplinkParams.setProperty("application_package_name", context.getPackageName());
String deferredApplinkUrlPath = String.format(DEFERRED_APP_LINK_PATH, applicationId);
AppLinkData appLinkData = null;
try {
Request deferredApplinkRequest = Request.newPostRequest(null, deferredApplinkUrlPath, deferredApplinkParams, null);
Response deferredApplinkResponse = deferredApplinkRequest.executeAndWait();
GraphObject wireResponse = deferredApplinkResponse.getGraphObject();
JSONObject jsonResponse = wireResponse != null ? wireResponse.getInnerJSONObject() : null;
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.model.GraphObject in project facebook-api-android-maven by avianey.
the class Utility method getStringPropertyAsJSON.
// Returns either a JSONObject or JSONArray representation of the 'key' property of 'jsonObject'.
public static Object getStringPropertyAsJSON(JSONObject jsonObject, String key, String nonJSONPropertyKey) throws JSONException {
Object value = jsonObject.opt(key);
if (value != null && value instanceof String) {
JSONTokener tokener = new JSONTokener((String) value);
value = tokener.nextValue();
}
if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) {
if (nonJSONPropertyKey != null) {
// Facebook sometimes gives us back a non-JSON value such as
// literal "true" or "false" as a result.
// If we got something like that, we present it to the caller as
// a GraphObject with a single
// property. We only do this if the caller wants that behavior.
jsonObject = new JSONObject();
jsonObject.putOpt(nonJSONPropertyKey, value);
return jsonObject;
} else {
throw new FacebookException("Got an unexpected non-JSON object.");
}
}
return value;
}
use of com.facebook.model.GraphObject in project facebook-api-android-maven by avianey.
the class Session method handlePermissionResponse.
/**
* This parses a server response to a call to me/permissions. It will return the list of granted permissions.
* It will optionally update a session with the requested permissions. It also handles the distinction between
* 1.0 and 2.0 calls to the endpoint.
*
* @param response The server response
* @return A list of granted permissions or null if an error
*/
static PermissionsPair handlePermissionResponse(Response response) {
if (response.getError() != null) {
return null;
}
GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
if (result == null) {
return null;
}
GraphObjectList<GraphObject> data = result.getData();
if (data == null || data.size() == 0) {
return null;
}
List<String> grantedPermissions = new ArrayList<String>(data.size());
List<String> declinedPermissions = new ArrayList<String>(data.size());
// Check if we are dealing with v2.0 or v1.0 behavior until the server is updated
GraphObject firstObject = data.get(0);
if (firstObject.getProperty("permission") != null) {
// v2.0
for (GraphObject graphObject : data) {
String permission = (String) graphObject.getProperty("permission");
if (permission.equals("installed")) {
continue;
}
String status = (String) graphObject.getProperty("status");
if (status.equals("granted")) {
grantedPermissions.add(permission);
} else if (status.equals("declined")) {
declinedPermissions.add(permission);
}
}
} else {
// v1.0
Map<String, Object> permissionsMap = firstObject.asMap();
for (Map.Entry<String, Object> entry : permissionsMap.entrySet()) {
if (entry.getKey().equals("installed")) {
continue;
}
if ((Integer) entry.getValue() == 1) {
grantedPermissions.add(entry.getKey());
}
}
}
return new PermissionsPair(grantedPermissions, declinedPermissions);
}
use of com.facebook.model.GraphObject in project facebook-api-android-maven by avianey.
the class Settings method publishInstallAndWaitForResponse.
static Response publishInstallAndWaitForResponse(final Context context, final String applicationId, final boolean isAutoPublish) {
try {
if (context == null || applicationId == null) {
throw new IllegalArgumentException("Both context and applicationId must be non-null");
}
AttributionIdentifiers identifiers = AttributionIdentifiers.getAttributionIdentifiers(context);
SharedPreferences preferences = context.getSharedPreferences(ATTRIBUTION_PREFERENCES, Context.MODE_PRIVATE);
String pingKey = applicationId + "ping";
String jsonKey = applicationId + "json";
long lastPing = preferences.getLong(pingKey, 0);
String lastResponseJSON = preferences.getString(jsonKey, null);
// prevent auto publish from occurring if we have an explicit call.
if (!isAutoPublish) {
setShouldAutoPublishInstall(false);
}
GraphObject publishParams = GraphObject.Factory.create();
publishParams.setProperty(ANALYTICS_EVENT, MOBILE_INSTALL_EVENT);
Utility.setAppEventAttributionParameters(publishParams, identifiers, Utility.getHashedDeviceAndAppID(context, applicationId), getLimitEventAndDataUsage(context));
publishParams.setProperty(AUTO_PUBLISH, isAutoPublish);
publishParams.setProperty("application_package_name", context.getPackageName());
String publishUrl = String.format(PUBLISH_ACTIVITY_PATH, applicationId);
Request publishRequest = Request.newPostRequest(null, publishUrl, publishParams, null);
if (lastPing != 0) {
GraphObject graphObject = null;
try {
if (lastResponseJSON != null) {
graphObject = GraphObject.Factory.create(new JSONObject(lastResponseJSON));
}
} catch (JSONException je) {
// return the default graph object if there is any problem reading the data.
}
if (graphObject == null) {
return Response.createResponsesFromString("true", null, new RequestBatch(publishRequest), true).get(0);
} else {
return new Response(null, null, null, graphObject, true);
}
} else if (identifiers == null || (identifiers.getAndroidAdvertiserId() == null && identifiers.getAttributionId() == null)) {
throw new FacebookException("No attribution id available to send to server.");
} else {
if (!Utility.queryAppSettings(applicationId, false).supportsAttribution()) {
throw new FacebookException("Install attribution has been disabled on the server.");
}
Response publishResponse = publishRequest.executeAndWait();
// denote success since no error threw from the post.
SharedPreferences.Editor editor = preferences.edit();
lastPing = System.currentTimeMillis();
editor.putLong(pingKey, lastPing);
// if we got an object response back, cache the string of the JSON.
if (publishResponse.getGraphObject() != null && publishResponse.getGraphObject().getInnerJSONObject() != null) {
editor.putString(jsonKey, publishResponse.getGraphObject().getInnerJSONObject().toString());
}
editor.commit();
return publishResponse;
}
} catch (Exception e) {
// if there was an error, fall through to the failure case.
Utility.logd("Facebook-publish", e);
return new Response(null, null, new FacebookRequestError(null, e));
}
}
use of com.facebook.model.GraphObject in project Klyph by jonathangerbaud.
the class Response method createResponseFromObject.
private static Response createResponseFromObject(Request request, HttpURLConnection connection, Object object, boolean isFromCache, Object originalResult) throws JSONException {
if (object instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) object;
FacebookRequestError error = FacebookRequestError.checkResponseAndCreateError(jsonObject, originalResult, connection);
if (error != null) {
if (error.getErrorCode() == INVALID_SESSION_FACEBOOK_ERROR_CODE) {
Session session = request.getSession();
if (session != null) {
session.closeAndClearTokenInformation();
}
}
return new Response(request, connection, error);
}
Object body = Utility.getStringPropertyAsJSON(jsonObject, BODY_KEY, NON_JSON_RESPONSE_PROPERTY);
if (body instanceof JSONObject) {
GraphObject graphObject = GraphObject.Factory.create((JSONObject) body);
return new Response(request, connection, graphObject, isFromCache);
} else if (body instanceof JSONArray) {
GraphObjectList<GraphObject> graphObjectList = GraphObject.Factory.createList((JSONArray) body, GraphObject.class);
return new Response(request, connection, graphObjectList, isFromCache);
}
// We didn't get a body we understand how to handle, so pretend we got nothing.
object = JSONObject.NULL;
}
if (object == JSONObject.NULL) {
return new Response(request, connection, (GraphObject) null, isFromCache);
} else {
throw new FacebookException("Got unexpected object type in response, class: " + object.getClass().getSimpleName());
}
}
Aggregations