From d36d911fac517f7a82f5a341290f67e2dd04979d Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Wed, 8 Sep 2010 17:57:21 -0700 Subject: [PATCH] Add a flag to check the current thread on db accesses Added Email.DEBUG_THREAD_CHECK. If true, EmailProvider warns if certain methods are called on the UI thread. Change-Id: I6db9e45f2e449a31850c223fc9eec0fb9a575cb1 --- src/com/android/email/Email.java | 14 ++++++++++++++ src/com/android/email/provider/EmailProvider.java | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/com/android/email/Email.java b/src/com/android/email/Email.java index 0d8a2ed71..40a3f8699 100644 --- a/src/com/android/email/Email.java +++ b/src/com/android/email/Email.java @@ -69,6 +69,11 @@ public class Email extends Application { */ public static final boolean LOGD = false; + /** + * If true, enable the UI thread check when accessing the filesystem. + */ + public static final boolean DEBUG_THREAD_CHECK = false; // DON'T SUBMIT WITH TRUE + /** * The MIME type(s) of attachments we're willing to send via attachments. * @@ -179,6 +184,8 @@ public class Email extends Application { private static File sTempDirectory; + private static Thread sUiThread; + /* package for testing */ static int getColorIndexFromAccountId(long accountId) { // Account id is 1-based, so - 1. // Use abs so that it won't possibly return negative. @@ -280,6 +287,7 @@ public class Email extends Application { @Override public void onCreate() { super.onCreate(); + sUiThread = Thread.currentThread(); Preferences prefs = Preferences.getPreferences(this); DEBUG = prefs.getEnableDebugLogging(); setTempDirectory(this); @@ -331,4 +339,10 @@ public class Email extends Application { public static synchronized boolean getNotifyUiAccountsChanged() { return sAccountsChangedNotification; } + + public static void warnIfUiThread() { + if (Thread.currentThread().equals(sUiThread)) { + Log.w(Email.LOG_TAG, "Method called on the UI thread", new Exception("STACK TRACE")); + } + } } diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index 5e3ce6fc2..6e8579e1b 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -884,6 +884,7 @@ public class EmailProvider extends ContentProvider { @Override public int delete(Uri uri, String selection, String[] selectionArgs) { + if (Email.DEBUG_THREAD_CHECK) Email.warnIfUiThread(); final int match = sURIMatcher.match(uri); Context context = getContext(); // Pick the correct database for this operation @@ -1029,6 +1030,7 @@ public class EmailProvider extends ContentProvider { @Override public Uri insert(Uri uri, ContentValues values) { + if (Email.DEBUG_THREAD_CHECK) Email.warnIfUiThread(); int match = sURIMatcher.match(uri); Context context = getContext(); // See the comment at delete(), above @@ -1142,6 +1144,7 @@ public class EmailProvider extends ContentProvider { @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + if (Email.DEBUG_THREAD_CHECK) Email.warnIfUiThread(); Cursor c = null; Uri notificationUri = EmailContent.CONTENT_URI; int match = sURIMatcher.match(uri); @@ -1237,6 +1240,7 @@ public class EmailProvider extends ContentProvider { @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + if (Email.DEBUG_THREAD_CHECK) Email.warnIfUiThread(); int match = sURIMatcher.match(uri); Context context = getContext(); // See the comment at delete(), above