Home | History | Annotate | Download | only in devfrm
      1 /*
      2 * Copyright (C) 2012 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 *      http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 
     17 package com.motorola.studio.android.emulator.core.devfrm;
     18 
     19 import java.util.ArrayList;
     20 import java.util.Collection;
     21 import java.util.HashSet;
     22 import java.util.LinkedHashSet;
     23 import java.util.Set;
     24 
     25 import org.eclipse.core.runtime.CoreException;
     26 import org.eclipse.core.runtime.IConfigurationElement;
     27 import org.eclipse.core.runtime.IExtension;
     28 import org.eclipse.core.runtime.IExtensionPoint;
     29 import org.eclipse.core.runtime.IExtensionRegistry;
     30 import org.eclipse.core.runtime.Platform;
     31 
     32 import com.motorola.studio.android.emulator.EmulatorPlugin;
     33 import com.motorola.studio.android.emulator.core.model.IAndroidEmulatorInstance;
     34 
     35 /**
     36  * DESCRIPTION:
     37  * This class manages the device frameworks that extend the deviceFramework
     38  * extension
     39  *
     40  * RESPONSIBILITY:
     41  * Retrieve all deviceFramework extension data and provide a compiled
     42  * view of the information provided by each extension implementer
     43  *
     44  * COLABORATORS:
     45  * None.
     46  *
     47  * USAGE:
     48  * Use each public method to get the desired information
     49  */
     50 public class DeviceFrameworkManager
     51 {
     52     /*
     53      * Extension point related ids section
     54      */
     55     private static final String DEV_FRAMEWORK_EXTENSION_POINT_ID =
     56             EmulatorPlugin.PLUGIN_ID + ".deviceFramework";
     57 
     58     private static final String DEV_FRAMEWORK_ELEM = "deviceFramework";
     59 
     60     private static final String DEV_FRAMEWORK_IMPL_CLASS_ATTR = "class";
     61 
     62     /**
     63      * This is a singleton class. The instance is stored in this attribute
     64      */
     65     private static DeviceFrameworkManager instance;
     66 
     67     /**
     68      * A collection containing the classes provided by each extension
     69      * implementer for retrieving framework data
     70      */
     71     private Collection<IDeviceFrameworkSupport> allFrameworks =
     72             new HashSet<IDeviceFrameworkSupport>();
     73 
     74     /**
     75      * Singleton private constructor
     76      */
     77     private DeviceFrameworkManager()
     78     {
     79         populateModel();
     80     }
     81 
     82     /**
     83      * Gets the instance of the class
     84      *
     85      * @return The instance of the class
     86      */
     87     public static DeviceFrameworkManager getInstance()
     88     {
     89         if (instance == null)
     90         {
     91             instance = new DeviceFrameworkManager();
     92         }
     93 
     94         return instance;
     95     }
     96 
     97     /**
     98      * Retrieves all instances managed by every device framework
     99      * which contributes with the deviceFramework extension point
    100      *
    101      * @return A collection containing all instances from all frameworks
    102      */
    103     public Collection<IAndroidEmulatorInstance> getAllInstances()
    104     {
    105         Collection<IAndroidEmulatorInstance> allInstancesSet =
    106                 new LinkedHashSet<IAndroidEmulatorInstance>();
    107         for (IDeviceFrameworkSupport devFramework : allFrameworks)
    108         {
    109             Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
    110             if (devFrmInstances != null)
    111             {
    112                 allInstancesSet.addAll(devFrmInstances);
    113             }
    114         }
    115 
    116         return allInstancesSet;
    117     }
    118 
    119     /**
    120      * Retrieve all registered and available instances
    121      *
    122      * @return List containing all registered and available instances
    123      */
    124     public Collection<IAndroidEmulatorInstance> getAvailableInstances()
    125     {
    126         Collection<IAndroidEmulatorInstance> allInstances = getAllInstances();
    127         Collection<IAndroidEmulatorInstance> enabledInstances =
    128                 new ArrayList<IAndroidEmulatorInstance>(allInstances.size());
    129 
    130         for (IAndroidEmulatorInstance emulatorInstance : allInstances)
    131         {
    132             if (emulatorInstance.isAvailable())
    133             {
    134                 enabledInstances.add(emulatorInstance);
    135             }
    136         }
    137         return enabledInstances;
    138     }
    139 
    140     /**
    141      * Retrieve a collection of names of all the IAndroidEmulatorInstance
    142      * of all Device frameworks...
    143      * @return A collection of all instances of IAndroidEmulatorInstance.
    144      */
    145     public Collection<String> getAllInstanceNames()
    146     {
    147         Collection<String> allInstancesNames = new LinkedHashSet<String>();
    148         for (IDeviceFrameworkSupport devFramework : allFrameworks)
    149         {
    150             for (IAndroidEmulatorInstance instance : devFramework.getAllInstances())
    151             {
    152                 allInstancesNames.add(instance.getName());
    153             }
    154         }
    155 
    156         return allInstancesNames;
    157 
    158     }
    159 
    160     /**
    161      * Retrieves the first occurrence of a IAndroidEmulatorInstance with the given name
    162      * provided by any framework.
    163      * @param name of the emulator instance to be retrieved.
    164      * @return reference to a IAndroidEmulatorInstance with the given name or a null
    165      * is there are no emulator instance with the given name.
    166      */
    167     public IAndroidEmulatorInstance getInstanceByName(String name)
    168     {
    169         IAndroidEmulatorInstance instanceToReturn = null;
    170 
    171         for (IAndroidEmulatorInstance instance : getAllInstances())
    172         {
    173             if (Platform.getOS().equals(Platform.WS_WIN32))
    174             {
    175                 if (instance.getName().toLowerCase().equals(name.toLowerCase()))
    176                 {
    177                     instanceToReturn = instance;
    178                     break;
    179                 }
    180             }
    181             else
    182             {
    183                 if (instance.getName().equals(name))
    184                 {
    185                     instanceToReturn = instance;
    186                     break;
    187                 }
    188             }
    189 
    190         }
    191         return instanceToReturn;
    192 
    193     }
    194 
    195     /**
    196      * Retrieves all <b>started</b> instances managed by every device framework
    197      * which contributes with the deviceFramework extension point
    198      *
    199      * @return A collection containing all started instances from all frameworks
    200      */
    201     public Collection<IAndroidEmulatorInstance> getAllStartedInstances()
    202     {
    203         Collection<IAndroidEmulatorInstance> startedInstancesSet =
    204                 new HashSet<IAndroidEmulatorInstance>();
    205         for (IDeviceFrameworkSupport devFramework : allFrameworks)
    206         {
    207             Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
    208             if (devFrmInstances != null)
    209             {
    210                 for (IAndroidEmulatorInstance instance : devFrmInstances)
    211                 {
    212                     if (instance.isStarted())
    213                     {
    214                         startedInstancesSet.add(instance);
    215                     }
    216                 }
    217             }
    218         }
    219 
    220         return startedInstancesSet;
    221     }
    222 
    223     /**
    224      * Retrieves all <b>connected</b> instances managed by every device framework
    225      * which contributes with the deviceFramework extension point
    226      *
    227      * @return A collection containing all connected instances from all frameworks
    228      */
    229     public Collection<IAndroidEmulatorInstance> getAllConnectedInstances()
    230     {
    231         Collection<IAndroidEmulatorInstance> connectedInstancesSet =
    232                 new HashSet<IAndroidEmulatorInstance>();
    233         for (IDeviceFrameworkSupport devFramework : allFrameworks)
    234         {
    235             Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
    236             if (devFrmInstances != null)
    237             {
    238                 for (IAndroidEmulatorInstance instance : devFrmInstances)
    239                 {
    240                     if (instance.isConnected())
    241                     {
    242                         connectedInstancesSet.add(instance);
    243                     }
    244                 }
    245             }
    246         }
    247 
    248         return connectedInstancesSet;
    249     }
    250 
    251     /**
    252      * Retrieves all started instances host addresses managed by every
    253      * device framework which contributes with the deviceFramework extension point
    254      *
    255      * @return A collection containing all instances from all frameworks
    256      */
    257     public Set<String> getAllStartedInstancesHosts()
    258     {
    259         Set<String> hostSet = new HashSet<String>();
    260         for (IDeviceFrameworkSupport devFramework : allFrameworks)
    261         {
    262             Collection<IAndroidEmulatorInstance> devFrmInstances = devFramework.getAllInstances();
    263             if (devFrmInstances != null)
    264             {
    265                 for (IAndroidEmulatorInstance instance : devFrmInstances)
    266                 {
    267                     if (instance.isStarted())
    268                     {
    269                         hostSet.add(instance.getInstanceIdentifier());
    270                     }
    271                 }
    272             }
    273         }
    274 
    275         return hostSet;
    276     }
    277 
    278     /**
    279      * Populates the allFrameworks collection with framework contributed
    280      * classes for retrieving framework information.
    281      */
    282     private void populateModel()
    283     {
    284         IExtensionRegistry extReg = Platform.getExtensionRegistry();
    285         IExtensionPoint extPoint = extReg.getExtensionPoint(DEV_FRAMEWORK_EXTENSION_POINT_ID);
    286         IExtension[] extensions = extPoint.getExtensions();
    287 
    288         for (IExtension aExtension : extensions)
    289         {
    290             IConfigurationElement[] configElements = aExtension.getConfigurationElements();
    291             for (IConfigurationElement aConfig : configElements)
    292             {
    293                 if (aConfig.getName().equals(DEV_FRAMEWORK_ELEM))
    294                 {
    295                     try
    296                     {
    297                         IDeviceFrameworkSupport devFramework =
    298                                 (IDeviceFrameworkSupport) aConfig
    299                                         .createExecutableExtension(DEV_FRAMEWORK_IMPL_CLASS_ATTR);
    300                         if (devFramework != null)
    301                         {
    302                             allFrameworks.add(devFramework);
    303                         }
    304                     }
    305                     catch (CoreException e)
    306                     {
    307                         // Do nothing.
    308                         // If a device framework cannot be instantiated, it will
    309                         // not be plugged to emulator core plugin.
    310                     }
    311                 }
    312             }
    313         }
    314     }
    315 }
    316