diff --git a/app/src/main/java/org/traccar/client/DatabaseHelper.java b/app/src/main/java/org/traccar/client/DatabaseHelper.java index e7166f59..3dd27678 100644 --- a/app/src/main/java/org/traccar/client/DatabaseHelper.java +++ b/app/src/main/java/org/traccar/client/DatabaseHelper.java @@ -18,6 +18,7 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.database.DatabaseUtils; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; @@ -27,8 +28,10 @@ public class DatabaseHelper extends SQLiteOpenHelper { - public static final int DATABASE_VERSION = 3; - public static final String DATABASE_NAME = "traccar.db"; + private static final int DATABASE_VERSION = 3; + private static final String DATABASE_NAME = "traccar.db"; + private static final String TABLE_NAME = "position"; + private static final String DROP_QUERY = "DROP TABLE IF EXISTS " + TABLE_NAME + ";"; public interface DatabaseHandler { void onComplete(boolean success, T result); @@ -39,7 +42,7 @@ private static abstract class DatabaseAsyncTask extends AsyncTask handler; private RuntimeException error; - public DatabaseAsyncTask(DatabaseHandler handler) { + DatabaseAsyncTask(DatabaseHandler handler) { this.handler = handler; } @@ -70,7 +73,7 @@ public DatabaseHelper(Context context) { @Override public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE position (" + + db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "deviceId TEXT," + "time INTEGER," + @@ -86,12 +89,12 @@ public void onCreate(SQLiteDatabase db) { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - db.execSQL("DROP TABLE IF EXISTS position;"); + db.execSQL(DROP_QUERY); onCreate(db); } public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { - db.execSQL("DROP TABLE IF EXISTS position;"); + db.execSQL(DROP_QUERY); onCreate(db); } @@ -108,7 +111,7 @@ public void insertPosition(Position position) { values.put("battery", position.getBattery()); values.put("mock", position.getMock() ? 1 : 0); - db.insertOrThrow("position", null, values); + db.insertOrThrow(TABLE_NAME, null, values); } public void insertPositionAsync(final Position position, DatabaseHandler handler) { @@ -124,7 +127,7 @@ protected Void executeMethod() { public Position selectPosition() { Position position = new Position(); - Cursor cursor = db.rawQuery("SELECT * FROM position ORDER BY id LIMIT 1", null); + Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " ORDER BY id LIMIT 1", null); try { if (cursor.getCount() > 0) { @@ -162,7 +165,7 @@ protected Position executeMethod() { } public void deletePosition(long id) { - if (db.delete("position", "id = ?", new String[] { String.valueOf(id) }) != 1) { + if (db.delete(TABLE_NAME, "id = ?", new String[] { String.valueOf(id) }) != 1) { throw new SQLException(); } } @@ -177,4 +180,8 @@ protected Void executeMethod() { }.execute(); } + public long getLocationsCount() { + return DatabaseUtils.queryNumEntries(db, TABLE_NAME); + } + } diff --git a/app/src/main/java/org/traccar/client/TrackingController.java b/app/src/main/java/org/traccar/client/TrackingController.java index 37626f00..960dbd0f 100644 --- a/app/src/main/java/org/traccar/client/TrackingController.java +++ b/app/src/main/java/org/traccar/client/TrackingController.java @@ -92,9 +92,14 @@ public void stop() { @Override public void onPositionUpdate(Position position) { - StatusActivity.addMessage(context.getString(R.string.status_location_update)); if (position != null) { write(position); + long count = databaseHelper.getLocationsCount(); + if (count > 0) { + StatusActivity.addMessage(context.getString(R.string.status_location_update) + " (" + count + ")"); + } else { + StatusActivity.addMessage(context.getString(R.string.status_location_update)); + } } }