Search in sources :

Example 26 with GraphObject

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

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));
    fetchedAppSettings.put(applicationId, result);
    return result;
}
Also used : Bundle(android.os.Bundle) GraphObject(com.facebook.model.GraphObject)

Example 27 with GraphObject

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

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)

Example 28 with GraphObject

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

the class AuthorizationClient method createReauthValidationBatch.

RequestBatch createReauthValidationBatch(final Result pendingResult) {
    // We need to ensure that the token we got represents the same fbid as the old one. We issue
    // a "me" request using the current token, a "me" request using the new token, and a "me/permissions"
    // request using the current token to get the permissions of the user.
    final ArrayList<String> fbids = new ArrayList<String>();
    final ArrayList<String> tokenPermissions = new ArrayList<String>();
    final String newToken = pendingResult.token.getToken();
    Request.Callback meCallback = new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            try {
                GraphUser user = response.getGraphObjectAs(GraphUser.class);
                if (user != null) {
                    fbids.add(user.getId());
                }
            } catch (Exception ex) {
            }
        }
    };
    String validateSameFbidAsToken = pendingRequest.getPreviousAccessToken();
    Request requestCurrentTokenMe = createGetProfileIdRequest(validateSameFbidAsToken);
    requestCurrentTokenMe.setCallback(meCallback);
    Request requestNewTokenMe = createGetProfileIdRequest(newToken);
    requestNewTokenMe.setCallback(meCallback);
    Request requestCurrentTokenPermissions = createGetPermissionsRequest(validateSameFbidAsToken);
    requestCurrentTokenPermissions.setCallback(new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            try {
                GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
                if (result != null) {
                    GraphObjectList<GraphObject> data = result.getData();
                    if (data != null && data.size() == 1) {
                        GraphObject permissions = data.get(0);
                        // The keys are the permission names.
                        tokenPermissions.addAll(permissions.asMap().keySet());
                    }
                }
            } catch (Exception ex) {
            }
        }
    });
    RequestBatch batch = new RequestBatch(requestCurrentTokenMe, requestNewTokenMe, requestCurrentTokenPermissions);
    batch.setBatchApplicationId(pendingRequest.getApplicationId());
    batch.addCallback(new RequestBatch.Callback() {

        @Override
        public void onBatchCompleted(RequestBatch batch) {
            try {
                Result result = null;
                if (fbids.size() == 2 && fbids.get(0) != null && fbids.get(1) != null && fbids.get(0).equals(fbids.get(1))) {
                    // Modify the token to have the right permission set.
                    AccessToken tokenWithPermissions = AccessToken.createFromTokenWithRefreshedPermissions(pendingResult.token, tokenPermissions);
                    result = Result.createTokenResult(pendingRequest, tokenWithPermissions);
                } else {
                    result = Result.createErrorResult(pendingRequest, "User logged in as different Facebook user.", null);
                }
                complete(result);
            } catch (Exception ex) {
                complete(Result.createErrorResult(pendingRequest, "Caught exception", ex.getMessage()));
            } finally {
                notifyBackgroundProcessingStop();
            }
        }
    });
    return batch;
}
Also used : GraphObjectList(com.facebook.model.GraphObjectList) GraphUser(com.facebook.model.GraphUser) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException) ActivityNotFoundException(android.content.ActivityNotFoundException) GraphMultiResult(com.facebook.model.GraphMultiResult) GraphMultiResult(com.facebook.model.GraphMultiResult)

Example 29 with GraphObject

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

the class TestSession method deleteTestAccount.

private void deleteTestAccount(String testAccountId, String appAccessToken) {
    Bundle parameters = new Bundle();
    parameters.putString("access_token", appAccessToken);
    Request request = new Request(null, testAccountId, parameters, HttpMethod.DELETE);
    Response response = request.executeAndWait();
    FacebookRequestError error = response.getError();
    GraphObject graphObject = response.getGraphObject();
    if (error != null) {
        Log.w(LOG_TAG, String.format("Could not delete test account %s: %s", testAccountId, error.getException().toString()));
    } else if (graphObject.getProperty(Response.NON_JSON_RESPONSE_PROPERTY) == (Boolean) false) {
        Log.w(LOG_TAG, String.format("Could not delete test account %s: unknown reason", testAccountId));
    }
}
Also used : Bundle(android.os.Bundle) GraphObject(com.facebook.model.GraphObject)

Example 30 with GraphObject

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

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