Pakiet SDK sterowników na Androida 4.0 wymaga zaktualizowania kodu w przypadku niektórych operacji. Ten przewodnik zawiera informacje o zmianach i o tym, co musisz zrobić, aby przeprowadzić migrację kodu.
W przypadku untrusted modelu kierowcy klienci dostawy muszą uwierzytelniać się tylko w przypadku typu usługiVEHICLE
, co umożliwia raportowanie lokalizacji. Klienci korzystający z zaufanego modelu kierowcy muszą uwierzytelnić w usłudze TASK
, aby umożliwić korzystanie z metod zgłaszania zatrzymania pojazdu w DeliveryVehicleReporter
.
Zmiana nazwy pakietu
Nazwa pakietu zmieniła się z com.google.android.libraries.ridesharing.driver
na com.google.android.libraries.mapsplatform.transportation.driver
. Zaktualizuj odwołania w kodzie.
Inicjowanie pakietu SDK
We wcześniejszych wersjach należało zainicjować pakiet SDK nawigacji, a następnie uzyskać odniesienie do klasy FleetEngine
. W pakiecie SDK sterownika w wersji 4 zainicjuj pakiet SDK w ten sposób:
Uzyskaj obiekt
Navigator
z klasyNavigationApi
.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; } } );
Utwórz obiekt
DriverContext
, wypełniając wymagane pola.DriverContext driverContext = DriverContext.builder(application) .setProviderId(providerId) .setVehicleId(vehicleId) .setAuthTokenFactory(authTokenFactory) .setNavigator(navigator) .setRoadSnappedLocationProvider( NavigationApi.getRoadSnappedLocationProvider(application)) .build();
Użyj obiektu
DriverContext
do zainicjowania*DriverApi
.DeliveryDriverApi deliveryDriverApi = DeliveryDriverApi.createInstance(driverContext);
Uzyskaj
NavigationVehicleReporter
z obiektu interfejsu API. Subskrypcja*VehicleReporter
została przedłużona oNavigationVehicleReporter
.DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
Włączanie i wyłączanie aktualizacji lokalizacji
We wcześniejszych wersjach należało włączyć aktualizacje lokalizacji po uzyskaniu odwołania do FleetEngine
. W pakiecie SDK sterownika w wersji 4 włącz aktualizacje lokalizacji w ten sposób:
DeliveryVehicleReporter reporter = ...;
reporter.enableLocationTracking();
Aby zaktualizować częstotliwość raportowania, użyj DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit)
.
Po zakończeniu zmiany kierowcy wyłącz aktualizacje lokalizacji i oznacz pojazd jako offline, dzwoniąc pod numer NavigationVehicleReporter.disableLocationTracking()
.
Raportowanie błędów za pomocą obiektu StatusListener
Element ErrorListener
został usunięty i połączony z właściwością StatusListener
, która może być zdefiniowana w ten sposób:
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.
}
}
Uwierzytelniam w: AuthTokenFactory
AuthTokenFactory
ma teraz tylko 1 metodę – getToken()
, która jako parametr przyjmuje 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);
}
}
}
Lista Task
zmieni się w listę TaskInfo
Lista Task
została zastąpiona listą TaskInfo
w VehicleStop
. Poniższy przykładowy kod pokazuje, jak utworzyć obiekt VehicleStop
.
VehicleStop vehicleStop = VehicleStop.builder()
.setTaskInfoList(taskInfoList)
.setWaypoint(waypoint)
.setVehicleStopState(vehicleStopState)
.build();