• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 versions present
37  * on this device. Use the {@link #getExtensionVersion(int) getExtension} method
38  * to lookup the version of a given extension.
39  *
40  * The extension version advances as the platform evolves and new APIs are added,
41  * so is suitable to use for determining API availability at runtime.
42  */
43 public class SdkExtensions {
44 
45     private static final int R_EXTENSION_INT;
46     private static final int S_EXTENSION_INT;
47     private static final int T_EXTENSION_INT;
48     private static final Map<Integer, Integer> ALL_EXTENSION_INTS;
49     static {
50         R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0);
51         S_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.s", 0);
52         T_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.t", 0);
53         Map<Integer, Integer> extensions = new HashMap<Integer, Integer>();
extensions.put(VERSION_CODES.R, R_EXTENSION_INT)54         extensions.put(VERSION_CODES.R, R_EXTENSION_INT);
55         if (SdkLevel.isAtLeastS()) {
extensions.put(VERSION_CODES.S, S_EXTENSION_INT)56             extensions.put(VERSION_CODES.S, S_EXTENSION_INT);
57         }
58         if (SdkLevel.isAtLeastT()) {
extensions.put(VERSION_CODES.TIRAMISU, T_EXTENSION_INT)59             extensions.put(VERSION_CODES.TIRAMISU, T_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_CODES.S, VERSION_CODES.TIRAMISU })
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      * This method is suitable to use in conditional statements to determine whether an API is
78      * available and is safe to use. For example:
79      * <pre>
80      * if (getExtensionVersion(VERSION_CODES.R) >= 3) {
81      *   // Safely use API available since R extensions version 3
82      * }
83      * </pre>
84      *
85      * @param extension the extension to get the version of.
86      * @throws IllegalArgumentException if extension is not a valid extension
87      */
getExtensionVersion(@xtension int extension)88     public static int getExtensionVersion(@Extension int extension) {
89         if (extension < VERSION_CODES.R) {
90             throw new IllegalArgumentException("not a valid extension: " + extension);
91         }
92 
93         if (extension == VERSION_CODES.R) {
94             return R_EXTENSION_INT;
95         }
96         if (extension == VERSION_CODES.S) {
97             return S_EXTENSION_INT;
98         }
99         if (extension == VERSION_CODES.TIRAMISU) {
100             return T_EXTENSION_INT;
101         }
102         return 0;
103     }
104 
105     /**
106      * Return all extension versions that exist on this device.
107      *
108      * @return a map from extension to extension version.
109      */
110     @NonNull
getAllExtensionVersions()111     public static Map<Integer, Integer> getAllExtensionVersions() {
112         return ALL_EXTENSION_INTS;
113     }
114 
115 }
116