summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/device/ui/wizard/WizardMainPage.java
blob: 35f7457907051d6f95253bb53305a105d10448f9 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* 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.motorola.studio.android.emulator.device.ui.wizard;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.sequoyah.device.framework.ui.wizard.DefaultDeviceTypeMenuWizardPage;
import org.eclipse.sequoyah.device.framework.ui.wizard.IInstanceProperties;
import org.eclipse.swt.widgets.Composite;

import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.SdkConstants;
import com.motorola.studio.android.adt.SdkUtils;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.emulator.EmulatorPlugin;
import com.motorola.studio.android.emulator.device.IDevicePropertiesConstants;
import com.motorola.studio.android.emulator.device.instance.AndroidDeviceInstance;
import com.motorola.studio.android.emulator.device.ui.AbstractPropertiesComposite;
import com.motorola.studio.android.emulator.device.ui.AbstractPropertiesComposite.PropertyCompositeChangeEvent;
import com.motorola.studio.android.emulator.device.ui.AbstractPropertiesComposite.PropertyCompositeChangeListener;
import com.motorola.studio.android.emulator.device.ui.PropertiesMainComposite;
import com.motorola.studio.android.emulator.i18n.EmulatorNLS;

/**
 * DESCRIPTION:
 * <br>
 * This class represents the first wizard page for Android Emulator Device Instance creation.
 * <br>
 * It shows all main information and validates it, setting an appropriate error message when
 * applicable.
 * <br>
 * RESPONSIBILITY:
 * <br>
 * - Allow user to enter main information for creating a new Android Emulator Device Instance
 * <br>
 * - Validate main information entered by user
 * <br>
 * COLABORATORS:
 * <br>
 * WizardPage: extends this class
 * <br>
 * PropertiesMainComposite: uses this composite as the main widget
 * <br>
 * USAGE:
 * <br>
 * This wizard page must be added as the first page on the class implementing the New Android
 * Emulator Device Instance Wizard.
 */
public class WizardMainPage extends WizardPage implements IInstanceProperties
{
    private PropertiesMainComposite mainComposite;

    DefaultDeviceTypeMenuWizardPage tmlPage = null;

    private PropertyCompositeChangeListener listener = new PropertyCompositeChangeListener()
    {
        public void compositeChanged(PropertyCompositeChangeEvent e)
        {

            String errorMessage = mainComposite.getErrorMessage();

            if (errorMessage != null)
            {
                setErrorMessage(errorMessage);
                setPageComplete(false);
            }
            else
            {
                setErrorMessage(null);
                setPageComplete(true);
                setMessage(EmulatorNLS.UI_WizardMainPage_PageName);
            }

        }
    };

    /**
     * Creates a WizardMainPage object.
     */
    public WizardMainPage()
    {
        super(EmulatorNLS.UI_WizardMainPage_PageName);
    }

