1 /* 2 * Copyright (C) 2022 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 package com.android.car.internal.util; 17 18 import android.annotation.ChecksSdkIntAtLeast; 19 import android.car.Car; 20 import android.car.PlatformVersion; 21 import android.car.PlatformVersionMismatchException; 22 import android.os.Build; 23 24 /** 25 * Utility class for platform and car API version check. 26 * 27 * @hide 28 */ 29 public final class VersionUtils { 30 31 /** 32 * Asserts if the current platform version is at least expected platform version. 33 * 34 * @throws PlatformVersionMismatchException if current platform version is not equal to or 35 * greater than expected platform version. 36 * 37 * @deprecated Use version specific call for example {@link #assertPlatformVersionAtLeastU()}. 38 */ assertPlatformVersionAtLeast(PlatformVersion expectedPlatformApiVersion)39 public static void assertPlatformVersionAtLeast(PlatformVersion expectedPlatformApiVersion) { 40 if (!isPlatformVersionAtLeast(expectedPlatformApiVersion)) { 41 throw new PlatformVersionMismatchException(expectedPlatformApiVersion); 42 } 43 } 44 45 /** 46 * Asserts if the current platform version is at least {@code UPSIDE_DOWN_CAKE}. 47 * 48 * @throws PlatformVersionMismatchException if current platform version is not equal to or 49 * greater than expected platform version. 50 */ assertPlatformVersionAtLeastU()51 public static void assertPlatformVersionAtLeastU() { 52 assertPlatformVersionAtLeast(PlatformVersion.VERSION_CODES.UPSIDE_DOWN_CAKE_0); 53 } 54 55 /** 56 * Checks if the current platform version is at least expected platform version. 57 * 58 * <p>{@link #isPlatformVersionAtLeast(PlatformVersion)} is a general call which can take any 59 * platform version object. Lint check needs in the annotation which version is supported. 60 * Example: {@code com.android.modules.utils.build.SdkLevel} class. 61 * 62 * @deprecated Use version specific call for example {@link #isPlatformVersionAtLeastU()}. 63 */ 64 @Deprecated isPlatformVersionAtLeast(PlatformVersion expectedPlatformVersion)65 public static boolean isPlatformVersionAtLeast(PlatformVersion expectedPlatformVersion) { 66 PlatformVersion currentPlatformVersion = Car.getPlatformVersion(); 67 return currentPlatformVersion.isAtLeast(expectedPlatformVersion); 68 } 69 70 /** 71 * Checks if the current platform version is at least {@code UPSIDE_DOWN_CAKE}. 72 * 73 * <p>The method will be used by the lint check to make sure that calls are properly version 74 * guarded. 75 */ 76 @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE, codename = "UpsideDownCake") isPlatformVersionAtLeastU()77 public static boolean isPlatformVersionAtLeastU() { 78 return isPlatformVersionAtLeast(PlatformVersion.VERSION_CODES.UPSIDE_DOWN_CAKE_0); 79 } 80 VersionUtils()81 private VersionUtils() { 82 throw new UnsupportedOperationException("contains only static method methods"); 83 } 84 } 85