private boolean initializeActiveSessionWithCachedToken(Context context) { if (context == null) { return false; } Session session = Session.getActiveSession(); if (session != null) { return session.isOpened(); } String applicationId = Utility.getMetadataApplicationId(context); if (applicationId == null) { return false; } return Session.openActiveSessionFromCache(context) != null; }
public void syncFB() { /* * FQL multiquery (see Facebook Android API for details) requesting: * 1) a maximum of 40 FUTURE events for the logged user; * 2) the event organizers for the retrieved events. * 3) friends birthdays in the next 2 months */ today_end = Calendar.getInstance(); today_begin = Calendar.getInstance(); today_begin.set(Calendar.HOUR_OF_DAY, 0); today_begin.set(Calendar.MINUTE, 0); today_begin.set(Calendar.SECOND, 0); char[] zeros = {'0', '0'}; DecimalFormat df = new DecimalFormat(String.valueOf(zeros)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz", Locale.getDefault()); int currentMonth = today_end.get(Calendar.MONTH); int currentYear = today_end.get(Calendar.YEAR); int currentDayMonth = today_end.get(Calendar.DAY_OF_MONTH); String eventsQuery = ""; String eventCreatorsQuery = ""; String birthdaysQuery = ""; if (sp.getBoolean("fb_evs_on", true)) { eventsQuery = "'events':'SELECT " + "creator, eid, end_time, has_profile_pic, is_date_only, location, name, pic_square, start_time, timezone, update_time " + "FROM event " + "WHERE eid IN (SELECT eid FROM event_member WHERE uid = me() ORDER BY start_time DESC) " + "AND start_time >= \"" + sdf.format(today_begin.getTime()) + "\""; eventCreatorsQuery = "'eventcreators':'SELECT name, id FROM profile WHERE id IN (SELECT creator FROM #events)'"; // if (isAppLaunchStage){ // today_end.set(Calendar.HOUR_OF_DAY, 23); // today_end.set(Calendar.MINUTE, 59); // today_end.set(Calendar.SECOND, 59); // eventsQuery += " AND start_time <= \"" + sdf.format(today_end.getTime()) + "\" ORDER BY // start_time DESC'"; // birthdaysQuery += " = \"" + df.format(currentMonth + 1) + "/" + // df.format(currentDayMonth) + "\"'"; // } // else { eventsQuery += " ORDER BY start_time DESC LIMIT 40'"; } if (sp.getBoolean("fb_bds_on", true)) { birthdaysQuery = "'birthdays':'SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,5),\"/" + currentYear + "\"), pic_square, uid FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND birthday_date != \"null\" " + "AND (substr(birthday_date,0,2)=\"" + df.format(currentMonth + 1) + "\" OR substr(birthday_date,0,2)=\"" + df.format(currentMonth + 2) + "\") " + "AND birthday_date"; birthdaysQuery += " > \"" + df.format(currentMonth + 1) + "/" + df.format(currentDayMonth) + "/" + currentYear + "\"'"; } // } String fqlQuery = "{"; if (eventsQuery.length() + birthdaysQuery.length() == 0) return; else { if (eventsQuery.length() > 0) { fqlQuery += eventsQuery + ", " + eventCreatorsQuery; } if (birthdaysQuery.length() > 0) { if (fqlQuery.length() > 1) fqlQuery += ", "; fqlQuery += birthdaysQuery; } fqlQuery += "}"; } Log.i(TAG, fqlQuery); Bundle params = new Bundle(); params.putString("q", fqlQuery); Session session = Session.openActiveSessionFromCache(this); if (session == null && !sp.getBoolean("fb_enable", false)) { Toast.makeText(getApplicationContext(), R.string.fb_login_missing, Toast.LENGTH_LONG).show(); return; } // Settings.setPlatformCompatibilityEnabled(true); Request request = new Request(session, "/fql", params, HttpMethod.GET, fbCallback, "v1.0"); Request.executeBatchAsync(request); return; }
@Override public void onCompleted(Response response) { /* * Check on FB session: if active one is null, * then die and optionally show a Toast (if FB sync is enabled). */ if (Session.openActiveSessionFromCache(getApplication()) == null) { Toast.makeText(getApplicationContext(), R.string.fb_login_missing, Toast.LENGTH_LONG) .show(); if (ptrl != null) { ptrl.setRefreshComplete(); } return; } json = null; /* * Extracting JSON object embedded to the returned graph object. * Dying on null response, and, if running in foreground, showing a Toast. */ if (response == null || response.getGraphObject() == null || response.getGraphObject().getInnerJSONObject() == null) { if (ptrl != null) { ptrl.setRefreshComplete(); Toast.makeText(getApplicationContext(), R.string.network_error, Toast.LENGTH_SHORT) .show(); } return; } // At this point, JSON object can be retrieved and split in two result sets (one per // query). json = response.getGraphObject().getInnerJSONObject(); JSONArray jArray; try { jArray = json.getJSONArray("data"); // Log.i(TAG, jArray.toString()); JSONObject o1 = jArray.getJSONObject(0); if (jArray.length() == 1) { // then only birthdays are being imported rs3 = o1.getJSONArray("fql_result_set"); } else { if (o1.getString("name").equals("birthdays")) { rs3 = o1.getJSONArray("fql_result_set"); } else { if (o1.getString("name").equals("events")) { rs1 = o1.getJSONArray("fql_result_set"); } else { rs2 = o1.getJSONArray("fql_result_set"); } } JSONObject o2 = jArray.getJSONObject(1); if (o2.getString("name").equals("events")) { rs1 = o2.getJSONArray("fql_result_set"); } else { if (o2.getString("name").equals("eventcreators")) { rs2 = o2.getJSONArray("fql_result_set"); } else { rs3 = o2.getJSONArray("fql_result_set"); } } if (jArray.length() > 2) { JSONObject o3 = jArray.getJSONObject(2); if (o3.getString("name").equals("eventcreators")) { rs2 = o3.getJSONArray("fql_result_set"); } else { if (o3.getString("name").equals("birthdays")) { rs3 = o3.getJSONArray("fql_result_set"); } else { rs1 = o3.getJSONArray("fql_result_set"); } } } } } catch (JSONException e) { Log.wtf(TAG, "Something is wrong with JSON response"); } // Now we can start CRUD operations on provider. // Log.i("rs1", rs1.toString()); // Log.i("rs2", rs2.toString()); Log.i("rs3", rs3.toString()); if (rs1 != null) queryFutureStoredEvents(); if (rs3 != null) queryFutureStoredBDays(); }