summaryrefslogtreecommitdiff
path: root/src/plugins/remote.device/src/com/motorola/studio/android/remote/RemoteDeviceUtils.java
blob: 6970af87b2d69f6eb65134c55aeed8d7e416d1fd (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
/*
 * 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.remote;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeoutException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.sequoyah.device.common.utilities.exception.SequoyahException;
import org.eclipse.sequoyah.device.framework.model.AbstractMobileInstance;
import org.eclipse.sequoyah.device.framework.model.IInstance;
import org.eclipse.ui.PlatformUI;

import com.motorola.studio.android.adt.DDMSFacade;
import com.motorola.studio.android.adt.ISerialNumbered;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.devices.DevicesManager;
import com.motorola.studio.android.remote.instance.RemoteDeviceInstance;

/**
 * Class that contains business methods and utilities.
 */
public class RemoteDeviceUtils
{

    /**
     * Handle Remote Device connection.
     * 
     * @param serialNumber the serial number of the connected device
     */
    public static void connectDevice(final String serialNumber)
    {
        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable()
        {
            public void run()
            {
                /*
                 * Check if it's a remote device
                 */
                if (DDMSFacade.isRemote(serialNumber))
                {

                    ISerialNumbered instance =
                            DevicesManager.getInstance().getDeviceBySerialNumber(serialNumber);

                    boolean isTransitioning =
                            ((instance != null) ? ((AbstractMobileInstance) instance)
                                    .getStateMachineHandler().isTransitioning() : false);

                    StudioLogger.debug("Handle remote device connected event. Serial Number: "
                            + serialNumber + " Instance: " + instance + " Transitioning: "
                            + isTransitioning);

                    /*
                     * If the instance exists and is transitioning, so skip this method, the
                     * connect handler will change the instance status
                     */
                    if ((instance == null) || ((instance != null) && (!isTransitioning)))
                    {

                        /*
                         * This method is necessary because sometimes (for example when the connection is refuses)
                         * the device appears in the adb devices list but it's not in the "online" state
                         */
                        boolean onlineDevice = waitForDeviceToBeOnline(serialNumber, instance);

                        if (onlineDevice)
                        {
                            /*
                             * If the device instance already exists
                             */
                            if (instance == null)
                            {

                                try
                                {

                                    StudioLogger
                                            .debug("Connecting Remote Device: device doesn't exist, create a new instance");

                                    DevicesManager.getInstance().createInstanceForDevice(
                                            serialNumber, RemoteDeviceConstants.DEVICE_ID,
                                            getInstanceBuilder(serialNumber),
                                            RemoteDeviceConstants.SERVICE_INIT_ID);
                                }
                                catch (SequoyahException e)
                                {
                                    StudioLogger
                                            .error("Connecting Remote Device: error while creating device instance "
                                                    + e.getMessage());
                                }
                            }

                            try
                            {
                                instance =
                                        DevicesManager.getInstance().getDeviceBySerialNumber(
                                                serialNumber);

                                StudioLogger
                                        .debug("Connecting Remote Device: the TmL service will be called");

                                Map<Object, Object> arguments = new HashMap<Object, Object>();
                                arguments.put(RemoteDeviceConstants.DUMMY_TRANSITION, true);
                                RemoteDevicePlugin.getConnectServiceHandler().run(
                                        (IInstance) instance, arguments);
                            }
                            catch (Exception e)
                            {
                                StudioLogger.error("Error when running TmL connect service: "
                                        + e.getMessage());
                            }
                        }
                    }
                }
            }
        });
    }

    /**
     * Handle Remote Device disconnection
     * 
     * @param serialNumber the serial number of the disconnected device
     */
    public static void disconnectDevice(String serialNumber)
    {
        if (DDMSFacade.isRemote(serialNumber))
        {

            ISerialNumbered instance =
                    DevicesManager.getInstance().getDeviceBySerialNumber(serialNumber);

            StudioLogger.debug("Handle remote device disconnected event. Serial Number: "
                    + serialNumber + " Instance: " + instance);

            if (instance != null)
            {
                Object volatileProperty =
                        ((RemoteDeviceInstance) instance).getProperties().get(
                                RemoteDeviceInstance.PROPERTY_VOLATILE);
                boolean isVolatile =
                        ((volatileProperty != null) ? ((Boolean) volatileProperty).booleanValue()
                                : false);

                if (!isVolatile)
                {
                    try
                    {
                        StudioLogger
                                .debug("Disconnecting Remote Device: the device is NOT volatile, the TmL service will be called");

                        Map<Object, Object> arguments = new HashMap<Object, Object>();
                        arguments.put(RemoteDeviceConstants.DUMMY_TRANSITION, true);
                        RemoteDevicePlugin.getDisconnectServiceHandler().run((IInstance) instance,
                                arguments);
                    }
                    catch (Exception e)
                    {
                        StudioLogger.error("Error when running TmL disconnect service: "
                                + e.getMessage());
                    }
                }
                else
                {
                    StudioLogger
                            .debug("Disconnecting Remote Device: the device is volatile, it will be deleted");
                    DevicesManager.getInstance().deleteInstanceOfDevice(serialNumber);
                }

            }

        }

    }

    /*
     * Wait until the device status becomes online
     * 
     * @param serialNumber device serial number
     * @param instance TmL instance, if it exists
     * @return true if the device became online, false otherwise
     */
    private static boolean waitForDeviceToBeOnline(String serialNumber, ISerialNumbered instance)
    {
        StudioLogger.debug("Wait device to be online: " + serialNumber);

        boolean instanceOnline = false;
        long timeoutLimit = 0;

        if (instance != null)
        {
            Properties prop = ((IInstance) instance).getProperties();
            String timeout = prop.getProperty(RemoteDeviceInstance.PROPERTY_TIMEOUT);
            timeoutLimit = System.currentTimeMillis() + (Integer.parseInt(timeout) * 1000);
        }
        else
        {
            timeoutLimit =
                    System.currentTimeMillis() + (RemoteDeviceConstants.DEFAULT_TIMEOUT * 1000);

        }

        while ((instanceOnline = DDMSFacade.isDeviceOnline(serialNumber)) == false)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                StudioLogger.error("Wait for device to be online: thread has been interrupted");
            }

            try
            {
                testTimeout(timeoutLimit);
            }
            catch (TimeoutException e)
            {
                StudioLogger.warn("Timeout reached wile wating device to be online: "
                        + serialNumber);
                break;
            }

        }
        return instanceOnline;

    }

    /*
     * Get the instance builder needed by TmL in order to create a new Remote Device instance
     * 
     * @param serialNumber serial number of the Remote Device that shall be added
     * @return the instance builder needed by TmL to create a new Remote Device instance
     */
    private static RemoteDeviceInstanceBuilder getInstanceBuilder(String serialNumber)
    {

        RemoteDeviceInstanceBuilder instanceBuilder = null;

        String[] serialNumberParts = serialNumber.split(":");
        String host = serialNumberParts[0];
        String port = serialNumberParts[1];

        Properties props = new Properties();
        props.put(RemoteDeviceInstance.PROPERTY_HOST, host);
        props.put(RemoteDeviceInstance.PROPERTY_PORT, port);
        props.put(RemoteDeviceInstance.PROPERTY_TIMEOUT,
                String.valueOf(RemoteDeviceConstants.DEFAULT_TIMEOUT));

        // mark this instance as volatile
        props.put(RemoteDeviceInstance.PROPERTY_VOLATILE, true);

        instanceBuilder = new RemoteDeviceInstanceBuilder(serialNumber, props);

        return instanceBuilder;
    }

    /*
     * Compare the device instance with a pair host:port to check if the device 
     * has the same host:port
     * 
     * @param device the device to be analyzed
     * @param host host IP or name
     * @param port port number
     * @return true if the the device has the same host:port, false otherwise
     */
    public static boolean hasSameHostAndPort(ISerialNumbered device, String host, int port)
    {
        boolean returnValue = false;

        String deviceHost =
                ((RemoteDeviceInstance) device).getProperties().getProperty(
                        RemoteDeviceInstance.PROPERTY_HOST);
        String devicePort =
                ((RemoteDeviceInstance) device).getProperties().getProperty(
                        RemoteDeviceInstance.PROPERTY_PORT);

        if ((host.equals(deviceHost)) && (String.valueOf(port).equals(devicePort)))
        {
            returnValue = true;
        }

        return returnValue;

    }

    /**
     * Execute a command.
     * 
     * @param cmd Array of strings holding the command to
     * be executed.
     * 
     * @return The {@link IStatus} of the command execution.
     * 
     * @throws IOException Exception thrown in case there are problems
     * executing the command.
     */
    public static IStatus executeCommand(String[] cmd) throws IOException
    {
        IStatus status = Status.OK_STATUS;

        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(cmd);

        try
        {
            // wait for the command to finish its execution
            process.waitFor();
        }
        catch (InterruptedException e)
        {
            StudioLogger.error(RemoteDeviceUtils.class, "Problems executing the command");
            status =
                    new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID,
                            "Problems executing the command", e);
        }
        // in case the is a problem with the command execution, create an error status
        if (process.exitValue() != 0)
        {
            StudioLogger.error(RemoteDeviceUtils.class, "The IP was not found");
            status =
                    new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID, "The IP was not found");
        }

        return status;
    }

    /*
     * Checks if the timeout limit has reached
     * 
     * @param timeoutLimit The system time limit that cannot be overtaken, in milliseconds
     * @throws StartTimeoutException When the system time limit is overtaken 
     */
    private static void testTimeout(long timeoutLimit) throws TimeoutException
    {
        if (System.currentTimeMillis() > timeoutLimit)
        {
            throw new TimeoutException();
        }
    }
}