summaryrefslogtreecommitdiff
path: root/src/plugins/devices.services/src/com/motorola/studio/android/devices/services/DeviceServicesPlugin.java
blob: e568e182c72c6b7e10bf7ac1abfdbe9c54fdb63f (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * 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.devices.services;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
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.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleInputStream;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.devices.services.console.ADBShellHandler;
import com.motorola.studio.android.devices.services.console.EmulatorConsoleHandler;
import com.motorola.studio.android.emulator.EmulatorPlugin;

/**
 * The activator class controls the plug-in life cycle
 */
public class DeviceServicesPlugin extends AbstractUIPlugin
{

    public static final String PLUGIN_ID = "com.motorola.studio.android.devices.services";

    public static final String DEPLOY_SERVICE_ID =
            "com.motorola.studio.android.devices.services.deployService";

    public static final String UNINSTALL_APP_SERVICE_ID =
            "com.motorola.studio.android.devices.services.uninstallAppService";

    private static ServiceHandler deployServiceHandler = null;

    private static ServiceHandler uninstallAppServiceHandler = null;

    public static final boolean IS_WIN32 = Platform.getOS().equals(Platform.OS_WIN32);

    private static String EMULATOR_CONSOLE_SERVICE_ID =
            "com.motorola.studio.android.devices.services.emulatorConsole";

    private static String ADB_SHELL_SERVICE_ID =
            "com.motorola.studio.android.devices.services.adbShell";

    private static DeviceServicesPlugin plugin;

    private static final String SCREENSHOT_SERVICE_ID = PLUGIN_ID + ".takescreenshot";

    private static final String MONKEY_SERVICE_ID = PLUGIN_ID + ".monkey";

    private static ServiceHandler screenshotServiceHandler = null;

    private static ServiceHandler monkeyServiceHandler = null;

    private static ServiceHandler adbShellServiceHandler = null;

    private static ServiceHandler emulatorConsoleServiceHandler = null;

    public static final String ANDROID_LANG_SERVICE_ID = PLUGIN_ID + ".changeLanguageService";

    public static final String LANG_PAGE_CONTEXT_HELP_ID = PLUGIN_ID + ".langPage";

    private static Collection<IConsoleKilledListener> listeners =
            new ArrayList<IConsoleKilledListener>();

    private final IWorkbenchListener workbenchListener = new IWorkbenchListener()
    {

        // killllll all consoles
        public boolean preShutdown(IWorkbench workbench, boolean forced)
        {
            List<IConsole> consolesToClose = new ArrayList<IConsole>();
            List<IConsoleKilledListener> copy = new ArrayList<IConsoleKilledListener>(listeners);
            for (IConsole console : ConsolePlugin.getDefault().getConsoleManager().getConsoles()
                    .clone())
            {
                if (console.getName().contains(EmulatorConsoleHandler.CONSOLE_NAME)
                        || console.getName().contains(ADBShellHandler.CONSOLE_NAME))
                {
                    for (IConsoleKilledListener listener : copy)
                    {
                        listener.consoleKilled(console.getName());
                    }

                    consolesToClose.add(console);
                }

            }
            if (consolesToClose.size() > 0)
            {

                ConsolePlugin.getDefault().getConsoleManager()
                        .removeConsoles(consolesToClose.toArray(new IConsole[0]));
            }
            return true;
        }

        public void postShutdown(IWorkbench workbench)
        {
            //do nothing;
        }
    };

    public interface IConsoleKilledListener
    {
        void consoleKilled(String consoleName);
    }

    public static void addConsoleKilledListener(IConsoleKilledListener listener)
    {
        /*
         * Keep the entire list, even if elements are the same object.
         * This will ensure that the last console killed will have their proper listener.
         */
        if (!listeners.contains(listener))
        {
            listeners.add(listener);
        }
    }

    public static void removeConsoleKilledListener(IConsoleKilledListener listener)
    {
        listeners.remove(listener);
    }

    public DeviceServicesPlugin()
    {
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception
    {
        StudioLogger.debug(DeviceServicesPlugin.class,
                "Starting MOTODEV Android Device Services Plugin...");

        super.start(context);
        plugin = this;
        PlatformUI.getWorkbench().addWorkbenchListener(workbenchListener);

        StudioLogger.debug(DeviceServicesPlugin.class,
                "MOTODEV Android Device Services Plugin started.");
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception
    {
        PlatformUI.getWorkbench().removeWorkbenchListener(workbenchListener);
        plugin = null;
        super.stop(context);
    }

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

    /**
     * Creates a console for a process
     * 
     * @param p The process
     */
    public static void redirectProcessStreamsToConsole(Process p, String consoleName)
    {
        InputStream processIn = p.getInputStream();
        OutputStream processOut = p.getOutputStream();
        redirectStreamsToConsole(processIn, processOut, consoleName);
    }

    /**
     * Creates a console for a process
     * 
     * @param p The process
     */
    public static void redirectStreamsToConsole(final InputStream in, final OutputStream out,
            final String consoleName)
    {
        final IOConsole console = new IOConsole(consoleName, null);

        console.activate();
        ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]
        {
            console
        });

        final IOConsoleOutputStream consoleOut = console.newOutputStream();
        final IOConsoleInputStream consoleIn = console.getInputStream();

        new Thread(new Runnable()
        {
            public void run()
            {
                boolean carriageReturn = false;

                while (true)
                {
                    try
                    {
                        int byteRead = in.read();
                        if (byteRead == -1)
                        {
                            throw new Exception();
                        }

                        if (carriageReturn && (byteRead != 13))
                        {
                            consoleOut.write(13);
                            consoleOut.write(byteRead);
                            carriageReturn = false;
                        }
                        else if (!carriageReturn && (byteRead == 13))
                        {
                            carriageReturn = true;
                        }
                        else
                        {
                            consoleOut.write(byteRead);
                            carriageReturn = false;
                        }
                        consoleOut.flush();
                    }
                    catch (Exception e)
                    {
                        ConsolePlugin.getDefault().getConsoleManager()
                                .removeConsoles(new IConsole[]
                                {
                                    console
                                });

                        Collection<IConsoleKilledListener> cloneListeners =
                                new ArrayList<IConsoleKilledListener>(listeners);
                        for (IConsoleKilledListener listener : cloneListeners)
                        {
                            listener.consoleKilled(consoleName);
                        }

                        break;
                    }
                }
            }
        }).start();

        new Thread(new Runnable()
        {
            public void run()
            {
                while (true)
                {
                    try
                    {
                        int byteRead = consoleIn.read();
                        out.write(byteRead);
                        out.flush();
                    }
                    catch (Exception e)
                    {
                        break;
                    }
                }
            }
        }).start();
    }

    /**
     * Retrieves the adb shell service handler.
     * 
     * @return The currently registered stop service handler, or <null> if no handler is registered.
     */
    public static ServiceHandler getADBShellServiceHandler()
    {
        if ((adbShellServiceHandler == null) && (ADB_SHELL_SERVICE_ID != 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(ADB_SHELL_SERVICE_ID))
                {
                    adbShellServiceHandler = (ServiceHandler) handler;
                    break;
                }
            }
        }
        return adbShellServiceHandler;
    }

    /**
     * Retrieves the emulator console service handler.
     * 
     * @return The currently registered stop service handler, or <null> if no handler is registered.
     */
    public static ServiceHandler getEmulatorConsoleServiceHandler()
    {
        if ((emulatorConsoleServiceHandler == null) && (EMULATOR_CONSOLE_SERVICE_ID != 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(EMULATOR_CONSOLE_SERVICE_ID))
                {
                    emulatorConsoleServiceHandler = (ServiceHandler) handler;
                    break;
                }
            }
        }

        return emulatorConsoleServiceHandler;
    }

    /**
     * Creates and returns a new image descriptor for an image file in this plug-in.
     * @param path the relative path of the image file, relative to the root of the plug-in; the path must be legal
     * @return an image descriptor, or null if no image could be found
     */
    public static ImageDescriptor getImageDescriptor(String path)
    {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    /**
     * Retrieves the deploy service handler.
     * 
     * @return The currently registered stop service handler, or <null> if no handler is registered.
     */
    public static ServiceHandler getDeployServiceHandler()
    {
        if ((deployServiceHandler == null) && (DEPLOY_SERVICE_ID != 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(DEPLOY_SERVICE_ID))
                {
                    deployServiceHandler = (ServiceHandler) handler;
                    break;
                }
            }
        }

        return deployServiceHandler;
    }

    /**
     * Retrieves the deploy service handler.
     * 
     * @return The currently registered stop service handler, or <null> if no handler is registered.
     */
    public static ServiceHandler getUninstallAppServiceHandler()
    {
        if ((uninstallAppServiceHandler == null) && (UNINSTALL_APP_SERVICE_ID != 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(UNINSTALL_APP_SERVICE_ID))
                {
                    uninstallAppServiceHandler = (ServiceHandler) handler;
                    break;
                }
            }
        }

        return uninstallAppServiceHandler;
    }

    /**
     * Retrieves the deploy service handler.
     * 
     * @return The currently registered stop service handler, or <null> if no handler is registered.
     */
    public static ServiceHandler getScreenshotServiceHandler()
    {
        if ((screenshotServiceHandler == null) && (SCREENSHOT_SERVICE_ID != 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(SCREENSHOT_SERVICE_ID))
                {
                    screenshotServiceHandler = (ServiceHandler) handler;
                    break;
                }
            }
        }

        return screenshotServiceHandler;
    }

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

        return monkeyServiceHandler;
    }
}