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 com.android.networkstack.apishim.common;
18 
19 import android.os.Build;
20 
21 /**
22  * Utility class for API shims.
23  */
24 public final class ShimUtils {
25     /**
26      * Check whether the device release or development API level is strictly higher than the passed
27      * in level.
28      *
29      * On a development build (codename != REL), the device will have the same API level as the
30      * last stable release, even though some additional APIs may be available. In this method the
31      * device API level is considered to be higher if the device supports a stable SDK with a higher
32      * version number, or if the device supports a development version of a SDK that has a higher
33      * version number.
34      *
35      * @return True if the device supports an SDK that has or will have a higher version number,
36      *         even if still in development.
37      */
isReleaseOrDevelopmentApiAbove(int apiLevel)38     public static boolean isReleaseOrDevelopmentApiAbove(int apiLevel) {
39         // In-development API n+1 will have SDK_INT == n and CODENAME != REL.
40         // Stable API n has SDK_INT == n and CODENAME == REL.
41         final int devApiLevel = Build.VERSION.SDK_INT
42                 + ("REL".equals(Build.VERSION.CODENAME) ? 0 : 1);
43         return devApiLevel > apiLevel;
44     }
45 
46     /**
47      * Check whether the device supports in-development or final R networking APIs.
48      */
isAtLeastR()49     public static boolean isAtLeastR() {
50         return isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q);
51     }
52 }
53