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.annotation.SystemApi; 22 import android.os.Build.VERSION_CODES; 23 import android.os.SystemProperties; 24 25 import com.android.modules.utils.build.SdkLevel; 26 27 import java.lang.annotation.Retention; 28 import java.lang.annotation.RetentionPolicy; 29 import java.util.Collections; 30 import java.util.HashMap; 31 import java.util.Map; 32 33 /** 34 * Methods for interacting with the extension SDK. 35 * 36 * This class provides information about the extension SDK version present 37 * on this device. Use the {@link #getExtensionVersion(int) getExtension} to 38 * query for the extension version for the given SDK version. 39 40 * @hide 41 */ 42 @SystemApi 43 public class SdkExtensions { 44 45 // S_VERSION_CODE is a separate field to simplify management across branches. 46 private static final int VERSION_CODE_S = VERSION_CODES.S; 47 private static final int R_EXTENSION_INT; 48 private static final int S_EXTENSION_INT; 49 private static final Map<Integer, Integer> ALL_EXTENSION_INTS; 50 static { 51 // Note: when adding more extension versions, the logic that records current 52 // extension versions when saving a rollback must also be updated. 53 // At the time of writing this is in RollbackManagerServiceImpl#getExtensionVersions() 54 R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0); 55 S_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.s", 0); 56 Map<Integer, Integer> extensions = new HashMap<Integer, Integer>(); extensions.put(VERSION_CODES.R, R_EXTENSION_INT)57 extensions.put(VERSION_CODES.R, R_EXTENSION_INT); 58 if (SdkLevel.isAtLeastS()) { extensions.put(VERSION_CODE_S, S_EXTENSION_INT)59 extensions.put(VERSION_CODE_S, S_EXTENSION_INT); 60 } 61 ALL_EXTENSION_INTS = Collections.unmodifiableMap(extensions); 62 } 63 64 /** 65 * Values suitable as parameters for {@link #getExtensionVersion(int)}. 66 * @hide 67 */ 68 @IntDef(value = { VERSION_CODES.R, VERSION_CODE_S }) 69 @Retention(RetentionPolicy.SOURCE) 70 public @interface Extension {} 71 SdkExtensions()72 private SdkExtensions() { } 73 74 /** 75 * Return the version of the specified extensions. 76 * 77 * @param extension the extension to get the version of. 78 * @throws IllegalArgumentException if extension is not a valid extension 79 */ getExtensionVersion(@xtension int extension)80 public static int getExtensionVersion(@Extension int extension) { 81 if (extension < VERSION_CODES.R) { 82 throw new IllegalArgumentException("not a valid extension: " + extension); 83 } 84 85 if (extension == VERSION_CODES.R) { 86 return R_EXTENSION_INT; 87 } 88 if (extension == VERSION_CODE_S) { 89 return S_EXTENSION_INT; 90 } 91 return 0; 92 } 93 94 /** 95 * Return all extension versions that exist on this device. 96 * 97 * @return a map from extension to extension version. 98 */ 99 @NonNull getAllExtensionVersions()100 public static Map<Integer, Integer> getAllExtensionVersions() { 101 return ALL_EXTENSION_INTS; 102 } 103 104 } 105