Search in sources :

Example 1 with GraphObject

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

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 2 with GraphObject

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

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 3 with GraphObject

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

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)

Example 4 with GraphObject

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

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 5 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