Exemple #1
0
 private void sync(String api, Bundle args) throws IOException {
   if (args == null) {
     return;
   }
   switch (api) {
     case Api.API_MEMBER:
       {
         UserIdentityApi userIdentityApi = new UserIdentityApi(mContext);
         String accountName = args.getString("accountName");
         userIdentityApi.verifyUserIdentity(accountName);
         break;
       }
     case Api.API_TOPICS_LATEST:
     case Api.API_TOPICS_HOT:
     case Api.API_TOPICS_SPECIFIC:
       {
         Api feedsApi = new Api(mContext, args, api, V2exDataHandler.DATA_KEY_FEEDS);
         // save the remote data to the database
         mDataHandler.applyData(new Bundle[] {feedsApi.sync(Api.HttpMethod.GET)});
         break;
       }
     case Api.API_NODES_ALL:
       {
         Api allNodesApi = new Api(mContext, args, api, V2exDataHandler.DATA_KEY_NODES);
         mDataHandler.applyData(new Bundle[] {allNodesApi.sync(Api.HttpMethod.GET)});
         break;
       }
     case Api.API_NODES_SPECIFIC:
       {
         Api specificNodesApi = new Api(mContext, args, api, V2exDataHandler.DATA_KEY_NODES);
         mDataHandler.applyData(new Bundle[] {specificNodesApi.sync(Api.HttpMethod.GET)});
         break;
       }
     case Api.API_REVIEWS:
       {
         Api reviewsApi = new Api(mContext, args, api, V2exDataHandler.DATA_KEY_REVIEWS);
         mDataHandler.applyData(new Bundle[] {reviewsApi.sync(Api.HttpMethod.GET)});
         break;
       }
   }
 }
Exemple #2
0
  /**
   * Attempts to perform data synchronization.
   *
   * @param syncResult (optional) the sync result object to update with statistics.
   * @param account the account associated with this sync
   * @return Whether or not the synchronization made any changes to the data.
   */
  public boolean performSync(SyncResult syncResult, Account account, Bundle extras) {

    final boolean remoteSync = extras.getBoolean(SyncAdapter.EXTRA_SYNC_REMOTE, true);

    // remote sync consists of these operations, which we try one by one (and tolerate
    // individual failures on each)
    String[] apisToPerform =
        remoteSync
            ? new String[] {Api.API_TOPICS_LATEST, Api.API_TOPICS_HOT}
            : new String[] {extras.getString(Api.ARG_API_NAME)};

    for (String api : apisToPerform) {
      try {
        sync(api, extras);
      } catch (Throwable throwable) {
        throwable.printStackTrace();
        EventBus.getDefault().postSticky(new ExceptionEvent(mContext.getString(R.string.err_io)));
        LOGE(TAG, "Error performing remote sync.");
        increaseIoExceptions(syncResult);
      }
    }

    int operations = mDataHandler.getContentProviderOperationsDone();
    if (syncResult != null && syncResult.stats != null) {
      syncResult.stats.numEntries += operations;
      syncResult.stats.numUpdates += operations;
    }

    LOGD(
        TAG,
        "SYNC STATS:\n"
            + " *  Account synced: "
            + (account == null ? "null" : account.name)
            + "\n"
            + " *  Content provider operations: "
            + operations);

    return true;
  }