    /**
     * Creates the UI for this wizard page.
     * It uses the PropertiesMainComposite only.
     */
    public void createControl(Composite parent)
    {

        // Collecting usage data for statistical purpose
        try
        {
            StudioLogger.collectUsageData(StudioLogger.WHAT_EMULATOR_CREATION_WIZARD,
                    StudioLogger.KIND_EMULATOR, StudioLogger.DESCRIPTION_DEFAULT,
                    EmulatorPlugin.PLUGIN_ID, EmulatorPlugin.getDefault().getBundle().getVersion()
                            .toString());
        }
        catch (Throwable e)
        {
            //Do nothing, but error on the log should never prevent app from working
        }

        setTitle(EmulatorNLS.UI_General_WizardTitle);
        setErrorMessage(null);
        setMessage(EmulatorNLS.UI_WizardMainPage_PageName);

        IAndroidTarget vmTarget = null;
        String vmSkin = ""; //$NON-NLS-1$
        String vmPath;
        String timeout;
        String useVnc;
        String useProxy;
        String abiType = SdkConstants.ABI_ARMEABI;
        String useSnapshot;
        String saveSnapshot;
        String startFromSnapshot;

        tmlPage = (DefaultDeviceTypeMenuWizardPage) this.getPreviousPage();

        IAndroidTarget targets[] = SdkUtils.getAllTargets();

        if ((targets != null) && (targets.length > 0))
        {

            // Sort the targets array by comparing the API level of them
            Arrays.sort(targets, new Comparator<IAndroidTarget>()
            {

                public int compare(IAndroidTarget o1, IAndroidTarget o2)
                {
                    int returnValue;
                    if (o1.getVersion().getApiLevel() == o2.getVersion().getApiLevel())
                    {
                        returnValue = 0;
                    }
                    else if (o1.getVersion().getApiLevel() > o2.getVersion().getApiLevel())
                    {
                        returnValue = 1;
                    }
                    else
                    {
                        returnValue = -1;
                    }

                    return returnValue;
                }

            });

            // Gets the first target with the highest API

            int maxAPILevel = targets[targets.length - 1].getVersion().getApiLevel();

            for (IAndroidTarget t : targets)
            {
                if (t.getVersion().getApiLevel() == maxAPILevel)
                {
                    vmTarget = t;
                    break;
                }

            }

            String skins[] = vmTarget.getSkins();
            vmSkin = vmTarget.getDefaultSkin();
            List<String> skinsList = Arrays.asList(skins);

            /*
             * Workaround to select WVGA skin on JIL SDK because HVGA skin is broken
             */
            if (SdkUtils.isJILSdk())
            {
                String tmpVmSkin = null;
                int i = 0;
                while ((tmpVmSkin == null) && (i < skins.length))
                {
                    if (skins[i].toLowerCase().trim().equals("wvga"))
                    {
                        tmpVmSkin = skins[i];
                    }
                    i++;
                }
                if (tmpVmSkin != null)
                {
                    vmSkin = tmpVmSkin;
                }
            }

            if (!skinsList.contains(vmSkin))
            {
                vmSkin = skins[0];
            }
        }

        vmPath = IDevicePropertiesConstants.defaultVmPath;

        // get the default properties value
        Properties defaultProperties = new Properties();
        AndroidDeviceInstance.populateWithDefaultProperties(defaultProperties);
        timeout = defaultProperties.getProperty(IDevicePropertiesConstants.timeout);
        useVnc = defaultProperties.getProperty(IDevicePropertiesConstants.useVnc);
        useProxy = defaultProperties.getProperty(IDevicePropertiesConstants.useProxy);
        useSnapshot = defaultProperties.getProperty(IDevicePropertiesConstants.useSnapshots);
        saveSnapshot = defaultProperties.getProperty(IDevicePropertiesConstants.saveSnapshot);
        startFromSnapshot = defaultProperties.getProperty(IDevicePropertiesConstants.startFromSnapshot);

        // When removing the emulator definition extension, remove this hardcoded set as
        // well as the constant declaration from the Activator. If the Mot skin plugin is used,
        // find another way to set this variable
        mainComposite =
                new PropertiesMainComposite(parent, tmlPage.getInstanceName(),
                        EmulatorPlugin.DEFAULT_EMULATOR_DEFINITION, timeout,
                        Boolean.parseBoolean(useVnc), Boolean.parseBoolean(useProxy),
                        Boolean.parseBoolean(useSnapshot), Boolean.parseBoolean(saveSnapshot),
                        Boolean.parseBoolean(startFromSnapshot), vmTarget, vmSkin, vmPath, abiType,
                        false, true, true);

        AbstractPropertiesComposite.addCompositeChangeListener(listener);

        setControl(mainComposite);

        if ((targets == null) || ((targets != null) && (targets.length <= 0)))
        {
            setMessage(EmulatorNLS.WizardMainPage_NO_SDK_CONFIGURED_MSG);
            setPageComplete(false);
            return;
        }

        String initialMessage = mainComposite.getErrorMessage();

        if (initialMessage != null)
        {
            setMessage(initialMessage);
        }
        setPageComplete(initialMessage == null);
    }

    @Override
    public void setVisible(boolean visible)
    {
        if (visible)
        {
            mainComposite.setName(tmlPage.getInstanceName());
        }
        super.setVisible(visible);
    }

    /**
     * Retrieves the skin id associated with this instance
     * 
     * @return the skin id
     */
    public String getSkinId()
    {
        return mainComposite.getSkinId();
    }

