From c77dcb645fa8f473bf7252464852645d41381b8d Mon Sep 17 00:00:00 2001 From: Olivier Girondel Date: Mon, 7 May 2018 19:17:54 +0200 Subject: [PATCH] Add driver (driverUniqueId) support --- .../java/org/traccar/client/DatabaseHelper.java | 5 ++++- .../java/org/traccar/client/MainFragment.java | 17 +++++++++++++++++ .../main/java/org/traccar/client/Position.java | 13 ++++++++++++- .../org/traccar/client/PositionProvider.java | 4 +++- .../org/traccar/client/ProtocolFormatter.java | 1 + .../org/traccar/client/ShortcutActivity.java | 1 + app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/preferences.xml | 6 ++++++ .../org/traccar/client/DatabaseHelperTest.java | 2 +- .../traccar/client/ProtocolFormatterTest.java | 12 ++++++------ 11 files changed, 53 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/traccar/client/DatabaseHelper.java b/app/src/main/java/org/traccar/client/DatabaseHelper.java index e7166f59..4dc71497 100644 --- a/app/src/main/java/org/traccar/client/DatabaseHelper.java +++ b/app/src/main/java/org/traccar/client/DatabaseHelper.java @@ -27,7 +27,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { - public static final int DATABASE_VERSION = 3; + public static final int DATABASE_VERSION = 4; public static final String DATABASE_NAME = "traccar.db"; public interface DatabaseHandler { @@ -73,6 +73,7 @@ public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE position (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "deviceId TEXT," + + "driverUniqueId TEXT," + "time INTEGER," + "latitude REAL," + "longitude REAL," + @@ -98,6 +99,7 @@ public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void insertPosition(Position position) { ContentValues values = new ContentValues(); values.put("deviceId", position.getDeviceId()); + values.put("driverUniqueId", position.getDriverUniqueId()); values.put("time", position.getTime().getTime()); values.put("latitude", position.getLatitude()); values.put("longitude", position.getLongitude()); @@ -132,6 +134,7 @@ public Position selectPosition() { position.setId(cursor.getLong(cursor.getColumnIndex("id"))); position.setDeviceId(cursor.getString(cursor.getColumnIndex("deviceId"))); + position.setDriverUniqueId(cursor.getString(cursor.getColumnIndex("driverUniqueId"))); position.setTime(new Date(cursor.getLong(cursor.getColumnIndex("time")))); position.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude"))); position.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude"))); diff --git a/app/src/main/java/org/traccar/client/MainFragment.java b/app/src/main/java/org/traccar/client/MainFragment.java index 0ce5ce77..a038e0ad 100644 --- a/app/src/main/java/org/traccar/client/MainFragment.java +++ b/app/src/main/java/org/traccar/client/MainFragment.java @@ -54,6 +54,7 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen private static final int ALARM_MANAGER_INTERVAL = 15000; public static final String KEY_DEVICE = "id"; + public static final String KEY_DRIVER_UNIQUE_ID = "driverUniqueId"; public static final String KEY_URL = "url"; public static final String KEY_INTERVAL = "interval"; public static final String KEY_DISTANCE = "distance"; @@ -87,6 +88,12 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { return newValue != null && !newValue.equals(""); } }); + findPreference(KEY_DRIVER_UNIQUE_ID).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + return newValue != null && !newValue.equals(""); + } + }); findPreference(KEY_URL).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { @@ -164,6 +171,7 @@ public void onPause() { private void setPreferencesEnabled(boolean enabled) { findPreference(KEY_DEVICE).setEnabled(enabled); + findPreference(KEY_DRIVER_UNIQUE_ID).setEnabled(enabled); findPreference(KEY_URL).setEnabled(enabled); findPreference(KEY_INTERVAL).setEnabled(enabled); findPreference(KEY_DISTANCE).setEnabled(enabled); @@ -181,6 +189,8 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin } } else if (key.equals(KEY_DEVICE)) { findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null)); + } else if (key.equals(KEY_DRIVER_UNIQUE_ID)) { + findPreference(KEY_DRIVER_UNIQUE_ID).setSummary(sharedPreferences.getString(KEY_DRIVER_UNIQUE_ID, null)); } } @@ -211,6 +221,13 @@ private void initPreferences() { ((EditTextPreference) findPreference(KEY_DEVICE)).setText(id); } findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null)); + + if (!sharedPreferences.contains(KEY_DRIVER_UNIQUE_ID)) { + String id = String.valueOf(new Random().nextInt(900000) + 100000); + sharedPreferences.edit().putString(KEY_DRIVER_UNIQUE_ID, id).apply(); + ((EditTextPreference) findPreference(KEY_DRIVER_UNIQUE_ID)).setText(id); + } + findPreference(KEY_DRIVER_UNIQUE_ID).setSummary(sharedPreferences.getString(KEY_DRIVER_UNIQUE_ID, null)); } private void startTrackingService(boolean checkPermission, boolean permission) { diff --git a/app/src/main/java/org/traccar/client/Position.java b/app/src/main/java/org/traccar/client/Position.java index ca4f2e70..7890d492 100644 --- a/app/src/main/java/org/traccar/client/Position.java +++ b/app/src/main/java/org/traccar/client/Position.java @@ -26,8 +26,9 @@ public class Position { public Position() { } - public Position(String deviceId, Location location, double battery) { + public Position(String deviceId, String driverUniqueId, Location location, double battery) { this.deviceId = deviceId; + this.driverUniqueId = driverUniqueId; time = new Date(location.getTime()); latitude = location.getLatitude(); longitude = location.getLongitude(); @@ -63,6 +64,16 @@ public void setDeviceId(String deviceId) { this.deviceId = deviceId; } + private String driverUniqueId; + + public String getDriverUniqueId() { + return driverUniqueId; + } + + public void setDriverUniqueId(String driverUniqueId) { + this.driverUniqueId = driverUniqueId; + } + private Date time; public Date getTime() { diff --git a/app/src/main/java/org/traccar/client/PositionProvider.java b/app/src/main/java/org/traccar/client/PositionProvider.java index 9362db0b..63f452b9 100644 --- a/app/src/main/java/org/traccar/client/PositionProvider.java +++ b/app/src/main/java/org/traccar/client/PositionProvider.java @@ -48,6 +48,7 @@ public interface PositionListener { private LocationManager locationManager; private String deviceId; + private String driverUniqueId; private long interval; private double distance; private double angle; @@ -63,6 +64,7 @@ public PositionProvider(Context context, PositionListener listener) { preferences = PreferenceManager.getDefaultSharedPreferences(context); deviceId = preferences.getString(MainFragment.KEY_DEVICE, "undefined"); + driverUniqueId = preferences.getString(MainFragment.KEY_DRIVER_UNIQUE_ID, "undefined"); interval = Long.parseLong(preferences.getString(MainFragment.KEY_INTERVAL, "600")) * 1000; distance = Integer.parseInt(preferences.getString(MainFragment.KEY_DISTANCE, "0")); angle = Integer.parseInt(preferences.getString(MainFragment.KEY_ANGLE, "0")); @@ -107,7 +109,7 @@ public void onLocationChanged(Location location) { || angle > 0 && Math.abs(location.getBearing() - lastLocation.getBearing()) >= angle)) { Log.i(TAG, "location new"); lastLocation = location; - listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel(context))); + listener.onPositionUpdate(new Position(deviceId, driverUniqueId, location, getBatteryLevel(context))); } else { Log.i(TAG, location != null ? "location ignored" : "location nil"); } diff --git a/app/src/main/java/org/traccar/client/ProtocolFormatter.java b/app/src/main/java/org/traccar/client/ProtocolFormatter.java index 2a58531d..38a6f7bd 100644 --- a/app/src/main/java/org/traccar/client/ProtocolFormatter.java +++ b/app/src/main/java/org/traccar/client/ProtocolFormatter.java @@ -27,6 +27,7 @@ public static String formatRequest(String url, Position position, String alarm) Uri serverUrl = Uri.parse(url); Uri.Builder builder = serverUrl.buildUpon() .appendQueryParameter("id", position.getDeviceId()) + .appendQueryParameter("driverUniqueId", position.getDriverUniqueId()) .appendQueryParameter("timestamp", String.valueOf(position.getTime().getTime() / 1000)) .appendQueryParameter("lat", String.valueOf(position.getLatitude())) .appendQueryParameter("lon", String.valueOf(position.getLongitude())) diff --git a/app/src/main/java/org/traccar/client/ShortcutActivity.java b/app/src/main/java/org/traccar/client/ShortcutActivity.java index d62eb9b1..3870a8fa 100644 --- a/app/src/main/java/org/traccar/client/ShortcutActivity.java +++ b/app/src/main/java/org/traccar/client/ShortcutActivity.java @@ -145,6 +145,7 @@ private void sendAlarmLocation(Location location) { Position position = new Position( preferences.getString(MainFragment.KEY_DEVICE, null), + preferences.getString(MainFragment.KEY_DRIVER_UNIQUE_ID, null), location, PositionProvider.getBatteryLevel(this)); String request = ProtocolFormatter.formatRequest( diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index b2439a90..7b5cf1b5 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -3,6 +3,7 @@ Client Traccar Traccar Identifiant de l\'appareil + Identifiant du conducteur URL du serveur URL du serveur de suivi Fréquence diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 826b6e2f..409636ba 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ Send SOS Primary Channel Device identifier + Driver identifier Server URL Tracking server URL Frequency diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index c69f09a1..2269d1e1 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -14,6 +14,12 @@ android:key="id" android:title="@string/settings_id_title" /> + +