private boolean shouldContinueSync(boolean uploadOnly) { if (uploadOnly) { LOGW(TAG, "Upload only, returning."); return false; } if (!NetworkUtils.isOnline(mContext)) { LOGI(TAG, "Skipping sync; offline"); return false; } if (PreferencesUtils.getSyncOnlyCharging(mContext) && !NetworkUtils.isCharging(mContext)) { LOGI(TAG, "Skipping sync; not charging"); return false; } if (PreferencesUtils.getSyncOnlyWifi(mContext) && !NetworkUtils.isOnWiFi(mContext)) { LOGI(TAG, "Skipping sync; not on wifi"); return false; } if (NetworkUtils.isBatteryLow(mContext)) { LOGI(TAG, "Skipping sync; battery low"); return false; } return true; }
private List<SyncTask> createTasks(Context context, final int type) { BggService service = Adapter.createWithAuth(context); List<SyncTask> tasks = new ArrayList<SyncTask>(); if ((type & SyncService.FLAG_SYNC_COLLECTION) == SyncService.FLAG_SYNC_COLLECTION) { if (PreferencesUtils.isSyncStatus(context)) { long lastCompleteSync = Authenticator.getLong(context, SyncService.TIMESTAMP_COLLECTION_COMPLETE); if (lastCompleteSync >= 0 && DateTimeUtils.howManyDaysOld(lastCompleteSync) < 7) { tasks.add(new SyncCollectionListModifiedSince(service)); } else { tasks.add(new SyncCollectionListComplete(service)); } } else { LOGI(TAG, "...no statuses set to sync"); } tasks.add(new SyncCollectionListUnupdated(service)); tasks.add(new SyncCollectionDetailOldest(service)); tasks.add(new SyncCollectionDetailUnupdated(service)); tasks.add(new SyncCollectionDetailMissing(service)); } if ((type & SyncService.FLAG_SYNC_PLAYS_UPLOAD) == SyncService.FLAG_SYNC_PLAYS_UPLOAD) { tasks.add(new SyncPlaysUpload(service)); } if ((type & SyncService.FLAG_SYNC_PLAYS_DOWNLOAD) == SyncService.FLAG_SYNC_PLAYS_DOWNLOAD) { tasks.add(new SyncPlays(service)); } if ((type & SyncService.FLAG_SYNC_BUDDIES) == SyncService.FLAG_SYNC_BUDDIES) { tasks.add(new SyncBuddiesList(service)); tasks.add(new SyncBuddiesDetailOldest(service)); tasks.add(new SyncBuddiesDetailUnupdated(service)); } return tasks; }
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); } }); } }
@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); }