    /**
     * Retrieves the timeout associated with this instance
     * 
     * @return the timeout
     */
    public String getTimeout()
    {
        return mainComposite.getTimeout();
    }

    /**
     * Retrieves the vnc option associated with this instance
     * 
     * @return the timeout
     */
    public String getUseVnc()
    {
        return mainComposite.getUseVnc();
    }

    /**
     * Retrieves the proxy option associated with this instance
     * 
     * @return the timeout
     */
    public String getUseProxy()
    {
        return mainComposite.getUseProxy();
    }

    /**
     * Retrieves the emulator definition name associated with this instance
     * 
     * @return the emulator definition name
     */
    public String getEmulatorDefId()
    {
        // When removing the emulator definition extension, remove this hardcoded set as
        // well as the constant declaration from the Activator. If the Mot skin plugin is used,
        // find another way to set this variable
        return EmulatorPlugin.DEFAULT_EMULATOR_DEFINITION;
    }

    public String getUseSnapshot()
    {
        return mainComposite.getUseSnapshot();
    }

    public void setInstanceName(String name)
    {
        mainComposite.setName(name);
    }

    /**
     * Retrieves VM target
     */
    public IAndroidTarget getVmTarget()
    {
        return mainComposite.getVmTarget();
    }

    /**
     * Retrieves VM target
     */
    public String getAbiType()
    {
        return mainComposite.getAbiType();
    }

    /**
     * Retrieves VM skin
     */
    public String getVmSkin()
    {
        return mainComposite.getVmSkin();
    }

    /**
     * Retrieves VM path
     */
    public String getVmPath()
    {
        return mainComposite.getVmPath();
    }

    /**
     * Retrieves SD Card info
     */
    public String getSDCard()
    {
        return mainComposite.getSDCard();
    }

    /**
     * Retrieves Command line
     */
    public String getCommandLine()
    {
        //The command line shall be editable through the GUI        
        Properties defaultProperties = new Properties();
        AndroidDeviceInstance.populateWithDefaultProperties(defaultProperties);
        return defaultProperties.getProperty(IDevicePropertiesConstants.commandline);
    }

    @Override
    public boolean isPageComplete()
    {
        return (mainComposite != null) && (mainComposite.getErrorMessage() == null);
    }

    @Override
    public void dispose()
    {
        AbstractPropertiesComposite.removeCompositeChangeListener(listener);
        setControl(null);
        if (mainComposite != null)
        {
            mainComposite.dispose();
            mainComposite = null;
        }

        super.dispose();
    }

    public Properties getProperties()
    {
        Properties properties = new Properties();

        properties.setProperty(IDevicePropertiesConstants.timeout, this.getTimeout());
        properties.setProperty(IDevicePropertiesConstants.useVnc, this.getUseVnc());
        properties.setProperty(IDevicePropertiesConstants.useProxy, this.getUseProxy());
        properties.setProperty(IDevicePropertiesConstants.emulatorDefId, this.getEmulatorDefId());
        properties.setProperty(IDevicePropertiesConstants.skinId, this.getSkinId());
        properties.setProperty(IDevicePropertiesConstants.vmSkin, this.getVmSkin());
        properties.setProperty(IDevicePropertiesConstants.vmPath, this.getVmPath());
        properties.setProperty(IDevicePropertiesConstants.abiType, this.getAbiType());
        properties.setProperty(IDevicePropertiesConstants.useSnapshots, this.getUseSnapshot());
        properties.setProperty(IDevicePropertiesConstants.saveSnapshot, this.getSaveSnapshot());
        properties.setProperty(IDevicePropertiesConstants.startFromSnapshot, this.getstartFromSnapshot());

        if (this.getVmTarget() != null)
        {
            properties.setProperty(IDevicePropertiesConstants.vmTarget, this.getVmTarget()
                    .getName());
        }
        else
        {
            properties.setProperty(IDevicePropertiesConstants.vmTarget, ""); //$NON-NLS-1$
        }

        return properties;
    }

    /**
     * @return
     */
    public String getstartFromSnapshot()
    {
        return mainComposite.getstartFromSnapshot();
    }

    /**
     * @return
     */
    public String getSaveSnapshot()
    {
        return mainComposite.getSaveSnapshot();
    }
}