Пример #1
0
  /**
   * Synchronization fails for various reasons. Notify the user to retry.
   *
   * @param msg error message
   */
  void notifyRetry(String msg) {
    syncHelper.uiHandler.appendToTextView(
        syncHelper.instructionView, msg + "\nSynchronization fails. Try again.\n");

    syncHelper.startTime = -1;
    syncHelper.uiHandler.enableButton(syncHelper.sync, true);
  }
Пример #2
0
  public void processPendingScheduleUpdates() {
    try {
      // Operate on a local copy of the schedule update list so as not to block
      // the main thread adding to this list
      List<Intent> scheduleUpdates = new ArrayList<Intent>();
      synchronized (mScheduleUpdates) {
        scheduleUpdates.addAll(mScheduleUpdates);
        mScheduleUpdates.clear();
      }

      SyncHelper syncHelper = new SyncHelper(this);
      for (Intent updateIntent : scheduleUpdates) {
        String sessionId = updateIntent.getStringExtra(EXTRA_SESSION_ID);
        boolean inSchedule = updateIntent.getBooleanExtra(EXTRA_IN_SCHEDULE, false);
        LOGI(
            TAG,
            "addOrRemoveSessionFromSchedule:"
                + " sessionId="
                + sessionId
                + " inSchedule="
                + inSchedule);
        syncHelper.addOrRemoveSessionFromSchedule(this, sessionId, inSchedule);
      }
    } catch (HandlerException.NoDevsiteProfileException e) {
      // The user doesn't have a profile, message this to them.
      // TODO: better UX for this type of error
      LOGW(TAG, "To sync your schedule to the web, login to developers.google.com.", e);
      mUiThreadHandler.post(
          new Runnable() {
            @Override
            public void run() {
              Toast.makeText(
                      ScheduleUpdaterService.this,
                      "To sync your schedule to the web, login to developers.google.com.",
                      Toast.LENGTH_LONG)
                  .show();
            }
          });

    } catch (IOException e) {
      // TODO: do something useful here, like revert the changes locally in the
      // content provider to maintain client/server sync
      LOGE(TAG, "Error processing schedule update", e);
    }
  }
  private ContentValues getQuestionContentValues(JSONObject question) {
    Log.d("QuestionSync", question.toString());
    ContentValues values = new ContentValues();
    values.put(
        QuestionsEntry.COLUMN_TIMESTAMP,
        SyncHelper.getUnixMillisecondsFromJsonDate(question.optString("LastChangedTime")));
    values.put(QuestionsEntry.COLUMN_QUESTION, question.optString("Content"));
    values.put(
        QuestionsEntry.COLUMN_DATE,
        SyncHelper.getUnixMillisecondsFromJsonDate(question.optString("CreationTime")));

    values.put(QuestionsEntry.COLUMN_TITLE, question.optString("Title"));
    values.put(QuestionsEntry.COLUMN_IS_PRIVATE, question.optString("IsPrivate"));
    values.put(
        QuestionsEntry.COLUMN_QUESTIONSTATE,
        getQuestionState(question.optInt("QuestionState")).toString());

    String answer = question.optString("Answer");
    if (answer != null) {
      values.put(QuestionsEntry.COLUMN_ANSWER_ID, SyncHelper.getIdFromURI(answer));
    }
    String answerer = question.optString("Answerer");
    if (answer != null) {
      values.put(QuestionsEntry.COLUMN_ANSWERER_ID, SyncHelper.getIdFromURI(answerer));
    }
    String asker = question.optString("User");
    if (answer != null) {
      values.put(QuestionsEntry.COLUMN_ASKER_ID, SyncHelper.getIdFromURI(asker));
    }
    String backendid = question.optString("Id");

    if (backendid != null) {
      values.put(QuestionsEntry.COLUMN_BACKEND_ID, SyncHelper.getIdFromURI(backendid));
    }
    return values;
  }
 @Override
 protected List<Business> doInBackground(Double... params) {
   return SyncHelper.getInstance()
       .getBusinessListBySortedLocation(uid, state, params[0], params[1]);
 }
