Cleanup: use autocloseable.

This commit is contained in:
Dennis Guse
2019-11-25 23:07:57 +01:00
parent d7526f93b8
commit 3bdce5aa08
@@ -297,8 +297,7 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return null; return null;
} }
try (Cursor cursor = getTrackCursor(null, TracksColumns._ID + "=?", try (Cursor cursor = getTrackCursor(null, TracksColumns._ID + "=?", new String[]{Long.toString(trackId)}, TracksColumns._ID)) {
new String[]{Long.toString(trackId)}, TracksColumns._ID)) {
if (cursor != null && cursor.moveToNext()) { if (cursor != null && cursor.moveToNext()) {
return createTrack(cursor); return createTrack(cursor);
} }
@@ -585,9 +584,7 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return -1L; return -1L;
} }
try (Cursor cursor = getWaypointCursor(new String[]{WaypointsColumns._ID}, try (Cursor cursor = getWaypointCursor(new String[]{WaypointsColumns._ID}, WaypointsColumns.TRACKID + "=?", new String[]{Long.toString(trackId)}, WaypointsColumns._ID, 1)) {
WaypointsColumns.TRACKID + "=?", new String[]{Long.toString(trackId)},
WaypointsColumns._ID, 1)) {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
return cursor.getLong(cursor.getColumnIndexOrThrow(WaypointsColumns._ID)); return cursor.getLong(cursor.getColumnIndexOrThrow(WaypointsColumns._ID));
} }
@@ -605,19 +602,13 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return null; return null;
} }
Cursor cursor = null; String selection = WaypointsColumns.TRACKID + "=? AND " + WaypointsColumns.TYPE + "=?";
try { String[] selectionArgs = new String[]{Long.toString(trackId), Integer.toString(waypointType.ordinal())};
String selection = WaypointsColumns.TRACKID + "=? AND " + WaypointsColumns.TYPE + "=?"; try (Cursor cursor = getWaypointCursor(null, selection, selectionArgs, WaypointsColumns._ID + " DESC", 1)) {
String[] selectionArgs = new String[]{
Long.toString(trackId), Integer.toString(waypointType.ordinal())};
cursor = getWaypointCursor(null, selection, selectionArgs, WaypointsColumns._ID + " DESC", 1);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
return createWaypoint(cursor); return createWaypoint(cursor);
} }
} finally {
if (cursor != null) {
cursor.close();
}
} }
return null; return null;
} }
@@ -633,25 +624,15 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return -1; return -1;
} }
Cursor cursor = null; String[] projection = {WaypointsColumns._ID};
try { String selection = WaypointsColumns.TRACKID + "=? AND " + WaypointsColumns.TYPE + "=?";
String[] projection = {WaypointsColumns._ID}; String[] selectionArgs = new String[]{Long.toString(trackId), Integer.toString(waypointType.ordinal())};
String selection = WaypointsColumns.TRACKID + "=? AND " + WaypointsColumns.TYPE + "=?"; try (Cursor cursor = getWaypointCursor(projection, selection, selectionArgs, WaypointsColumns._ID, -1)) {
String[] selectionArgs = new String[]{
Long.toString(trackId), Integer.toString(waypointType.ordinal())};
cursor = getWaypointCursor(projection, selection, selectionArgs, WaypointsColumns._ID, -1);
if (cursor != null) { if (cursor != null) {
int count = cursor.getCount(); int count = cursor.getCount();
/* // For statistics markers, the first marker is for the track statistics, so return the count as the next user visible number.
* For statistics markers, the first marker is for the track statistics,
* so return the count as the next user visible number.
*/
return waypointType == WaypointType.STATISTICS ? count : count + 1; return waypointType == WaypointType.STATISTICS ? count : count + 1;
} }
} finally {
if (cursor != null) {
cursor.close();
}
} }
return -1; return -1;
} }
@@ -685,8 +666,7 @@ public class ContentProviderUtils {
* @param maxWaypoints the maximum number of waypoints to return. -1 for no * @param maxWaypoints the maximum number of waypoints to return. -1 for no
* limit * limit
*/ */
public Cursor getWaypointCursor( public Cursor getWaypointCursor(String selection, String[] selectionArgs, String sortOrder, int maxWaypoints) {
String selection, String[] selectionArgs, String sortOrder, int maxWaypoints) {
return getWaypointCursor(null, selection, selectionArgs, sortOrder, maxWaypoints); return getWaypointCursor(null, selection, selectionArgs, sortOrder, maxWaypoints);
} }
@@ -755,8 +735,7 @@ public class ContentProviderUtils {
* @param waypoint the waypoint * @param waypoint the waypoint
*/ */
public boolean updateWaypoint(Waypoint waypoint) { public boolean updateWaypoint(Waypoint waypoint) {
int rows = contentResolver.update(WaypointsColumns.CONTENT_URI, createContentValues(waypoint), int rows = contentResolver.update(WaypointsColumns.CONTENT_URI, createContentValues(waypoint), WaypointsColumns._ID + "=?", new String[]{Long.toString(waypoint.getId())});
WaypointsColumns._ID + "=?", new String[]{Long.toString(waypoint.getId())});
return rows == 1; return rows == 1;
} }
@@ -818,20 +797,13 @@ public class ContentProviderUtils {
} }
private Waypoint getNextStatisticsWaypointAfter(Waypoint waypoint) { private Waypoint getNextStatisticsWaypointAfter(Waypoint waypoint) {
Cursor cursor = null; String selection = WaypointsColumns._ID + ">? AND " + WaypointsColumns.TRACKID + "=? AND "
try { + WaypointsColumns.TYPE + "=" + WaypointType.STATISTICS.ordinal();
String selection = WaypointsColumns._ID + ">? AND " + WaypointsColumns.TRACKID + "=? AND " String[] selectionArgs = new String[]{Long.toString(waypoint.getId()), Long.toString(waypoint.getTrackId())};
+ WaypointsColumns.TYPE + "=" + WaypointType.STATISTICS.ordinal(); try (Cursor cursor = getWaypointCursor(null, selection, selectionArgs, WaypointsColumns._ID, 1)) {
String[] selectionArgs = new String[]{
Long.toString(waypoint.getId()), Long.toString(waypoint.getTrackId())};
cursor = getWaypointCursor(null, selection, selectionArgs, WaypointsColumns._ID, 1);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
return createWaypoint(cursor); return createWaypoint(cursor);
} }
} finally {
if (cursor != null) {
cursor.close();
}
} }
return null; return null;
} }
@@ -845,8 +817,7 @@ public class ContentProviderUtils {
* @param sortOrder the sort order * @param sortOrder the sort order
* @param maxWaypoints the maximum number of waypoints * @param maxWaypoints the maximum number of waypoints
*/ */
private Cursor getWaypointCursor(String[] projection, String selection, String[] selectionArgs, private Cursor getWaypointCursor(String[] projection, String selection, String[] selectionArgs, String sortOrder, int maxWaypoints) {
String sortOrder, int maxWaypoints) {
if (sortOrder == null) { if (sortOrder == null) {
sortOrder = WaypointsColumns._ID; sortOrder = WaypointsColumns._ID;
} }
@@ -897,21 +868,14 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return -1L; return -1L;
} }
Cursor cursor = null; String selection = TrackPointsColumns._ID + "=(select min(" + TrackPointsColumns._ID
try { + ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID
String selection = TrackPointsColumns._ID + "=(select min(" + TrackPointsColumns._ID + "=?)";
+ ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID String[] selectionArgs = new String[]{Long.toString(trackId)};
+ "=?)"; try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) {
String[] selectionArgs = new String[]{Long.toString(trackId)};
cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection,
selectionArgs, TrackPointsColumns._ID);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
return cursor.getLong(cursor.getColumnIndexOrThrow(TrackPointsColumns._ID)); return cursor.getLong(cursor.getColumnIndexOrThrow(TrackPointsColumns._ID));
} }
} finally {
if (cursor != null) {
cursor.close();
}
} }
return -1L; return -1L;
} }
@@ -926,21 +890,14 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return -1L; return -1L;
} }
Cursor cursor = null; String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID
try { + ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID
String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID + "=?)";
+ ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID String[] selectionArgs = new String[]{Long.toString(trackId)};
+ "=?)"; try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) {
String[] selectionArgs = new String[]{Long.toString(trackId)};
cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection,
selectionArgs, TrackPointsColumns._ID);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
return cursor.getLong(cursor.getColumnIndexOrThrow(TrackPointsColumns._ID)); return cursor.getLong(cursor.getColumnIndexOrThrow(TrackPointsColumns._ID));
} }
} finally {
if (cursor != null) {
cursor.close();
}
} }
return -1L; return -1L;
} }
@@ -956,22 +913,14 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return -1L; return -1L;
} }
Cursor cursor = null; String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID
try { + ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID
String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID + "=? AND " + TrackPointsColumns.TIME + "=?)";
+ ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID String[] selectionArgs = new String[]{Long.toString(trackId), Long.toString(location.getTime())};
+ "=? AND " + TrackPointsColumns.TIME + "=?)"; try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) {
String[] selectionArgs = new String[]{
Long.toString(trackId), Long.toString(location.getTime())};
cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection,
selectionArgs, TrackPointsColumns._ID);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
return cursor.getLong(cursor.getColumnIndexOrThrow(TrackPointsColumns._ID)); return cursor.getLong(cursor.getColumnIndexOrThrow(TrackPointsColumns._ID));
} }
} finally {
if (cursor != null) {
cursor.close();
}
} }
return -1L; return -1L;
} }