summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/device/refresh/InstancesListRefresh.java
blob: 23295e9220a2b5b647464d740467cd12dc6e3f84 (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
/*
* 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.refresh;

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

import java.util.Collection;
import java.util.Properties;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.sequoyah.device.common.utilities.exception.SequoyahException;
import org.eclipse.sequoyah.device.framework.DevicePlugin;
import org.eclipse.sequoyah.device.framework.factory.DeviceTypeRegistry;
import org.eclipse.sequoyah.device.framework.manager.InstanceManager;
import org.eclipse.sequoyah.device.framework.model.IDeviceType;

import com.motorola.studio.android.adt.SdkUtils;
import com.motorola.studio.android.emulator.EmulatorPlugin;
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.instance.AndroidDevInstBuilder;
import com.motorola.studio.android.emulator.device.instance.AndroidDeviceInstance;

/**
 * This class is responsible for refreshing the TML Instances List It checks if
 * the user has created more Android VMs by himself
 * 
 */
public class InstancesListRefresh
{

    /**
     * If the number of Android VMs is different from the number of TML
     * Instances, the TML Instances list is updated
     */
    public static synchronized void refresh()
    {

        SdkUtils.reloadAvds();

        DeviceFrameworkManager devFramework = DeviceFrameworkManager.getInstance();

        if (SdkUtils.getCurrentSdk() != null)
        {
            final Collection<String> vmInstances = SdkUtils.getAllVmNames();
            final Collection<String> validVmInstances = SdkUtils.getAllValidVmNames();
            final Collection<String> emulatorInstances = devFramework.getAllInstanceNames();

            createAndUpdateEmulatorInstances(vmInstances, validVmInstances, emulatorInstances);
        }
    }

    /**
     * Creates Emulator instances to represent every VM available in the system.
     * @param validVmInstances 
     **/
    public static void createAndUpdateEmulatorInstances(Collection<String> vmInstances,
            Collection<String> validVmInstances,
            Collection<String> emulatorInstances)
    {

        IDeviceType device =
                DeviceTypeRegistry.getInstance().getDeviceTypeById(EmulatorPlugin.DEVICE_ID);

        for (String instanceName : vmInstances)
        {
            /*
             * In case the is no TmL instances for a given VM, create the TmL
             * Instance
             */
            if (!emulatorInstances.contains(instanceName))
            {

                Properties instanceProperties = new Properties();

                AndroidDeviceInstance.populateWithVMInfo(instanceName, instanceProperties);

                AndroidDeviceInstance.populateWithDefaultProperties(instanceProperties);

                AndroidDevInstBuilder projectBuilder =
                        new AndroidDevInstBuilder(instanceName, instanceProperties);

                try
                {
                    InstanceManager
                            .createProject(device, projectBuilder, new NullProgressMonitor());
                }
                catch (SequoyahException e)
                {
                    error("There was an error while creating an emulator instance: " + instanceName
                            + ". Message: " + e.getMessage());
                }

                refreshStatus(validVmInstances, instanceName);

                info("Added instance " + instanceName + " using default emulator definitions ");
            }

        }

        for (String emulatorInstance : emulatorInstances)
        {
            refreshStatus(validVmInstances, emulatorInstance);
        }

    }

    public static void refreshStatus(Collection<String> vmInstances, String instanceName)
    {
        /*
         * Refresh status
         */
        IAndroidEmulatorInstance instance =
                DeviceFrameworkManager.getInstance().getInstanceByName(instanceName);

        AndroidDeviceInstance androidDeviceInstance = (AndroidDeviceInstance) instance;

        AndroidDeviceInstance.populateWithVMInfo(androidDeviceInstance.getName(),
                androidDeviceInstance.getProperties());

        String currentStatus = androidDeviceInstance.getStatus();

        if (androidDeviceInstance.hasDevice())
        {
            if ((androidDeviceInstance.getStatus().equals(EmulatorPlugin.STATUS_NOT_AVAILABLE))
                    || (androidDeviceInstance.getStatus().equals(DevicePlugin.SEQUOYAH_STATUS_OFF)))
            {
                if (!EmulatorPlugin.STATUS_OFFLINE_NO_DATA.equals(currentStatus))
                {
                    androidDeviceInstance.setNameSuffix(null);
                    androidDeviceInstance.setStatus(EmulatorPlugin.STATUS_OFFLINE_NO_DATA);
                }
            }
            AndroidDeviceUtils.fireDummyStartTransition(androidDeviceInstance,
                    androidDeviceInstance.getSerialNumber());
        }
        else
        {

            if (vmInstances.contains(androidDeviceInstance.getName()))
            {
                if (androidDeviceInstance.isClean())
                {
                    if (!EmulatorPlugin.STATUS_OFFLINE_NO_DATA.equals(currentStatus))
                    {
                        androidDeviceInstance.setNameSuffix(null);
                        androidDeviceInstance.setStatus(EmulatorPlugin.STATUS_OFFLINE_NO_DATA);
                    }
                }
                else
                {
                    if (!EmulatorPlugin.STATUS_OFFLINE.equals(currentStatus))
                    {
                        androidDeviceInstance.setNameSuffix(null);
                        androidDeviceInstance.setStatus(EmulatorPlugin.STATUS_OFFLINE);
                    }
                }
            }
            else
            {
                if (!EmulatorPlugin.STATUS_NOT_AVAILABLE.equals(currentStatus))
                {
                    androidDeviceInstance.setNameSuffix(null);
                    androidDeviceInstance.setStatus(EmulatorPlugin.STATUS_NOT_AVAILABLE);
                }
            }
        }
    }
}