@@ -26,14 +26,6 @@ public SQLiteLocationDAO(SQLiteDatabase db) {
2626 this .db = db ;
2727 }
2828
29- public long getLastInsertRowId (SQLiteDatabase db ) {
30- Cursor cur = db .rawQuery ("SELECT last_insert_rowid()" , null );
31- cur .moveToFirst ();
32- long id = cur .getLong (0 );
33- cur .close ();
34- return id ;
35- }
36-
3729 /**
3830 * Get all locations that match whereClause
3931 *
@@ -166,6 +158,30 @@ public Long persistLocationWithLimit(BackgroundLocation location, Integer maxRow
166158 shouldVacuum = true ;
167159 }
168160
161+ // get oldest location id to be overwritten
162+ Cursor cursor = null ;
163+ Long locationId ;
164+ try {
165+ cursor = db .query (
166+ LocationEntry .TABLE_NAME ,
167+ new String [] { LocationEntry ._ID },
168+ TextUtils .join ("" , new String []{
169+ LocationEntry .COLUMN_NAME_TIME ,
170+ "= (SELECT min(" ,
171+ LocationEntry .COLUMN_NAME_TIME ,
172+ ") FROM " ,
173+ LocationEntry .TABLE_NAME ,
174+ ")"
175+ }),
176+ null , null , null , null );
177+ cursor .moveToFirst ();
178+ locationId = cursor .getLong (0 );
179+ } finally {
180+ if (cursor != null ) {
181+ cursor .close ();
182+ }
183+ }
184+
169185 sql = new StringBuilder ("UPDATE " )
170186 .append (LocationEntry .TABLE_NAME ).append (" SET " )
171187 .append (LocationEntry .COLUMN_NAME_PROVIDER ).append ("= ?," )
@@ -185,9 +201,8 @@ public Long persistLocationWithLimit(BackgroundLocation location, Integer maxRow
185201 .append (LocationEntry .COLUMN_NAME_LOCATION_PROVIDER ).append ("= ?," )
186202 .append (LocationEntry .COLUMN_NAME_BATCH_START_MILLIS ).append ("= ?," )
187203 .append (LocationEntry .COLUMN_NAME_VALID ).append ("= ?" )
188- .append (" WHERE " ).append (LocationEntry .COLUMN_NAME_TIME )
189- .append ("= (SELECT min(" ).append (LocationEntry .COLUMN_NAME_TIME ).append (") FROM " )
190- .append (LocationEntry .TABLE_NAME ).append (")" )
204+ .append (" WHERE " ).append (LocationEntry ._ID )
205+ .append ("= ?" )
191206 .toString ();
192207 db .execSQL (sql , new Object [] {
193208 location .getProvider (),
@@ -206,16 +221,16 @@ public Long persistLocationWithLimit(BackgroundLocation location, Integer maxRow
206221 location .hasRadius () ? 1 : 0 ,
207222 location .getLocationProvider (),
208223 location .getBatchStartMillis (),
209- location .isValid () ? 1 : 0
224+ location .isValid () ? 1 : 0 ,
225+ locationId
210226 });
211227
212- rowId = getLastInsertRowId (db );
213228 db .setTransactionSuccessful ();
214229 db .endTransaction ();
215230
216231 if (shouldVacuum ) { db .execSQL ("VACUUM" ); }
217232
218- return rowId ;
233+ return locationId ;
219234 }
220235
221236 /**
0 commit comments