-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -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 | ||
|
@@ -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)); | ||
|
||
return ContentUris.withAppendedId(uri, rowId); | ||
} | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
} | ||
|
||
return count; | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
} | ||
|
||
return count; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Uri syncUri = AbstractProvider.makeUriFromSyncAdapter(mPostsUri); | ||
assertTrue("The query parameter should resolve to true", syncUri.getQueryParameter(TestProvider.QUERY_CALLER_IS_SYNC_ADAPTER).equals("1")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add null-check here.