Android용 Driver SDK 4.0 출시에서는 특정 작업의 코드를 업데이트해야 합니다. 이 가이드에서는 변경사항과 코드를 이전하기 위해 수행해야 하는 작업을 간략히 설명합니다.
untrusted 드라이버 모델의 경우 전송 클라이언트는 위치 보고를 사용 설정하는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();
위치 업데이트 사용 설정 및 중지
이전 버전에서는 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()
메서드 하나만 있습니다.
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
목록이 VehicleStop
의 TaskInfo
목록으로 대체되었습니다. 다음 코드 예는 VehicleStop
객체를 만드는 방법을 보여줍니다.
VehicleStop vehicleStop = VehicleStop.builder()
.setTaskInfoList(taskInfoList)
.setWaypoint(waypoint)
.setVehicleStopState(vehicleStopState)
.build();