1
0
mirror of https://github.com/etesync/android synced 2024-12-23 07:08:16 +00:00

throw RecordNotFoundExceptions only when specific records are requested

This commit is contained in:
rfc2822 2013-12-21 20:54:54 +01:00
parent 8a651f135b
commit f4bb3639e6
3 changed files with 37 additions and 26 deletions

View File

@ -144,7 +144,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
Contact c = (Contact)res;
try {
@Cleanup("close") Cursor cursor = providerClient.query(ContentUris.withAppendedId(entriesURI(), c.getLocalID()),
@Cleanup Cursor cursor = providerClient.query(ContentUris.withAppendedId(entriesURI(), c.getLocalID()),
new String[] { entryColumnUID(), RawContacts.STARRED }, null, null, null);
if (cursor != null && cursor.moveToNext()) {
c.setUid(cursor.getString(0));
@ -169,7 +169,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
private void populateStructuredName(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] {
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] {
/* 0 */ StructuredName.DISPLAY_NAME, StructuredName.PREFIX, StructuredName.GIVEN_NAME,
/* 3 */ StructuredName.MIDDLE_NAME, StructuredName.FAMILY_NAME, StructuredName.SUFFIX,
/* 6 */ StructuredName.PHONETIC_GIVEN_NAME, StructuredName.PHONETIC_MIDDLE_NAME, StructuredName.PHONETIC_FAMILY_NAME
@ -191,7 +191,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populatePhoneNumbers(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { Phone.TYPE, Phone.LABEL, Phone.NUMBER },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Phone.TYPE, Phone.LABEL, Phone.NUMBER },
Phone.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), Phone.CONTENT_ITEM_TYPE }, null);
while (cursor != null && cursor.moveToNext()) {
@ -268,7 +268,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populateEmailAddresses(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { Email.TYPE, Email.ADDRESS, Email.LABEL },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Email.TYPE, Email.ADDRESS, Email.LABEL },
Email.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), Email.CONTENT_ITEM_TYPE }, null);
while (cursor != null && cursor.moveToNext()) {
@ -306,7 +306,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populateOrganization(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { Organization.COMPANY, Organization.TITLE },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Organization.COMPANY, Organization.TITLE },
Photo.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), Organization.CONTENT_ITEM_TYPE }, null);
if (cursor != null && cursor.moveToNext()) {
@ -320,7 +320,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populateIMPPs(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { Im.DATA, Im.TYPE, Im.LABEL, Im.PROTOCOL, Im.CUSTOM_PROTOCOL },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Im.DATA, Im.TYPE, Im.LABEL, Im.PROTOCOL, Im.CUSTOM_PROTOCOL },
Photo.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), Im.CONTENT_ITEM_TYPE }, null);
while (cursor != null && cursor.moveToNext()) {
@ -378,7 +378,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populateNickname(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { Nickname.NAME },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Nickname.NAME },
Nickname.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), Nickname.CONTENT_ITEM_TYPE }, null);
if (cursor != null && cursor.moveToNext())
@ -386,7 +386,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populateNote(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { Note.NOTE },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Note.NOTE },
Website.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), Note.CONTENT_ITEM_TYPE }, null);
if (cursor != null && cursor.moveToNext())
@ -394,7 +394,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populatePostalAddresses(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] {
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] {
/* 0 */ StructuredPostal.FORMATTED_ADDRESS, StructuredPostal.TYPE, StructuredPostal.LABEL,
/* 3 */ StructuredPostal.STREET, StructuredPostal.POBOX, StructuredPostal.NEIGHBORHOOD,
/* 6 */ StructuredPostal.CITY, StructuredPostal.REGION, StructuredPostal.POSTCODE,
@ -430,7 +430,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populateURLs(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { Website.URL },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Website.URL },
Website.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), Website.CONTENT_ITEM_TYPE }, null);
if (cursor != null && cursor.moveToNext())
@ -438,7 +438,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
protected void populateEvents(Contact c) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(dataURI(), new String[] { CommonDataKinds.Event.TYPE, CommonDataKinds.Event.START_DATE },
@Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { CommonDataKinds.Event.TYPE, CommonDataKinds.Event.START_DATE },
Photo.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[] { String.valueOf(c.getLocalID()), CommonDataKinds.Event.CONTENT_ITEM_TYPE }, null);
while (cursor != null && cursor.moveToNext()) {

View File

@ -138,7 +138,7 @@ public class LocalCalendar extends LocalCollection<Event> {
}
public static LocalCalendar[] findAll(Account account, ContentProviderClient providerClient) throws RemoteException {
@Cleanup("close") Cursor cursor = providerClient.query(calendarsURI(account),
@Cleanup Cursor cursor = providerClient.query(calendarsURI(account),
new String[] { Calendars._ID, Calendars.NAME, COLLECTION_COLUMN_CTAG },
Calendars.DELETED + "=0 AND " + Calendars.SYNC_EVENTS + "=1", null, null);
@ -199,7 +199,7 @@ public class LocalCalendar extends LocalCollection<Event> {
Event e = (Event)resource;
try {
@Cleanup("close") Cursor cursor = providerClient.query(ContentUris.withAppendedId(entriesURI(), e.getLocalID()),
@Cleanup Cursor cursor = providerClient.query(ContentUris.withAppendedId(entriesURI(), e.getLocalID()),
new String[] {
/* 0 */ Events.TITLE, Events.EVENT_LOCATION, Events.DESCRIPTION,
/* 3 */ Events.DTSTART, Events.DTEND, Events.EVENT_TIMEZONE, Events.EVENT_END_TIMEZONE, Events.ALL_DAY,
@ -317,7 +317,7 @@ public class LocalCalendar extends LocalCollection<Event> {
Uri attendeesUri = Attendees.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.build();
@Cleanup("close") Cursor c = providerClient.query(attendeesUri, new String[] {
@Cleanup Cursor c = providerClient.query(attendeesUri, new String[] {
/* 0 */ Attendees.ATTENDEE_EMAIL, Attendees.ATTENDEE_NAME, Attendees.ATTENDEE_TYPE,
/* 3 */ Attendees.ATTENDEE_RELATIONSHIP, Attendees.STATUS
}, Attendees.EVENT_ID + "=?", new String[] { String.valueOf(e.getLocalID()) }, null);
@ -377,7 +377,7 @@ public class LocalCalendar extends LocalCollection<Event> {
Uri remindersUri = Reminders.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.build();
@Cleanup("close") Cursor c = providerClient.query(remindersUri, new String[] {
@Cleanup Cursor c = providerClient.query(remindersUri, new String[] {
/* 0 */ Reminders.MINUTES, Reminders.METHOD
}, Reminders.EVENT_ID + "=?", new String[] { String.valueOf(e.getLocalID()) }, null);
while (c != null && c.moveToNext()) {

View File

@ -69,14 +69,18 @@ public abstract class LocalCollection<T extends Resource> {
if (entryColumnParentID() != null)
where += " AND " + entryColumnParentID() + "=" + String.valueOf(getId());
try {
@Cleanup("close") Cursor cursor = providerClient.query(entriesURI(),
@Cleanup Cursor cursor = providerClient.query(entriesURI(),
new String[] { entryColumnID(), entryColumnRemoteName(), entryColumnETag() },
where, null, null);
LinkedList<T> dirty = new LinkedList<T>();
while (cursor != null && cursor.moveToNext()) {
T resource = findById(cursor.getLong(0), true);
if (resource != null)
long id = cursor.getLong(0);
try {
T resource = findById(id, true);
dirty.add(resource);
} catch (RecordNotFoundException e) {
Log.w(TAG, "Couldn't load dirty resource: " + ContentUris.appendId(entriesURI().buildUpon(), id), e);
}
}
return dirty.toArray(new Resource[0]);
} catch(RemoteException ex) {
@ -89,14 +93,18 @@ public abstract class LocalCollection<T extends Resource> {
if (entryColumnParentID() != null)
where += " AND " + entryColumnParentID() + "=" + String.valueOf(getId());
try {
@Cleanup("close") Cursor cursor = providerClient.query(entriesURI(),
@Cleanup Cursor cursor = providerClient.query(entriesURI(),
new String[] { entryColumnID(), entryColumnRemoteName(), entryColumnETag() },
where, null, null);
LinkedList<T> deleted = new LinkedList<T>();
while (cursor != null && cursor.moveToNext()) {
T resource = findById(cursor.getLong(0), false);
if (resource != null)
long id = cursor.getLong(0);
try {
T resource = findById(id, false);
deleted.add(resource);
} catch (RecordNotFoundException e) {
Log.w(TAG, "Couldn't load resource marked for deletion: " + ContentUris.appendId(entriesURI().buildUpon(), id), e);
}
}
return deleted.toArray(new Resource[0]);
} catch(RemoteException ex) {
@ -109,13 +117,14 @@ public abstract class LocalCollection<T extends Resource> {
if (entryColumnParentID() != null)
where += " AND " + entryColumnParentID() + "=" + String.valueOf(getId());
try {
@Cleanup("close") Cursor cursor = providerClient.query(entriesURI(),
@Cleanup Cursor cursor = providerClient.query(entriesURI(),
new String[] { entryColumnID() },
where, null, null);
LinkedList<T> fresh = new LinkedList<T>();
while (cursor != null && cursor.moveToNext()) {
T resource = findById(cursor.getLong(0), true);
if (resource != null) {
long id = cursor.getLong(0);
try {
T resource = findById(id, true);
resource.initialize();
// new record: set generated UID + remote file name in database
@ -126,6 +135,8 @@ public abstract class LocalCollection<T extends Resource> {
.build());
fresh.add(resource);
} catch (RecordNotFoundException e) {
Log.w(TAG, "Couldn't load fresh resource: " + ContentUris.appendId(entriesURI().buildUpon(), id), e);
}
}
return fresh.toArray(new Resource[0]);
@ -136,7 +147,7 @@ public abstract class LocalCollection<T extends Resource> {
public T findById(long localID, boolean populate) throws LocalStorageException {
try {
@Cleanup("close") Cursor cursor = providerClient.query(ContentUris.withAppendedId(entriesURI(), localID),
@Cleanup Cursor cursor = providerClient.query(ContentUris.withAppendedId(entriesURI(), localID),
new String[] { entryColumnRemoteName(), entryColumnETag() }, null, null, null);
if (cursor != null && cursor.moveToNext()) {
T resource = newResource(localID, cursor.getString(0), cursor.getString(1));
@ -152,7 +163,7 @@ public abstract class LocalCollection<T extends Resource> {
public T findByRemoteName(String remoteName, boolean populate) throws LocalStorageException {
try {
@Cleanup("close") Cursor cursor = providerClient.query(entriesURI(),
@Cleanup Cursor cursor = providerClient.query(entriesURI(),
new String[] { entryColumnID(), entryColumnRemoteName(), entryColumnETag() },
entryColumnRemoteName() + "=?", new String[] { remoteName }, null);
if (cursor != null && cursor.moveToNext()) {