diff --git a/src/com/android/email/Utility.java b/src/com/android/email/Utility.java index c2a1105f1..000eeb874 100644 --- a/src/com/android/email/Utility.java +++ b/src/com/android/email/Utility.java @@ -16,7 +16,6 @@ package com.android.email; -import com.android.email.activity.Welcome; import com.android.email.provider.EmailContent; import com.android.email.provider.EmailContent.Account; import com.android.email.provider.EmailContent.AccountColumns; @@ -851,6 +850,21 @@ public class Utility { } }; + /** + * @return if {@code original} is to the EmailProvider, add "?limit=1". Otherwise just returns + * {@code original}. + * + * Other providers don't support the limit param. Also, changing URI passed from other apps + * can cause permission errors. + */ + /* package */ static Uri buildLimitOneUri(Uri original) { + if ("content".equals(original.getScheme()) && + EmailContent.AUTHORITY.equals(original.getAuthority())) { + return EmailContent.uriWithLimit(original, 1); + } + return original; + } + /** * @return a generic in column {@code column} of the first result row, if the query returns at * least 1 row. Otherwise returns {@code defaultValue}. @@ -859,7 +873,7 @@ public class Utility { String[] projection, String selection, String[] selectionArgs, String sortOrder, int column, T defaultValue, CursorGetter getter) { // Use PARAMETER_LIMIT to restrict the query to the single row we need - uri = uri.buildUpon().appendQueryParameter(EmailContent.PARAMETER_LIMIT, "1").build(); + uri = buildLimitOneUri(uri); Cursor c = context.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); if (c != null) { diff --git a/tests/src/com/android/email/UtilityMediumTests.java b/tests/src/com/android/email/UtilityMediumTests.java index 9b95fddcf..2a655c2a6 100644 --- a/tests/src/com/android/email/UtilityMediumTests.java +++ b/tests/src/com/android/email/UtilityMediumTests.java @@ -26,6 +26,7 @@ import com.android.email.provider.EmailProvider; import com.android.email.provider.ProviderTestUtils; import android.content.Context; +import android.net.Uri; import android.test.ProviderTestCase2; import android.test.suitebuilder.annotation.MediumTest; @@ -121,6 +122,20 @@ public class UtilityMediumTests extends ProviderTestCase2 { assertTrue(Utility.attachmentExists(mMockContext, attachment)); } + public void testBuildLimitOneUri() { + // EmailProvider supports "?limit=" + assertEquals(Uri.parse("content://com.android.email.provider?limit=1"), + Utility.buildLimitOneUri(Uri.parse("content://com.android.email.provider"))); + + // Others don't -- so don't add it. + assertEquals(Uri.parse("content://com.android.email.attachmentprovider"), + Utility.buildLimitOneUri(Uri.parse("content://com.android.email.attachmentprovider" + ))); + assertEquals(Uri.parse("content://gmail-ls/android@gmail.com"), + Utility.buildLimitOneUri(Uri.parse("content://gmail-ls/android@gmail.com" + ))); + } + public void testGetFirstRowLong() { Account account1 = ProviderTestUtils.setupAccount("1", true, mMockContext); Account account2 = ProviderTestUtils.setupAccount("X1", true, mMockContext); diff --git a/tests/src/com/android/email/activity/ContactStatusLoaderTest.java b/tests/src/com/android/email/activity/ContactStatusLoaderTest.java index bef10acc8..9661d1bae 100644 --- a/tests/src/com/android/email/activity/ContactStatusLoaderTest.java +++ b/tests/src/com/android/email/activity/ContactStatusLoaderTest.java @@ -113,7 +113,7 @@ public class ContactStatusLoaderTest assertTrue(mProvider.mUris.size() >= 2); assertEquals("content://com.android.contacts/data/emails/lookup/a%40b.c", mProvider.mUris.get(0)); - assertEquals("content://com.android.contacts/data/12345?limit=1", + assertEquals("content://com.android.contacts/data/12345", mProvider.mUris.get(1)); // Check result