aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/welcome/UsagePermissionPage.java
blob: d8413bb45b769ea986432c29a3947e9816a01572 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.welcome;

import com.android.sdkstats.SdkStatsPermissionDialog;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;

import java.net.URL;

/** Page which displays the permission dialog for collecting usage statistics */
public class UsagePermissionPage extends WizardPage implements SelectionListener {
    private Link mLink;
    private Button mYesRadio;
    private Button mNoRadio;

    /**
     * Create the wizard.
     */
    public UsagePermissionPage() {
        super("usageData");
        setTitle("Contribute Usage Statistics?");
        setDescription(SdkStatsPermissionDialog.NOTICE_TEXT);
    }

    /**
     * Create contents of the wizard.
     *
     * @param parent parent to create page into
     */
    @Override
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);

        setControl(container);
        container.setLayout(new GridLayout(1, false));

        Label label = new Label(container, SWT.WRAP);
        GridData gd_lblByChoosingTo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_lblByChoosingTo.widthHint = 580;
        label.setLayoutData(gd_lblByChoosingTo);
        label.setText(SdkStatsPermissionDialog.BODY_TEXT);

        Label blankLine = new Label(container, SWT.NONE);

        Label questionLabel = new Label(container, SWT.NONE);
        questionLabel.setText("Send usage statistics to Google?");

        mYesRadio = new Button(container, SWT.RADIO);
        mYesRadio.setText("Yes");
        mYesRadio.addSelectionListener(this);

        mNoRadio = new Button(container, SWT.RADIO);
        mNoRadio.setText("No");
        mNoRadio.addSelectionListener(this);

        Label laterLabel = new Label(container, SWT.WRAP);
        GridData gdLaterLabel = new GridData(SWT.FILL, SWT.BOTTOM, false, true, 1, 1);
        gdLaterLabel.widthHint = 580;
        laterLabel.setLayoutData(gdLaterLabel);
        laterLabel.setText("If you later decide to change this setting, you can do so in the " +
               "options panel under Android > Usage Stats");

        mLink = new Link(container, SWT.NONE);
        mLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        mLink.setText(SdkStatsPermissionDialog.PRIVACY_POLICY_LINK_TEXT);
        mLink.addSelectionListener(this);

        validatePage();
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        mYesRadio.setFocus();
    }

    boolean isUsageCollectionApproved() {
        return mYesRadio.getSelection();
    }

    @Override
    public void widgetSelected(SelectionEvent event) {
        if (event.getSource() == mLink) {
            try {
                IWorkbench workbench = PlatformUI.getWorkbench();
                IWebBrowser browser = workbench.getBrowserSupport().getExternalBrowser();
                browser.openURL(new URL(event.text));
            } catch (Exception e) {
                String message = String.format("Could not open browser. Vist\n%1$s\ninstead.",
                        event.text);
                MessageDialog.openError(getWizard().getContainer().getShell(),
                        "Browser Error", message);
            }
        } else {
            // Radio buttons selected
            validatePage();
        }
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
    }

    private void validatePage() {
        String error = null;

        if (!mYesRadio.getSelection() && !mNoRadio.getSelection()) {
            error = "Select Yes or No";
        }

        setPageComplete(error == null);
        if (error != null) {
            setMessage(error, IMessageProvider.ERROR);
        } else {
            setErrorMessage(null);
            setMessage(null);
        }
    }
}