คุณต้องอัปเดตโค้ดสำหรับรุ่น Driver SDK สำหรับ Android 3.0 สำหรับการดำเนินการบางอย่าง คู่มือนี้จะอธิบายถึงการเปลี่ยนแปลงและ คุณต้องทำเพื่อย้ายโค้ด
การเปลี่ยนชื่อแพ็กเกจ
ชื่อแพ็กเกจเปลี่ยนจาก
com.google.android.libraries.ridesharing.driver
ถึง
com.google.android.libraries.mapsplatform.transportation.driver
โปรด
อัปเดตการอ้างอิงในโค้ดของคุณ
การเริ่มต้น SDK
ในเวอร์ชันก่อนหน้านี้ คุณจะต้องเริ่มต้น Navigation SDK จากนั้นรับ
การอ้างอิงชั้นเรียน FleetEngine
ใน SDK ไดรเวอร์
v3 ให้เริ่มต้น 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
RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
รับ
NavigationVehicleReporter
จากออบเจ็กต์ API*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()
มีข้อผิดพลาด
ระบบจะเผยแพร่การอัปเดตสถานะของพาหนะโดยใช้ตัวเลือกที่มีให้
StatusListener
ที่ตั้งไว้ในDriverContext
การรายงานข้อผิดพลาดด้วย 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
เป็นพารามิเตอร์ ลูกค้าบริการร่วมเดินทางต้อง
ตรวจสอบสิทธิ์สำหรับบริการประเภท 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);
}
}
}