From 87023ebdb34822e0b8d094dab4a35a92099a6767 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Tue, 8 Mar 2016 21:26:55 +0100 Subject: [PATCH] Don't retrieve the column index again and again in a long loop Also: fix weird iteration logic --- .../database/ActivityDatabaseHandler.java | 25 +++++++++++-------- .../externalevents/K9Receiver.java | 5 ++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java index bc1facb3..3b40db5b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java @@ -221,17 +221,20 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl try (SQLiteDatabase db = this.getReadableDatabase()) { try (Cursor cursor = db.query(TABLE_GBACTIVITYSAMPLES, null, where, null, null, null, order)) { LOG.info("Activity query result: " + cursor.getCount() + " samples"); - if (cursor.moveToFirst()) { - do { - GBActivitySample sample = new GBActivitySample( - provider, - cursor.getInt(cursor.getColumnIndex(KEY_TIMESTAMP)), - cursor.getInt(cursor.getColumnIndex(KEY_INTENSITY)), - cursor.getInt(cursor.getColumnIndex(KEY_STEPS)), - cursor.getInt(cursor.getColumnIndex(KEY_TYPE)), - cursor.getInt(cursor.getColumnIndex(KEY_CUSTOM_SHORT))); - samples.add(sample); - } while (cursor.moveToNext()); + int colTimeStamp = cursor.getColumnIndex(KEY_TIMESTAMP); + int colIntensity = cursor.getColumnIndex(KEY_INTENSITY); + int colSteps = cursor.getColumnIndex(KEY_STEPS); + int colType = cursor.getColumnIndex(KEY_TYPE); + int colCustomShort = cursor.getColumnIndex(KEY_CUSTOM_SHORT); + while (cursor.moveToFirst()) { + GBActivitySample sample = new GBActivitySample( + provider, + cursor.getInt(colTimeStamp), + cursor.getInt(colIntensity), + cursor.getInt(colSteps), + cursor.getInt(colType), + cursor.getInt(colCustomShort)); + samples.add(sample); } } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java index 27b7132c..f9460d60 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java @@ -67,8 +67,7 @@ public class K9Receiver extends BroadcastReceiver { try { if (c != null) { - c.moveToFirst(); - do { + while (c.moveToFirst()) { String uri = c.getString(c.getColumnIndex("uri")); if (uri.equals(uriWanted)) { notificationSpec.sender = c.getString(c.getColumnIndex("senderAddress")); @@ -76,7 +75,7 @@ public class K9Receiver extends BroadcastReceiver { notificationSpec.body = c.getString(c.getColumnIndex("preview")); break; } - } while (c.moveToNext()); + } } } finally { if (c != null) {