示例#1
0
  @Override
  public void run() {
    //			// Drill into the JSON response to find the content body
    //			JSONObject resp = (JSONObject) new JSONTokener(content.toString())
    //					.nextValue();
    //			// JSONObject query = resp.getJSONObject("uuid");
    //			// JSONObject pages = query.getJSONObject("pages");
    //			// JSONObject page = pages.getJSONObject((String)
    //			// pages.keys().next());
    //			// JSONArray revisions = page.getJSONArray("revisions");
    //			// JSONObject revision = revisions.getJSONObject(0);
    //			String uuid = resp.getString("uuid");
    //			inform(uuid, Events.FINISHED);
    //		} catch (IOException e) {
    //			Log.e(TAG, e.toString());
    //			inform("Error on syncing" + e.toString(), Events.FINISHED);
    //			e.printStackTrace();
    try {
      dbAccessor.beginTransaction();
      Long now = Long.valueOf(System.currentTimeMillis());
      Log.d(TAG, "Sync started at " + now.toString());

      // Get remote Uuid and it's lastUpdated time
      Pair<String, Long> remoteInfo = getRemoteInfo();
      inform(remoteInfo.getFirst(), Events.TEXT);
      Long lastUpdated = dbAccessor.getLastUpdated(remoteInfo.getFirst());
      inform(lastUpdated.toString(), Events.TEXT);

      // TODO Upload local updates

      // Download remote updates
      processRemoteUpdates(lastUpdated);

      dbAccessor.setTransactionSuccessful();

      inform("Finished", Events.FINISHED);
    } catch (Exception e) {
      Log.e(TAG, e.toString());
      inform("Error on syncing " + e.toString(), Events.ERROR);
      e.printStackTrace();
    } finally {
      dbAccessor.endTransaction();
    }
  }
示例#2
0
  private void updateTask(Task task) {
    boolean needUpdate = false;
    Task remote = new Task(task);
    try {
      dbAccessor.getDbObject(task, task.getUuid());
    } catch (InvalidKeyException e) {
      Log.d(TAG, "There are no local task " + task.getUuid());
      needUpdate = true;
    }
    if (!needUpdate && remote.getGlobalUpdated() > task.getGlobalUpdated()) {
      needUpdate = true;
      Log.d(TAG, "Local task is too old - " + task.getUuid());
    }

    if (needUpdate) {
      Log.d(TAG, "Should update task " + remote.getUuid());
      dbAccessor.setDbObject(remote);
    } else {
      Log.d(TAG, "Should NOT update task " + remote.getUuid());
      Log.d(TAG, "Local task - " + task.toString());
    }
  }