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.SystemApi;
21 import android.os.Build.VERSION_CODES;
22 import android.os.SystemProperties;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Methods for interacting with the extension SDK.
29  *
30  * This class provides information about the extension SDK version present
31  * on this device. Use the {@link #getExtensionVersion(int) getExtension} to
32  * query for the extension version for the given SDK version.
33 
34  * @hide
35  */
36 @SystemApi
37 public class SdkExtensions {
38 
39     private static final int R_EXTENSION_INT;
40     static {
41         // Note: when adding more extension versions, the logic that records current
42         // extension versions when saving a rollback must also be updated.
43         // At the time of writing this is in RollbackManagerServiceImpl#getExtensionVersions()
44         R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0);
45     }
46 
47     /**
48      * Values suitable as parameters for {@link #getExtensionVersion(int)}.
49      * @hide
50      */
51     @IntDef(value = { VERSION_CODES.R })
52     @Retention(RetentionPolicy.SOURCE)
53     public @interface SdkVersion {}
54 
SdkExtensions()55     private SdkExtensions() { }
56 
57     /**
58      * Return the version of the extension to the given SDK.
59      *
60      * @param sdk the SDK version to get the extension version of.
61      * @see SdkVersion
62      * @throws IllegalArgumentException if sdk is not an sdk version with extensions
63      */
getExtensionVersion(@dkVersion int sdk)64     public static int getExtensionVersion(@SdkVersion int sdk) {
65         if (sdk < VERSION_CODES.R) {
66             throw new IllegalArgumentException(String.valueOf(sdk) + " does not have extensions");
67         }
68 
69         if (sdk == VERSION_CODES.R) {
70             return R_EXTENSION_INT;
71         }
72         return 0;
73     }
74 
75 }
76