בגרסה של Driver SDK ל-Android 4.0 צריך לעדכן את הקוד לפעולות מסוימות. במדריך הזה מפורטים השינויים ומה צריך לעשות כדי להעביר את הקוד.
בדגם מנהל התקן untrusted, לקוחות מסירה צריכים לבצע אימות רק עבור סוג השירותVEHICLE
, שמאפשר דיווח על מיקום. כדי להפעיל שיטות דיווח על עצירת רכב ב-DeliveryVehicleReporter
, לקוחות עם מודל נהיגה מהימן צריכים לספק אימות לסוג השירות TASK
.
שינוי שם החבילה
שם החבילה השתנה מ-com.google.android.libraries.ridesharing.driver
ל-com.google.android.libraries.mapsplatform.transportation.driver
. צריך לעדכן את ההפניות בקוד.
אתחול ה-SDK
בגרסאות קודמות, הפעלתם את ה-SDK של הניווט ולאחר מכן קיבלתם הפניה למחלקה FleetEngine
. ב-Driver SDK v4, מפעילים את ה-SDK באופן הבא:
מקבלים אובייקט
Navigator
מה-NavigationApi
.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);
מקבלים את ה-
NavigationVehicleReporter
מאובייקט ה-API. תקופת הניסיון של*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
רק שיטה אחת, 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();