适用于 Android 4.0 版本的驱动程序 SDK 需要您更新代码 执行某些操作本指南概述了具体变化以及 迁移代码所需的操作
对于不可信的驱动程序模型,交付客户端只需要进行身份验证 (为VEHICLE
服务类型启用位置报告)。客户
必须为可信驱动程序模型提供身份验证,
可在以下位置启用车辆停止报告方法的 TASK
服务类型:
DeliveryVehicleReporter
。
软件包名称更改
软件包名称已更改,原为
com.google.android.libraries.ridesharing.driver
至
com.google.android.libraries.mapsplatform.transportation.driver
。请
更新代码中的引用
初始化 SDK
在早期版本中,您需要初始化 Navigation SDK,然后获取
对 FleetEngine
类的引用。在驱动程序 SDK 中
v4,请按如下方式初始化 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
。DeliveryDriverApi deliveryDriverApi = DeliveryDriverApi.createInstance(driverContext);
从 API 对象获取
NavigationVehicleReporter
。*VehicleReporter
扩展了NavigationVehicleReporter
。DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
启用和停用位置更新
在早期版本中,您可以在 Google Cloud 控制台
FleetEngine
引用。在驱动程序 SDK v4 中,启用
位置更新如下:
DeliveryVehicleReporter reporter = ...;
reporter.enableLocationTracking();
要更新报告间隔,请使用
DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit)
。
换档完成后,停用位置信息更新
并通过调用 NavigationVehicleReporter.disableLocationTracking()
将车辆标记为离线。
使用 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()
,
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
列表已替换为
TaskInfo
列于
VehicleStop
。以下代码示例演示了如何创建 VehicleStop
对象。
VehicleStop vehicleStop = VehicleStop.builder()
.setTaskInfoList(taskInfoList)
.setWaypoint(waypoint)
.setVehicleStopState(vehicleStopState)
.build();