public class

CarHardwareHostDispatcher

extends java.lang.Object

 java.lang.Object

↳androidx.car.app.hardware.common.CarHardwareHostDispatcher

Gradle dependencies

compile group: 'androidx.car.app', name: 'app-projected', version: '1.7.0-beta01'

  • groupId: androidx.car.app
  • artifactId: app-projected
  • version: 1.7.0-beta01

Artifact androidx.car.app:app-projected:1.7.0-beta01 it located at Google repository (https://maven.google.com/)

Overview

Dispatcher of calls to the host and manages possible exceptions.

Since the standard HostDispatcher does not know about the this wrapper fetches it and then does direct dispatch.

Summary

Constructors
publicCarHardwareHostDispatcher(HostDispatcher hostDispatcher)

Creates an instance of CarHardwareHostDispatcher with the given root dispatcher.

Methods
public voiddispatchGetCarHardwareResult(int resultType, Bundleable bundle, ICarHardwareResult result)

Dispatches a call to for the given resultType.

public voiddispatchSubscribeCarHardwareResult(int resultType, Bundleable bundle, ICarHardwareResult callback)

Dispatches a call to for the given resultType.

public voiddispatchUnsubscribeCarHardwareResult(int resultType, Bundleable bundle)

Dispatches a call to for the resultType.

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

Constructors

public CarHardwareHostDispatcher(HostDispatcher hostDispatcher)

Creates an instance of CarHardwareHostDispatcher with the given root dispatcher.

Parameters:

hostDispatcher: root dispatcher that is used to get the initially

Methods

public void dispatchGetCarHardwareResult(int resultType, Bundleable bundle, ICarHardwareResult result)

Dispatches a call to for the given resultType.

Parameters:

resultType: the result type to fetch
bundle: parameters or additional info for the call
result: the callback where the result is returned

public void dispatchSubscribeCarHardwareResult(int resultType, Bundleable bundle, ICarHardwareResult callback)

Dispatches a call to for the given resultType.

Parameters:

resultType: the result type to fetch
bundle: parameters or additional info for the call
callback: the callback where the result is returned

public void dispatchUnsubscribeCarHardwareResult(int resultType, Bundleable bundle)

Dispatches a call to for the resultType.

Parameters:

resultType: the result type to fetch

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.common;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import static java.util.Objects.requireNonNull;

import android.os.RemoteException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.car.app.CarContext;
import androidx.car.app.HostDispatcher;
import androidx.car.app.ICarHost;
import androidx.car.app.hardware.ICarHardwareHost;
import androidx.car.app.hardware.ICarHardwareResult;
import androidx.car.app.serialization.Bundleable;
import androidx.car.app.utils.RemoteUtils;

/**
 * Dispatcher of calls to the host and manages possible exceptions.
 *
 * <p>Since the standard {@link HostDispatcher} does not know about the {@link ICarHardwareHost}
 * this wrapper fetches it and then does direct dispatch.
 *
 */
@RestrictTo(LIBRARY_GROUP)
public class CarHardwareHostDispatcher {

    @NonNull
    private final HostDispatcher mHostDispatcher;

    @Nullable
    private ICarHardwareHost mICarHardwareHost;

    /**
     * Creates an instance of {@link CarHardwareHostDispatcher} with the given root dispatcher.
     *
     * @param hostDispatcher root dispatcher that is used to get the {@link ICarHardwareHost}
     *                       initially
     * @throws NullPointerException if {@code hostDispatcher} is {@code null}
     */
    public CarHardwareHostDispatcher(@NonNull HostDispatcher hostDispatcher) {
        mHostDispatcher = requireNonNull(hostDispatcher);
    }

    /**
     * Dispatches a call to {@link ICarHardwareHost#getCarHardwareResult} for the given {@code
     * resultType}.
     *
     * @param resultType the result type to fetch
     * @param bundle     parameters or additional info for the call
     * @param result     the callback where the result is returned
     * @throws NullPointerException if {@code result} is {@code null}
     */
    @SuppressWarnings("NullAway") // b/316639429
    public void dispatchGetCarHardwareResult(int resultType, @Nullable Bundleable bundle,
            @NonNull ICarHardwareResult result) {
        requireNonNull(result);
        RemoteUtils.dispatchCallToHost("getCarHardwareResult",

                () -> {
                    getHost().getCarHardwareResult(
                            resultType,
                            bundle,
                            result);
                    return null;
                });
    }

    /**
     * Dispatches a call to {@link ICarHardwareHost#subscribeCarHardwareResult} for the given {@code
     * resultType}.
     *
     * @param resultType the result type to fetch
     * @param bundle     parameters or additional info for the call
     * @param callback   the callback where the result is returned
     * @throws NullPointerException if {@code callback} is {@code null}
     */
    @SuppressWarnings("NullAway") // b/316639429
    public void dispatchSubscribeCarHardwareResult(int resultType, @Nullable Bundleable bundle,
            @NonNull ICarHardwareResult callback) {
        requireNonNull(callback);
        RemoteUtils.dispatchCallToHost("subscribeCarHardwareResult",
                () -> {
                    getHost().subscribeCarHardwareResult(
                            resultType,
                            bundle,
                            callback);
                    return null;
                });
    }

    /**
     * Dispatches a call to {@link ICarHardwareHost#unsubscribeCarHardwareResult} for the
     * {@code resultType}.
     *
     * @param resultType the result type to fetch
     * @throws NullPointerException if {@code callback} is {@code null}
     */
    @SuppressWarnings("NullAway") // b/316639429
    public void dispatchUnsubscribeCarHardwareResult(int resultType, @Nullable Bundleable bundle) {
        RemoteUtils.dispatchCallToHost("unsubscribeCarHardwareResult",
                () -> {
                    getHost().unsubscribeCarHardwareResult(resultType, bundle);
                    return null;
                });
    }

    @NonNull
    private ICarHardwareHost getHost() throws RemoteException {
        ICarHardwareHost host = mICarHardwareHost;
        if (host == null) {
            host = requireNonNull(mHostDispatcher.dispatchForResult(CarContext.CAR_SERVICE,
                    "getHost(CarHardware)",
                    (ICarHost carHost) ->
                            ICarHardwareHost.Stub.asInterface(
                                    carHost.getHost(CarContext.HARDWARE_SERVICE))
            ));
            mICarHardwareHost = host;
        }
        return host;
    }
}