• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 com.google.android.setupcompat.util;
18 
19 import android.os.Build;
20 
21 /**
22  * An util class to check whether the current OS version is higher or equal to sdk version of
23  * device.
24  */
25 public final class BuildCompatUtils {
26 
27   /**
28    * Implementation of BuildCompat.isAtLeast*() suitable for use in Setup
29    *
30    * <p>BuildCompat.isAtLeast*() can be changed by Android Release team, and once that is changed it
31    * may take weeks for that to propagate to stable/prerelease/experimental SDKs in Google3. Also it
32    * can be different in all these channels. This can cause random issues, especially with sidecars
33    * (i.e., the code running on R may not know that it runs on R).
34    *
35    * <p>This still should try using BuildCompat.isAtLeastR() as source of truth, but also checking
36    * for VERSION_SDK_INT and VERSION.CODENAME in case when BuildCompat implementation returned
37    * false. Note that both checks should be >= and not = to make sure that when Android version
38    * increases (i.e., from R to S), this does not stop working.
39    *
40    * <p>Supported configurations:
41    *
42    * <ul>
43    *   <li>For current Android release: while new API is not finalized yet (CODENAME = "S", SDK_INT
44    *       = 30|31)
45    *   <li>For current Android release: when new API is finalized (CODENAME = "REL", SDK_INT = 31)
46    *   <li>For next Android release (CODENAME = "T", SDK_INT = 30+)
47    * </ul>
48    *
49    * <p>Note that Build.VERSION_CODES.S cannot be used here until final SDK is available in all
50    * Google3 channels, because it is equal to Build.VERSION_CODES.CUR_DEVELOPMENT before API
51    * finalization.
52    *
53    * @return Whether the current OS version is higher or equal to S.
54    */
isAtLeastS()55   public static boolean isAtLeastS() {
56     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
57       return false;
58     }
59     return (Build.VERSION.CODENAME.equals("REL") && Build.VERSION.SDK_INT >= 31)
60         || (Build.VERSION.CODENAME.length() == 1
61             && Build.VERSION.CODENAME.charAt(0) >= 'S'
62             && Build.VERSION.CODENAME.charAt(0) <= 'Z');
63   }
64 
BuildCompatUtils()65   private BuildCompatUtils() {}
66 }
67