private static synchronized void retrieveTestAccountsForAppIfNeeded() { if (appTestAccounts != null) { return; } appTestAccounts = new HashMap<String, TestAccount>(); // The data we need is split across two different FQL tables. We construct two queries, submit // them // together (the second one refers to the first one), then cross-reference the results. // Get the test accounts for this app. String testAccountQuery = String.format( "SELECT id,access_token FROM test_account WHERE app_id = %s", testApplicationId); // Get the user names for those accounts. String userQuery = "SELECT uid,name FROM user WHERE uid IN (SELECT id FROM #test_accounts)"; Bundle parameters = new Bundle(); // Build a JSON string that contains our queries and pass it as the 'q' parameter of the query. JSONObject multiquery; try { multiquery = new JSONObject(); multiquery.put("test_accounts", testAccountQuery); multiquery.put("users", userQuery); } catch (JSONException exception) { throw new FacebookException(exception); } parameters.putString("q", multiquery.toString()); // We need to authenticate as this app. parameters.putString("access_token", getAppAccessToken()); Request request = new Request(null, "fql", parameters, null); Response response = request.executeAndWait(); if (response.getError() != null) { throw response.getError().getException(); } FqlResponse fqlResponse = response.getGraphObjectAs(FqlResponse.class); GraphObjectList<FqlResult> fqlResults = fqlResponse.getData(); if (fqlResults == null || fqlResults.size() != 2) { throw new FacebookException("Unexpected number of results from FQL query"); } // We get back two sets of results. The first is from the test_accounts query, the second from // the users query. Collection<TestAccount> testAccounts = fqlResults.get(0).getFqlResultSet().castToListOf(TestAccount.class); Collection<UserAccount> userAccounts = fqlResults.get(1).getFqlResultSet().castToListOf(UserAccount.class); // Use both sets of results to populate our static array of accounts. populateTestAccounts(testAccounts, userAccounts); return; }
// 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; }
private TestAccount createTestAccountAndFinishAuth() { Bundle parameters = new Bundle(); parameters.putString("installed", "true"); parameters.putString("permissions", getPermissionsString()); parameters.putString("access_token", getAppAccessToken()); // If we're in shared mode, we want to rename this user to encode its permissions, so we can // find it later // in another shared session. If we're in private mode, don't bother renaming it since we're // just going to // delete it at the end of the session. if (mode == Mode.SHARED) { parameters.putString( "name", String.format("Shared %s Testuser", getSharedTestAccountIdentifier())); } String graphPath = String.format("%s/accounts/test-users", testApplicationId); Request createUserRequest = new Request(null, graphPath, parameters, HttpMethod.POST); Response response = createUserRequest.executeAndWait(); FacebookRequestError error = response.getError(); TestAccount testAccount = response.getGraphObjectAs(TestAccount.class); if (error != null) { finishAuthOrReauth(null, error.getException()); return null; } else { assert testAccount != null; // If we are in shared mode, store this new account in the dictionary so we can re-use it // later. if (mode == Mode.SHARED) { // Remember the new name we gave it, since we didn't get it back in the results of the // create request. testAccount.setName(parameters.getString("name")); storeTestAccount(testAccount); } finishAuthWithTestAccount(testAccount); return testAccount; } }
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)); } }
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)); } }