Skip to main content
summaryrefslogtreecommitdiffstats
blob: a518be4860c26733e26b863dfb49750b701472e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package org.eclipse.mylar.internal.tasklist;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylar.core.util.MylarStatusHandler;
import org.eclipse.mylar.core.util.ITimerThreadListener;
import org.eclipse.mylar.core.util.TimerThread;

/**
 * Timer that periodically runs saveRequested() on its client as a job
 * 
 * @author Wesley Coelho
 */
public class BackgroundSaveTimer implements ITimerThreadListener {

	private final static int DEFAULT_SAVE_INTERVAL = 1 * 60 * 1000;
	
	private int saveInterval = DEFAULT_SAVE_INTERVAL;

	private IBackgroundSaveListener listener = null;

	private TimerThread timer = null;

	private boolean forceSyncExec = false;
	
	public BackgroundSaveTimer(IBackgroundSaveListener listener) {
		this.listener = listener;
		timer = new TimerThread(saveInterval / 1000); // This constructor wants seconds
		timer.addListener(this);
	}

	public void start() {
		timer.start();
	}
	

	public void stop() {
		timer.kill();
	}
	
	public void setSaveIntervalMillis(int saveIntervalMillis) {
		this.saveInterval = saveIntervalMillis;
		timer.setTimeoutMillis(saveIntervalMillis);
	}

	public int getSaveIntervalMillis() {
		return saveInterval;
	}

	/**
	 * For testing
	 */
	public void setForceSyncExec(boolean forceSyncExec) {
		this.forceSyncExec = forceSyncExec;
	}

	/**
	 * Called by the ActivityTimerThread Calls save in a new job
	 */
	public void fireTimedOut() {
		try {
			if (!forceSyncExec) {
				final SaveJob job = new SaveJob("Saving Task Data", listener);
				job.schedule();
			} else {
				listener.saveRequested();
			}
		} catch (RuntimeException e) {
			MylarStatusHandler.log("Could not schedule save job", this);
		}
	}

	/** Job that makes the save call */
	private class SaveJob extends Job {
		private IBackgroundSaveListener listener = null;

		public SaveJob(String name, IBackgroundSaveListener listener) {
			super(name);
			this.listener = listener;
		}

		protected IStatus run(IProgressMonitor monitor) {
			listener.saveRequested();
			return Status.OK_STATUS;
		}
	}

	public void intervalElapsed() {
		// ignore
	}

}

Back to the top