1 /* 2 * Copyright (C) 2019 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 android.os.ext; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.os.Build.VERSION_CODES; 22 import android.os.SystemProperties; 23 24 import com.android.modules.utils.build.SdkLevel; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import java.util.Collections; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /** 33 * Methods for interacting with the extension SDK. 34 * 35 * <p>This class provides information about the extension SDK versions present on this device. Use 36 * the {@link #getExtensionVersion(int) getExtension} method to lookup the version of a given 37 * extension. 38 * 39 * <p>The extension version advances as the platform evolves and new APIs are added, so is suitable 40 * to use for determining API availability at runtime. 41 */ 42 public class SdkExtensions { 43 44 public static final int AD_SERVICES = 1_000_000; 45 46 private static final int R_EXTENSION_INT; 47 private static final int S_EXTENSION_INT; 48 private static final int T_EXTENSION_INT; 49 private static final int U_EXTENSION_INT; 50 private static final int V_EXTENSION_INT; 51 private static final int AD_SERVICES_EXTENSION_INT; 52 private static final Map<Integer, Integer> ALL_EXTENSION_INTS; 53 54 static { 55 R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0); 56 S_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.s", 0); 57 T_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.t", 0); 58 U_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.u", 0); 59 V_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.v", 0); 60 AD_SERVICES_EXTENSION_INT = 61 SystemProperties.getInt("build.version.extensions.ad_services", 0); 62 Map<Integer, Integer> extensions = new HashMap<Integer, Integer>(); extensions.put(VERSION_CODES.R, R_EXTENSION_INT)63 extensions.put(VERSION_CODES.R, R_EXTENSION_INT); 64 if (SdkLevel.isAtLeastS()) { extensions.put(VERSION_CODES.S, S_EXTENSION_INT)65 extensions.put(VERSION_CODES.S, S_EXTENSION_INT); 66 } 67 if (SdkLevel.isAtLeastT()) { extensions.put(VERSION_CODES.TIRAMISU, T_EXTENSION_INT)68 extensions.put(VERSION_CODES.TIRAMISU, T_EXTENSION_INT); extensions.put(AD_SERVICES, AD_SERVICES_EXTENSION_INT)69 extensions.put(AD_SERVICES, AD_SERVICES_EXTENSION_INT); 70 } 71 if (SdkLevel.isAtLeastU()) { extensions.put(VERSION_CODES.UPSIDE_DOWN_CAKE, U_EXTENSION_INT)72 extensions.put(VERSION_CODES.UPSIDE_DOWN_CAKE, U_EXTENSION_INT); 73 } 74 if (SdkLevel.isAtLeastV()) { extensions.put(VERSION_CODES.VANILLA_ICE_CREAM, V_EXTENSION_INT)75 extensions.put(VERSION_CODES.VANILLA_ICE_CREAM, V_EXTENSION_INT); 76 } 77 ALL_EXTENSION_INTS = Collections.unmodifiableMap(extensions); 78 } 79 80 /** 81 * Values suitable as parameters for {@link #getExtensionVersion(int)}. 82 * 83 * @hide 84 */ 85 @IntDef( 86 value = { 87 VERSION_CODES.R, 88 VERSION_CODES.S, 89 VERSION_CODES.TIRAMISU, 90 VERSION_CODES.UPSIDE_DOWN_CAKE, 91 VERSION_CODES.VANILLA_ICE_CREAM, 92 AD_SERVICES, 93 }) 94 @Retention(RetentionPolicy.SOURCE) 95 public @interface Extension {} 96 SdkExtensions()97 private SdkExtensions() {} 98 99 /** 100 * Return the version of the specified extensions. 101 * 102 * <p>This method is suitable to use in conditional statements to determine whether an API is 103 * available and is safe to use. For example: 104 * 105 * <pre> 106 * if (getExtensionVersion(VERSION_CODES.R) >= 3) { 107 * // Safely use API available since R extensions version 3 108 * } 109 * </pre> 110 * 111 * @param extension the extension to get the version of. 112 * @throws IllegalArgumentException if extension is not a valid extension 113 */ getExtensionVersion(@xtension int extension)114 public static int getExtensionVersion(@Extension int extension) { 115 if (extension < VERSION_CODES.R) { 116 throw new IllegalArgumentException("not a valid extension: " + extension); 117 } 118 119 if (extension == VERSION_CODES.R) { 120 return R_EXTENSION_INT; 121 } 122 if (extension == VERSION_CODES.S) { 123 return S_EXTENSION_INT; 124 } 125 if (extension == VERSION_CODES.TIRAMISU) { 126 return T_EXTENSION_INT; 127 } 128 if (extension == VERSION_CODES.UPSIDE_DOWN_CAKE) { 129 return U_EXTENSION_INT; 130 } 131 if (extension == VERSION_CODES.VANILLA_ICE_CREAM) { 132 return V_EXTENSION_INT; 133 } 134 if (extension == AD_SERVICES) { 135 return AD_SERVICES_EXTENSION_INT; 136 } 137 return 0; 138 } 139 140 /** 141 * Return all extension versions that exist on this device. 142 * 143 * @return a map from extension to extension version. 144 */ 145 @NonNull getAllExtensionVersions()146 public static Map<Integer, Integer> getAllExtensionVersions() { 147 return ALL_EXTENSION_INTS; 148 } 149 } 150