Search in sources :

Example 11 with GraphObject

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

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) FacebookException(com.facebook.FacebookException) JSONArray(org.json.JSONArray) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject)

Example 12 with GraphObject

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

the class Utility method getAppSettingsQueryResponse.

// Note that this method makes a synchronous Graph API call, so should not be called from the main thread.
private static GraphObject getAppSettingsQueryResponse(String applicationId) {
    Bundle appSettingsParams = new Bundle();
    appSettingsParams.putString(APPLICATION_FIELDS, TextUtils.join(",", APP_SETTING_FIELDS));
    Request request = Request.newGraphPathRequest(null, applicationId, null);
    request.setSkipClientToken(true);
    request.setParameters(appSettingsParams);
    GraphObject response = request.executeAndWait().getGraphObject();
    return response;
}
Also used : Bundle(android.os.Bundle) Request(com.facebook.Request) GraphObject(com.facebook.model.GraphObject)

Example 13 with GraphObject

use of com.facebook.model.GraphObject in project Klyph by jonathangerbaud.

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");
        }
        String attributionId = Settings.getAttributionId(context.getContentResolver());
        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, attributionId, 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, graphObject, true);
            }
        } else if (attributionId == null) {
            throw new FacebookException("No attribution id returned from the Facebook application");
        } 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));
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) JSONException(org.json.JSONException) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 14 with GraphObject

use of com.facebook.model.GraphObject in project facebook-api-android-maven by avianey.

the class Utility method queryAppSettings.

// Note that this method makes a synchronous Graph API call, so should not be called from the main thread.
public static FetchedAppSettings queryAppSettings(final String applicationId, final boolean forceRequery) {
    // Cache the last app checked results.
    if (!forceRequery && fetchedAppSettings.containsKey(applicationId)) {
        return fetchedAppSettings.get(applicationId);
    }
    Bundle appSettingsParams = new Bundle();
    appSettingsParams.putString(APPLICATION_FIELDS, TextUtils.join(",", APP_SETTING_FIELDS));
    Request request = Request.newGraphPathRequest(null, applicationId, null);
    request.setParameters(appSettingsParams);
    GraphObject supportResponse = request.executeAndWait().getGraphObject();
    FetchedAppSettings result = new FetchedAppSettings(safeGetBooleanFromResponse(supportResponse, SUPPORTS_ATTRIBUTION), safeGetBooleanFromResponse(supportResponse, SUPPORTS_IMPLICIT_SDK_LOGGING), safeGetStringFromResponse(supportResponse, NUX_CONTENT), safeGetBooleanFromResponse(supportResponse, NUX_ENABLED));
    fetchedAppSettings.put(applicationId, result);
    return result;
}
Also used : Bundle(android.os.Bundle) Request(com.facebook.Request) GraphObject(com.facebook.model.GraphObject)

Example 15 with GraphObject

use of com.facebook.model.GraphObject in project facebook-api-android-maven by avianey.

the class GraphObjectAdapter method rebuildSections.

private void rebuildSections() {
    sectionKeys = new ArrayList<String>();
    graphObjectsBySection = new HashMap<String, ArrayList<T>>();
    graphObjectsById = new HashMap<String, T>();
    displaySections = false;
    if (cursor == null || cursor.getCount() == 0) {
        return;
    }
    int objectsAdded = 0;
    cursor.moveToFirst();
    do {
        T graphObject = cursor.getGraphObject();
        if (!filterIncludesItem(graphObject)) {
            continue;
        }
        objectsAdded++;
        String sectionKeyOfItem = getSectionKeyOfGraphObject(graphObject);
        if (!graphObjectsBySection.containsKey(sectionKeyOfItem)) {
            sectionKeys.add(sectionKeyOfItem);
            graphObjectsBySection.put(sectionKeyOfItem, new ArrayList<T>());
        }
        List<T> section = graphObjectsBySection.get(sectionKeyOfItem);
        section.add(graphObject);
        graphObjectsById.put(getIdOfGraphObject(graphObject), graphObject);
    } while (cursor.moveToNext());
    if (sortFields != null) {
        final Collator collator = Collator.getInstance();
        for (List<T> section : graphObjectsBySection.values()) {
            Collections.sort(section, new Comparator<GraphObject>() {

                @Override
                public int compare(GraphObject a, GraphObject b) {
                    return compareGraphObjects(a, b, sortFields, collator);
                }
            });
        }
    }
    Collections.sort(sectionKeys, Collator.getInstance());
    displaySections = sectionKeys.size() > 1 && objectsAdded > DISPLAY_SECTIONS_THRESHOLD;
}
Also used : GraphObject(com.facebook.model.GraphObject) Collator(java.text.Collator)

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