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

Added support for sync adapter access. #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public abstract class AbstractProvider extends ContentProvider {

protected final String mLogTag;
protected SQLiteDatabase mDatabase;
public static final String QUERY_CALLER_IS_SYNC_ADAPTER = "caller_is_sync_adapter";
private static final String PARAM_TRUE = "1";

protected AbstractProvider() {
mLogTag = getClass().getName();
Expand Down Expand Up @@ -54,6 +56,10 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
return false;
}

public static Uri makeUriFromSyncAdapter(Uri uri) {
return uri.buildUpon().appendQueryParameter(QUERY_CALLER_IS_SYNC_ADAPTER, PARAM_TRUE).build();
}

/**
* Called when the database needs to be updated and after <code>AbstractProvider</code> has
* done its own work. That is, after creating columns that have been added using the
Expand Down Expand Up @@ -142,7 +148,7 @@ public Uri insert(Uri uri, ContentValues values) {
long rowId = mDatabase.insert(segments.get(0), null, values);

if (rowId > -1) {
getContentResolver().notifyChange(uri, null);
getContentResolver().notifyChange(uri, null, !uri.getQueryParameter(QUERY_CALLER_IS_SYNC_ADAPTER).equals(PARAM_TRUE));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add null-check here.


return ContentUris.withAppendedId(uri, rowId);
}
Expand All @@ -156,7 +162,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = builder.where(selection, selectionArgs).delete(mDatabase);

if (count > 0) {
getContentResolver().notifyChange(uri, null);
getContentResolver().notifyChange(uri, null, !uri.getQueryParameter(QUERY_CALLER_IS_SYNC_ADAPTER).equals(PARAM_TRUE));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

}

return count;
Expand All @@ -168,7 +174,7 @@ public int update(Uri uri, ContentValues values, String selection, String[] sele
int count = builder.where(selection, selectionArgs).update(mDatabase, values);

if (count > 0) {
getContentResolver().notifyChange(uri, null);
getContentResolver().notifyChange(uri, null, !uri.getQueryParameter(QUERY_CALLER_IS_SYNC_ADAPTER).equals(PARAM_TRUE));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

}

return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ public void testQueryById() {
CONTENT_2, c.getString(c.getColumnIndex(TestProvider.Post.CONTENT)));
assertFalse("There shouldn't be any more entries", c.moveToNext());
}

@Test
public void testSyncAdapterQuery() {
assertFalse("The query parameter should resolve to false", mPostsUri.getQueryParameter(TestProvider.QUERY_CALLER_IS_SYNC_ADAPTER).equals("1"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getQueryParameter will return null in this case. Please change your test accordingly.

Uri syncUri = AbstractProvider.makeUriFromSyncAdapter(mPostsUri);
assertTrue("The query parameter should resolve to true", syncUri.getQueryParameter(TestProvider.QUERY_CALLER_IS_SYNC_ADAPTER).equals("1"));
}
}