diff options
author | spingel | 2009-10-08 03:41:16 +0000 |
---|---|---|
committer | spingel | 2009-10-08 03:41:16 +0000 |
commit | a5853aaa8e2aec6ef7a6813d91626530739dbb74 (patch) | |
tree | 3e728598aa97541d0b1c78125a888e9a01216553 | |
parent | 0d79f0766a20e416c7a304bb22eead2579f4e0ce (diff) | |
download | org.eclipse.mylyn.tasks-a5853aaa8e2aec6ef7a6813d91626530739dbb74.tar.gz org.eclipse.mylyn.tasks-a5853aaa8e2aec6ef7a6813d91626530739dbb74.tar.xz org.eclipse.mylyn.tasks-a5853aaa8e2aec6ef7a6813d91626530739dbb74.zip |
NEW - bug 291237: suppress Task List synchronization when the user is idle
https://bugs.eclipse.org/bugs/show_bug.cgi?id=291237
3 files changed, 105 insertions, 13 deletions
diff --git a/org.eclipse.mylyn.tasks.ui/.options b/org.eclipse.mylyn.tasks.ui/.options index 54de56bb0..3978e6380 100644 --- a/org.eclipse.mylyn.tasks.ui/.options +++ b/org.eclipse.mylyn.tasks.ui/.options @@ -1 +1,2 @@ -org.eclipse.mylyn.tasks.ui/debug/httpclient=true
\ No newline at end of file +org.eclipse.mylyn.tasks.ui/debug/synchronization=true +org.eclipse.mylyn.tasks.ui/debug/httpclient=true diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TaskListSynchronizationScheduler.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TaskListSynchronizationScheduler.java index 3a2b081f8..e94ef8c81 100644 --- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TaskListSynchronizationScheduler.java +++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TaskListSynchronizationScheduler.java @@ -11,24 +11,49 @@ package org.eclipse.mylyn.internal.tasks.ui; +import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.jobs.IJobChangeEvent; import org.eclipse.core.runtime.jobs.JobChangeAdapter; +import org.eclipse.mylyn.commons.core.DateUtil; import org.eclipse.mylyn.internal.tasks.core.ITaskJobFactory; +import org.eclipse.mylyn.monitor.ui.IUserAttentionListener; import org.eclipse.mylyn.tasks.core.sync.SynchronizationJob; /** * @author Steffen Pingel */ -public class TaskListSynchronizationScheduler { +public class TaskListSynchronizationScheduler implements IUserAttentionListener { + + private static final boolean TRACE_ENABLED = Boolean.valueOf(Platform.getDebugOption("org.eclipse.mylyn.tasks.ui/debug/synchronization")); //$NON-NLS-1$ private long interval; + private long incactiveInterval; + private final ITaskJobFactory jobFactory; private SynchronizationJob refreshJob; + private boolean userActive; + + private long scheduledTime; + + private long lastSyncTime; + + private final JobChangeAdapter jobListener; + public TaskListSynchronizationScheduler(ITaskJobFactory jobFactory) { this.jobFactory = jobFactory; + this.userActive = true; + this.jobListener = new JobChangeAdapter() { + @Override + public void done(IJobChangeEvent event) { + scheduledTime = 0; + lastSyncTime = System.currentTimeMillis(); + reschedule(); + } + + }; } private SynchronizationJob createRefreshJob() { @@ -45,9 +70,49 @@ public class TaskListSynchronizationScheduler { } private synchronized void reschedule() { - if (this.interval != 0) { - refreshJob.schedule(interval); + long delay = this.interval; + if (delay != 0) { + if (!userActive) { + // triple scheduling interval each time + this.incactiveInterval *= 3; + delay = this.incactiveInterval; + if (TRACE_ENABLED) { + System.err.println("Set inactive interval to " + DateUtil.getFormattedDurationShort(this.incactiveInterval)); //$NON-NLS-1$ + } + } + if (this.scheduledTime != 0) { + if (this.scheduledTime < System.currentTimeMillis() + delay) { + // already scheduled, nothing to do + if (TRACE_ENABLED) { + System.err.println("Synchronzation already scheduled in " + DateUtil.getFormattedDurationShort(this.scheduledTime - System.currentTimeMillis())); //$NON-NLS-1$ + } + return; + } else { + // reschedule for an earlier time + cancel(); + } + } + + schedule(delay); + } + } + + private synchronized void cancel() { + // prevent listener from rescheduling due to cancel + if (TRACE_ENABLED) { + System.err.println("Canceling synchronization in " + DateUtil.getFormattedDurationShort(this.scheduledTime - System.currentTimeMillis())); //$NON-NLS-1$ } + refreshJob.removeJobChangeListener(jobListener); + refreshJob.cancel(); + refreshJob.addJobChangeListener(jobListener); + } + + private void schedule(long interval) { + if (TRACE_ENABLED) { + System.err.println("Scheduling synchronzation in " + DateUtil.getFormattedDurationShort(interval)); //$NON-NLS-1$ + } + this.scheduledTime = System.currentTimeMillis() + interval; + refreshJob.schedule(interval); } public synchronized void setInterval(long interval) { @@ -57,23 +122,47 @@ public class TaskListSynchronizationScheduler { public synchronized void setInterval(long delay, long interval) { if (this.interval != interval) { this.interval = interval; + this.incactiveInterval = interval; + this.scheduledTime = 0; + if (refreshJob != null) { - refreshJob.cancel(); + refreshJob.removeJobChangeListener(jobListener); + cancel(); refreshJob = null; } if (interval > 0) { refreshJob = createRefreshJob(); - refreshJob.addJobChangeListener(new JobChangeAdapter() { - @Override - public void done(IJobChangeEvent event) { - reschedule(); - } + refreshJob.addJobChangeListener(jobListener); + schedule(delay); + } + } + } - }); - refreshJob.schedule(delay); + public void userAttentionGained() { + synchronized (this) { + if (!userActive) { + if (TRACE_ENABLED) { + System.err.println("User activity detected"); //$NON-NLS-1$ + } + this.userActive = true; + // reset inactive interval each time the user becomes active + this.incactiveInterval = interval; + if (interval != 0 && System.currentTimeMillis() - lastSyncTime > interval) { + // the last sync was long ago, sync right away + cancel(); + schedule(0); + } else { + reschedule(); + } } } } + public void userAttentionLost() { + synchronized (this) { + this.userActive = false; + } + } + } diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TasksUiPlugin.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TasksUiPlugin.java index e2d8ca97a..f912cbd0a 100644 --- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TasksUiPlugin.java +++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/TasksUiPlugin.java @@ -51,6 +51,7 @@ import org.eclipse.mylyn.commons.net.AuthenticationType; import org.eclipse.mylyn.commons.net.WebUtil; import org.eclipse.mylyn.context.core.ContextCore; import org.eclipse.mylyn.internal.context.core.ContextCorePlugin; +import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin; import org.eclipse.mylyn.internal.provisional.commons.ui.AbstractNotification; import org.eclipse.mylyn.internal.provisional.commons.ui.CommonColors; import org.eclipse.mylyn.internal.provisional.commons.ui.CommonFonts; @@ -471,7 +472,7 @@ public class TasksUiPlugin extends AbstractUIPlugin { } } - @SuppressWarnings( { "restriction" }) + @SuppressWarnings({ "restriction" }) @Override public void start(BundleContext context) throws Exception { super.start(context); @@ -1257,6 +1258,7 @@ public class TasksUiPlugin extends AbstractUIPlugin { getBackupManager(); synchronizationScheduler = new TaskListSynchronizationScheduler(taskJobFactory); + MonitorUiPlugin.getDefault().getActivityContextManager().addListener(synchronizationScheduler); updateSynchronizationScheduler(true); } catch (Throwable t) { StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, |