@Override
 public void gDriveConnected(final boolean connected, final boolean canceled) {
   mPullToRefreshLayout.setRefreshing(false);
   if (connected) {
     statusText.setText(R.string.gDriveConnected);
     gDrive.getListOfBackups(GDriveBackupsAdapter);
   } else if (gDrive.isError()) {
     GDriveBackupsAdapter.clear();
     statusText.setText(gDrive.getErrorMessage());
   } else {
     GDriveBackupsAdapter.clear();
     statusText.setText(R.string.gDriveDisconnected);
   }
   getActivity().invalidateOptionsMenu();
 }
 public int onStartCommand(Intent intent, int flags, int startId) {
   sp = PreferenceManager.getDefaultSharedPreferences(this);
   ptrl = AgendaNOW.getPtr_reference();
   if (ptrl != null) {
     ptrl.setRefreshing(true);
   }
   if (sp.getBoolean("fb_enable", false)) {
     res = getResources();
     syncFB();
   } else {
     if (ptrl != null) {
       ptrl.setRefreshComplete();
     }
   }
   return Service.START_REDELIVER_INTENT;
 }
 @Override
 public void showProgress(boolean inProgress, String text) {
   statusText.setText(text);
   mPullToRefreshLayout.setRefreshing(inProgress);
 }
 /*
  * Handling actions to execute on the completion of queries, updates, deletions, insertions.
  * Order of the operations:
  * 1) Querying for future FB events stored;
  * 2) Deletion of canceled FB events;
  * 3) For each event got from the JSON, check if there is a row to update
  *    or if there is no row: in the latter case, create it.
  *
  * NB: on completion of queries, updates, deletes, insertions the operation counters
  * are checked; if they are all set to 0, then sync process has been completed.
  * In this case, if there is a PTR layout active, then its animation is stopped.
  */
 @Override
 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
   switch (token) {
     case QUERY_FUTURE_STORED_EVS:
       deleteCanceledEvents(cursor);
       for (int i = 0; i < rs1.length(); i++) {
         try {
           JSONObject ev = rs1.getJSONObject(i);
           queryEventToUpdate(ev);
           queryEventToInsert(ev);
         } catch (JSONException e1) {
           e1.printStackTrace();
         }
       }
       checkIfUptodate();
       break;
     case QUERY_FUTURE_STORED_BDS:
       deleteCanceledBdays(cursor);
       for (int i = 0; i < rs3.length(); i++) {
         try {
           JSONObject bd = rs3.getJSONObject(i);
           queryBdayToUpdate(bd);
           queryBdayToInsert(bd);
         } catch (JSONException e1) {
           e1.printStackTrace();
         }
       }
       checkIfUptodate();
       break;
     case QUERY_EV_TO_UPDATE:
       CustomAsyncHandler updateHandler = new CustomAsyncHandler(getContentResolver(), this);
       if (cursor.getCount() > 0) {
         cursor.moveToFirst();
         if (ptrl != null) {
           if (!ptrl.isRefreshing()) ptrl.setRefreshing(true);
         }
         updateCounter++;
         JSONObject ev = (JSONObject) cookie;
         String selection = null;
         try {
           selection =
               EventTable.COLUMN_PLATFORM
                   + " = \""
                   + EventTable.PLATFORM_FACEBOOK
                   + "\" AND "
                   + EventTable.COLUMN_BEGIN
                   + " >= "
                   + today_begin.getTimeInMillis()
                   + " AND "
                   + EventTable.COLUMN_OID
                   + " = "
                   + ev.getString("eid")
                   + " AND "
                   + EventTable.COLUMN_UPDATED_TIME
                   + " <> "
                   + ev.getString("update_time");
           if (isAppLaunchStage) {
             selection += " AND " + EventTable.COLUMN_BEGIN + "<=" + today_end.getTimeInMillis();
           }
           updateHandler.startUpdate(
               UPDATE_STORED_EV,
               null,
               AgendaNowProvider.CONTENT_URI_EV,
               setContentValues(
                   ev,
                   UPDATE_STORED_EV,
                   cursor.getString(cursor.getColumnIndex(EventTable.COLUMN_PIC_URL))),
               selection,
               null);
         } catch (JSONException e) {
           e.printStackTrace();
         }
       }
       cursor.close();
       uChecksCounter--;
       checkIfFinished();
       break;
     case QUERY_BD_TO_UPDATE:
       CustomAsyncHandler updateHandler2 = new CustomAsyncHandler(getContentResolver(), this);
       if (cursor.getCount() > 0) {
         cursor.moveToFirst();
         if (ptrl != null) {
           if (!ptrl.isRefreshing()) ptrl.setRefreshing(true);
         }
         updateCounter++;
         JSONObject bd = (JSONObject) cookie;
         String selection = null;
         try {
           selection =
               EventTable.COLUMN_CALENDAR_ID
                   + " = \""
                   + EventTable.CALENDAR_ID_FB_BIRTHDAYS
                   + "\" AND "
                   + EventTable.COLUMN_OID
                   + " = "
                   + bd.getString("uid");
           updateHandler2.startUpdate(
               UPDATE_STORED_BD,
               null,
               AgendaNowProvider.CONTENT_URI_EV,
               setContentValues(
                   bd,
                   UPDATE_STORED_BD,
                   cursor.getString(cursor.getColumnIndex(EventTable.COLUMN_PIC_URL))),
               selection,
               null);
         } catch (JSONException e) {
           e.printStackTrace();
         }
       }
       cursor.close();
       uChecksCounter--;
       checkIfFinished();
       break;
     case QUERY_EV_TO_INSERT:
       CustomAsyncHandler insertHandler = new CustomAsyncHandler(getContentResolver(), this);
       if (cursor.getCount() < 1) {
         if (ptrl != null) {
           if (!ptrl.isRefreshing()) ptrl.setRefreshing(true);
         }
         insertionCounter++;
         JSONObject ev = (JSONObject) cookie;
         try {
           insertHandler.startInsert(
               INSERT_NEW_EV,
               null,
               AgendaNowProvider.CONTENT_URI_EV,
               setContentValues(ev, INSERT_NEW_EV, null));
         } catch (JSONException e) {
           e.printStackTrace();
         }
       }
       cursor.close();
       iChecksCounter--;
       checkIfFinished();
       break;
     case 101:
       CustomAsyncHandler insertHandler2 = new CustomAsyncHandler(getContentResolver(), this);
       if (cursor.getCount() < 1) {
         if (ptrl != null) {
           if (!ptrl.isRefreshing()) ptrl.setRefreshing(true);
         }
         insertionCounter++;
         JSONObject bd = (JSONObject) cookie;
         try {
           insertHandler2.startInsert(
               INSERT_NEW_BD,
               null,
               AgendaNowProvider.CONTENT_URI_EV,
               setContentValues(bd, INSERT_NEW_BD, null));
         } catch (JSONException e) {
           e.printStackTrace();
         }
       }
       cursor.close();
       iChecksCounter--;
       checkIfFinished();
       break;
   }
 }