public final class

CarAppApiLevels

extends java.lang.Object

 java.lang.Object

↳androidx.car.app.versioning.CarAppApiLevels

Gradle dependencies

compile group: 'androidx.car.app', name: 'app', version: '1.2.0-rc01'

  • groupId: androidx.car.app
  • artifactId: app
  • version: 1.2.0-rc01

Artifact androidx.car.app:app:1.2.0-rc01 it located at Google repository (https://maven.google.com/)

Overview

API levels supported by this library.

Each level denotes a set of elements (classes, fields and methods) known to both clients and hosts.

Summary

Fields
public static final intLEVEL_1

Initial API level.

public static final intLEVEL_2

API level 2.

public static final intLEVEL_3

API level 3.

public static final intLEVEL_4

API level 4.

public static final intUNKNOWN

Unknown API level.

Methods
public static intgetLatest()

Returns the highest API level implemented by this library.

public static intgetOldest()

Returns the lowest API level implemented by this library.

public static booleanisValid(int carApiLevel)

Returns whether the given integer is a valid CarAppApiLevel

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int LEVEL_4

API level 4.

public static final int LEVEL_3

API level 3.

Includes a car hardware manager for access to sensors and other vehicle properties.

public static final int LEVEL_2

API level 2.

Includes features such as sign-in template, long-message template, and multi-variant text support.

public static final int LEVEL_1

Initial API level.

Includes core API services and managers, and templates for parking, charging, and navigation apps.

public static final int UNKNOWN

Unknown API level.

Used when the API level hasn't been established yet

Methods

public static boolean isValid(int carApiLevel)

Returns whether the given integer is a valid CarAppApiLevel

public static int getLatest()

Returns the highest API level implemented by this library.

public static int getOldest()

Returns the lowest API level implemented by this library.

Source

/*
 * Copyright 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 androidx.car.app.versioning;

import static java.util.Objects.requireNonNull;

import androidx.annotation.RestrictTo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * API levels supported by this library.
 *
 * <p>Each level denotes a set of elements (classes, fields and methods) known to both clients and
 * hosts.
 *
 * @see androidx.car.app.CarContext#getCarAppApiLevel()
 */
public final class CarAppApiLevels {
    /**
     * API level 4.
     */
    @CarAppApiLevel
    public static final int LEVEL_4 = 4;

    /**
     * API level 3.
     *
     * <p>Includes a car hardware manager for access to sensors and other vehicle properties.
     */
    @CarAppApiLevel
    public static final int LEVEL_3 = 3;

    /**
     * API level 2.
     *
     * <p>Includes features such as sign-in template, long-message template, and multi-variant
     * text support.
     */
    @CarAppApiLevel
    public static final int LEVEL_2 = 2;

    /**
     * Initial API level.
     *
     * <p>Includes core API services and managers, and templates for parking, charging, and
     * navigation apps.
     */
    @CarAppApiLevel
    public static final int LEVEL_1 = 1;

    /**
     * Unknown API level.
     *
     * <p>Used when the API level hasn't been established yet
     *
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    @CarAppApiLevel
    public static final int UNKNOWN = 0;

    private static final String CAR_API_LEVEL_FILE = "car-app-api.level";

    /**
     * Returns whether the given integer is a valid {@link CarAppApiLevel}
     *
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    public static boolean isValid(int carApiLevel) {
        return carApiLevel >= getOldest() && carApiLevel <= getLatest();
    }

    /**
     * Returns the highest API level implemented by this library.
     */
    @CarAppApiLevel
    public static int getLatest() {
        // The latest Car API level is defined as java resource, generated via build.gradle. This
        // has to be read through the class loader because we do not have access to the context
        // to retrieve an Android resource.
        ClassLoader classLoader = requireNonNull(CarAppApiLevels.class.getClassLoader());
        InputStream inputStream = classLoader.getResourceAsStream(CAR_API_LEVEL_FILE);

        if (inputStream == null) {
            throw new IllegalStateException(String.format("Car API level file %s not found",
                    CAR_API_LEVEL_FILE));
        }

        try {
            InputStreamReader streamReader = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(streamReader);
            String line = reader.readLine();


            int apiLevel = Integer.parseInt(line);
            if (apiLevel < LEVEL_1 || apiLevel > LEVEL_4) {
                throw new IllegalStateException("Unrecognized Car API level: " + line);
            }
            return apiLevel;
        } catch (IOException e) {
            throw new IllegalStateException("Unable to read Car API level file");
        }
    }

    /**
     * Returns the lowest API level implemented by this library.
     */
    @CarAppApiLevel
    public static int getOldest() {
        return LEVEL_1;
    }

    private CarAppApiLevels() {
    }
}