1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
private final ContentResolver mContentResolver;
public void updateLocalFeedData(final InputStream stream, final SyncResult syncResult)
throws IOException, XmlPullParserException, RemoteException,
OperationApplicationException, ParseException {
final FeedParser feedParser = new FeedParser();
final ContentResolver contentResolver = getContext().getContentResolver();
Log.i(TAG, "Parsing stream as Atom feed");
final List<FeedParser.Entry> entries = feedParser.parse(stream);
Log.i(TAG, "Parsing complete. Found " + entries.size() + " entries");
ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>();
// Build hash table of incoming entries
HashMap<String, FeedParser.Entry> entryMap = new HashMap<String, FeedParser.Entry>();
for (FeedParser.Entry e : entries) {
entryMap.put(e.id, e);
}
// Get list of all items
Log.i(TAG, "Fetching local entries for merge");
Uri uri = FeedContract.Entry.CONTENT_URI; // Get all entries
Cursor c = contentResolver.query(uri, PROJECTION, null, null, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
assert c != null;
Log.i(TAG, "Found " + c.getCount() + " local entries. Computing merge solution...");
// Find stale data
int id;
String entryId;
String title;
String link;
String imag_link;
String text_entry;
long published;
while (c.moveToNext()) {
syncResult.stats.numEntries++;
id = c.getInt(COLUMN_ID);
entryId = c.getString(COLUMN_ENTRY_ID);
title = c.getString(COLUMN_TITLE);
link = c.getString(COLUMN_LINK);
imag_link = c.getString(COLUMN_IMAG_LINK);
text_entry = c.getString(COLUMN_TEXT_ENTRY);
published = c.getLong(COLUMN_PUBLISHED);
FeedParser.Entry match = entryMap.get(entryId);
if (match != null) {
// Entry exists. Remove from entry map to prevent insert later.
entryMap.remove(entryId);
// Check to see if the entry needs to be updated
Uri existingUri = FeedContract.Entry.CONTENT_URI.buildUpon()
.appendPath(Integer.toString(id)).build();
if ((match.title != null && !match.title.equals(title)) ||
(match.link != null && !match.link.equals(link)) ||
(match.imag_link != null && !match.imag_link.equals(imag_link)) ||
(match.text_entry != null && !match.text_entry.equals(text_entry)) ||
(match.published != published))
{
// Update existing record
Log.i(TAG, "Scheduling update: " + existingUri);
batch.add(ContentProviderOperation.newUpdate(existingUri)
.withValue(FeedContract.Entry.COLUMN_NAME_TITLE, title)
.withValue(FeedContract.Entry.COLUMN_NAME_LINK, link)
.withValue(FeedContract.Entry.COLUMN_IMAG_LINK, imag_link)
.withValue(FeedContract.Entry.COLUMN_TEXT_ENTRY, text_entry)
.withValue(FeedContract.Entry.COLUMN_NAME_PUBLISHED, published)
.build());
syncResult.stats.numUpdates++;
} else {
Log.i(TAG, "No action: " + existingUri);
}
} else {
// Entry doesn't exist. Remove it from the database.
Uri deleteUri = FeedContract.Entry.CONTENT_URI.buildUpon()
.appendPath(Integer.toString(id)).build();
Log.i(TAG, "Scheduling delete: " + deleteUri);
batch.add(ContentProviderOperation.newDelete(deleteUri).build());
syncResult.stats.numDeletes++;
}
}
c.close();
//Entry e = null;
// Add new items
for (FeedParser.Entry e : entryMap.values()) {
sendNotification(e);
ID_NOTIF = ID_NOTIF + 1;
Log.i(TAG, "Scheduling insert: entry_id=" + e.id);
batch.add(ContentProviderOperation.newInsert(FeedContract.Entry.CONTENT_URI)
.withValue(FeedContract.Entry.COLUMN_NAME_ENTRY_ID, e.id)
.withValue(FeedContract.Entry.COLUMN_NAME_TITLE, e.title)
.withValue(FeedContract.Entry.COLUMN_NAME_LINK, e.link)
.withValue(FeedContract.Entry.COLUMN_IMAG_LINK, e.imag_link)
.withValue(FeedContract.Entry.COLUMN_TEXT_ENTRY, e.text_entry)
.withValue(FeedContract.Entry.COLUMN_NAME_PUBLISHED, e.published)
.build());
syncResult.stats.numInserts++;
}
Log.i(TAG, "Merge solution ready. Applying batch update");
mContentResolver.applyBatch(FeedContract.CONTENT_AUTHORITY, batch);
mContentResolver.notifyChange(
FeedContract.Entry.CONTENT_URI, // URI where data was modified
null, // No local observer
false); // IMPORTANT: Do not sync to network
// This sample doesn't support uploads, but if *your* code does, make sure you set
// syncToNetwork=false in the line above to prevent duplicate syncs.
} |
Partager