Remove GAL lookup dependency on running ExchangeService

Change-Id: I4ff4017bcbb29862e07fde63e2ac01e3552ccd83
This commit is contained in:
Marc Blank 2010-09-15 13:10:25 -07:00
parent 0b24baa130
commit 74444e73c2
3 changed files with 62 additions and 9 deletions

View File

@ -811,7 +811,12 @@ public class EasSyncService extends AbstractSyncService {
* TODO: figure out why sendHttpClientPost() hangs - possibly pool exhaustion
*/
static public GalResult searchGal(Context context, long accountId, String filter, int limit) {
// Try to get the cached account from ExchangeService (saves a database access)
Account acct = ExchangeService.getAccountById(accountId);
if (acct == null) {
// ExchangeService isn't running; get the account directly from EmailProvider
acct = Account.restoreAccountWithId(context, accountId);
}
if (acct != null) {
HostAuth ha = HostAuth.restoreHostAuthWithId(context, acct.mHostAuthKeyRecv);
EasSyncService svc = new EasSyncService("%GalLookupk%");

View File

@ -17,16 +17,19 @@
package com.android.exchange.provider;
import com.android.email.R;
import com.android.email.Utility;
import com.android.email.VendorPolicyLoader;
import com.android.email.mail.PackedString;
import com.android.email.provider.EmailContent;
import com.android.email.provider.EmailContent.Account;
import com.android.email.provider.EmailContent.AccountColumns;
import com.android.exchange.EasSyncService;
import com.android.exchange.ExchangeService;
import com.android.exchange.provider.GalResult.GalData;
import android.accounts.AccountManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
@ -64,6 +67,7 @@ public class ExchangeDirectoryProvider extends ContentProvider {
private static final int GAL_EMAIL_FILTER = GAL_BASE + 4;
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
/*package*/ final HashMap<String, Long> mAccountIdMap = new HashMap<String, Long>();
static {
sURIMatcher.addURI(EXCHANGE_GAL_AUTHORITY, "directories", GAL_DIRECTORIES);
@ -169,6 +173,24 @@ public class ExchangeDirectoryProvider extends ContentProvider {
}
}
/**
* Find the record id of an Account, given its name (email address)
* @param accountName the name of the account
* @return the record id of the Account, or -1 if not found
*/
/*package*/ long getAccountIdByName(Context context, String accountName) {
Long accountId = mAccountIdMap.get(accountName);
if (accountId == null) {
accountId = Utility.getFirstRowLong(context, Account.CONTENT_URI,
EmailContent.ID_PROJECTION, AccountColumns.EMAIL_ADDRESS + "=?",
new String[] {accountName}, null, EmailContent.ID_PROJECTION_COLUMN , -1L);
if (accountId != -1) {
mAccountIdMap.put(accountName, accountId);
}
}
return accountId;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
@ -242,15 +264,17 @@ public class ExchangeDirectoryProvider extends ContentProvider {
}
}
Account account = ExchangeService.getAccountByName(accountName);
if (account == null) {
return null;
}
long callingId = Binder.clearCallingIdentity();
try {
// Find the account id to pass along to EasSyncService
long accountId = getAccountIdByName(getContext(), accountName);
if (accountId == -1) {
// The account was deleted?
return null;
}
// Get results from the Exchange account
GalResult galResult = EasSyncService.searchGal(getContext(), account.mId,
GalResult galResult = EasSyncService.searchGal(getContext(), accountId,
filter, limit);
if (galResult != null) {
return buildGalResultCursor(projection, galResult);

View File

@ -17,20 +17,28 @@
package com.android.exchange.provider;
import com.android.email.mail.PackedString;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.ProviderTestUtils;
import com.android.email.provider.EmailContent.Account;
import com.android.exchange.provider.GalResult.GalData;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.test.AndroidTestCase;
import android.test.ProviderTestCase2;
/**
* You can run this entire test case with:
* runtest -c com.android.exchange.provider.ExchangeDirectoryProviderTests email
*/
public class ExchangeDirectoryProviderTests extends AndroidTestCase {
public class ExchangeDirectoryProviderTests extends ProviderTestCase2<EmailProvider> {
public ExchangeDirectoryProviderTests() {
super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY);
}
// Create a test projection; we should only get back values for display name and email address
private static final String[] GAL_RESULT_PROJECTION =
@ -120,4 +128,20 @@ public class ExchangeDirectoryProviderTests extends AndroidTestCase {
assertEquals(EXPECTED_DISPLAY_NAMES[i], ps.get(GalData.DISPLAY_NAME));
}
}
public void testGetAccountIdByName() {
Context context = getMockContext();
ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
// Nothing up my sleeve
assertNull(provider.mAccountIdMap.get("foo@android.com"));
assertNull(provider.mAccountIdMap.get("bar@android.com"));
// Create accounts; the email addresses will be the first argument + "@android.com"
Account acctFoo = ProviderTestUtils.setupAccount("foo", true, context);
Account acctBar = ProviderTestUtils.setupAccount("bar", true, context);
// Make sure we can retrieve them, and that the map is populated
assertEquals(acctFoo.mId, provider.getAccountIdByName(context, "foo@android.com"));
assertEquals(acctBar.mId, provider.getAccountIdByName(context, "bar@android.com"));
assertEquals((Long)acctFoo.mId, provider.mAccountIdMap.get("foo@android.com"));
assertEquals((Long)acctBar.mId, provider.mAccountIdMap.get("bar@android.com"));
}
}