1 /* 2 * Copyright 2010 Google Inc. 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 package com.google.android.testing.mocking; 17 18 import java.lang.reflect.Field; 19 import java.util.ArrayList; 20 import java.util.List; 21 22 23 /** 24 * Represents different SDK versions of the Android SDK. 25 * 26 * @author swoodward (at) google.com (Stephen Woodward) 27 */ 28 public enum SdkVersion { 29 UNKNOWN("", -1), CUPCAKE("v15", 3), DONUT("v16", 4), ECLAIR_0_1("v201", 6), 30 ECLAIR_MR1("v21", 7), FROYO("v22", 8), GINGERBREAD("v23", 9); 31 32 private static final int SDK_VERSION; 33 34 static { 35 String sdkString = null; 36 int sdkInt; 37 try { 38 Class<?> buildClass = Class.forName("android.os.Build$VERSION"); 39 Field sdkField = buildClass.getField("SDK"); 40 sdkString = (String) sdkField.get(null); 41 sdkInt = Integer.parseInt(sdkString); 42 } catch (Exception e) { 43 // This will always happen on the desktop side. No big deal. 44 if (sdkString != null) { 45 // But this is unexpected 46 System.out.println(e.toString()); 47 e.printStackTrace(); 48 } 49 sdkInt = -1; 50 } 51 SDK_VERSION = sdkInt; 52 } 53 54 private final String prefix; 55 private final String versionName; 56 private final int apiLevel; 57 58 private SdkVersion(String packagePrefix, int apiLevel) { 59 versionName = packagePrefix; 60 prefix = packagePrefix.length() == 0 ? "" : packagePrefix + "."; 61 this.apiLevel = apiLevel; 62 } 63 64 /** 65 * Returns an array of SdkVersion objects. This is to be favoured over the 66 * {@link #values()} method, since that method will also return the UNKNOWN 67 * SDK version, which is not usually a valid version on which to operate. 68 * 69 * @return an array of SdkVersion objects. 70 */ 71 public static SdkVersion[] getAllVersions() { 72 List<SdkVersion> versions = new ArrayList<SdkVersion>(); 73 for (SdkVersion version : values()) { 74 if (!version.equals(UNKNOWN)) { 75 versions.add(version); 76 } 77 } 78 return versions.toArray(new SdkVersion[versions.size()]); 79 } 80 81 public String getVersionName() { 82 return versionName; 83 } 84 85 public String getPackagePrefix() { 86 return prefix; 87 } 88 89 /** 90 * Returns the current SDK version, or UNKNOWN if the version cannot be determined (for instance 91 * if this method is invoked from within a J2SE environment). 92 * @return the current SDK version. 93 */ 94 public static SdkVersion getCurrentVersion() { 95 return getVersionFor(SDK_VERSION); 96 } 97 98 static SdkVersion getVersionFor(int apiLevel) { 99 for (SdkVersion version : values()) { 100 if (version.apiLevel == apiLevel) { 101 return version; 102 } 103 } 104 return UNKNOWN; 105 } 106 } 107