Android Driver SDK 4.0 移行ガイド

Driver SDK for Android 4.0 リリースでは、コードの更新が必要 パフォーマンスが向上します。このガイドでは、変更点の概要と変更内容について説明します。 いくつかご紹介します

パッケージ名の変更

パッケージ名が com.google.android.libraries.ridesharing.drivercom.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 を初期化します。

    RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
    
  4. API オブジェクトから NavigationVehicleReporter を取得します。 *VehicleReporterNavigationVehicleReporter を拡張します。

    RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
    

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

それより前のバージョンでは、位置情報の更新を有効にするには、 FleetEngine 参照。Driver SDK v4 で以下を有効にします。 次のような方法で位置情報を更新します。

RidesharingVehicleReporter reporter = ...;

reporter.enableLocationTracking();

レポート間隔を更新するには、次のコマンドを使用します。 RidesharingVehicleReporter.setLocationReportingInterval(long, TimeUnit)

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

サーバーで車両の状態を設定する

それより前のバージョンでは、FleetEngine オブジェクトを使用して、 車両の状態。Driver SDK v4 では、次のコマンドを使用します。 RidesharingVehicleReporter オブジェクトを使用して車両の状態を設定します。

RidesharingVehicleReporter reporter = ...;

reporter.enableLocationTracking();
reporter.setVehicleState(VehicleState.ONLINE);

車両の状態を OFFLINE に設定するには、呼び出します。 RidesharingVehicleReporter.disableLocationTracking()。エラー数 車両の状態の更新は、オプションの DriverContextStatusListener を設定します。

StatusListener を使用した 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() というメソッドが 1 つしかありません。 AuthTokenContext をパラメータとして指定します。ServiceType のサポートが終了しました。今すぐ 車両 ID についてのみ申請が必要となり、 ServiceType

class JsonAuthTokenFactory implements AuthTokenFactory {
  private String token;  // initially null
  private long expiryTimeMs = 0;

  // This method is called on a thread whose only responsibility is to send
  // location updates. Blocking is OK, but just know that no location 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(authTokenContext.getVehicleId());
    }
    return token;
  }

  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();
      token = obj.get("Token").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 is notified, and passes along the failed
      // update warning.
      throw new RuntimeException("Could not get auth token", e);
    }
  }
}