blob: 171eff8cdff1d26f87e9afb022cc6931e0862143 [file] [log] [blame]
Anton Hanssonda4972f2020-01-08 09:48:18 +00001/*
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
17package android.os.ext;
18
19import android.annotation.IntDef;
20import android.annotation.SystemApi;
21import android.os.Build.VERSION_CODES;
22import android.os.SystemProperties;
23
24import java.lang.annotation.Retention;
25import 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
37public class SdkExtensions {
38
39 private static final int R_EXTENSION_INT;
Anton Hanssona9afcb92020-11-30 11:43:05 +000040 private static final int S_EXTENSION_INT;
Anton Hanssonda4972f2020-01-08 09:48:18 +000041 static {
Oli Lan83840182020-04-17 17:58:33 +010042 // Note: when adding more extension versions, the logic that records current
43 // extension versions when saving a rollback must also be updated.
44 // At the time of writing this is in RollbackManagerServiceImpl#getExtensionVersions()
Anton Hansson6e7f3282020-02-07 19:05:33 +000045 R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0);
Anton Hanssona9afcb92020-11-30 11:43:05 +000046 S_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.s", 0);
Anton Hanssonda4972f2020-01-08 09:48:18 +000047 }
48
49 /**
50 * Values suitable as parameters for {@link #getExtensionVersion(int)}.
51 * @hide
52 */
Anton Hanssona9afcb92020-11-30 11:43:05 +000053 @IntDef(value = { VERSION_CODES.R, VERSION_CODES.S })
Anton Hanssonda4972f2020-01-08 09:48:18 +000054 @Retention(RetentionPolicy.SOURCE)
55 public @interface SdkVersion {}
56
57 private SdkExtensions() { }
58
59 /**
60 * Return the version of the extension to the given SDK.
61 *
62 * @param sdk the SDK version to get the extension version of.
63 * @see SdkVersion
64 * @throws IllegalArgumentException if sdk is not an sdk version with extensions
65 */
66 public static int getExtensionVersion(@SdkVersion int sdk) {
67 if (sdk < VERSION_CODES.R) {
68 throw new IllegalArgumentException(String.valueOf(sdk) + " does not have extensions");
69 }
Anton Hanssonc84a34b2020-04-20 14:36:42 +010070
71 if (sdk == VERSION_CODES.R) {
72 return R_EXTENSION_INT;
73 }
Anton Hanssona9afcb92020-11-30 11:43:05 +000074 if (sdk == VERSION_CODES.S) {
75 return S_EXTENSION_INT;
76 }
Anton Hanssonc84a34b2020-04-20 14:36:42 +010077 return 0;
Anton Hanssonda4972f2020-01-08 09:48:18 +000078 }
79
80}