• 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.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 B_EXTENSION_INT;
52     private static final int AD_SERVICES_EXTENSION_INT;
53     private static final Map<Integer, Integer> ALL_EXTENSION_INTS;
54 
55     static {
56         R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0);
57         S_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.s", 0);
58         T_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.t", 0);
59         U_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.u", 0);
60         V_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.v", 0);
61         B_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.b", 0);
62         AD_SERVICES_EXTENSION_INT =
63                 SystemProperties.getInt("build.version.extensions.ad_services", 0);
64         Map<Integer, Integer> extensions = new HashMap<Integer, Integer>();
extensions.put(VERSION_CODES.R, R_EXTENSION_INT)65         extensions.put(VERSION_CODES.R, R_EXTENSION_INT);
66         if (SdkLevel.isAtLeastS()) {
extensions.put(VERSION_CODES.S, S_EXTENSION_INT)67             extensions.put(VERSION_CODES.S, S_EXTENSION_INT);
68         }
69         if (SdkLevel.isAtLeastT()) {
extensions.put(VERSION_CODES.TIRAMISU, T_EXTENSION_INT)70             extensions.put(VERSION_CODES.TIRAMISU, T_EXTENSION_INT);
extensions.put(AD_SERVICES, AD_SERVICES_EXTENSION_INT)71             extensions.put(AD_SERVICES, AD_SERVICES_EXTENSION_INT);
72         }
73         if (SdkLevel.isAtLeastU()) {
extensions.put(VERSION_CODES.UPSIDE_DOWN_CAKE, U_EXTENSION_INT)74             extensions.put(VERSION_CODES.UPSIDE_DOWN_CAKE, U_EXTENSION_INT);
75         }
76         if (SdkLevel.isAtLeastV()) {
extensions.put(VERSION_CODES.VANILLA_ICE_CREAM, V_EXTENSION_INT)77             extensions.put(VERSION_CODES.VANILLA_ICE_CREAM, V_EXTENSION_INT);
78         }
79         if (SdkLevel.isAtLeastB()) {
extensions.put(VERSION_CODES.BAKLAVA, B_EXTENSION_INT)80             extensions.put(VERSION_CODES.BAKLAVA, B_EXTENSION_INT);
81         }
82         ALL_EXTENSION_INTS = Collections.unmodifiableMap(extensions);
83     }
84 
85     /**
86      * Values suitable as parameters for {@link #getExtensionVersion(int)}.
87      *
88      * @hide
89      */
90     @IntDef(
91             value = {
92                 VERSION_CODES.R,
93                 VERSION_CODES.S,
94                 VERSION_CODES.TIRAMISU,
95                 VERSION_CODES.UPSIDE_DOWN_CAKE,
96                 VERSION_CODES.VANILLA_ICE_CREAM,
97                 VERSION_CODES.BAKLAVA,
98                 AD_SERVICES,
99             })
100     @Retention(RetentionPolicy.SOURCE)
101     public @interface Extension {}
102 
SdkExtensions()103     private SdkExtensions() {}
104 
105     /**
106      * Return the version of the specified extensions.
107      *
108      * <p>This method is suitable to use in conditional statements to determine whether an API is
109      * available and is safe to use. For example:
110      *
111      * <pre>
112      * if (getExtensionVersion(VERSION_CODES.R) >= 3) {
113      *   // Safely use API available since R extensions version 3
114      * }
115      * </pre>
116      *
117      * @param extension the extension to get the version of.
118      * @throws IllegalArgumentException if extension is not a valid extension
119      */
getExtensionVersion(@xtension int extension)120     public static int getExtensionVersion(@Extension int extension) {
121         if (extension < VERSION_CODES.R) {
122             throw new IllegalArgumentException("not a valid extension: " + extension);
123         }
124 
125         if (extension == VERSION_CODES.R) {
126             return R_EXTENSION_INT;
127         }
128         if (extension == VERSION_CODES.S) {
129             return S_EXTENSION_INT;
130         }
131         if (extension == VERSION_CODES.TIRAMISU) {
132             return T_EXTENSION_INT;
133         }
134         if (extension == VERSION_CODES.UPSIDE_DOWN_CAKE) {
135             return U_EXTENSION_INT;
136         }
137         if (extension == VERSION_CODES.VANILLA_ICE_CREAM) {
138             return V_EXTENSION_INT;
139         }
140         if (extension == VERSION_CODES.BAKLAVA) {
141             return B_EXTENSION_INT;
142         }
143         if (extension == AD_SERVICES) {
144             return AD_SERVICES_EXTENSION_INT;
145         }
146         return 0;
147     }
148 
149     /**
150      * Return all extension versions that exist on this device.
151      *
152      * @return a map from extension to extension version.
153      */
154     @NonNull
getAllExtensionVersions()155     public static Map<Integer, Integer> getAllExtensionVersions() {
156         return ALL_EXTENSION_INTS;
157     }
158 }
159