Merge pull request #93 from andrewgaul/cursor-close-quietly

Introduce and use Utility.closeQuietly(Cursor)
This commit is contained in:
Andrew Chen 2011-11-04 16:20:22 -07:00
commit 4a1046a893
5 changed files with 39 additions and 78 deletions

View file

@ -231,12 +231,8 @@ public class ContactsSdk3_4 extends com.fsck.k9.helper.Contacts {
} catch (Exception e) { } catch (Exception e) {
Log.e(K9.LOG_TAG, "Failed to get email data", e); Log.e(K9.LOG_TAG, "Failed to get email data", e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close(); Utility.closeQuietly(cursor2);
}
if (cursor2 != null) {
cursor2.close();
}
} }
return email; return email;

View file

@ -201,9 +201,7 @@ public class ContactsSdk5 extends com.fsck.k9.helper.Contacts {
} catch (Exception e) { } catch (Exception e) {
Log.e(K9.LOG_TAG, "Failed to get email data", e); Log.e(K9.LOG_TAG, "Failed to get email data", e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return email; return email;

View file

@ -1,6 +1,7 @@
package com.fsck.k9.helper; package com.fsck.k9.helper;
import android.database.Cursor;
import android.text.Editable; import android.text.Editable;
import android.util.Log; import android.util.Log;
import android.widget.EditText; import android.widget.EditText;
@ -585,4 +586,16 @@ public class Utility {
} }
return false; return false;
} }
/**
* Unconditionally close a Cursor. Equivalent to {@link Cursor#close()},
* if cursor is non-null. This is typically used in finally blocks.
*
* @param cursor cursor to close
*/
public static void closeQuietly(final Cursor cursor) {
if (cursor != null) {
cursor.close();
}
}
} }

View file

@ -309,9 +309,7 @@ public class LocalStore extends Store implements Serializable {
Log.e(K9.LOG_TAG, "Exception while upgrading database to v41. folder classes may have vanished", e); Log.e(K9.LOG_TAG, "Exception while upgrading database to v41. folder classes may have vanished", e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
if (db.getVersion() == 41) { if (db.getVersion() == 41) {
@ -524,9 +522,7 @@ public class LocalStore extends Store implements Serializable {
cursor.moveToFirst(); cursor.moveToFirst();
return cursor.getInt(0); // message count return cursor.getInt(0); // message count
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -600,9 +596,7 @@ public class LocalStore extends Store implements Serializable {
stats.flaggedMessageCount = cursor.getInt(1); stats.flaggedMessageCount = cursor.getInt(1);
return null; return null;
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -619,9 +613,7 @@ public class LocalStore extends Store implements Serializable {
cursor.moveToFirst(); cursor.moveToFirst();
return cursor.getInt(0); // folder count return cursor.getInt(0); // folder count
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -654,9 +646,7 @@ public class LocalStore extends Store implements Serializable {
} catch (MessagingException e) { } catch (MessagingException e) {
throw new WrappedException(e); throw new WrappedException(e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -723,9 +713,7 @@ public class LocalStore extends Store implements Serializable {
} }
} }
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
if (!force) { if (!force) {
@ -793,9 +781,7 @@ public class LocalStore extends Store implements Serializable {
} }
return commands; return commands;
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -1005,9 +991,7 @@ public class LocalStore extends Store implements Serializable {
} catch (Exception e) { } catch (Exception e) {
Log.d(K9.LOG_TAG, "Got an exception", e); Log.d(K9.LOG_TAG, "Got an exception", e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return i; return i;
} }
@ -1049,9 +1033,7 @@ public class LocalStore extends Store implements Serializable {
attachmentInfo.type = type; attachmentInfo.type = type;
return attachmentInfo; return attachmentInfo;
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -1185,9 +1167,7 @@ public class LocalStore extends Store implements Serializable {
} catch (MessagingException e) { } catch (MessagingException e) {
throw new WrappedException(e); throw new WrappedException(e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return null; return null;
} }
@ -1251,9 +1231,7 @@ public class LocalStore extends Store implements Serializable {
return false; return false;
} }
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -1309,9 +1287,7 @@ public class LocalStore extends Store implements Serializable {
cursor.moveToFirst(); cursor.moveToFirst();
return cursor.getInt(0); //messagecount return cursor.getInt(0); //messagecount
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
}); });
@ -1662,9 +1638,7 @@ public class LocalStore extends Store implements Serializable {
} catch (Exception e) { } catch (Exception e) {
Log.e(K9.LOG_TAG, "Exception fetching message:", e); Log.e(K9.LOG_TAG, "Exception fetching message:", e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
try { try {
@ -1729,9 +1703,7 @@ public class LocalStore extends Store implements Serializable {
mp.addBodyPart(bp); mp.addBodyPart(bp);
} }
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
if (mp.getCount() == 0) { if (mp.getCount() == 0) {
@ -1819,9 +1791,7 @@ public class LocalStore extends Store implements Serializable {
popMessages.get(id).addHeader(name, value); popMessages.get(id).addHeader(name, value);
} }
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return null; return null;
} }
@ -1852,9 +1822,7 @@ public class LocalStore extends Store implements Serializable {
} }
message.populateFromGetMessageCursor(cursor); message.populateFromGetMessageCursor(cursor);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return message; return message;
} catch (MessagingException e) { } catch (MessagingException e) {
@ -2491,9 +2459,7 @@ public class LocalStore extends Store implements Serializable {
} }
} }
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
} }
@ -2697,9 +2663,7 @@ public class LocalStore extends Store implements Serializable {
} }
} }
} finally { } finally {
if (attachmentsCursor != null) { Utility.closeQuietly(attachmentsCursor);
attachmentsCursor.close();
}
} }
return null; return null;
} }
@ -2725,9 +2689,7 @@ public class LocalStore extends Store implements Serializable {
} catch (MessagingException e) { } catch (MessagingException e) {
throw new WrappedException(e); throw new WrappedException(e);
} finally { } finally {
if (messagesCursor != null) { Utility.closeQuietly(messagesCursor);
messagesCursor.close();
}
} }
return null; return null;
} }
@ -2840,9 +2802,7 @@ public class LocalStore extends Store implements Serializable {
} catch (Exception e) { } catch (Exception e) {
Log.e(K9.LOG_TAG, "Unable to updateLastUid: ", e); Log.e(K9.LOG_TAG, "Unable to updateLastUid: ", e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return null; return null;
} }
@ -2867,9 +2827,7 @@ public class LocalStore extends Store implements Serializable {
} catch (Exception e) { } catch (Exception e) {
Log.e(K9.LOG_TAG, "Unable to fetch oldest message date: ", e); Log.e(K9.LOG_TAG, "Unable to fetch oldest message date: ", e);
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return null; return null;
} }

View file

@ -185,9 +185,7 @@ public class Storage implements SharedPreferences {
storage.put(key, value); storage.put(key, value);
} }
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
if (mDb != null) { if (mDb != null) {
mDb.close(); mDb.close();
} }
@ -384,9 +382,7 @@ public class Storage implements SharedPreferences {
} }
} }
} finally { } finally {
if (cursor != null) { Utility.closeQuietly(cursor);
cursor.close();
}
} }
return value; return value;