summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/logic/AndroidLogicUtils.java
blob: aad3b076ba172ef6a39d1501e731fe98e9da836b (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
/*
* 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.logic;

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

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osgi.util.NLS;

import com.motorola.studio.android.adt.DDMSFacade;
import com.motorola.studio.android.adt.ISerialNumbered;
import com.motorola.studio.android.common.exception.AndroidException;
import com.motorola.studio.android.emulator.core.exception.InstanceStartException;
import com.motorola.studio.android.emulator.core.exception.StartCancelledException;
import com.motorola.studio.android.emulator.core.exception.StartTimeoutException;
import com.motorola.studio.android.emulator.i18n.EmulatorNLS;

/**
 * This class is used as an utilities class for operations related to Android Devices
 * 
 */
public class AndroidLogicUtils
{
    private static final int INITIAL_VNC_PORT_VALUE = 5900;

    public static final String ORIENTATION_BASE_COMMAND = "sendevent /dev/input/event0 ";

    /**
     * Execute the VM process
     * 
     * @param cmd the command to be executed  
     * @return the VM process
     * 
     * @throws AndroidException if the command failed to execute
     */
    public static Process executeProcess(final String cmd) throws AndroidException
    {
        try
        {
            info("Executing command " + cmd);
            Process vmProcess = Runtime.getRuntime().exec(cmd);
            return vmProcess;
        }
        catch (IOException e)
        {
            error("Falied to execute the command: " + cmd);
            throw new AndroidException(NLS.bind(
                    EmulatorNLS.EXC_AndroidLogicUtils_CannotStartProcess, cmd));
        }
    }

    /**
     * Execute the VM process
     * 
     * @param cmd the command to be executed, described as an array  
     * @return the VM process
     * 
     * @throws AndroidException if the command failed to execute
     */
    public static Process executeProcess(final String[] cmd) throws AndroidException
    {
        String cmdString = "";
        for (int i = 0; i < cmd.length; i++)
        {
            cmdString += cmd[i] + " ";

        }

        return executeProcess(cmd, cmdString);
    }

    /**
     * Execute the VM process
     * 
     * @param cmd The command to be executed, described as an array
     * @param cmdToLog The command to be logged. 
     *   
     * @return the VM process
     * 
     * @throws AndroidException if the command failed to execute
     */
    public static Process executeProcess(final String[] cmd, final String cmdToLog)
            throws AndroidException
    {
        try
        {
            info("Executing command " + cmdToLog);
            Process vmProcess = Runtime.getRuntime().exec(cmd);
            return vmProcess;
        }
        catch (IOException e)
        {
            error("Falied to execute the command: " + cmd);
            throw new AndroidException(NLS.bind(
                    EmulatorNLS.EXC_AndroidLogicUtils_CannotStartProcess, cmd));
        }
    }

    /**
    }

    /**
     * Checks if the user has canceled the VM startup
     *
     * @param monitor A progress monitor that will give the user feedback about this
     *                long running operation
     * @param instanceHost The IP address of the started emulator instance
     *
     * @return True if the operation can proceed, false otherwise
     * 
     * @throws StartCancelledException If the user has canceled the start process
     */
    public static void testCanceled(IProgressMonitor monitor) throws StartCancelledException
    {
        if (monitor.isCanceled())
        {
            info("Operation canceled by the user");
            monitor.subTask(EmulatorNLS.MON_AndroidEmulatorStarter_Canceling);

            throw new StartCancelledException(
                    EmulatorNLS.EXC_AndroidEmulatorStarter_EmulatorStartCanceled);
        }
    }

    /**
     * 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 
     */
    public static void testTimeout(long timeoutLimit, String timeoutErrorMessage)
            throws StartTimeoutException
    {
        if (System.currentTimeMillis() > timeoutLimit)
        {
            error("The emulator was not up within the set timeout");
            throw new StartTimeoutException(timeoutErrorMessage);
        }
    }

    /**
     * Get the relative timeout limit, which is the the current time plus the timeout value
     * 
     * @param timeout timeout value (in milliseconds)
     * @return Relative timeout limit
     */
    public static long getTimeoutLimit(int timeout)
    {
        return System.currentTimeMillis() + timeout;
    }

    /**
     * Check if the given process is still up and running
     * 
     * @param p process
     * @throws InstanceStartException
     */
    public static void testProcessStatus(Process p) throws InstanceStartException
    {

        boolean isRunning;
        int exitCode;

        try
        {
            exitCode = p.exitValue();
            isRunning = false;
        }
        catch (Exception e)
        {
            // emulator process is still running... so everything looks fine...
            isRunning = true;
            exitCode = 0;
        }

        if (!isRunning)
        {
            error("Emulator process is not running! Exit code:" + exitCode);
            StringBuffer outBuf = null;
            InputStream inStream = null;

            int ch;

            //Getting error output stream
            String processAnswer = "";
            inStream = p.getErrorStream();
            outBuf = new StringBuffer();
            try
            {
                while ((ch = inStream.read()) != -1)
                {
                    outBuf.append((char) ch + "");
                }
            }
            catch (IOException e)
            {
                error("Cannot read error output  stream from Emulator proccess");
            }

            processAnswer = outBuf.toString();

            if (processAnswer.length() == 0)
            {
                //if no error came from process, get standard output stream
                inStream = p.getInputStream();
                outBuf = new StringBuffer();
                try
                {
                    while ((ch = inStream.read()) != -1)
                    {
                        outBuf.append((char) ch + "");
                    }
                }
                catch (IOException e)
                {
                    error("Cannot read standard output stream from Emulator proccess");
                }

                processAnswer = outBuf.toString();

            }
            String msg = EmulatorNLS.EXC_AndroidEmulatorStarter_ProcessTerminated;
            msg += processAnswer;
            throw new InstanceStartException(msg);
        }

    }

    /**
     * Kill the communication channel
     * 
     * @param instance Android instance
     */
    public static void kill(IAndroidLogicInstance instance)
    {
        if (instance instanceof ISerialNumbered)
        {
            String serialNumber = ((ISerialNumbered) instance).getSerialNumber();
            DDMSFacade.kill(serialNumber);
            Process process = instance.getProcess();
            if (process != null)
            {
                int tries = 0;
                Integer exitValue = null;
                while ((process != null) && (tries < 10) && (exitValue == null))
                {
                    try
                    {
                        exitValue = process.exitValue();
                    }
                    catch (Throwable t)
                    {
                        tries++;
                        try
                        {
                            Thread.sleep(250);
                        }
                        catch (InterruptedException e)
                        {
                            //do nothing
                        }
                    }
                }
                process.destroy();
                instance.setProcess(null);
            }
        }
    }

    /**
     * Get the VNC port forward
     * 
     * @param serial port number
     * @return VNC port
     */
    public static int getVncServerPortFoward(String serial)
    {
        if (serial == null)
        {
            return 0;
        }

        int stringSize = serial.length();
        String lastTwoNumbers = serial.substring(stringSize - 2, stringSize);

        int port = INITIAL_VNC_PORT_VALUE;

        try
        {
            port += Integer.valueOf(lastTwoNumbers);
        }
        catch (NumberFormatException e)
        {
            // do nothing (this should not happen)
        }

        return port;

    }

    public static int getEmulatorPort(String serial)
    {
        if (serial == null)
        {
            return 0;
        }

        int stringSize = serial.length();
        String lastFourNumbers = serial.substring(stringSize - 4, stringSize);

        int port = 0;

        try
        {
            port = Integer.valueOf(lastFourNumbers);
        }
        catch (NumberFormatException e)
        {
            // do nothing (this should not happen)
        }
        return port;
    }

    /**
     * Checks if the Device is still online... 
     * If the device is not online it is not possible to communicate with it.
     * Notice it is a verification of the status of the Device wich may be different than the status of the Tml Instance...
     *
     * @param serialNumber serial number of the device 
     * 
     * @throws AndroidException If the device is not started
     */
    public static void testDeviceStatus(String serialNumber) throws AndroidException
    {
        if (!DDMSFacade.isDeviceOnline(serialNumber))
        {
            info("Device is offline: " + serialNumber);

            throw new AndroidException(EmulatorNLS.EXC_AndroidLogicUtils_DeviceIsOffline);
        }
    }
}