summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/device/instance/AndroidDevInstListener.java
blob: 8e9ff9b15d675f5f2f123fd7172f9509a7b9f040 (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
/* 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.instance;

import org.eclipse.sequoyah.device.framework.DevicePlugin;
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.model.IInstance;

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.core.model.IAndroidEmulatorInstance;
import com.motorola.studio.android.emulator.core.utils.EmulatorCoreUtils;
import com.motorola.studio.android.emulator.ui.view.AbstractAndroidView;

/**
 * DESCRIPTION:
 * Implementation of IInstanceListener for device related actions that depend
 * on the TmL instance registry state. 
 * <br>
 * RESPONSIBILITY:
 * Guarantee that the emulator views are updated
 * Run the initialization service when an instance is loaded
 * <br>
 * COLABORATORS:
 * None.
 * <br>
 * USAGE:
 * This class shall be used by Eclipse only.
 */
public class AndroidDevInstListener extends InstanceAdapter
{

    /**
     * @see IInstanceListener#instanceLoaded(InstanceEvent)
     */
    @Override
    public void instanceLoaded(InstanceEvent e)
    {
        IInstance instance = e.getInstance();

        if (instance instanceof IAndroidEmulatorInstance)
        {
            // The service definition defined (by convention) that 
            // stopped-dirty is the success state, and not available 
            // is the failure state. The exception is being thrown for
            // the framework to set the state correctly. 
            if (instance.getStatus().equals(DevicePlugin.SEQUOYAH_STATUS_OFF))
            {
                instance.setStatus(EmulatorPlugin.STATUS_NOT_AVAILABLE);
            }
        }
    }

    /**
     * @see IInstanceListener#instanceDeleted(InstanceEvent)
     */
    @Override
    public void instanceDeleted(InstanceEvent ev)
    {
        IInstance instance = ev.getInstance();
        if (instance instanceof AndroidDeviceInstance)
        {
            SdkUtils.deleteVm(instance.getName());
        }
    }

    /**
     * @see IInstanceListener#instanceTransitioned(InstanceEvent)
     */
    @Override
    public void instanceTransitioned(InstanceEvent e)
    {
        IInstance instance = e.getInstance();

        if (instance instanceof AndroidDeviceInstance)
        {
            final AndroidDeviceInstance androidDevice = (AndroidDeviceInstance) instance;
            StudioLogger.info("The android device instance status was updated: " + instance
                    + " Status: " + instance.getStatus());

            if (androidDevice.isStarted())
            {
                String transitionId = e.getTransitionId();
                if ((transitionId != null)
                        && transitionId.equals("com.motorola.studio.android.emulator.startService"))
                {
                    // If it is coming from other state than the started, 
                    // connect to VNC server
                    StudioLogger
                            .info("The emulator "
                                    + instance
                                    + " transitioned to started state. Try to estabilish a VNC connection...");

                    new Thread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            AbstractAndroidView.showView();
                            EmulatorCoreUtils.refreshEmulatorViews();
                        }
                    }).start();
                }
            }
            else if (instance.getStatus().equals(EmulatorPlugin.STATUS_OFFLINE))
            {
                androidDevice.resetRuntimeVariables();
                EmulatorCoreUtils.refreshEmulatorViews();
            }

        }

    }

}