Пример #5
0
  @Override
  public void run() {

    // send out sync pck
    byte[] syncPckBytes;
    try {
      syncPckBytes = new Message(Message.SYNC).toBytes();
      DatagramPacket syncPck =
          new DatagramPacket(
              syncPckBytes, syncPckBytes.length, syncHelper.group, MainActivity.PORT);
      syncHelper.ms.send(syncPck);
    } catch (JSONException e) {
      Log.e(TAG, e.getMessage());
      notifyRetry(e.getMessage());
      return;
    } catch (IOException e) {
      Log.e(TAG, e.getMessage());
      notifyRetry(e.getMessage());
      return;
    }
    // record start_time
    syncHelper.startTime = System.currentTimeMillis();
    // wait for ACK
    byte[] recvBuf = new byte[512];
    DatagramPacket recvPck = new DatagramPacket(recvBuf, recvBuf.length);
    try {
      syncHelper.ms.setSoTimeout(RECV_TIMEOUT);
    } catch (SocketException e1) {
      Log.e(TAG, e1.getMessage());
      notifyRetry(e1.getMessage());
      return;
    }
    while (true) {
      try {
        syncHelper.ms.receive(recvPck);
      } catch (SocketTimeoutException e) {
        // No sync_ack received
        notifyRetry("No SYNC_ACK received.");
        break;
      } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        notifyRetry(e.getMessage());
        break;
      }
      // check the recv pck
      Message recvMsg;
      try {
        recvMsg = new Message(recvBuf, recvPck.getLength());
        if (recvMsg.getInt("type") != Message.SYNC_ACK) {
          syncHelper.uiHandler.appendToTextView(
              syncHelper.instructionView, "Received non-ACK message.");
          continue;
        }
        // sync_ack successfully received
        syncHelper.uiHandler.appendToTextView(
            syncHelper.instructionView,
            "Synchronization succeeds. "
                + "Press start on both sender and receiver"
                + " to schedule one round's measurement.");
        syncHelper.uiHandler.enableButton(syncHelper.start, true);
        break;
      } catch (JSONException e) {
        Log.i(TAG, e.getMessage());
        continue;
      }
    }
  }
  public void syncQuestions(JSONArray questionResultArray)
      throws AuthenticationException, ParseException, OperationCanceledException,
          AuthenticatorException, JSONException, IOException, RemoteException {

    // JSONArray questionsToUpload = new JSONArray();
    ArrayList<JSONObject> questionsToAddToDB = new ArrayList<JSONObject>();
    // Log.d("sync", questionResultArray.toString());
    Uri uri = BackendContentProvider.CONTENT_QUESTIONS;

    Cursor questionsCursor;

    questionsCursor = contentProviderClient.query(uri, null, null, null, null);
    Log.d("sync", "uri");
    idColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_BACKEND_ID);
    int localIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_BACKEND_ID);

    //			questionColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_QUESTION);
    //			dateColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_DATE);
    //			titleColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_TITLE);
    //			answerIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_ANSWER_ID);
    //			answererIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_ANSWERER_ID);
    //			isPrivateColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_IS_PRIVATE);
    //			askerIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_ASKER_ID);
    //			questionStateColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_QUESTIONSTATE);

    questionsCursor.moveToFirst();

    for (int i = 0; i < questionResultArray.length(); i++) {
      questionsToAddToDB.add(questionResultArray.getJSONObject(i));
    }

    while (!questionsCursor.isAfterLast()) {

      JSONObject serverQuestion = null;
      boolean questionFoundInResult = false;

      for (int i = 0; i < questionsToAddToDB.size(); i++) {

        serverQuestion = questionsToAddToDB.get(i);

        if (SyncHelper.getIdFromURI(serverQuestion.getString("Id"))
            == (questionsCursor.getInt(idColumn))) {
          questionFoundInResult = true;
          questionsToAddToDB.remove(i);

          if (SyncHelper.isServerObjectNewer(serverQuestion, questionsCursor)) {
            updateQuestion(serverQuestion, questionsCursor.getInt(localIdColumn));
          }
          break;
        }
      }
      if (!questionFoundInResult) {

        String deleteUri = uri + "/" + questionsCursor.getInt(localIdColumn);
        questionsCursor.moveToNext();
        contentProviderClient.delete(Uri.parse(deleteUri), null, null);
      }
      questionsCursor.moveToNext();
    }

    questionsCursor.close();

    for (int i = 0; i < questionsToAddToDB.size(); i++) {
      addQuestionToDb(questionsToAddToDB.get(i));
    }
  }