summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/core/devfrm/DeviceFrameworkManager.java
blob: 401d3ff5dfaaf1f716dc8e1ce828b7ffc16fbd06 (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
/*
* 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.core.devfrm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

import com.motorola.studio.android.emulator.EmulatorPlugin;
import com.motorola.studio.android.emulator.core.model.IAndroidEmulatorInstance;

/**
 * DESCRIPTION:
 * This class manages the device frameworks that extend the deviceFramework
 * extension  
 *
 * RESPONSIBILITY:
 * Retrieve all deviceFramework extension data and provide a compiled
 * view of the information provided by each extension implementer
 *
 * COLABORATORS:
 * None.
 *
 * USAGE:
 * Use each public method to get the desired information 
 */
public class DeviceFrameworkManager
{
    /*
     * Extension point related ids section
     */
    private static final String DEV_FRAMEWORK_EXTENSION_POINT_ID =
            EmulatorPlugin.PLUGIN_ID + ".deviceFramework";

    private static final String DEV_FRAMEWORK_ELEM = "deviceFramework";

    private static final String DEV_FRAMEWORK_IMPL_CLASS_ATTR = "class";

    /**
     * This is a singleton class. The instance is stored in this attribute
     */
    private static DeviceFrameworkManager instance;

    /**
     * A collection containing the classes provided by each extension
     * implementer for retrieving framework data
     */
    private Collection<IDeviceFrameworkSupport> allFrameworks =
            new HashSet<IDeviceFrameworkSupport>();

    /**
     * Singleton private constructor
     */
    private DeviceFrameworkManager()
    {
        populateModel();
    }

    /**
     * Gets the instance of the class
     * 
     * @return The instance of the class
     */
    public static DeviceFrameworkManager getInstance()
    {
        if (instance == null)
        {
            instance = new DeviceFrameworkManager();
        }

        return instance;
    }

    /**
     * Retrieves all instances managed by every device framework
     * which contributes with the deviceFramework extension point 
     * 
     * @return A collection containing all instances from all frameworks
     */
    public Collection<IAndroidEmulatorInstance> getAllInstances()
    {
        Collection<IAndroidEmulatorInstance> allInstancesSet =
                new LinkedHashSet<IAndroidEmulatorInstance>();
        for (IDeviceFrameworkSupport devFramework : allFrameworks)
        {
            Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
            if (devFrmInstances != null)
            {
                allInstancesSet.addAll(devFrmInstances);
            }
        }

        return allInstancesSet;
    }

    /**
     * Retrieve all registered and available instances
     * 
     * @return List containing all registered and available instances
     */
    public Collection<IAndroidEmulatorInstance> getAvailableInstances()
    {
        Collection<IAndroidEmulatorInstance> allInstances = getAllInstances();
        Collection<IAndroidEmulatorInstance> enabledInstances =
                new ArrayList<IAndroidEmulatorInstance>(allInstances.size());

        for (IAndroidEmulatorInstance emulatorInstance : allInstances)
        {
            if (emulatorInstance.isAvailable())
            {
                enabledInstances.add(emulatorInstance);
            }
        }
        return enabledInstances;
    }

    /**
     * Retrieve a collection of names of all the IAndroidEmulatorInstance
     * of all Device frameworks...
     * @return A collection of all instances of IAndroidEmulatorInstance.
     */
    public Collection<String> getAllInstanceNames()
    {
        Collection<String> allInstancesNames = new LinkedHashSet<String>();
        for (IDeviceFrameworkSupport devFramework : allFrameworks)
        {
            for (IAndroidEmulatorInstance instance : devFramework.getAllInstances())
            {
                allInstancesNames.add(instance.getName());
            }
        }

        return allInstancesNames;

    }

    /**
     * Retrieves the first occurrence of a IAndroidEmulatorInstance with the given name
     * provided by any framework.
     * @param name of the emulator instance to be retrieved.
     * @return reference to a IAndroidEmulatorInstance with the given name or a null 
     * is there are no emulator instance with the given name. 
     */
    public IAndroidEmulatorInstance getInstanceByName(String name)
    {
        IAndroidEmulatorInstance instanceToReturn = null;

        for (IAndroidEmulatorInstance instance : getAllInstances())
        {
            if (Platform.getOS().equals(Platform.WS_WIN32))
            {
                if (instance.getName().toLowerCase().equals(name.toLowerCase()))
                {
                    instanceToReturn = instance;
                    break;
                }
            }
            else
            {
                if (instance.getName().equals(name))
                {
                    instanceToReturn = instance;
                    break;
                }
            }

        }
        return instanceToReturn;

    }

    /**
     * Retrieves all <b>started</b> instances managed by every device framework
     * which contributes with the deviceFramework extension point 
     * 
     * @return A collection containing all started instances from all frameworks
     */
    public Collection<IAndroidEmulatorInstance> getAllStartedInstances()
    {
        Collection<IAndroidEmulatorInstance> startedInstancesSet =
                new HashSet<IAndroidEmulatorInstance>();
        for (IDeviceFrameworkSupport devFramework : allFrameworks)
        {
            Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
            if (devFrmInstances != null)
            {
                for (IAndroidEmulatorInstance instance : devFrmInstances)
                {
                    if (instance.isStarted())
                    {
                        startedInstancesSet.add(instance);
                    }
                }
            }
        }

        return startedInstancesSet;
    }

    /**
     * Retrieves all <b>connected</b> instances managed by every device framework
     * which contributes with the deviceFramework extension point 
     * 
     * @return A collection containing all connected instances from all frameworks
     */
    public Collection<IAndroidEmulatorInstance> getAllConnectedInstances()
    {
        Collection<IAndroidEmulatorInstance> connectedInstancesSet =
                new HashSet<IAndroidEmulatorInstance>();
        for (IDeviceFrameworkSupport devFramework : allFrameworks)
        {
            Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
            if (devFrmInstances != null)
            {
                for (IAndroidEmulatorInstance instance : devFrmInstances)
                {
                    if (instance.isConnected())
                    {
                        connectedInstancesSet.add(instance);
                    }
                }
            }
        }

        return connectedInstancesSet;
    }

    /**
     * Retrieves all started instances host addresses managed by every
     * device framework which contributes with the deviceFramework extension point 
     * 
     * @return A collection containing all instances from all frameworks
     */
    public Set<String> getAllStartedInstancesHosts()
    {
        Set<String> hostSet = new HashSet<String>();
        for (IDeviceFrameworkSupport devFramework : allFrameworks)
        {
            Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
            if (devFrmInstances != null)
            {
                for (IAndroidEmulatorInstance instance : devFrmInstances)
                {
                    if (instance.isStarted())
                    {
                        hostSet.add(instance.getInstanceIdentifier());
                    }
                }
            }
        }

        return hostSet;
    }

    /**
     * Populates the allFrameworks collection with framework contributed
     * classes for retrieving framework information. 
     */
    private void populateModel()
    {
        IExtensionRegistry extReg = Platform.getExtensionRegistry();
        IExtensionPoint extPoint = extReg.getExtensionPoint(DEV_FRAMEWORK_EXTENSION_POINT_ID);
        IExtension[] extensions = extPoint.getExtensions();

        for (IExtension aExtension : extensions)
        {
            IConfigurationElement[] configElements = aExtension.getConfigurationElements();
            for (IConfigurationElement aConfig : configElements)
            {
                if (aConfig.getName().equals(DEV_FRAMEWORK_ELEM))
                {
                    try
                    {
                        IDeviceFrameworkSupport devFramework =
                                (IDeviceFrameworkSupport) aConfig
                                        .createExecutableExtension(DEV_FRAMEWORK_IMPL_CLASS_ATTR);
                        if (devFramework != null)
                        {
                            allFrameworks.add(devFramework);
                        }
                    }
                    catch (CoreException e)
                    {
                        // Do nothing.
                        // If a device framework cannot be instantiated, it will
                        // not be plugged to emulator core plugin.
                    }
                }
            }
        }
    }
}