Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow implementors to control network syncing #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/edu/mit/mobile/android/content/SimpleContentProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
}
final int count = mDBHelperMapper.delete(match, this, db, uri, selection, selectionArgs);

getContext().getContentResolver().notifyChange(uri, null);
// Never need to synchronize deletes.
getContext().getContentResolver().notifyChange(uri, null, false);

return count;
}
Expand Down Expand Up @@ -557,7 +558,7 @@ public Uri insert(Uri uri, ContentValues values) {
}
final Uri newUri = mDBHelperMapper.insert(match, this, db, uri, values);
if (newUri != null) {
getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(uri, null, shouldSyncChanges(values));
}
return newUri;
}
Expand All @@ -579,17 +580,19 @@ public int bulkInsert(Uri uri, ContentValues[] values) {
int numSuccessfulAdds = 0;
db.beginTransaction();
try {
boolean syncToNetwork = false;

for (final ContentValues cv : values) {
final Uri newUri = mDBHelperMapper.insert(match, this, db, uri, cv);
final Uri newUri = mDBHelperMapper.insert(match, this, db, uri, cv);
if (newUri != null) {
numSuccessfulAdds++;
syncToNetwork = syncToNetwork || shouldSyncChanges(cv);
}
}
db.setTransactionSuccessful();

if (numSuccessfulAdds > 0) {
getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(uri, null, syncToNetwork);
}
} finally {
db.endTransaction();
Expand Down Expand Up @@ -647,12 +650,22 @@ public int update(Uri uri, ContentValues values, String selection, String[] sele
final int changed = mDBHelperMapper.update(match, this, db, uri, values, selection,
selectionArgs);
if (changed != 0) {
getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(uri, null, shouldSyncChanges(values));
}
return changed;
}

// ///////////////////// private methods

/**
* Given the values, determine if the network sync should be called.
* @param values
* @return true if changes should be synced to network.
*/
protected boolean shouldSyncChanges(ContentValues values) {
return true;
}

/**
* Generates a name for the database from the content provider.
*
Expand Down