Skip to content

Instantly share code, notes, and snippets.

@jobinlawrance
Last active October 30, 2017 05:22
Show Gist options
  • Save jobinlawrance/4ba0b04b732f79fa8116e1c9cda8417a to your computer and use it in GitHub Desktop.
Save jobinlawrance/4ba0b04b732f79fa8116e1c9cda8417a to your computer and use it in GitHub Desktop.
Custom Content Provider Example
<application>
<provider
android:name=".sqlite.TeamProvider"
android:authorities="com.jobinlawrance"
android:grantUriPermissions="true"
android:exported="true"/>
</application>
<!-- Handle uri permissions in a better way-->
ContentResolver resolver = getContentResolver();
Cursor cursor =
resolver.query(Uri.parse("content://com.jobinlawrance/team"),
new String[]{"*"}, //all the columns -> duh
null,
null,
null);
if (cursor.moveToFirst()) {
do {
long id = cursor.getLong(0);
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.d("###Main",name);
} while (cursor.moveToNext());
}
cursor.close();
/**
* Created by jobinlawrance on 6/8/17.
*/
class TeamProvider : ContentProvider() {
var dbHelper: DbHelper? = null
companion object {
/**
* The Content Authority is a name for the entire content provider, similar to the relationship
* between a domain name and its website. A convenient string to use for content authority is
* the package name for the app, since it is guaranteed to be unique on the device.
*/
val CONTENT_AUTHORITY: String = "com.jobinlawrance"
/**
* The content authority is used to create the base of all URIs which apps will use to
* contact this content provider.
*/
val BASE_CONTENT_URI: Uri = Uri.parse("content://" + CONTENT_AUTHORITY)
/**
* A list of possible paths that will be appended to the base URI for each of the different
* tables.
*/
val PATH_TEAM: String = "team"
// Content URI represents the base location for the table
// content://com.jobinlawrance/team
val CONTENT_URI : Uri = BASE_CONTENT_URI.buildUpon().appendPath(PATH_TEAM).build();
// These are special type prefixes that specify if a URI returns a list or a specific item
val CONTENT_TYPE = "vnd.android.cursor.dir/" + CONTENT_URI + "/" + PATH_TEAM;
val CONTENT_ITEM_TYPE = "vnd.android.cursor.item/" + CONTENT_URI + "/" + PATH_TEAM;
val TEAM = 100
val TEAM_ID = 101
fun buildUriMatcher(): UriMatcher {
// All paths to the UriMatcher have a corresponding code to return
// when a match is found (the ints above).
val matcher = UriMatcher(UriMatcher.NO_MATCH)
matcher.addURI(CONTENT_AUTHORITY, PATH_TEAM, TEAM)
matcher.addURI(CONTENT_AUTHORITY, PATH_TEAM + "/#", TEAM_ID)
return matcher
}
}
override fun onCreate(): Boolean {
Log.d("###TeamProvider", CONTENT_URI.toString())
return true;
}
override fun getType(uri: Uri?): String {
when(buildUriMatcher().match(uri)) {
TEAM -> return CONTENT_TYPE
TEAM_ID -> return CONTENT_ITEM_TYPE
else -> return ""
}
}
override fun query(uri: Uri?, tableColumns: Array<out String>?, whereClause: String?, whereArgs: Array<out String>?, sortOrder: String?): Cursor? {
dbHelper = MyApplication.getInstance().appComponent.dbHelper
val db = dbHelper!!.readableDatabase
var cursor: Cursor? = null
when(buildUriMatcher().match(uri)){
TEAM -> {
cursor = db.query(DbHelper.TABLE_CLUB, tableColumns, whereClause, whereArgs,null,null,sortOrder)
}
TEAM_ID -> {
//https://stackoverflow.com/a/10601764/6448823
cursor = db.query(DbHelper.TABLE_CLUB,
tableColumns,
DbHelper.KEY_ID + "= ?",
arrayOf(uri?.lastPathSegment),
null,
null,
sortOrder)
}
else -> UnsupportedOperationException("Unknown uri: " + uri);
}
cursor?.setNotificationUri(context.contentResolver,uri)
return cursor
}
override fun insert(p0: Uri?, p1: ContentValues?): Uri? = null
override fun update(p0: Uri?, p1: ContentValues?, p2: String?, p3: Array<out String>?): Int = 0
override fun delete(p0: Uri?, p1: String?, p2: Array<out String>?): Int = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment