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 を使用した 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
を指定します。配車サービスの利用者は、
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);
}
}
}