Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Don't load contacts on UI thread
Browse files Browse the repository at this point in the history
There's a route through a Conversation constructor that will
result in the conversation's contacts getting loaded on the
UI thread. Add an explicit "allowQuery" parameter to give callers
the ability to throttle this behavior. Fixes bug 2280762.

Conflicts:

	src/com/android/mms/data/Conversation.java
	src/com/android/mms/ui/ComposeMessageActivity.java
  • Loading branch information
Tom Taylor committed Jan 12, 2010
1 parent ef9af5d commit 68f969d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
42 changes: 20 additions & 22 deletions src/com/android/mms/data/Conversation.java
Expand Up @@ -73,9 +73,9 @@ private Conversation(Context context) {
mThreadId = 0;
}

private Conversation(Context context, long threadId) {
private Conversation(Context context, long threadId, boolean allowQuery) {
mContext = context;
if (!loadFromThreadId(threadId)) {
if (!loadFromThreadId(threadId, allowQuery)) {
mRecipients = new ContactList();
mThreadId = 0;
}
Expand All @@ -98,27 +98,25 @@ public static Conversation createNew(Context context) {
/**
* Find the conversation matching the provided thread ID.
*/
public static Conversation get(Context context, long threadId) {
synchronized (Cache.getInstance()) {
Conversation conv = Cache.get(threadId);
if (conv != null)
return conv;

conv = new Conversation(context, threadId);
try {
Cache.put(conv);
} catch (IllegalStateException e) {
LogTag.error("Tried to add duplicate Conversation to Cache");
}
public static Conversation get(Context context, long threadId, boolean allowQuery) {
Conversation conv = Cache.get(threadId);
if (conv != null)
return conv;

conv = new Conversation(context, threadId, allowQuery);
try {
Cache.put(conv);
} catch (IllegalStateException e) {
LogTag.error("Tried to add duplicate Conversation to Cache");
}
return conv;
}

/**
* Find the conversation matching the provided recipient set.
* When called with an empty recipient list, equivalent to {@link createEmpty}.
*/
public static Conversation get(Context context, ContactList recipients) {
public static Conversation get(Context context, ContactList recipients, boolean allowQuery) {
// If there are no recipients in the list, make a new conversation.
if (recipients.size() < 1) {
return createNew(context);
Expand All @@ -130,7 +128,7 @@ public static Conversation get(Context context, ContactList recipients) {
return conv;

long threadId = getOrCreateThreadId(context, recipients);
conv = new Conversation(context, threadId);
conv = new Conversation(context, threadId, allowQuery);

try {
Cache.put(conv);
Expand All @@ -148,7 +146,7 @@ public static Conversation get(Context context, ContactList recipients) {
* {@value sms:+12124797990}.
* When called with a null Uri, equivalent to {@link createEmpty}.
*/
public static Conversation get(Context context, Uri uri) {
public static Conversation get(Context context, Uri uri, boolean allowQuery) {
if (uri == null) {
return createNew(context);
}
Expand All @@ -163,15 +161,15 @@ public static Conversation get(Context context, Uri uri) {
if (DEBUG) {
Log.v(TAG, "Conversation get threadId: " + threadId);
}
return get(context, threadId);
return get(context, threadId, allowQuery);
} catch (NumberFormatException exception) {
LogTag.error("Invalid URI: " + uri);
}
}

String recipient = uri.getSchemeSpecificPart();
return get(context, ContactList.getByNumbers(recipient,
false /* don't block */, true /* replace number */));
allowQuery /* don't block */, true /* replace number */), allowQuery);
}

/**
Expand Down Expand Up @@ -519,7 +517,7 @@ private static void fillFromCursor(Context context, Conversation conv,
// Fill in as much of the conversation as we can before doing the slow stuff of looking
// up the contacts associated with this conversation.
String recipientIds = c.getString(RECIPIENT_IDS);
ContactList recipients = ContactList.getByIds(recipientIds, allowQuery);;
ContactList recipients = ContactList.getByIds(recipientIds, allowQuery);
synchronized (conv) {
conv.mRecipients = recipients;
}
Expand Down Expand Up @@ -725,12 +723,12 @@ private static void cacheAllThreads(Context context) {
Cache.keepOnly(threadsOnDisk);
}

private boolean loadFromThreadId(long threadId) {
private boolean loadFromThreadId(long threadId, boolean allowQuery) {
Cursor c = mContext.getContentResolver().query(sAllThreadsUri, ALL_THREADS_PROJECTION,
"_id=" + Long.toString(threadId), null, null);
try {
if (c.moveToFirst()) {
fillFromCursor(mContext, this, c, true);
fillFromCursor(mContext, this, c, allowQuery);
} else {
LogTag.error("loadFromThreadId: Can't find thread ID " + threadId);
return false;
Expand Down
24 changes: 17 additions & 7 deletions src/com/android/mms/ui/ComposeMessageActivity.java
Expand Up @@ -1760,7 +1760,7 @@ protected void onNewIntent(Intent intent) {

boolean sameThread = false;
if (threadId > 0) {
conversation = Conversation.get(this, threadId);
conversation = Conversation.get(this, threadId, false);
} else {
if (mConversation.getThreadId() == 0) {
// We've got a draft. See if the new intent's recipient is the same as
Expand All @@ -1772,7 +1772,7 @@ protected void onNewIntent(Intent intent) {
if (!sameThread) {
// Otherwise, try to get a conversation based on the
// data URI passed to our intent.
conversation = Conversation.get(this, intentUri);
conversation = Conversation.get(this, intentUri, false);
}
}

Expand All @@ -1786,6 +1786,7 @@ protected void onNewIntent(Intent intent) {
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
log("onNewIntent: same conversation");
}
addRecipientsListeners();
} else {
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
log("onNewIntent: different conversation, initialize...");
Expand Down Expand Up @@ -2917,7 +2918,8 @@ private void initActivityState(Bundle bundle, Intent intent) {
String recipients = bundle.getString("recipients");
mConversation = Conversation.get(this,
ContactList.getByNumbers(recipients,
false /* don't block */, true /* replace number */));
false /* don't block */, true /* replace number */), false);
addRecipientsListeners();
mExitOnSent = bundle.getBoolean("exit_on_sent", false);
mWorkingMessage.readStateFromBundle(bundle);
return;
Expand All @@ -2927,24 +2929,25 @@ private void initActivityState(Bundle bundle, Intent intent) {
// conversation.
long threadId = intent.getLongExtra("thread_id", 0);
if (threadId > 0) {
mConversation = Conversation.get(this, threadId);
mConversation = Conversation.get(this, threadId, false);
} else {
Uri intentData = intent.getData();

if (intentData != null) {
// try to get a conversation based on the data URI passed to our intent.
mConversation = Conversation.get(this, intent.getData());
mConversation = Conversation.get(this, intent.getData(), false);
} else {
// special intent extra parameter to specify the address
String address = intent.getStringExtra("address");
if (!TextUtils.isEmpty(address)) {
mConversation = Conversation.get(this, ContactList.getByNumbers(address,
false /* don't block */, true /* replace number */));
false /* don't block */, true /* replace number */), false);
} else {
mConversation = Conversation.createNew(this);
}
}
}
addRecipientsListeners();

mExitOnSent = intent.getBooleanExtra("exit_on_sent", false);
mWorkingMessage.setText(intent.getStringExtra("sms_body"));
Expand Down Expand Up @@ -3161,18 +3164,25 @@ public void onUpdate(final Contact updated) {
// Using an existing handler for the post, rather than conjuring up a new one.
mMessageListItemHandler.post(new Runnable() {
public void run() {
ContactList recipients = isRecipientsEditorVisible() ?
mRecipientsEditor.constructContactsFromInput() : getRecipients();
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
log("[CMA] onUpdate contact updated: " + updated);
log("[CMA] onUpdate recipients: " + recipients);
}
ContactList recipients = getRecipients();
if (recipients.size() == 1) {
updatePresence(recipients.get(0));
} else {
updatePresence(null);
}

// The contact information for one (or more) of the recipients has changed.
// Rebuild the message list so each MessageItem will get the last contact info.
ComposeMessageActivity.this.mMsgListAdapter.notifyDataSetChanged();

if (mRecipientsEditor != null) {
mRecipientsEditor.populate(recipients);
}
}
});
}
Expand Down

0 comments on commit 68f969d

Please sign in to comment.