public final class

Speed

extends java.lang.Object

 java.lang.Object

↳androidx.car.app.hardware.info.Speed

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

Information about the current car speed.

Summary

Methods
public booleanequals(java.lang.Object other)

public CarValue<java.lang.Float>getDisplaySpeedMetersPerSecond()

Returns the display speed of the car in meters/second.

public CarValue<java.lang.Float>getRawSpeedMetersPerSecond()

Returns the raw speed of the car in meters/second.

public CarValue<java.lang.Integer>getSpeedDisplayUnit()

Returns the units used to display speed from the car settings.

public inthashCode()

public java.lang.StringtoString()

from java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, wait

Methods

public CarValue<java.lang.Float> getRawSpeedMetersPerSecond()

Returns the raw speed of the car in meters/second.

The value is positive when the vehicle is moving forward, negative when moving backwards and zero when stopped.

public CarValue<java.lang.Float> getDisplaySpeedMetersPerSecond()

Returns the display speed of the car in meters/second.

Some cars display a slightly slower speed than the actual speed. This is usually displayed on the speedometer.

The value is positive when the vehicle is moving forward, negative when moving backwards and zero when stopped.

public CarValue<java.lang.Integer> getSpeedDisplayUnit()

Returns the units used to display speed from the car settings.

See CarUnit for valid speed units.

public java.lang.String toString()

public int hashCode()

public boolean equals(java.lang.Object other)

Source

/*
 * Copyright 2021 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.hardware.info;

import static androidx.car.app.hardware.common.CarUnit.CarSpeedUnit;

import static java.util.Objects.requireNonNull;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.car.app.annotations.CarProtocol;
import androidx.car.app.annotations.RequiresCarApi;
import androidx.car.app.hardware.common.CarUnit;
import androidx.car.app.hardware.common.CarValue;

import java.util.Objects;

/**
 * Information about the current car speed.
 */
@CarProtocol
@RequiresCarApi(3)
public final class Speed {
    @Keep
    @Nullable
    private final CarValue<Float> mRawSpeedMetersPerSecond;

    @Keep
    @Nullable
    private final CarValue<Float> mDisplaySpeedMetersPerSecond;

    @Keep
    @NonNull
    private final CarValue<@CarSpeedUnit Integer> mSpeedDisplayUnit;

    /**
     * Returns the raw speed of the car in meters/second.
     *
     * <p>The value is positive when the vehicle is moving forward, negative when moving
     * backwards and zero when stopped.
     */
    @NonNull
    public CarValue<Float> getRawSpeedMetersPerSecond() {
        return requireNonNull(mRawSpeedMetersPerSecond);
    }

    /**
     * Returns the display speed of the car in meters/second.
     *
     * <p>Some cars display a slightly slower speed than the actual speed. This is usually
     * displayed on the speedometer.
     *
     * <p>The value is positive when the vehicle is moving forward, negative when moving
     * backwards and zero when stopped.
     */
    @NonNull
    public CarValue<Float> getDisplaySpeedMetersPerSecond() {
        return requireNonNull(mDisplaySpeedMetersPerSecond);
    }

    /**
     * Returns the units used to display speed from the car settings.
     *
     * <p>See {@link CarUnit} for valid speed units.
     */
    @NonNull
    public CarValue<@CarSpeedUnit Integer> getSpeedDisplayUnit() {
        return requireNonNull(mSpeedDisplayUnit);
    }

    @Override
    @NonNull
    public String toString() {
        return "[ raw speed: "
                + getRawSpeedMetersPerSecond()
                + ", display speed: "
                + getDisplaySpeedMetersPerSecond()
                + ", speed display unit: "
                + mSpeedDisplayUnit
                + "]";
    }

    @Override
    public int hashCode() {
        return Objects.hash(getRawSpeedMetersPerSecond(), getDisplaySpeedMetersPerSecond(),
                mSpeedDisplayUnit);
    }

    @Override
    public boolean equals(@Nullable Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof Speed)) {
            return false;
        }
        Speed otherSpeed = (Speed) other;

        return Objects.equals(getRawSpeedMetersPerSecond(), otherSpeed.getRawSpeedMetersPerSecond())
                && Objects.equals(getDisplaySpeedMetersPerSecond(),
                otherSpeed.getDisplaySpeedMetersPerSecond())
                && Objects.equals(mSpeedDisplayUnit, otherSpeed.mSpeedDisplayUnit);
    }

    Speed(Builder builder) {
        mRawSpeedMetersPerSecond = requireNonNull(builder.mRawSpeedMetersPerSecond);
        mDisplaySpeedMetersPerSecond = requireNonNull(builder.mDisplaySpeedMetersPerSecond);
        mSpeedDisplayUnit = requireNonNull(builder.mSpeedDisplayUnit);
    }

    /** Constructs an empty instance, used by serialization code. */
    private Speed() {
        mRawSpeedMetersPerSecond = CarValue.UNIMPLEMENTED_FLOAT;
        mDisplaySpeedMetersPerSecond = CarValue.UNIMPLEMENTED_FLOAT;
        mSpeedDisplayUnit = CarValue.UNIMPLEMENTED_INTEGER;
    }

    /** A builder of {@link Speed}. */
    public static final class Builder {
        CarValue<Float> mRawSpeedMetersPerSecond = CarValue.UNIMPLEMENTED_FLOAT;
        CarValue<Float> mDisplaySpeedMetersPerSecond = CarValue.UNIMPLEMENTED_FLOAT;
        CarValue<@CarSpeedUnit Integer> mSpeedDisplayUnit = CarValue.UNIMPLEMENTED_INTEGER;

        /**
         * Sets the raw speed in meters per second.
         *
         * @throws NullPointerException if {@code rawSpeedMetersPerSecond} is {@code null}
         */
        @NonNull
        public Builder setRawSpeedMetersPerSecond(
                @NonNull CarValue<Float> rawSpeedMetersPerSecond) {
            mRawSpeedMetersPerSecond = requireNonNull(rawSpeedMetersPerSecond);
            return this;
        }

        /**
         * Sets the display speed in meters per second. *
         *
         * @throws NullPointerException if {@code displaySpeedMetersPerSecond} is {@code null}
         */
        @NonNull
        public Builder setDisplaySpeedMetersPerSecond(
                @NonNull CarValue<Float> displaySpeedMetersPerSecond) {
            mDisplaySpeedMetersPerSecond = requireNonNull(displaySpeedMetersPerSecond);
            return this;
        }

        /**
         * Sets the units used to display speed from the car hardware settings.
         *
         * <p>See {@link CarUnit} for valid speed units.
         *
         * @throws NullPointerException if {@code speedDisplayUnit} is {@code null}
         */
        @NonNull
        public Builder setSpeedDisplayUnit(
                @NonNull CarValue<@CarSpeedUnit Integer> speedDisplayUnit) {
            mSpeedDisplayUnit = requireNonNull(speedDisplayUnit);
            return this;
        }

        /**
         * Constructs the {@link Speed} defined by this builder.
         */
        @NonNull
        public Speed build() {
            return new Speed(this);
        }
    }
}