示例#1
0
  public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
    mContext = context;
    mShowNotifications = PreferencesUtils.getSyncShowNotifications(mContext);

    // // noinspection ConstantConditions,PointlessBooleanExpression
    if (!BuildConfig.DEBUG) {
      Thread.setDefaultUncaughtExceptionHandler(
          new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
              LOGE(TAG, "Uncaught sync exception, suppressing UI in release build.", throwable);
            }
          });
    }
  }
示例#2
0
  @Override
  public void onPerformSync(
      Account account,
      Bundle extras,
      String authority,
      ContentProviderClient provider,
      SyncResult syncResult) {
    mIsCancelled = false;
    final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false);
    final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
    final boolean initialize = extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false);
    final int type = extras.getInt(SyncService.EXTRA_SYNC_TYPE, SyncService.FLAG_SYNC_ALL);

    LOGI(
        TAG,
        "Beginning sync for account "
            + account.name
            + ","
            + " uploadOnly="
            + uploadOnly
            + " manualSync="
            + manualSync
            + " initialize="
            + initialize
            + ", type="
            + type);

    if (initialize) {
      ContentResolver.setIsSyncable(account, authority, 1);
      ContentResolver.setSyncAutomatically(account, authority, true);
      Bundle b = new Bundle();
      ContentResolver.addPeriodicSync(account, authority, b, 8 * 60 * 60); // 8 hours
    }

    if (!shouldContinueSync(uploadOnly)) {
      return;
    }

    toggleReceiver(true);
    mShowNotifications = PreferencesUtils.getSyncShowNotifications(mContext);
    NotificationCompat.Builder builder = createNotificationBuilder();
    List<SyncTask> tasks = createTasks(mContext, type);
    for (int i = 0; i < tasks.size(); i++) {
      if (mIsCancelled) {
        showError(mContext.getString(R.string.sync_notification_error_cancel), null);
        break;
      }
      mCurrentTask = tasks.get(i);
      try {
        if (mShowNotifications) {
          builder.setProgress(tasks.size(), i, true);
          builder.setContentText(mContext.getString(mCurrentTask.getNotification()));
          NotificationCompat.InboxStyle detail = new NotificationCompat.InboxStyle(builder);
          detail.setSummaryText(
              String.format(
                  mContext.getString(R.string.sync_notification_step_summary),
                  i + 1,
                  tasks.size()));
          for (int j = i; j >= 0; j--) {
            detail.addLine(mContext.getString(tasks.get(j).getNotification()));
          }
          NotificationUtils.notify(mContext, NotificationUtils.ID_SYNC, builder);
        }
        mCurrentTask.execute(mContext, account, syncResult);
      } catch (Exception e) {
        LOGE(TAG, "Syncing " + mCurrentTask, e);
        syncResult.stats.numIoExceptions++;
        showError(e);
      }
    }
    toggleReceiver(false);
    NotificationUtils.cancel(mContext, NotificationUtils.ID_SYNC);
  }