Anton Hansson | da4972f | 2020-01-08 09:48:18 +0000 | [diff] [blame] | 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; |
Anton Hansson | b1a217c | 2020-11-30 11:43:05 +0000 | [diff] [blame] | 40 | private static final int S_EXTENSION_INT; |
Anton Hansson | da4972f | 2020-01-08 09:48:18 +0000 | [diff] [blame] | 41 | static { |
Oli Lan | 76a29e0 | 2020-04-17 17:58:33 +0100 | [diff] [blame] | 42 | // 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 Hansson | 6e7f328 | 2020-02-07 19:05:33 +0000 | [diff] [blame] | 45 | R_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.r", 0); |
Anton Hansson | b1a217c | 2020-11-30 11:43:05 +0000 | [diff] [blame] | 46 | S_EXTENSION_INT = SystemProperties.getInt("build.version.extensions.s", 0); |
Anton Hansson | da4972f | 2020-01-08 09:48:18 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Values suitable as parameters for {@link #getExtensionVersion(int)}. |
| 51 | * @hide |
| 52 | */ |
Anton Hansson | b1a217c | 2020-11-30 11:43:05 +0000 | [diff] [blame] | 53 | @IntDef(value = { VERSION_CODES.R, VERSION_CODES.CUR_DEVELOPMENT }) |
Anton Hansson | da4972f | 2020-01-08 09:48:18 +0000 | [diff] [blame] | 54 | @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 Hansson | c84a34b | 2020-04-20 14:36:42 +0100 | [diff] [blame] | 70 | |
| 71 | if (sdk == VERSION_CODES.R) { |
| 72 | return R_EXTENSION_INT; |
| 73 | } |
Anton Hansson | b1a217c | 2020-11-30 11:43:05 +0000 | [diff] [blame] | 74 | if (sdk == VERSION_CODES.CUR_DEVELOPMENT) { |
| 75 | return S_EXTENSION_INT; |
| 76 | } |
Anton Hansson | c84a34b | 2020-04-20 14:36:42 +0100 | [diff] [blame] | 77 | return 0; |
Anton Hansson | da4972f | 2020-01-08 09:48:18 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } |