1 /*
2  * Copyright (C) 2017 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.wallpaper.compat;
17 
18 import android.os.Build;
19 import android.os.Build.VERSION;
20 import android.os.Build.VERSION_CODES;
21 
22 /**
23  * Provides the SDK version in a manner that can be stubbed out in a test environment.
24  */
25 public class BuildCompat {
26     public static final int JB_MR2_SDK_VERSION = VERSION_CODES.JELLY_BEAN_MR2;
27     public static final int L_SDK_VERSION = VERSION_CODES.LOLLIPOP;
28     public static final int N_SDK_VERSION = Build.VERSION_CODES.N;
29     public static final int N_MR1_SDK_VERSION = Build.VERSION_CODES.N_MR1;
30 
31     private static int sSdk = Build.VERSION.SDK_INT;
32 
33     /**
34      * Returns whether the framework on the current Android device is JellyBean MR2 (API 18) or
35      * higher. Used to determine if it's safe to use APIs added in that API version such as
36      * HandlerThread#quitSafely.
37      */
isAtLeastJBMR2()38     public static boolean isAtLeastJBMR2() {
39         return sSdk >= JB_MR2_SDK_VERSION;
40     }
41 
42     /**
43      * Returns whether the framework on the current Android device is L (API 21) or higher. Used to
44      * determine whether framework classes introduced in L such as JobScheduler can be used on this
45      * device.
46      */
isAtLeastL()47     public static boolean isAtLeastL() {
48         return sSdk >= L_SDK_VERSION;
49     }
50 
51     /**
52      * Returns whether the framework on the current Android device is N or higher. Used to determine
53      * whether new N-specific wallpaper APIs are available.
54      */
isAtLeastN()55     public static boolean isAtLeastN() {
56         return sSdk >= N_SDK_VERSION;
57     }
58 
59     /**
60      * Returns whether the framework on the current Android device is N-MR1 or higher. Used to
61      * determine whether new N-MR1-specific wallpaper APIs are available.
62      */
isAtLeastNMR1()63     public static boolean isAtLeastNMR1() {
64         return sSdk >= N_MR1_SDK_VERSION;
65     }
66 
67     /**
68      * Returns whether the framework on the current Android device is N-MR2 or higher. Used to
69      * determine if new N-MR2 specific API behavior is present on the device.
70      */
isAtLeastNMR2()71     public static boolean isAtLeastNMR2() {
72         return sSdk > N_MR1_SDK_VERSION
73                 || (sSdk == N_MR1_SDK_VERSION && VERSION.RELEASE.equals("7.1.2"));
74     }
75 
76     /**
77      * Returns whether the framework on the current Android device is O or higher.
78      */
isAtLeastO()79     public static boolean isAtLeastO() {
80         return sSdk >= Build.VERSION_CODES.O;
81     }
82 
83     /**
84      * Returns whether the framework on the current Android device is O-MR1 or higher.
85      */
isAtLeastOMR1()86     public static boolean isAtLeastOMR1() {
87         return sSdk >= VERSION_CODES.O_MR1;
88     }
89 
90     /**
91      * Sets the SDK version that BuildCompat will consider the current device to be on. Used for
92      * testing only.
93      */
setSdkVersionForTesting(int sdk)94     public static void setSdkVersionForTesting(int sdk) {
95         sSdk = sdk;
96     }
97 
isAtLeastQ()98     public static boolean isAtLeastQ() {
99         return sSdk >= VERSION_CODES.Q;
100     }
101 }
102