Android Driver SDK 4.0 Migration Guide

The Driver SDK for Android 4.0 release requires that you update your code for certain operations. This guide outlines the changes and what you'll need to do to migrate your code.

For the untrusted driver model, delivery clients only need to authenticate for the VEHICLE service type, which enables location reporting. Clients with a trusted driver model must provide authentication for the TASK service type to enable vehicle-stop reporting methods in DeliveryVehicleReporter.

Package name change

The package name has changed from com.google.android.libraries.ridesharing.driver to com.google.android.libraries.mapsplatform.transportation.driver. Please update references in your code.

Initializing the SDK

In earlier versions, you would initialize the Navigation SDK and then obtain a reference to the FleetEngine class. In Driver SDK v4, initialize the SDK as follows:

  1. Obtain a Navigator object from the NavigationApi.

    NavigationApi.getNavigator(
        this, // Activity
        new NavigationApi.NavigatorListener() {
          @Override
          public void onNavigatorReady(Navigator navigator) {
            // Keep a reference to the Navigator (used to configure and start nav)
            this.navigator = navigator;
          }
        }
    );
    
  2. Create a DriverContext object, populating the required fields.

    DriverContext driverContext = DriverContext.builder(application)
        .setProviderId(providerId)
        .setVehicleId(vehicleId)
        .setAuthTokenFactory(authTokenFactory)
        .setNavigator(navigator)
        .setRoadSnappedLocationProvider(
            NavigationApi.getRoadSnappedLocationProvider(application))
        .build();
    
  3. Use the DriverContext object to initialize the *DriverApi.

    DeliveryDriverApi deliveryDriverApi = DeliveryDriverApi.createInstance(driverContext);
    
  4. Obtain the NavigationVehicleReporter from the API object. *VehicleReporter extends NavigationVehicleReporter.

    DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
    

Enabling and disabling location updates

In earlier versions, you would enable location updates after obtaining a FleetEngine reference. In Driver SDK v4, enable location updates as follows:

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

To update the reporting interval, use DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit).

When the driver's shift is finished, disable location updates and mark the vehicle as offline by calling NavigationVehicleReporter.disableLocationTracking().

Error Reporting with StatusListener

ErrorListener has been removed and combined with StatusListener, which may be defined like the following:

class MyStatusListener implements StatusListener {
  /** Called when background status is updated, during actions such as location reporting. */
  @Override
  public void updateStatus(
      StatusLevel statusLevel, StatusCode statusCode, String statusMsg) {
    // Status handling stuff goes here.
    // StatusLevel may be DEBUG, INFO, WARNING, or ERROR.
    // StatusCode may be DEFAULT, UNKNOWN_ERROR, VEHICLE_NOT_FOUND,
    // BACKEND_CONNECTIVITY_ERROR, or PERMISSION_DENIED.
  }
}

Authenticating with AuthTokenFactory

AuthTokenFactory now has only one method, getToken(), which takes AuthTokenContext as the parameter.

class JsonAuthTokenFactory implements AuthTokenFactory {
  // Initially null.
  private String vehicleServiceToken;
  // Initially null. Only used in the trusted driver model to authenticate
  // vehicle-stop reporting.
  private String taskServiceToken;
  private long expiryTimeMs = 0;

  // This method is called on a thread that only sends location updates (and
  // vehicle stop updates if you choose to report them). Blocking is OK, but just
  // know that no updates can occur until this method returns.
  @Override
  public String getToken(AuthTokenContext authTokenContext) {
    if (System.currentTimeMillis() > expiryTimeMs) {
      // The token has expired, go get a new one.
      fetchNewToken(vehicleId);
    }
    if (ServiceType.VEHICLE.equals(authTokenContext.getServiceType())) {
      return vehicleServiceToken;
    } else if (ServiceType.TASK.equals(authTokenContext.getServiceType())) {
      // Only used for the trusted driver model to access vehicle-stop reporting
      // methods in DeliveryVehicleReporter.
      return taskServiceToken;
    } else {
      throw new RuntimeException("Unsupported ServiceType: " + authTokenContext.getServiceType());
    }
  }

  private void fetchNewToken(String vehicleId) {
    String url = "https://yourauthserver.example/token/" + vehicleId;

    try (Reader r = new InputStreamReader(new URL(url).openStream())) {
      com.google.gson.JsonObject obj
          = com.google.gson.JsonParser.parseReader(r).getAsJsonObject();
      vehicleServiceToken = obj.get("VehicleServiceToken").getAsString();
      taskServiceToken = obj.get("TaskServiceToken").getAsString();
      expiryTimeMs = obj.get("TokenExpiryMs").getAsLong();

      // The expiry time could be an hour from now, but just to try and avoid
      // passing expired tokens, we subtract 10 minutes from that time.
      expiryTimeMs -= 10 * 60 * 1000;
    } catch (IOException e) {
      // It's OK to throw exceptions here. The StatusListener you passed to
      // create the DriverContext class will be notified and passed along the failed
      // update warning.
      throw new RuntimeException("Could not get auth token", e);
    }
  }
}

Task list becomes TaskInfo list

Task list has been replaced with TaskInfo list in VehicleStop. The following code example demonstrates how to create a VehicleStop object.

VehicleStop vehicleStop = VehicleStop.builder()
    .setTaskInfoList(taskInfoList)
    .setWaypoint(waypoint)
    .setVehicleStopState(vehicleStopState)
    .build();