aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/export/KeystoreSelectionPage.java
blob: eabee15a25b9dbcb078d4ce9ee8975516bbe78c3 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (C) 2008 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.wizards.export;

import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
import com.android.ide.eclipse.adt.internal.wizards.export.ExportWizard.ExportWizardPage;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import java.io.File;

/**
 * Keystore selection page. This page allows to choose to create a new keystore or use an
 * existing one.
 */
final class KeystoreSelectionPage extends ExportWizardPage {

    private final ExportWizard mWizard;
    private Button mUseExistingKeystore;
    private Button mCreateKeystore;
    private Text mKeystore;
    private Text mKeystorePassword;
    private Label mConfirmLabel;
    private Text mKeystorePassword2;
    private boolean mDisableOnChange = false;

    protected KeystoreSelectionPage(ExportWizard wizard, String pageName) {
        super(pageName);
        mWizard = wizard;

        setTitle("Keystore selection");
        setDescription(""); //TODO
    }

    @Override
    public void createControl(Composite parent) {
        Composite composite = new Composite(parent, SWT.NULL);
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        GridLayout gl = new GridLayout(3, false);
        composite.setLayout(gl);

        GridData gd;

        mUseExistingKeystore = new Button(composite, SWT.RADIO);
        mUseExistingKeystore.setText("Use existing keystore");
        mUseExistingKeystore.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
        gd.horizontalSpan = 3;
        mUseExistingKeystore.setSelection(true);

        mCreateKeystore = new Button(composite, SWT.RADIO);
        mCreateKeystore.setText("Create new keystore");
        mCreateKeystore.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
        gd.horizontalSpan = 3;

        new Label(composite, SWT.NONE).setText("Location:");
        mKeystore = new Text(composite, SWT.BORDER);
        mKeystore.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
        final Button browseButton = new Button(composite, SWT.PUSH);
        browseButton.setText("Browse...");
        browseButton.addSelectionListener(new SelectionAdapter() {
           @Override
           public void widgetSelected(SelectionEvent e) {
               FileDialog fileDialog;
               if (mUseExistingKeystore.getSelection()) {
                   fileDialog = new FileDialog(browseButton.getShell(),SWT.OPEN);
                   fileDialog.setText("Load Keystore");
               } else {
                   fileDialog = new FileDialog(browseButton.getShell(),SWT.SAVE);
                   fileDialog.setText("Select Keystore Name");
               }

               String fileName = fileDialog.open();
               if (fileName != null) {
                   mKeystore.setText(fileName);
               }
           }
        });

        new Label(composite, SWT.NONE).setText("Password:");
        mKeystorePassword = new Text(composite, SWT.BORDER | SWT.PASSWORD);
        mKeystorePassword.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
        mKeystorePassword.addVerifyListener(sPasswordVerifier);
        new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
        gd.heightHint = gd.widthHint = 0;

        mConfirmLabel = new Label(composite, SWT.NONE);
        mConfirmLabel.setText("Confirm:");
        mKeystorePassword2 = new Text(composite, SWT.BORDER | SWT.PASSWORD);
        mKeystorePassword2.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
        mKeystorePassword2.addVerifyListener(sPasswordVerifier);
        new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
        gd.heightHint = gd.widthHint = 0;
        mKeystorePassword2.setEnabled(false);

        // Show description the first time
        setErrorMessage(null);
        setMessage(null);
        setControl(composite);

        mUseExistingKeystore.addSelectionListener(new SelectionAdapter() {
           @Override
           public void widgetSelected(SelectionEvent e) {
               boolean createStore = !mUseExistingKeystore.getSelection();
               mKeystorePassword2.setEnabled(createStore);
               mConfirmLabel.setEnabled(createStore);
               mWizard.setKeystoreCreationMode(createStore);
               onChange();
            }
        });

        mKeystore.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                mWizard.setKeystore(mKeystore.getText().trim());
                onChange();
            }
        });

        mKeystorePassword.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                mWizard.setKeystorePassword(mKeystorePassword.getText());
                onChange();
            }
        });

        mKeystorePassword2.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                onChange();
            }
        });
    }

    @Override
    public IWizardPage getNextPage() {
        if (mUseExistingKeystore.getSelection()) {
            return mWizard.getKeySelectionPage();
        }

        return mWizard.getKeyCreationPage();
    }

    @Override
    void onShow() {
        // fill the texts with information loaded from the project.
        if ((mProjectDataChanged & DATA_PROJECT) != 0) {
            // reset the keystore/alias from the content of the project
            IProject project = mWizard.getProject();

            // disable onChange for now. we'll call it once at the end.
            mDisableOnChange = true;

            String keystore = ProjectHelper.loadStringProperty(project,
                    ExportWizard.PROPERTY_KEYSTORE);
            if (keystore != null) {
                mKeystore.setText(keystore);
            }

            // reset the passwords
            mKeystorePassword.setText(""); //$NON-NLS-1$
            mKeystorePassword2.setText(""); //$NON-NLS-1$

            // enable onChange, and call it to display errors and enable/disable pageCompleted.
            mDisableOnChange = false;
            onChange();
        }
    }

    /**
     * Handles changes and update the error message and calls {@link #setPageComplete(boolean)}.
     */
    private void onChange() {
        if (mDisableOnChange) {
            return;
        }

        setErrorMessage(null);
        setMessage(null);

        boolean createStore = !mUseExistingKeystore.getSelection();

        // checks the keystore path is non null.
        String keystore = mKeystore.getText().trim();
        if (keystore.length() == 0) {
            setErrorMessage("Enter path to keystore.");
            setPageComplete(false);
            return;
        } else {
            File f = new File(keystore);
            if (f.exists() == false) {
                if (createStore == false) {
                    setErrorMessage("Keystore does not exist.");
                    setPageComplete(false);
                    return;
                }
            } else if (f.isDirectory()) {
                setErrorMessage("Keystore path is a directory.");
                setPageComplete(false);
                return;
            } else if (f.isFile()) {
                if (createStore) {
                    setErrorMessage("File already exists.");
                    setPageComplete(false);
                    return;
                }
            }
        }

        String value = mKeystorePassword.getText();
        if (value.length() == 0) {
            setErrorMessage("Enter keystore password.");
            setPageComplete(false);
            return;
        } else if (createStore && value.length() < 6) {
            setErrorMessage("Keystore password is too short - must be at least 6 characters.");
            setPageComplete(false);
            return;
        }

        if (createStore) {
            if (mKeystorePassword2.getText().length() == 0) {
                setErrorMessage("Confirm keystore password.");
                setPageComplete(false);
                return;
            }

            if (mKeystorePassword.getText().equals(mKeystorePassword2.getText()) == false) {
                setErrorMessage("Keystore passwords do not match.");
                setPageComplete(false);
                return;
            }
        }

        setPageComplete(true);
    }
}