Search in sources :

Example 36 with GraphObject

use of com.facebook.model.GraphObject in project phonegap-facebook-plugin by Wizcorp.

the class ConnectPlugin method makeGraphCall.

private void makeGraphCall() {
    Session session = Session.getActiveSession();
    Request.Callback graphCallback = new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            if (graphContext != null) {
                if (response.getError() != null) {
                    graphContext.error(getFacebookRequestErrorResponse(response.getError()));
                } else {
                    GraphObject graphObject = response.getGraphObject();
                    graphContext.success(graphObject.getInnerJSONObject());
                }
                graphPath = null;
                graphContext = null;
            }
        }
    };
    //If you're using the paging URLs they will be URLEncoded, let's decode them.
    try {
        graphPath = URLDecoder.decode(graphPath, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    String[] urlParts = graphPath.split("\\?");
    String graphAction = urlParts[0];
    Request graphRequest = Request.newGraphPathRequest(null, graphAction, graphCallback);
    Bundle params = graphRequest.getParameters();
    if (urlParts.length > 1) {
        String[] queries = urlParts[1].split("&");
        for (String query : queries) {
            int splitPoint = query.indexOf("=");
            if (splitPoint > 0) {
                String key = query.substring(0, splitPoint);
                String value = query.substring(splitPoint + 1, query.length());
                params.putString(key, value);
            }
        }
    }
    params.putString("access_token", session.getAccessToken());
    graphRequest.setParameters(params);
    graphRequest.executeAsync();
}
Also used : Response(com.facebook.Response) GraphUserCallback(com.facebook.Request.GraphUserCallback) Bundle(android.os.Bundle) Request(com.facebook.Request) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GraphObject(com.facebook.model.GraphObject) Session(com.facebook.Session)

Example 37 with GraphObject

use of com.facebook.model.GraphObject in project phonegap-facebook-plugin by Wizcorp.

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.apply();
            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));
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) JSONException(org.json.JSONException) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JSONObject(org.json.JSONObject) AttributionIdentifiers(com.facebook.internal.AttributionIdentifiers)

Example 38 with GraphObject

use of com.facebook.model.GraphObject in project phonegap-facebook-plugin by Wizcorp.

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);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException)

Example 39 with GraphObject

use of com.facebook.model.GraphObject in project openkit-android by OpenKit.

the class GraphObjectAdapter method getPictureUriOfGraphObject.

protected URI getPictureUriOfGraphObject(T graphObject) {
    String uri = null;
    Object o = graphObject.getProperty(PICTURE);
    if (o instanceof String) {
        uri = (String) o;
    } else if (o instanceof JSONObject) {
        ItemPicture itemPicture = GraphObject.Factory.create((JSONObject) o).cast(ItemPicture.class);
        ItemPictureData data = itemPicture.getData();
        if (data != null) {
            uri = data.getUrl();
        }
    }
    if (uri != null) {
        try {
            return new URI(uri);
        } catch (URISyntaxException e) {
        }
    }
    return null;
}
Also used : JSONObject(org.json.JSONObject) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 40 with GraphObject

use of com.facebook.model.GraphObject in project openkit-android by OpenKit.

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject)

Aggregations

GraphObject (com.facebook.model.GraphObject)42 JSONObject (org.json.JSONObject)23 Bundle (android.os.Bundle)13 JSONException (org.json.JSONException)11 JSONArray (org.json.JSONArray)10 GraphObjectList (com.facebook.model.GraphObjectList)6 SharedPreferences (android.content.SharedPreferences)5 Request (com.facebook.Request)4 GraphMultiResult (com.facebook.model.GraphMultiResult)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 Collator (java.text.Collator)4 JSONTokener (org.json.JSONTokener)4 GraphUser (com.facebook.model.GraphUser)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Uri (android.net.Uri)2 AppLink (bolts.AppLink)2 Task (bolts.Task)2 FacebookException (com.facebook.FacebookException)2 GraphUserCallback (com.facebook.Request.GraphUserCallback)2