private void showError(String text, String message) { if (!mShowNotifications) return; NotificationCompat.Builder builder = NotificationUtils.createNotificationBuilder(mContext, R.string.sync_notification_title) .setContentText(text); if (!TextUtils.isEmpty(message)) { builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message).setSummaryText(text)); } NotificationUtils.notify(mContext, NotificationUtils.ID_SYNC_ERROR, builder); }
private NotificationCompat.Builder createNotificationBuilder() { PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, new Intent(SyncService.ACTION_CANCEL_SYNC), 0); return NotificationUtils.createNotificationBuilder(mContext, R.string.sync_notification_title) .setPriority(NotificationCompat.PRIORITY_LOW) .setOngoing(true) .addAction(R.drawable.ic_stat_cancel, mContext.getString(R.string.cancel), pi); }
@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); }