use of com.facebook.model.GraphObject in project facebook-api-android-maven by avianey.
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.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));
}
}
use of com.facebook.model.GraphObject in project phonegap-facebook-plugin by Wizcorp.
the class PickerFragment method updateAdapter.
void updateAdapter(SimpleGraphObjectCursor<T> data) {
if (adapter != null) {
// As we fetch additional results and add them to the table, we do not
// want the items displayed jumping around seemingly at random, frustrating the user's
// attempts at scrolling, etc. Since results may be added anywhere in
// the table, we choose to try to keep the first visible row in a fixed
// position (from the user's perspective). We try to keep it positioned at
// the same offset from the top of the screen so adding new items seems
// smoother, as opposed to having it "snap" to a multiple of row height
// We use the second row, to give context above and below it and avoid
// cases where the first row is only barely visible, thus providing little context.
// The exception is where the very first row is visible, in which case we use that.
View view = listView.getChildAt(1);
int anchorPosition = listView.getFirstVisiblePosition();
if (anchorPosition > 0) {
anchorPosition++;
}
GraphObjectAdapter.SectionAndItem<T> anchorItem = adapter.getSectionAndItem(anchorPosition);
final int top = (view != null && anchorItem.getType() != GraphObjectAdapter.SectionAndItem.Type.ACTIVITY_CIRCLE) ? view.getTop() : 0;
// Now actually add the results.
boolean dataChanged = adapter.changeCursor(data);
if (view != null && anchorItem != null) {
// Put the item back in the same spot it was.
final int newPositionOfItem = adapter.getPosition(anchorItem.sectionKey, anchorItem.graphObject);
if (newPositionOfItem != -1) {
listView.setSelectionFromTop(newPositionOfItem, top);
}
}
if (dataChanged && onDataChangedListener != null) {
onDataChangedListener.onDataChanged(PickerFragment.this);
}
if (selectionHint != null && !selectionHint.isEmpty() && data != null) {
data.moveToFirst();
boolean changed = false;
for (int i = 0; i < data.getCount(); i++) {
data.moveToPosition(i);
T graphObject = data.getGraphObject();
if (!graphObject.asMap().containsKey("id"))
continue;
Object obj = graphObject.getProperty("id");
if (!(obj instanceof String)) {
continue;
}
String id = (String) obj;
if (selectionHint.contains(id)) {
selectionStrategy.toggleSelection(id);
selectionHint.remove(id);
changed = true;
}
if (selectionHint.isEmpty()) {
break;
}
}
if (onSelectionChangedListener != null && changed) {
onSelectionChangedListener.onSelectionChanged(PickerFragment.this);
}
}
}
}
use of com.facebook.model.GraphObject in project phonegap-facebook-plugin by Wizcorp.
the class Utility method loadAppSettingsAsync.
public static void loadAppSettingsAsync(final Context context, final String applicationId) {
if (Utility.isNullOrEmpty(applicationId) || fetchedAppSettings.containsKey(applicationId) || initialAppSettingsLoadTask != null) {
return;
}
final String settingsKey = String.format(APP_SETTINGS_PREFS_KEY_FORMAT, applicationId);
initialAppSettingsLoadTask = new AsyncTask<Void, Void, GraphObject>() {
@Override
protected GraphObject doInBackground(Void... params) {
return getAppSettingsQueryResponse(applicationId);
}
@Override
protected void onPostExecute(GraphObject result) {
if (result != null) {
JSONObject resultJSON = result.getInnerJSONObject();
parseAppSettingsFromJSON(applicationId, resultJSON);
SharedPreferences sharedPrefs = context.getSharedPreferences(APP_SETTINGS_PREFS_STORE, Context.MODE_PRIVATE);
sharedPrefs.edit().putString(settingsKey, resultJSON.toString()).apply();
}
initialAppSettingsLoadTask = null;
}
};
initialAppSettingsLoadTask.execute((Void[]) null);
// Also see if we had a cached copy and use that immediately.
SharedPreferences sharedPrefs = context.getSharedPreferences(APP_SETTINGS_PREFS_STORE, Context.MODE_PRIVATE);
String settingsJSONString = sharedPrefs.getString(settingsKey, null);
if (!isNullOrEmpty(settingsJSONString)) {
JSONObject settingsJSON = null;
try {
settingsJSON = new JSONObject(settingsJSONString);
} catch (JSONException je) {
logd(LOG_TAG, je);
}
if (settingsJSON != null) {
parseAppSettingsFromJSON(applicationId, settingsJSON);
}
}
}
use of com.facebook.model.GraphObject in project phonegap-facebook-plugin by Wizcorp.
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;
}
use of com.facebook.model.GraphObject in project phonegap-facebook-plugin by Wizcorp.
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;
}
Aggregations