1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.welcome;
18 
19 import com.android.sdkstats.SdkStatsPermissionDialog;
20 
21 import org.eclipse.jface.dialogs.IMessageProvider;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Link;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.browser.IWebBrowser;
36 
37 import java.net.URL;
38 
39 /** Page which displays the permission dialog for collecting usage statistics */
40 public class UsagePermissionPage extends WizardPage implements SelectionListener {
41     private Link mLink;
42     private Button mYesRadio;
43     private Button mNoRadio;
44 
45     /**
46      * Create the wizard.
47      */
UsagePermissionPage()48     public UsagePermissionPage() {
49         super("usageData");
50         setTitle("Contribute Usage Statistics?");
51         setDescription(SdkStatsPermissionDialog.NOTICE_TEXT);
52     }
53 
54     /**
55      * Create contents of the wizard.
56      *
57      * @param parent parent to create page into
58      */
59     @Override
createControl(Composite parent)60     public void createControl(Composite parent) {
61         Composite container = new Composite(parent, SWT.NULL);
62 
63         setControl(container);
64         container.setLayout(new GridLayout(1, false));
65 
66         Label label = new Label(container, SWT.WRAP);
67         GridData gd_lblByChoosingTo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
68         gd_lblByChoosingTo.widthHint = 580;
69         label.setLayoutData(gd_lblByChoosingTo);
70         label.setText(SdkStatsPermissionDialog.BODY_TEXT);
71 
72         Label blankLine = new Label(container, SWT.NONE);
73 
74         Label questionLabel = new Label(container, SWT.NONE);
75         questionLabel.setText("Send usage statistics to Google?");
76 
77         mYesRadio = new Button(container, SWT.RADIO);
78         mYesRadio.setText("Yes");
79         mYesRadio.addSelectionListener(this);
80 
81         mNoRadio = new Button(container, SWT.RADIO);
82         mNoRadio.setText("No");
83         mNoRadio.addSelectionListener(this);
84 
85         Label laterLabel = new Label(container, SWT.WRAP);
86         GridData gdLaterLabel = new GridData(SWT.FILL, SWT.BOTTOM, false, true, 1, 1);
87         gdLaterLabel.widthHint = 580;
88         laterLabel.setLayoutData(gdLaterLabel);
89         laterLabel.setText("If you later decide to change this setting, you can do so in the " +
90                "options panel under Android > Usage Stats");
91 
92         mLink = new Link(container, SWT.NONE);
93         mLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
94         mLink.setText(SdkStatsPermissionDialog.PRIVACY_POLICY_LINK_TEXT);
95         mLink.addSelectionListener(this);
96 
97         validatePage();
98     }
99 
100     @Override
setVisible(boolean visible)101     public void setVisible(boolean visible) {
102         super.setVisible(visible);
103         mYesRadio.setFocus();
104     }
105 
isUsageCollectionApproved()106     boolean isUsageCollectionApproved() {
107         return mYesRadio.getSelection();
108     }
109 
110     @Override
widgetSelected(SelectionEvent event)111     public void widgetSelected(SelectionEvent event) {
112         if (event.getSource() == mLink) {
113             try {
114                 IWorkbench workbench = PlatformUI.getWorkbench();
115                 IWebBrowser browser = workbench.getBrowserSupport().getExternalBrowser();
116                 browser.openURL(new URL(event.text));
117             } catch (Exception e) {
118                 String message = String.format("Could not open browser. Vist\n%1$s\ninstead.",
119                         event.text);
120                 MessageDialog.openError(getWizard().getContainer().getShell(),
121                         "Browser Error", message);
122             }
123         } else {
124             // Radio buttons selected
125             validatePage();
126         }
127     }
128 
129     @Override
widgetDefaultSelected(SelectionEvent e)130     public void widgetDefaultSelected(SelectionEvent e) {
131     }
132 
validatePage()133     private void validatePage() {
134         String error = null;
135 
136         if (!mYesRadio.getSelection() && !mNoRadio.getSelection()) {
137             error = "Select Yes or No";
138         }
139 
140         setPageComplete(error == null);
141         if (error != null) {
142             setMessage(error, IMessageProvider.ERROR);
143         } else {
144             setErrorMessage(null);
145             setMessage(null);
146         }
147     }
148 }
149