From 82efb6f1dbf290f86433416535179000cb084ba9 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 14 Sep 2022 17:51:22 +0200 Subject: [PATCH] code clean up --- .../siacs/conversations/entities/Account.java | 34 +++++++++---------- .../persistance/DatabaseBackend.java | 29 +++++++--------- .../ui/adapter/MessageAdapter.java | 10 +++--- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 10f6ed8a8..1c16ab20b 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -125,31 +125,31 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable public static Account fromCursor(final Cursor cursor) { final Jid jid; try { - String resource = cursor.getString(cursor.getColumnIndex(RESOURCE)); + String resource = cursor.getString(cursor.getColumnIndexOrThrow(RESOURCE)); jid = Jid.of( - cursor.getString(cursor.getColumnIndex(USERNAME)), - cursor.getString(cursor.getColumnIndex(SERVER)), + cursor.getString(cursor.getColumnIndexOrThrow(USERNAME)), + cursor.getString(cursor.getColumnIndexOrThrow(SERVER)), resource == null || resource.trim().isEmpty() ? null : resource); } catch (final IllegalArgumentException ignored) { - Log.d(Config.LOGTAG, cursor.getString(cursor.getColumnIndex(USERNAME)) + "@" + cursor.getString(cursor.getColumnIndex(SERVER))); + Log.d(Config.LOGTAG, cursor.getString(cursor.getColumnIndexOrThrow(USERNAME)) + "@" + cursor.getString(cursor.getColumnIndexOrThrow(SERVER))); throw new AssertionError(ignored); } - return new Account(cursor.getString(cursor.getColumnIndex(UUID)), + return new Account(cursor.getString(cursor.getColumnIndexOrThrow(UUID)), jid, - cursor.getString(cursor.getColumnIndex(PASSWORD)), - cursor.getInt(cursor.getColumnIndex(OPTIONS)), - cursor.getString(cursor.getColumnIndex(ROSTERVERSION)), - cursor.getString(cursor.getColumnIndex(KEYS)), - cursor.getString(cursor.getColumnIndex(AVATAR)), - cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)), - cursor.getString(cursor.getColumnIndex(HOSTNAME)), - cursor.getInt(cursor.getColumnIndex(PORT)), - Presence.Status.fromShowString(cursor.getString(cursor.getColumnIndex(STATUS))), - cursor.getString(cursor.getColumnIndex(STATUS_MESSAGE))); + cursor.getString(cursor.getColumnIndexOrThrow(PASSWORD)), + cursor.getInt(cursor.getColumnIndexOrThrow(OPTIONS)), + cursor.getString(cursor.getColumnIndexOrThrow(ROSTERVERSION)), + cursor.getString(cursor.getColumnIndexOrThrow(KEYS)), + cursor.getString(cursor.getColumnIndexOrThrow(AVATAR)), + cursor.getString(cursor.getColumnIndexOrThrow(DISPLAY_NAME)), + cursor.getString(cursor.getColumnIndexOrThrow(HOSTNAME)), + cursor.getInt(cursor.getColumnIndexOrThrow(PORT)), + Presence.Status.fromShowString(cursor.getString(cursor.getColumnIndexOrThrow(STATUS))), + cursor.getString(cursor.getColumnIndexOrThrow(STATUS_MESSAGE))); } - public boolean httpUploadAvailable(long filesize) { - return xmppConnection != null && xmppConnection.getFeatures().httpUpload(filesize); + public boolean httpUploadAvailable(long size) { + return xmppConnection != null && xmppConnection.getFeatures().httpUpload(size); } public boolean httpUploadAvailable() { diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index de9bc0d2c..9e4bf9f8e 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -967,33 +967,28 @@ public class DatabaseBackend extends SQLiteOpenHelper { } public List getAccountJids(final boolean enabledOnly) { - SQLiteDatabase db = this.getReadableDatabase(); + final SQLiteDatabase db = this.getReadableDatabase(); final List jids = new ArrayList<>(); final String[] columns = new String[]{Account.USERNAME, Account.SERVER}; - String where = enabledOnly ? "not options & (1 <<1)" : null; - Cursor cursor = db.query(Account.TABLENAME, columns, where, null, null, null, null); - try { - while (cursor.moveToNext()) { + final String where = enabledOnly ? "not options & (1 <<1)" : null; + try (final Cursor cursor = db.query(Account.TABLENAME, columns, where, null, null, null, null)) { + while (cursor != null && cursor.moveToNext()) { jids.add(Jid.of(cursor.getString(0), cursor.getString(1), null)); } + } catch (final Exception e) { return jids; - } catch (Exception e) { - return jids; - } finally { - if (cursor != null) { - cursor.close(); - } } + return jids; } private List getAccounts(SQLiteDatabase db) { - List list = new ArrayList<>(); - Cursor cursor = db.query(Account.TABLENAME, null, null, null, null, - null, null); - while (cursor.moveToNext()) { - list.add(Account.fromCursor(cursor)); + final List list = new ArrayList<>(); + try (final Cursor cursor = + db.query(Account.TABLENAME, null, null, null, null, null, null)) { + while (cursor != null && cursor.moveToNext()) { + list.add(Account.fromCursor(cursor)); + } } - cursor.close(); return list; } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index bb954f45e..fb7aec14d 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -87,19 +87,19 @@ public class MessageAdapter extends ArrayAdapter { private OnContactPictureClicked mOnContactPictureClickedListener; private OnContactPictureLongClicked mOnContactPictureLongClickedListener; private boolean mUseGreenBackground = false; - private boolean mForceNames = false; + private final boolean mForceNames; - public MessageAdapter(XmppActivity activity, List messages) { + public MessageAdapter(final XmppActivity activity, final List messages, final boolean forceNames) { super(activity, 0, messages); this.audioPlayer = new AudioPlayer(this); this.activity = activity; metrics = getContext().getResources().getDisplayMetrics(); updatePreferences(); + this.mForceNames = forceNames; } - public MessageAdapter(XmppActivity activity, List messages, boolean forceNames) { - this(activity, messages); - mForceNames = forceNames; + public MessageAdapter(final XmppActivity activity, final List messages) { + this(activity, messages, false); } private static void resetClickListener(View... views) {