Android용 Driver SDK 3.0 출시에서는 특정 작업을 위해 코드를 업데이트해야 합니다. 이 가이드에서는 변경사항과 코드를 이전하기 위해 수행해야 하는 작업을 간략히 설명합니다.
패키지 이름 변경
패키지 이름이 com.google.android.libraries.ridesharing.driver
에서 com.google.android.libraries.mapsplatform.transportation.driver
로 변경되었습니다. 코드에서 참조를 업데이트하세요.
SDK 초기화
이전 버전에서는 Navigation SDK를 초기화한 다음 FleetEngine
클래스 참조를 얻었습니다. 드라이버 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()
메서드 하나만 있습니다. 차량 공유 클라이언트는 위치 보고 및 차량 상태 (온라인/오프라인) 보고를 사용 설정하는 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);
}
}
}