Home | History | Annotate | Download | only in repository
      1 /*
      2  * Copyright (C) 2009 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.sdkuilib.internal.repository;
     18 
     19 import com.android.sdklib.internal.repository.Archive;
     20 import com.android.sdklib.internal.repository.IDescription;
     21 import com.android.sdklib.internal.repository.Package;
     22 
     23 import java.util.ArrayList;
     24 import java.util.Collection;
     25 
     26 /**
     27  * Represents an archive that we want to install.
     28  * Note that the installer deals with archives whereas the user mostly sees packages
     29  * but as far as we are concerned for installation there's a 1-to-1 mapping.
     30  * <p/>
     31  * A new archive is always a remote archive that needs to be downloaded and then
     32  * installed. It can replace an existing local one. It can also depends on another
     33  * (new or local) archive, which means the dependent archive needs to be successfully
     34  * installed first. Finally this archive can also be a dependency for another one.
     35  * <p/>
     36  * The accepted and rejected flags are used by {@link UpdateChooserDialog} to follow
     37  * user choices. The installer should never install something that is not accepted.
     38  * <p/>
     39  * <em>Note</em>: There is currently no logic to support more than one level of
     40  * dependency, either here or in the {@link UpdateChooserDialog}, since we currently
     41  * have no need for it.
     42  *
     43  * @see ArchiveInfo#ArchiveInfo(Archive, Archive, ArchiveInfo[])
     44  */
     45 class ArchiveInfo implements IDescription {
     46 
     47     private final Archive mNewArchive;
     48     private final Archive mReplaced;
     49     private final ArchiveInfo[] mDependsOn;
     50     private final ArrayList<ArchiveInfo> mDependencyFor = new ArrayList<ArchiveInfo>();
     51     private boolean mAccepted;
     52     private boolean mRejected;
     53 
     54     /**
     55      *
     56      * @param newArchive A "new archive" to be installed. This is always an archive
     57      *          that comes from a remote site. This <em>may</em> be null.
     58      * @param replaced An optional local archive that the new one will replace.
     59      *          Can be null if this archive does not replace anything.
     60      * @param dependsOn An optional new or local dependency, that is an archive that
     61      *          <em>this</em> archive depends upon. In other words, we can only install
     62      *          this archive if the dependency has been successfully installed. It also
     63      *          means we need to install the dependency first. Can be null or empty.
     64      *          However it cannot contain nulls.
     65      */
     66     public ArchiveInfo(Archive newArchive, Archive replaced, ArchiveInfo[] dependsOn) {
     67         mNewArchive = newArchive;
     68         mReplaced = replaced;
     69         mDependsOn = dependsOn;
     70     }
     71 
     72     /**
     73      * Returns the "new archive" to be installed.
     74      * This <em>may</em> be null for missing archives.
     75      */
     76     public Archive getNewArchive() {
     77         return mNewArchive;
     78     }
     79 
     80     /**
     81      * Returns an optional local archive that the new one will replace.
     82      * Can be null if this archive does not replace anything.
     83      */
     84     public Archive getReplaced() {
     85         return mReplaced;
     86     }
     87 
     88     /**
     89      * Returns an optional new or local dependency, that is an archive that <em>this</em>
     90      * archive depends upon. In other words, we can only install this archive if the
     91      * dependency has been successfully installed. It also means we need to install the
     92      * dependency first.
     93      * This array can be null or empty. It can't contain nulls though.
     94      */
     95     public ArchiveInfo[] getDependsOn() {
     96         return mDependsOn;
     97     }
     98 
     99     /**
    100      * Returns true if this new archive is a dependency for <em>another</em> one that we
    101      * want to install.
    102      */
    103     public boolean isDependencyFor() {
    104         return mDependencyFor.size() > 0;
    105     }
    106 
    107     /**
    108      * Adds an {@link ArchiveInfo} for which <em>this</em> package is a dependency.
    109      * This means the package added here depends on this package.
    110      */
    111     public void addDependencyFor(ArchiveInfo dependencyFor) {
    112         if (!mDependencyFor.contains(dependencyFor)) {
    113             mDependencyFor.add(dependencyFor);
    114         }
    115     }
    116 
    117     public Collection<ArchiveInfo> getDependenciesFor() {
    118         return mDependencyFor;
    119     }
    120 
    121     /**
    122      * Sets whether this archive was accepted (either manually by the user or
    123      * automatically if it doesn't have a license) for installation.
    124      */
    125     public void setAccepted(boolean accepted) {
    126         mAccepted = accepted;
    127     }
    128 
    129     /**
    130      * Returns whether this archive was accepted (either manually by the user or
    131      * automatically if it doesn't have a license) for installation.
    132      */
    133     public boolean isAccepted() {
    134         return mAccepted;
    135     }
    136 
    137     /**
    138      * Sets whether this archive was rejected manually by the user.
    139      * An archive can neither accepted nor rejected.
    140      */
    141     public void setRejected(boolean rejected) {
    142         mRejected = rejected;
    143     }
    144 
    145     /**
    146      * Returns whether this archive was rejected manually by the user.
    147      * An archive can neither accepted nor rejected.
    148      */
    149     public boolean isRejected() {
    150         return mRejected;
    151     }
    152 
    153     /**
    154      * Returns the long description of the parent package of the new archive, if not null.
    155      * Otherwise returns an empty string.
    156      */
    157     public String getLongDescription() {
    158         if (mNewArchive != null) {
    159             Package p = mNewArchive.getParentPackage();
    160             if (p != null) {
    161                 return p.getLongDescription();
    162             }
    163         }
    164         return "";
    165     }
    166 
    167     /**
    168      * Returns the short description of the parent package of the new archive, if not null.
    169      * Otherwise returns an empty string.
    170      */
    171     public String getShortDescription() {
    172         if (mNewArchive != null) {
    173             Package p = mNewArchive.getParentPackage();
    174             if (p != null) {
    175                 return p.getShortDescription();
    176             }
    177         }
    178         return "";
    179     }
    180 }
    181