Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 23776499 authored by David Su's avatar David Su
Browse files

Create SdkLevelUtil for Mainline

Mainline modules should disable new APIs on older SDK levels
by checking the Build.VERSION constants.

Before S SDK finalization, checking
`VERSION.SDK_INT >= VERSION_CODES.S` doesn't work on S development
builds, because SDK_INT = R = 30, while VERSION_CODES.S = 10000.
Thus, check Build.VERSION.CODENAME as a workaround.

Bug: 154742276
Test: tested on S dev build
Change-Id: I51cb8136f6700fc93eb24f1941717b41d344dba4
parent cde03184
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.wifi.util;

import android.os.Build;

/**
 * Utility to check the SDK version of the device that the code is running on.
 *
 * This can be used to disable new Wifi APIs added in Mainline updates on older SDK versions.
 *
 * @hide
 */
public class SdkLevelUtil {

    /** This class is instantiable to allow easy mocking. */
    public SdkLevelUtil() { }

    /** Returns true if the Android platform SDK is at least "S", false otherwise. */
    public boolean isAtLeastS() {
        // TODO(b/167575586): after S SDK finalization, this method should just be
        //  `return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;`

        // at least S: return true
        // this condition only evaluates to true after S SDK finalization when VERSION_CODES.S
        // is set to something like "31", before SDK finalization the value is "10000"
        // Note that Build.VERSION_CODES.S is inlined at compile time. If it's inlined to 10000,
        // this condition never evaluates to true.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            return true;
        }

        // Assume for now that S = R + 1
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
            return true;
        }

        // R: check CODENAME
        // Before S SDK finalization, SDK_INT = R = 30 i.e. remains on the previous version
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
            // CODENAME = "REL" on R release builds
            // CODENAME = "S" on S development builds
            return "S".equals(Build.VERSION.CODENAME);
        }

        // older than R: return false
        return false;
    }
}