summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/EmulatorPlugin.java
blob: eaf7512507fbf2ed9d03f23373b7910c836810c7 (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
446
447
448
449
450
451
452
453
454
455
456
/*
* 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;

import static com.motorola.studio.android.common.log.StudioLogger.error;
import static com.motorola.studio.android.common.log.StudioLogger.info;

import java.util.List;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.osgi.util.NLS;
import org.eclipse.sequoyah.device.common.utilities.BasePlugin;
import org.eclipse.sequoyah.device.framework.events.IInstanceListener;
import org.eclipse.sequoyah.device.framework.events.InstanceAdapter;
import org.eclipse.sequoyah.device.framework.events.InstanceEvent;
import org.eclipse.sequoyah.device.framework.events.InstanceEvent.InstanceEventType;
import org.eclipse.sequoyah.device.framework.events.InstanceEventManager;
import org.eclipse.sequoyah.device.framework.factory.DeviceTypeRegistry;
import org.eclipse.sequoyah.device.framework.model.IDeviceType;
import org.eclipse.sequoyah.device.framework.model.IService;
import org.eclipse.sequoyah.device.framework.model.handler.IServiceHandler;
import org.eclipse.sequoyah.device.framework.model.handler.ServiceHandler;
import org.eclipse.sequoyah.device.framework.ui.DeviceUIPlugin;
import org.eclipse.sequoyah.device.framework.ui.view.InstanceMgtView;
import org.eclipse.sequoyah.device.framework.ui.wizard.DefaultDeviceTypeMenuWizardPage;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.motorola.studio.android.AndroidPlugin;
import com.motorola.studio.android.adt.DDMSFacade;
import com.motorola.studio.android.adt.DdmsRunnable;
import com.motorola.studio.android.adt.StudioAndroidEventManager;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.preferences.DialogWithToggleUtils;
import com.motorola.studio.android.emulator.core.devfrm.DeviceFrameworkManager;
import com.motorola.studio.android.emulator.core.model.IAndroidEmulatorInstance;
import com.motorola.studio.android.emulator.device.AndroidDeviceUtils;
import com.motorola.studio.android.emulator.device.IDevicePropertiesConstants;
import com.motorola.studio.android.emulator.device.SequoyahLogRedirector;
import com.motorola.studio.android.emulator.device.instance.AndroidDevInstListener;
import com.motorola.studio.android.emulator.device.instance.AndroidDeviceInstance;
import com.motorola.studio.android.emulator.device.refresh.InstancesListRefresh;
import com.motorola.studio.android.emulator.device.sync.DeviceViewsSync;
import com.motorola.studio.android.emulator.i18n.EmulatorNLS;
import com.motorola.studio.android.emulator.ui.view.AbstractAndroidView;

/**
 * The activator class controls the plug-in life cycle
 */
public class EmulatorPlugin extends AbstractUIPlugin
{
    // The plug-in ID
    public static final String PLUGIN_ID = "com.motorola.studio.android.emulator";

    // The shared instance
    private static EmulatorPlugin plugin;

    // The ID of the device declared by this plug-in
    public static final String DEVICE_ID = PLUGIN_ID + ".androidDevice";

    // The ID of all the status declared by this plug-in
    public static final String STATUS_ONLINE_ID = PLUGIN_ID + ".status.online";

    public static final String STATUS_OFFLINE_NO_DATA = PLUGIN_ID + ".status.offlineNoData";

    public static final String STATUS_OFFLINE = PLUGIN_ID + ".status.offline";

    public static final String STATUS_NOT_AVAILABLE = PLUGIN_ID + ".status.notavailable";

    public static final String SERVICE_INIT_ID = PLUGIN_ID + ".initEmulatorService";

    public static final String STOP_SERVICE_ID = PLUGIN_ID + ".stopService";

    public static final String START_SERVICE_ID = PLUGIN_ID + ".startService";

    private static final String DEV_MANAGER_HELP = DeviceUIPlugin.PLUGIN_ID + ".devmgr";

    private static final String NEW_DEVICE_HELP = DeviceUIPlugin.PLUGIN_ID + ".newdev";

    /**
     * Reference the id of the extension point with the default Android Emulator definitions...
     */
    public static String DEFAULT_EMULATOR_DEFINITION =
            "com.motorola.studio.android.emulator10.defaultEmulatorDefinitions";

    public static final String FORCE_ATTR = "force";

    public static final String EMULATOR_UNEXPECTEDLY_STOPPED = "emulator.unexpectedly.stopped";

    private static AndroidDevInstListener instanceListener;

    private static DdmsRunnable connectedListener = new DdmsRunnable()
    {
        @Override
        public void run(String serialNumber)
        {
            if (DDMSFacade.isEmulator(serialNumber))
            {
                InstancesListRefresh.refresh();

                info("New Device connected at " + serialNumber);

                String vmName = DDMSFacade.getNameBySerialNumber(serialNumber);

                if (vmName != null)
                {
                    DeviceFrameworkManager devFrameworkManager =
                            DeviceFrameworkManager.getInstance();

                    IAndroidEmulatorInstance instance =
                            devFrameworkManager.getInstanceByName(vmName);

                    if (instance instanceof AndroidDeviceInstance)
                    {
                        final AndroidDeviceInstance emulatorInstance =
                                (AndroidDeviceInstance) instance;

                        AndroidDeviceUtils.fireDummyStartTransition(emulatorInstance, serialNumber);

                    }
                }
            }
        }
    };

    private static DdmsRunnable disconnectedListener = new DdmsRunnable()
    {
        @Override
        public void run(String serialNum)
        {
            if (DDMSFacade.isEmulator(serialNum))
            {
                info("Device just disconnected from serial=" + serialNum);

                String vmName = DDMSFacade.getNameBySerialNumber(serialNum);

                if (vmName != null)
                {
                    IAndroidEmulatorInstance instance =
                            DeviceFrameworkManager.getInstance().getInstanceByName(vmName);

                    if ((instance != null) && (instance.isStarted()))
                    {
                        try
                        {
                            instance.stop(true);
                            DialogWithToggleUtils.showError(EMULATOR_UNEXPECTEDLY_STOPPED,
                                    EmulatorNLS.GEN_Error, NLS.bind(
                                            EmulatorNLS.ERR_AndroidLogicPlugin_EmulatorStopped,
                                            instance.getName()));

                        }
                        catch (Exception e)
                        {
                            error("Error trying to force the stop process on instance associated to disconnected device: "
                                    + instance);
                        }
                    }

                    if (instance instanceof AndroidDeviceInstance)
                    {
                        ((AndroidDeviceInstance) instance).setNameSuffix(null);
                        InstanceEventManager.getInstance().notifyListeners(
                                new InstanceEvent(InstanceEventType.INSTANCE_UPDATED,
                                        (AndroidDeviceInstance) instance));
                    }
                }
                else
                {
                    // This block is executed if we get a vmName == null condition. This can happen if
                    // ADT updates the device in a way that it makes the name not accessible.
                    // 
                    // What is needed to be done in such a case is to iterate on all TmL instances, looking for 
                    // objects that contain serialNumber as the instance suffix. This guarantees that we will not
                    // leave a not consistent serial number being displayed at the Instance Management view. 

                    for (IAndroidEmulatorInstance instance : DeviceFrameworkManager.getInstance()
                            .getAllInstances())
                    {
                        if (instance instanceof AndroidDeviceInstance)
                        {
                            AndroidDeviceInstance androidInstance =
                                    (AndroidDeviceInstance) instance;
                            String instanceSuffix = androidInstance.getNameSuffix();

                            if ((instanceSuffix != null) && instanceSuffix.equals(serialNum))
                            {
                                androidInstance.setNameSuffix(null);

                                InstanceEventManager.getInstance().notifyListeners(
                                        new InstanceEvent(InstanceEventType.INSTANCE_UPDATED,
                                                androidInstance));
                            }
                        }
                    }
                }
            }
        }
    };

    private static final Runnable sdkLoaderListener = new Runnable()
    {
        @Override
        public void run()
        {
            InstancesListRefresh.refresh();
            if (!Platform.getOS().equals(Platform.OS_MACOSX))
            {
                IPreferenceStore store = getDefault().getPreferenceStore();
                boolean deviceStartupOptionsUpdated =
                        store.getBoolean("DeviceStartupOptionsUpdated");
                if (!deviceStartupOptionsUpdated)
                {
                    for (IAndroidEmulatorInstance instance : DeviceFrameworkManager.getInstance()
                            .getAllInstances())
                    {
                        if (instance instanceof AndroidDeviceInstance)
                        {
                            AndroidDeviceInstance androidInstance =
                                    (AndroidDeviceInstance) instance;

                            Properties emuProperties = androidInstance.getProperties();

                            String commandline =
                                    emuProperties.getProperty(
                                            IDevicePropertiesConstants.commandline, "");
                            if (commandline.contains("-no-window"))
                            {
                                commandline = commandline.replace("-no-window", "");
                            }
                            emuProperties.setProperty(IDevicePropertiesConstants.commandline,
                                    commandline);
                            androidInstance.setProperties(emuProperties);

                            InstanceEventManager.getInstance().notifyListeners(
                                    new InstanceEvent(InstanceEventType.INSTANCE_UPDATED,
                                            androidInstance));
                        }
                    }
                    store.setValue("DeviceStartupOptionsUpdated", true);
                }
            }
        }
    };

    private static IInstanceListener sequoyahInstanceListener = new InstanceAdapter()
    {
        @Override
        public void instanceUpdated(InstanceEvent e)
        {
            AbstractAndroidView.updateInstanceName(e.getInstance());
        }
    };

    private static ServiceHandler stopServiceHandler = null;

    private static ServiceHandler startServiceHandler = null;

    private static String stopServiceId = null;

    private static String startServiceId = null;

    /**
     * The constructor
     */
    public EmulatorPlugin()
    {
        plugin = this;
    }

    /**
     * Activates the plug-in and initializes the logger
     *
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception
    {
        StudioLogger.debug(EmulatorPlugin.class, "Starting MOTODEV Android Emulator Plugin...");

        super.start(context);

        start();

        StudioLogger.debug(EmulatorPlugin.class, "MOTODEV Android Emulator Plugin started.");
    }

    private void start()
    {
        // Setting the TmL logger to redirect logs to the logger controlled
        // by this class
        SequoyahLogRedirector tmlLogger = new SequoyahLogRedirector();
        org.eclipse.sequoyah.vnc.utilities.logger.Logger.setLogger(tmlLogger);
        BasePlugin.getBaseDefault().setLogger(tmlLogger);

        instanceListener = new AndroidDevInstListener();
        InstanceEventManager.getInstance().addInstanceListener(instanceListener);
        StudioAndroidEventManager.asyncAddDeviceChangeListeners(connectedListener,
                disconnectedListener);

        AndroidPlugin.getDefault().addSDKLoaderListener(sdkLoaderListener);
        // Emulator Views synchronization
        DeviceViewsSync.getInstance().initialize();
        // Setting context sensitive help IDs for the TmL screens we use 
        DefaultDeviceTypeMenuWizardPage.setHelpContextId(NEW_DEVICE_HELP);
        InstanceMgtView.setHelp(DEV_MANAGER_HELP);
        InstanceEventManager.getInstance().addInstanceListener(sequoyahInstanceListener);
        registerStopServiceId(STOP_SERVICE_ID);
        registerStartServiceId(START_SERVICE_ID);
    }

    /**
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception
    {
        AndroidPlugin.getDefault().removeSDKLoaderListener(sdkLoaderListener);
        InstanceEventManager.getInstance().removeInstanceListener(instanceListener);
        StudioAndroidEventManager.asyncRemoveDeviceChangeListeners(connectedListener,
                disconnectedListener);
        InstanceEventManager.getInstance().removeInstanceListener(sequoyahInstanceListener);
        unregisterStopServiceHandler();
        unregisterStartServiceHandler();
        plugin = null;
        super.stop(context);
    }

    /**
     * Registers a stop service id, through which the stop service handler will be found and
     * used to delegate stop action of the instances
     * if possible 
     * 
     * @param stopServiceId The stop service id to be registered
     */
    public static void registerStopServiceId(String stopServiceId)
    {
        EmulatorPlugin.stopServiceId = stopServiceId;
    }

    /**
     * Unregisters the current stop service handler and stop service id.
     * 
     * After this method is called, it will not be possible for the instance class to delegate the
     * stop action to a handler.  
     */
    public static void unregisterStopServiceHandler()
    {
        stopServiceHandler = null;
        stopServiceId = null;
    }

    /**
     * Retrieves the stop service handler.
     * 
     * @return The currently registered stop service handler, or <null> if no handler is registered.
     */
    public static ServiceHandler getStopServiceHandler()
    {
        if ((stopServiceHandler == null) && (stopServiceId != null))
        {
            // find the appropriate stop service handler
            IDeviceType device =
                    DeviceTypeRegistry.getInstance().getDeviceTypeById(EmulatorPlugin.DEVICE_ID);
            List<IService> services = device.getServices();
            for (IService service : services)
            {
                IServiceHandler handler = service.getHandler();
                if (handler.getService().getId().equals(stopServiceId))
                {
                    stopServiceHandler = (ServiceHandler) handler;
                    break;
                }
            }
        }

        return stopServiceHandler;
    }

    /**
     * Registers a start service id, through which the stop service handler will be found and
     * used to delegate start action of the instances
     * if possible 
     * 
     * @param stopServiceId The stop service id to be registered
     */
    public static void registerStartServiceId(String startServiceId)
    {
        EmulatorPlugin.startServiceId = startServiceId;
    }

    /**
     * Unregisters the current start service handler and stop service id.
     * 
     * After this method is called, it will not be possible for the instance class to delegate the
     * start action to a handler.  
     */
    public static void unregisterStartServiceHandler()
    {
        startServiceHandler = null;
        startServiceId = null;
    }

    /**
     * Retrieves the start service handler.
     * 
     * @return The currently registered start service handler, or <null> if no handler is registered.
     */
    public static ServiceHandler getStartServiceHandler()
    {
        if ((startServiceHandler == null) && (startServiceId != null))
        {
            // find the appropriate stop service handler
            IDeviceType device =
                    DeviceTypeRegistry.getInstance().getDeviceTypeById(EmulatorPlugin.DEVICE_ID);
            List<IService> services = device.getServices();
            for (IService service : services)
            {
                IServiceHandler handler = service.getHandler();
                if (handler.getService().getId().equals(startServiceId))
                {
                    startServiceHandler = (ServiceHandler) handler;
                    break;
                }
            }
        }

        return startServiceHandler;
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static EmulatorPlugin getDefault()
    {
        return plugin;
    }
}