Home | History | Annotate | Download | only in sdklib
      1 /*
      2  * Copyright (C) 2011 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.android.sdklib;
     18 
     19 import com.android.sdklib.io.FileOp;
     20 
     21 import java.io.File;
     22 
     23 
     24 /**
     25  * Describes a system image as used by an {@link IAndroidTarget}.
     26  * A system image has an installation path, a location type and an ABI type.
     27  */
     28 public class SystemImage implements ISystemImage {
     29 
     30     public static final String ANDROID_PREFIX = "android-";     //$NON-NLS-1$
     31 
     32     private final LocationType mLocationtype;
     33     private final String mAbiType;
     34     private final File mLocation;
     35 
     36     /**
     37      * Creates a {@link SystemImage} description for an existing system image folder.
     38      *
     39      * @param location The location of an installed system image.
     40      * @param locationType Where the system image folder is located for this ABI.
     41      * @param abiType The ABI type. For example, one of {@link SdkConstants#ABI_ARMEABI},
     42      *          {@link SdkConstants#ABI_ARMEABI_V7A} or  {@link SdkConstants#ABI_INTEL_ATOM}.
     43      */
     44     public SystemImage(File location, LocationType locationType, String abiType) {
     45         mLocation = location;
     46         mLocationtype = locationType;
     47         mAbiType = abiType;
     48     }
     49 
     50     /**
     51      * Creates a {@link SystemImage} description for a non-existing system image folder.
     52      * The actual location is computed based on the {@code locationtype}.
     53      *
     54      * @param sdkManager The current SDK manager.
     55      * @param locationType Where the system image folder is located for this ABI.
     56      * @param abiType The ABI type. For example, one of {@link SdkConstants#ABI_ARMEABI},
     57      *          {@link SdkConstants#ABI_ARMEABI_V7A} or  {@link SdkConstants#ABI_INTEL_ATOM}.
     58      * @throws IllegalArgumentException if the {@code target} used for
     59      *         {@link ISystemImage.LocationType#IN_SYSTEM_IMAGE} is not a {@link PlatformTarget}.
     60      */
     61     public SystemImage(
     62             SdkManager sdkManager,
     63             IAndroidTarget target,
     64             LocationType locationType,
     65             String abiType) {
     66         mLocationtype = locationType;
     67         mAbiType = abiType;
     68 
     69         File location = null;
     70         switch(locationType) {
     71         case IN_PLATFORM_LEGACY:
     72             location = new File(target.getLocation(), SdkConstants.OS_IMAGES_FOLDER);
     73             break;
     74 
     75         case IN_PLATFORM_SUBFOLDER:
     76             location = FileOp.append(target.getLocation(), SdkConstants.OS_IMAGES_FOLDER, abiType);
     77             break;
     78 
     79         case IN_SYSTEM_IMAGE:
     80             if (!target.isPlatform()) {
     81                 throw new IllegalArgumentException(
     82                         "Add-ons do not support the system-image location type"); //$NON-NLS-1$
     83             }
     84 
     85             location = getCanonicalFolder(sdkManager.getLocation(), target.getVersion(), abiType);
     86             break;
     87         default:
     88             // This is not supposed to happen unless LocationType is
     89             // extended without adjusting this code.
     90             assert false : "SystemImage used with an incorrect locationType";       //$NON-NLS-1$
     91         }
     92         mLocation = location;
     93     }
     94 
     95     /**
     96      * Static helper method that returns the canonical path for a system-image that uses
     97      * the {@link ISystemImage.LocationType#IN_SYSTEM_IMAGE} location type.
     98      * <p/>
     99      * Such an image is located in {@code SDK/system-images/android-N/abiType}.
    100      * For this reason this method requires the root SDK as well as the platform and the ABI type.
    101      *
    102      * @param sdkOsPath The OS path to the SDK.
    103      * @param platformVersion The platform version.
    104      * @param abiType An optional ABI type. If null, the parent directory is returned.
    105      * @return A file that represents the location of the canonical system-image folder
    106      *         for this configuration.
    107      */
    108     public static File getCanonicalFolder(
    109             String sdkOsPath,
    110             AndroidVersion platformVersion,
    111             String abiType) {
    112         File root = FileOp.append(
    113                 sdkOsPath,
    114                 SdkConstants.FD_SYSTEM_IMAGES,
    115                 ANDROID_PREFIX + platformVersion.getApiString());
    116         if (abiType == null) {
    117             return root;
    118         } else {
    119             return FileOp.append(root, abiType);
    120         }
    121     }
    122 
    123     /** Returns the actual location of an installed system image. */
    124     public File getLocation() {
    125         return mLocation;
    126     }
    127 
    128     /** Indicates the location strategy for this system image in the SDK. */
    129     public LocationType getLocationType() {
    130         return mLocationtype;
    131     }
    132 
    133     /**
    134      * Returns the ABI type. For example, one of {@link SdkConstants#ABI_ARMEABI},
    135      * {@link SdkConstants#ABI_ARMEABI_V7A} or  {@link SdkConstants#ABI_INTEL_ATOM}.
    136      * Cannot be null nor empty.
    137      */
    138     public String getAbiType() {
    139         return mAbiType;
    140     }
    141 
    142     public int compareTo(ISystemImage other) {
    143         // Sort by ABI name only. This is what matters from a user point of view.
    144         return this.getAbiType().compareToIgnoreCase(other.getAbiType());
    145     }
    146 
    147     /**
    148      * Generates a string representation suitable for debug purposes.
    149      * The string is not intended to be displayed to the user.
    150      *
    151      * {@inheritDoc}
    152      */
    153     @Override
    154     public String toString() {
    155         return String.format("SystemImage ABI=%s, location %s='%s'",           //$NON-NLS-1$
    156                 mAbiType,
    157                 mLocationtype.toString().replace('_', ' ').toLowerCase(),
    158                 mLocation
    159                 );
    160     }
    161 
    162 
    163 }
    164