From b3707021b1af8dffa34fc23be91120c497756571 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Thu, 23 Apr 2015 06:05:15 -0500 Subject: [PATCH] maintain one ContactsDatabase instance per Loader Fixes #3004 Closes #3041 Closes #3034 // FREEBIE --- .../PushContactSelectionListFragment.java | 6 ---- .../contacts/ContactsCursorLoader.java | 30 +++++++++++++++++-- .../securesms/contacts/ContactsDatabase.java | 14 +-------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/org/thoughtcrime/securesms/PushContactSelectionListFragment.java b/src/org/thoughtcrime/securesms/PushContactSelectionListFragment.java index 87f15a2f19..b5b76c18ef 100644 --- a/src/org/thoughtcrime/securesms/PushContactSelectionListFragment.java +++ b/src/org/thoughtcrime/securesms/PushContactSelectionListFragment.java @@ -86,12 +86,6 @@ public class PushContactSelectionListFragment extends Fragment super.onPause(); } - @Override - public void onDestroyView() { - super.onDestroyView(); - ContactsDatabase.destroyInstance(); - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.push_contact_selection_list_activity, container, false); diff --git a/src/org/thoughtcrime/securesms/contacts/ContactsCursorLoader.java b/src/org/thoughtcrime/securesms/contacts/ContactsCursorLoader.java index 67fdd6890c..9c57bbd42c 100644 --- a/src/org/thoughtcrime/securesms/contacts/ContactsCursorLoader.java +++ b/src/org/thoughtcrime/securesms/contacts/ContactsCursorLoader.java @@ -19,6 +19,11 @@ package org.thoughtcrime.securesms.contacts; import android.content.Context; import android.database.Cursor; import android.support.v4.content.CursorLoader; +import android.util.Log; + +import junit.framework.Assert; + +import java.util.concurrent.Semaphore; /** * CursorLoader that initializes a ContactsDatabase instance @@ -26,10 +31,13 @@ import android.support.v4.content.CursorLoader; * @author Jake McGinty */ public class ContactsCursorLoader extends CursorLoader { + private static final String TAG = ContactsCursorLoader.class.getSimpleName(); + private static final int DB_PERMITS = 100; private final Context context; private final String filter; private final boolean pushOnly; + private final Semaphore dbSemaphore = new Semaphore(DB_PERMITS); private ContactsDatabase db; public ContactsCursorLoader(Context context, String filter, boolean pushOnly) { @@ -37,17 +45,33 @@ public class ContactsCursorLoader extends CursorLoader { this.context = context; this.filter = filter; this.pushOnly = pushOnly; + this.db = new ContactsDatabase(context); } @Override public Cursor loadInBackground() { - ContactsDatabase.destroyInstance(); - db = ContactsDatabase.getInstance(context); - return db.query(filter, pushOnly); + try { + dbSemaphore.acquire(); + return db.query(filter, pushOnly); + } catch (InterruptedException ie) { + throw new AssertionError(ie); + } finally { + dbSemaphore.release(); + } } @Override public void onReset() { + Log.w(TAG, "onReset()"); + try { + dbSemaphore.acquire(DB_PERMITS); + db.close(); + db = new ContactsDatabase(context); + } catch (InterruptedException ie) { + throw new AssertionError(ie); + } finally { + dbSemaphore.release(DB_PERMITS); + } super.onReset(); } } diff --git a/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java b/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java index e3223d19a5..a999a907d2 100644 --- a/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java +++ b/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java @@ -75,19 +75,7 @@ public class ContactsDatabase { public static final int PUSH_TYPE = 1; public static final int GROUP_TYPE = 2; - private static ContactsDatabase instance = null; - - public synchronized static ContactsDatabase getInstance(Context context) { - if (instance == null) instance = new ContactsDatabase(context); - return instance; - } - - public synchronized static void destroyInstance() { - if (instance != null) instance.close(); - instance = null; - } - - private ContactsDatabase(Context context) { + public ContactsDatabase(Context context) { this.dbHelper = new DatabaseOpenHelper(context); this.context = context; }