Driver SDK for Android 3.0 リリースでは、特定のオペレーション用のコードを更新する必要があります。このガイドでは、変更点の概要と、コードを移行するために必要な作業について説明します。
パッケージ名の変更
パッケージ名が com.google.android.libraries.ridesharing.driver
から com.google.android.libraries.mapsplatform.transportation.driver
に変更されました。コード内の参照を更新してください。
SDK の初期化
それより前のバージョンでは、Navigation SDK を初期化してから、FleetEngine
クラスへの参照を取得していました。Driver SDK v3 では、次のように SDK を初期化します。
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; } } );
DriverContext
オブジェクトを作成して、必須フィールドを設定します。DriverContext driverContext = DriverContext.builder(application) .setProviderId(providerId) .setVehicleId(vehicleId) .setAuthTokenFactory(authTokenFactory) .setNavigator(navigator) .setRoadSnappedLocationProvider( NavigationApi.getRoadSnappedLocationProvider(application)) .build()
DriverContext
オブジェクトを使用して*DriverApi
を初期化します。RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
API オブジェクトから
NavigationVehicleReporter
を取得します。*VehicleReporter
はNavigationVehicleReporter
を拡張します。RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
位置情報の更新を有効または無効にする
それより前のバージョンでは、FleetEngine
参照を取得した後に位置情報の更新を有効にしていました。Driver SDK v3 では、次のように位置情報の更新を有効にします。
RidesharingVehicleReporter reporter = ...;
reporter.enableLocationTracking();
レポート間隔を更新するには、RidesharingVehicleReporter.setLocationReportingInterval(long, TimeUnit)
または DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit)
を使用します。
ドライバーのシフトが終了したら、NavigationVehicleReporter.disableLocationTracking()
を呼び出して位置情報の更新を無効にし、車両をオフラインとしてマークします。
サーバーで車両の状態を設定する
それより前のバージョンでは、FleetEngine
オブジェクトを使用して車両の状態を設定していました。Driver SDK v3 では、RidesharingVehicleReporter
オブジェクトを使用して車両の状態を設定します。
RidesharingVehicleReporter reporter = ...;
reporter.enableLocationTracking();
reporter.setVehicleState(VehicleState.ONLINE);
車両の状態を OFFLINE
に設定するには、RidesharingVehicleReporter.disableLocationTracking()
を呼び出します。車両の状態を更新するエラーは、DriverContext
で設定され、オプションで提供された StatusListener
を使用して伝播されます。
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 つだけになりました。ライドシェアリング クライアントは、VEHICLE
サービスタイプの認証が必要です。これにより、位置情報と車両の状態(オンライン/オフライン)の報告が有効になります。
class JsonAuthTokenFactory implements AuthTokenFactory {
String vehicleServiceToken; // initially null
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(vehicleId);
}
if (ServiceType.VEHICLE.equals(authTokenContext.getServiceType)) {
return vehicleServiceToken;
} else {
throw new RuntimeException("Unsupported ServiceType: " + authTokenContext.getServiceType());
}
}
private void fetchNewToken(String vehicleId) {
String url = "https://yourauthserver.example/token/" + vehicleId;
try (Reader r = new URL(url).openStream()) {
com.google.gson.JsonObject obj
= new com.google.gson.JsonParser().parse(r);
vehicleServiceToken = obj.get("VehicleServiceToken").getAsString();
expiryTimeMs = obj.getAsLong("TokenExpiryMs");
// 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 FleetEngine class will be notified and pass along the failed
// update warning.
throw new RuntimeException("Could not get auth token", e);
}
}
}