Android 驱动程序 SDK 4.0 迁移指南

适用于 Android 4.0 的驱动程序 SDK 版本要求您更新某些操作的代码。本指南简要介绍了相关变更以及迁移代码所需执行的步骤。

对于untrusted驱动程序模型,传送客户端只需针对 VEHICLE 服务类型进行身份验证,即启用位置报告。具有可信驾驶模型的客户端必须为 TASK 服务类型提供身份验证,才能在 DeliveryVehicleReporter 中启用停靠报告方法。

软件包名称更改

软件包名称已从 com.google.android.libraries.ridesharing.driver 更改为 com.google.android.libraries.mapsplatform.transportation.driver。请更新代码中的引用。

初始化 SDK

在早期版本中,您应初始化 Navigation SDK,然后获取对 FleetEngine 类的引用。在驱动程序 SDK v4 中,按如下方式初始化 SDK:

  1. NavigationApi 获取 Navigator 对象。

    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. 创建一个 DriverContext 对象,填充必填字段。

    DriverContext driverContext = DriverContext.builder(application)
        .setProviderId(providerId)
        .setVehicleId(vehicleId)
        .setAuthTokenFactory(authTokenFactory)
        .setNavigator(navigator)
        .setRoadSnappedLocationProvider(
            NavigationApi.getRoadSnappedLocationProvider(application))
        .build();
    
  3. 使用 DriverContext 对象初始化 *DriverApi

    DeliveryDriverApi deliveryDriverApi = DeliveryDriverApi.createInstance(driverContext);
    
  4. 从 API 对象获取 NavigationVehicleReporter*VehicleReporter 扩展 NavigationVehicleReporter

    DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
    

启用和停用位置信息更新

在早期版本中,您可以在获取 FleetEngine 引用后启用位置信息更新。在驱动程序 SDK v4 中,按如下方式启用位置信息更新:

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

如需更新报告间隔,请使用 DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit)

驾驶员结束轮班后,通过调用 NavigationVehicleReporter.disableLocationTracking() 停用位置信息更新并将车辆标记为离线。

使用状态监听器的 Error Reporting

ErrorListener 已被移除,并与 StatusListener 结合使用,其定义如下所示:

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.
  }
}

正在通过 AuthTokenFactory 进行身份验证

AuthTokenFactory 现在只有一个方法,即 getToken(),该方法接受 AuthTokenContext 作为参数。

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”列表变为“TaskInfo”列表

Task 列表已替换为 VehicleStop 中的 TaskInfo 列表。以下代码示例演示了如何创建 VehicleStop 对象。

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