| 1 |
/* |
| 2 |
* Copyright (C) 2015 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.SdkConstants; |
| 20 |
import com.android.annotations.NonNull; |
| 21 |
import com.android.annotations.Nullable; |
| 22 |
import com.google.common.base.Objects; |
| 23 |
|
| 24 |
import java.util.regex.Pattern; |
| 25 |
|
| 26 |
/** |
| 27 |
* <p> |
| 28 |
* Represents the version of a target or device. |
| 29 |
* </p> |
| 30 |
* A version is defined by an API level and an optional code name. |
| 31 |
* <ul><li>Release versions of the Android platform are identified by their API level (integer), |
| 32 |
* (technically the code name for release version is "REL" but this class will return |
| 33 |
* <code>null</code> instead.)</li> |
| 34 |
* <li>Preview versions of the platform are identified by a code name. Their API level |
| 35 |
* is usually set to the value of the previous platform.</li></ul> |
| 36 |
* <p> |
| 37 |
* While this class contains both values, its goal is to abstract them, so that code comparing 2+ |
| 38 |
* versions doesn't have to deal with the logic of handle both values. |
| 39 |
* </p> |
| 40 |
* <p> |
| 41 |
* There are some cases where ones may want to access the values directly. This can be done |
| 42 |
* with {@link #getApiLevel()} and {@link #getCodename()}. |
| 43 |
* </p> |
| 44 |
* For generic UI display of the API version, {@link #getApiString()} is to be used. |
| 45 |
*/ |
| 46 |
public final class AndroidVersion implements Comparable<AndroidVersion> { |
| 47 |
|
| 48 |
private final int mApiLevel; |
| 49 |
@Nullable |
| 50 |
private final String mCodename; |
| 51 |
|
| 52 |
/** The default AndroidVersion for minSdkVersion and targetSdkVersion if not specified. */ |
| 53 |
public static final AndroidVersion DEFAULT = new AndroidVersion(1, null); |
| 54 |
|
| 55 |
/** First version to use ART by default. */ |
| 56 |
public static final AndroidVersion ART_RUNTIME = new AndroidVersion(21, null); |
| 57 |
|
| 58 |
/** First version to feature binder's common interface "cmd" for sending shell commands to services. */ |
| 59 |
public static final AndroidVersion BINDER_CMD_AVAILABLE = new AndroidVersion(24, null); |
| 60 |
|
| 61 |
/** First version to allow split apks */ |
| 62 |
public static final AndroidVersion ALLOW_SPLIT_APK_INSTALLATION = new AndroidVersion(21, null); |
| 63 |
|
| 64 |
/** First version to have multi-user support (JB-MR2, API 17) */ |
| 65 |
public static final AndroidVersion SUPPORTS_MULTI_USER = new AndroidVersion(17, null); |
| 66 |
|
| 67 |
/** |
| 68 |
* Thrown when an {@link AndroidVersion} object could not be created. |
| 69 |
*/ |
| 70 |
public static final class AndroidVersionException extends Exception { |
| 71 |
private static final long serialVersionUID = 1L; |
| 72 |
|
| 73 |
AndroidVersionException(String message, Throwable cause) { |
| 74 |
super(message, cause); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Creates an {@link AndroidVersion} with the given api level and codename. |
| 80 |
* Codename should be null for a release version, otherwise it's a preview codename. |
| 81 |
*/ |
| 82 |
public AndroidVersion(int apiLevel, @Nullable String codename) { |
| 83 |
mApiLevel = apiLevel; |
| 84 |
mCodename = sanitizeCodename(codename); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* <p> |
| 89 |
* Creates an {@link AndroidVersion} from a string that may be an integer API |
| 90 |
* level or a string codename. |
| 91 |
* </p> |
| 92 |
* <Em>Important</em>: An important limitation of this method is that cannot possible |
| 93 |
* recreate the API level integer from a pure string codename. This is only OK to use |
| 94 |
* if the caller can guarantee that only {@link #getApiString()} will be used later. |
| 95 |
* Wrong things will happen if the caller then tries to resolve the numeric |
| 96 |
* {@link #getApiLevel()}. |
| 97 |
* |
| 98 |
* @param apiOrCodename A non-null API integer or a codename in its "ALL_CAPS" format. |
| 99 |
* "REL" is notable not a valid codename. |
| 100 |
* @throws AndroidVersionException if the input isn't a pure integer or doesn't look like |
| 101 |
* a valid string codename. |
| 102 |
*/ |
| 103 |
public AndroidVersion(@NonNull String apiOrCodename) throws AndroidVersionException { |
| 104 |
int apiLevel = 0; |
| 105 |
String codename = null; |
| 106 |
try { |
| 107 |
apiLevel = Integer.parseInt(apiOrCodename); |
| 108 |
} catch (NumberFormatException ignore) { |
| 109 |
// We don't know the API level. Android platform codenames are all caps. |
| 110 |
// REL is a release-reserved keyword which we can use here. |
| 111 |
|
| 112 |
if (!SdkConstants.CODENAME_RELEASE.equals(apiOrCodename)) { |
| 113 |
if (Pattern.matches("[A-Z_]+", apiOrCodename)) { |
| 114 |
codename = apiOrCodename; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
mApiLevel = apiLevel; |
| 120 |
mCodename = sanitizeCodename(codename); |
| 121 |
|
| 122 |
if (mApiLevel <= 0 && codename == null) { |
| 123 |
throw new AndroidVersionException( |
| 124 |
"Invalid android API or codename " + apiOrCodename, //$NON-NLS-1$ |
| 125 |
null); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns the api level as an integer. |
| 131 |
* <p>For target that are in preview mode, this can be superseded by |
| 132 |
* {@link #getCodename()}.</p> |
| 133 |
* <p>To display the API level in the UI, use {@link #getApiString()}, which will use the |
| 134 |
* codename if applicable.</p> |
| 135 |
* @see #getCodename() |
| 136 |
* @see #getApiString() |
| 137 |
*/ |
| 138 |
public int getApiLevel() { |
| 139 |
return mApiLevel; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the API level as an integer. If this is a preview platform, it |
| 144 |
* will return the expected final version of the API rather than the current API |
| 145 |
* level. This is the "feature level" as opposed to the "release level" returned by |
| 146 |
* {@link #getApiLevel()} in the sense that it is useful when you want |
| 147 |
* to check the presence of a given feature from an API, and we consider the feature |
| 148 |
* present in preview platforms as well. |
| 149 |
* |
| 150 |
* @return the API level of this version, +1 for preview platforms |
| 151 |
*/ |
| 152 |
public int getFeatureLevel() { |
| 153 |
//noinspection VariableNotUsedInsideIf |
| 154 |
return mCodename != null ? mApiLevel + 1 : mApiLevel; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Returns the version code name if applicable, null otherwise. |
| 159 |
* <p>If the codename is non null, then the API level should be ignored, and this should be |
| 160 |
* used as a unique identifier of the target instead.</p> |
| 161 |
*/ |
| 162 |
@Nullable |
| 163 |
public String getCodename() { |
| 164 |
return mCodename; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns a string representing the API level and/or the code name. |
| 169 |
*/ |
| 170 |
@NonNull |
| 171 |
public String getApiString() { |
| 172 |
if (mCodename != null) { |
| 173 |
return mCodename; |
| 174 |
} |
| 175 |
|
| 176 |
return Integer.toString(mApiLevel); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns whether or not the version is a preview version. |
| 181 |
*/ |
| 182 |
public boolean isPreview() { |
| 183 |
return mCodename != null; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Checks whether a device running a version similar to the receiver can run a project compiled |
| 188 |
* for the given <var>version</var>. |
| 189 |
* <p> |
| 190 |
* Be aware that this is not a perfect test, as other properties could break compatibility |
| 191 |
* despite this method returning true. |
| 192 |
* </p> |
| 193 |
* <p> |
| 194 |
* Nevertheless, when testing if an application can run on a device (where there is no |
| 195 |
* access to the list of optional libraries), this method can give a good indication of whether |
| 196 |
* there is a chance the application could run, or if there's a direct incompatibility. |
| 197 |
* </p> |
| 198 |
*/ |
| 199 |
public boolean canRun(@NonNull AndroidVersion appVersion) { |
| 200 |
// if the application is compiled for a preview version, the device must be running exactly |
| 201 |
// the same. |
| 202 |
if (appVersion.mCodename != null) { |
| 203 |
return appVersion.mCodename.equals(mCodename); |
| 204 |
} |
| 205 |
|
| 206 |
// otherwise, we check the api level (note that a device running a preview version |
| 207 |
// will have the api level of the previous platform). |
| 208 |
return mApiLevel >= appVersion.mApiLevel; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Returns <code>true</code> if the AndroidVersion is an API level equals to |
| 213 |
* <var>apiLevel</var>. |
| 214 |
*/ |
| 215 |
public boolean equals(int apiLevel) { |
| 216 |
return mCodename == null && apiLevel == mApiLevel; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public boolean equals(Object obj) { |
| 221 |
if (!(obj instanceof AndroidVersion)) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
AndroidVersion other = (AndroidVersion) obj; |
| 225 |
return mApiLevel == other.mApiLevel && Objects.equal(mCodename, other.mCodename); |
| 226 |
} |
| 227 |
|
| 228 |
public int hashCode() { |
| 229 |
if (mCodename != null) { |
| 230 |
return mCodename.hashCode(); |
| 231 |
} |
| 232 |
|
| 233 |
// there may be some collisions between the hashcode of the codename and the api level |
| 234 |
// but it's acceptable. |
| 235 |
return mApiLevel; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns a string with the API Level and optional codename. |
| 240 |
* Useful for debugging. |
| 241 |
* For display purpose, please use {@link #getApiString()} instead. |
| 242 |
*/ |
| 243 |
@Override |
| 244 |
public String toString() { |
| 245 |
String s = String.format("API %1$d", mApiLevel); //$NON-NLS-1$ |
| 246 |
if (isPreview()) { |
| 247 |
s += String.format(", %1$s preview", mCodename); //$NON-NLS-1$ |
| 248 |
} |
| 249 |
return s; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Compares this object with the specified object for order. Returns a |
| 254 |
* negative integer, zero, or a positive integer as this object is less |
| 255 |
* than, equal to, or greater than the specified object. |
| 256 |
* |
| 257 |
* @param o the Object to be compared. |
| 258 |
* @return a negative integer, zero, or a positive integer as this object is |
| 259 |
* less than, equal to, or greater than the specified object. |
| 260 |
*/ |
| 261 |
@Override |
| 262 |
public int compareTo(@NonNull AndroidVersion o) { |
| 263 |
return compareTo(o.mApiLevel, o.mCodename); |
| 264 |
} |
| 265 |
|
| 266 |
public int compareTo(int apiLevel, @Nullable String codename) { |
| 267 |
if (mCodename == null) { |
| 268 |
if (codename == null) { |
| 269 |
return mApiLevel - apiLevel; |
| 270 |
} else { |
| 271 |
if (mApiLevel == apiLevel) { |
| 272 |
return -1; // same api level but argument is a preview for next version |
| 273 |
} |
| 274 |
|
| 275 |
return mApiLevel - apiLevel; |
| 276 |
} |
| 277 |
} else { |
| 278 |
// 'this' is a preview |
| 279 |
if (mApiLevel == apiLevel) { |
| 280 |
if (codename == null) { |
| 281 |
return +1; |
| 282 |
} else { |
| 283 |
return mCodename.compareTo(codename); // strange case where the 2 previews |
| 284 |
// have different codename? |
| 285 |
} |
| 286 |
} else { |
| 287 |
return mApiLevel - apiLevel; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Compares this version with the specified API and returns true if this version |
| 294 |
* is greater or equal than the requested API -- that is the current version is a |
| 295 |
* suitable min-api-level for the argument API. |
| 296 |
*/ |
| 297 |
public boolean isGreaterOrEqualThan(int api) { |
| 298 |
return compareTo(api, null /*codename*/) >= 0; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Sanitizes the codename string according to the following rules: |
| 303 |
* - A codename should be {@code null} for a release version or it should be a non-empty |
| 304 |
* string for an actual preview. |
| 305 |
* - In input, spacing is trimmed since it is irrelevant. |
| 306 |
* - An empty string or the special codename "REL" means a release version |
| 307 |
* and is converted to {@code null}. |
| 308 |
* |
| 309 |
* @param codename A possible-null codename. |
| 310 |
* @return Null for a release version or a non-empty codename. |
| 311 |
*/ |
| 312 |
@Nullable |
| 313 |
private static String sanitizeCodename(@Nullable String codename) { |
| 314 |
if (codename != null) { |
| 315 |
codename = codename.trim(); |
| 316 |
if (codename.isEmpty() || SdkConstants.CODENAME_RELEASE.equals(codename)) { |
| 317 |
codename = null; |
| 318 |
} |
| 319 |
} |
| 320 |
return codename; |
| 321 |
} |
| 322 |
} |