Skip to main content
summaryrefslogtreecommitdiffstats
blob: 14d10b4d9350461d4a1774a870f7464c65059b59 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2009 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.internal.monitor.usage;

import java.util.Date;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin;
import org.eclipse.mylyn.internal.monitor.usage.wizards.UsageSubmissionWizard;
import org.eclipse.mylyn.internal.monitor.usage.wizards.UsageSubmissionWizardDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.UIJob;

/**
 * @author Shawn Minto
 */
public class CheckForUploadJob extends UIJob {

	public CheckForUploadJob(Display display) {
		super(display, Messages.CheckForUploadJob_Check_For_Upload);
	}

	@Override
	public IStatus runInUIThread(IProgressMonitor monitor) {
		if (getDisplay() == null || getDisplay().isDisposed() || PlatformUI.getWorkbench().isClosing()) {
			return Status.CANCEL_STATUS;
		}
		if (!MonitorUiPlugin.getDefault().suppressConfigurationWizards() && ContextCorePlugin.getDefault() != null) {
			checkForStatisticsUpload();
		}
		return Status.OK_STATUS;
	}

	synchronized void checkForStatisticsUpload() {

		Date currentTime = new Date();
		if (shouldAskForUpload(currentTime)) {

			String ending = getUserPromptDelay() == 1 ? "" : "s"; //$NON-NLS-1$//$NON-NLS-2$
			MessageDialog message = new MessageDialog(Display.getDefault().getActiveShell(),
					Messages.UiUsageMonitorPlugin_Send_Usage_Feedback, null,
					Messages.UiUsageMonitorPlugin_Help_Improve_Eclipse_And_Mylyn, MessageDialog.QUESTION, new String[] {
							Messages.UiUsageMonitorPlugin_Submit_Feedback,
							NLS.bind(Messages.UiUsageMonitorPlugin_Remind_Me_In_X_Days, getUserPromptDelay(), ending),
							Messages.UiUsageMonitorPlugin_Dont_Ask_Again, }, 0);
			int result = message.open();
			if (result == 0) {
				// time must be stored right away into preferences, to prevent
				// other threads
				UiUsageMonitorPlugin.getDefault().getPreferenceStore().setValue(
						MonitorPreferenceConstants.PREF_PREVIOUS_TRANSMIT_DATE, currentTime.getTime());

				if (!UiUsageMonitorPlugin.getDefault().getPreferenceStore().contains(
						MonitorPreferenceConstants.PREF_MONITORING_MYLYN_ECLIPSE_ORG_CONSENT_VIEWED)
						|| !UiUsageMonitorPlugin.getDefault().getPreferenceStore().getBoolean(
								MonitorPreferenceConstants.PREF_MONITORING_MYLYN_ECLIPSE_ORG_CONSENT_VIEWED)) {
					MessageDialog consentMessage = new MessageDialog(Display.getDefault().getActiveShell(),
							Messages.UiUsageMonitorPlugin_Consent, null, Messages.UiUsageMonitorPlugin_All_Data_Public,
							MessageDialog.INFORMATION, new String[] { IDialogConstants.OK_LABEL }, 0);
					consentMessage.open();
					UiUsageMonitorPlugin.getDefault().getPreferenceStore().setValue(
							MonitorPreferenceConstants.PREF_MONITORING_MYLYN_ECLIPSE_ORG_CONSENT_VIEWED, true);
				}

				UsageSubmissionWizard wizard = new UsageSubmissionWizard();
				wizard.init(PlatformUI.getWorkbench(), null);
				// Instantiates the wizard container with the wizard and
				// opens it
				WizardDialog dialog = new UsageSubmissionWizardDialog(Display.getDefault().getActiveShell(), wizard);
				dialog.create();
				dialog.open();

				/*
				 * the UI usage report is loaded asynchronously so there's no
				 * synchronous way to know if it failed if (wizard.failed()) {
				 * lastTransmit.setTime(currentTime.getTime() + DELAY_ON_FAILURE -
				 * studyParameters.getTransmitPromptPeriod());
				 * plugin.getPreferenceStore().setValue(MylynMonitorPreferenceConstants.PREF_PREVIOUS_TRANSMIT_DATE,
				 * currentTime.getTime()); }
				 */

			} else {
				if (result == 1) {
					UiUsageMonitorPlugin.getDefault().userCancelSubmitFeedback(currentTime, true);
				} else {
					UiUsageMonitorPlugin.getDefault().getPreferenceStore().setValue(
							MonitorPreferenceConstants.PREF_MONITORING_ENABLE_SUBMISSION, false);
				}
			}
			message.close();
		}
	}

	private boolean shouldAskForUpload(Date currentTime) {
		if (UiUsageMonitorPlugin.getDefault().isSubmissionWizardOpen()) {
			return false;
		}

		StudyParameters studyParameters = UiUsageMonitorPlugin.getDefault().getStudyParameters();
		if (!UiUsageMonitorPlugin.getDefault().isMonitoringEnabled() || studyParameters == null
				|| !studyParameters.shouldPromptForSubmission()) {
			return false;
		}

		Date lastTransmit = UiUsageMonitorPlugin.getDefault().getLastTransmitDate();

		if (currentTime.getTime() > lastTransmit.getTime() + studyParameters.getTransmitPromptPeriod()
				&& UiUsageMonitorPlugin.getDefault().getPreferenceStore().getBoolean(
						MonitorPreferenceConstants.PREF_MONITORING_ENABLE_SUBMISSION)) {
			return true;

		}
		return false;
	}

	private long getUserPromptDelay() {
		return UiUsageMonitorPlugin.DELAY_ON_USER_REQUEST / UiUsageMonitorPlugin.DAY;
	}

}

Back to the top