• 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  * This class provides information about the extension SDK versions present
36  * on this device. Use the {@link #getExtensionVersion(int) getExtension} method
37  * to lookup the version of a given extension.
38  *
39  * The extension version advances as the platform evolves and new APIs are added,
40  * so is suitable 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 AD_SERVICES_EXTENSION_INT;
51     private static final Map<Integer, Integer> ALL_EXTENSION_INTS;
52     static {
53         R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0);
54         S_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.s", 0);
55         T_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.t", 0);
56         U_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.u", 0);
57         AD_SERVICES_EXTENSION_INT =
58                 SystemProperties.getInt("build.version.extensions.ad_services", 0);
59         Map<Integer, Integer> extensions = new HashMap<Integer, Integer>();
extensions.put(VERSION_CODES.R, R_EXTENSION_INT)60         extensions.put(VERSION_CODES.R, R_EXTENSION_INT);
61         if (SdkLevel.isAtLeastS()) {
extensions.put(VERSION_CODES.S, S_EXTENSION_INT)62             extensions.put(VERSION_CODES.S, S_EXTENSION_INT);
63         }
64         if (SdkLevel.isAtLeastT()) {
extensions.put(VERSION_CODES.TIRAMISU, T_EXTENSION_INT)65             extensions.put(VERSION_CODES.TIRAMISU, T_EXTENSION_INT);
extensions.put(AD_SERVICES, AD_SERVICES_EXTENSION_INT)66             extensions.put(AD_SERVICES, AD_SERVICES_EXTENSION_INT);
67         }
68         if (SdkLevel.isAtLeastU()) {
extensions.put(VERSION_CODES.UPSIDE_DOWN_CAKE, U_EXTENSION_INT)69             extensions.put(VERSION_CODES.UPSIDE_DOWN_CAKE, U_EXTENSION_INT);
70         }
71         ALL_EXTENSION_INTS = Collections.unmodifiableMap(extensions);
72     }
73 
74     /**
75      * Values suitable as parameters for {@link #getExtensionVersion(int)}.
76      * @hide
77      */
78     @IntDef(value = {
79           VERSION_CODES.R,
80           VERSION_CODES.S,
81           VERSION_CODES.TIRAMISU,
82           VERSION_CODES.UPSIDE_DOWN_CAKE,
83           AD_SERVICES,
84         })
85     @Retention(RetentionPolicy.SOURCE)
86     public @interface Extension {}
87 
SdkExtensions()88     private SdkExtensions() { }
89 
90     /**
91      * Return the version of the specified extensions.
92      *
93      * This method is suitable to use in conditional statements to determine whether an API is
94      * available and is safe to use. For example:
95      * <pre>
96      * if (getExtensionVersion(VERSION_CODES.R) >= 3) {
97      *   // Safely use API available since R extensions version 3
98      * }
99      * </pre>
100      *
101      * @param extension the extension to get the version of.
102      * @throws IllegalArgumentException if extension is not a valid extension
103      */
getExtensionVersion(@xtension int extension)104     public static int getExtensionVersion(@Extension int extension) {
105         if (extension < VERSION_CODES.R) {
106             throw new IllegalArgumentException("not a valid extension: " + extension);
107         }
108 
109         if (extension == VERSION_CODES.R) {
110             return R_EXTENSION_INT;
111         }
112         if (extension == VERSION_CODES.S) {
113             return S_EXTENSION_INT;
114         }
115         if (extension == VERSION_CODES.TIRAMISU) {
116             return T_EXTENSION_INT;
117         }
118         if (extension == VERSION_CODES.UPSIDE_DOWN_CAKE) {
119             return U_EXTENSION_INT;
120         }
121         if (extension == AD_SERVICES) {
122             return AD_SERVICES_EXTENSION_INT;
123         }
124         return 0;
125     }
126 
127     /**
128      * Return all extension versions that exist on this device.
129      *
130      * @return a map from extension to extension version.
131      */
132     @NonNull
getAllExtensionVersions()133     public static Map<Integer, Integer> getAllExtensionVersions() {
134         return ALL_EXTENSION_INTS;
135     }
136 
137 }
138