Android Driver SDK 4.0 移行ガイド

Driver SDK for Android 4.0 リリースでは、特定の操作のためにコードを更新する必要があります。このガイドでは、変更点の概要と、コードを移行するために必要な作業について説明します。

untrusted信頼できないドライバモデルの場合、配信クライアントは VEHICLE サービスタイプの認証のみを行うだけで済み、位置情報レポートが有効になります。信頼できるドライバーモデルを使用するクライアントは、DeliveryVehicleReporter で車両停止報告方法を有効にするには、TASK サービスタイプの認証を提供する必要があります。

パッケージ名の変更

パッケージ名が com.google.android.libraries.ridesharing.driver から com.google.android.libraries.mapsplatform.transportation.driver に変更されました。コード内の参照を更新してください。

SDK の初期化

それより前のバージョンでは、Navigation SDK を初期化してから、FleetEngine クラスへの参照を取得していました。Driver 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 を取得します。*VehicleReporterNavigationVehicleReporter を拡張します。

    DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
    

位置情報の更新を有効または無効にする

それより前のバージョンでは、FleetEngine 参照を取得した後に位置情報の更新を有効にしていました。Driver SDK v4 では、次のように位置情報の更新を有効にします。

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

レポート間隔を更新するには、DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit) を使用します。

ドライバーのシフトが終了したら、NavigationVehicleReporter.disableLocationTracking() を呼び出して位置情報の更新を無効にし、車両をオフラインとしてマークします。

StatusListener を使用したエラー報告

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 には、パラメータとして AuthTokenContext を受け取る getToken() メソッドが 1 つだけになりました。

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 リストになります

VehicleStopTask リストが TaskInfo リストに置き換えられました。次のコード例は、VehicleStop オブジェクトの作成方法を示しています。

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