Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. package com.example.roomproject
  2.  
  3. import android.content.ContentProvider
  4. import android.content.ContentUris
  5. import android.content.ContentValues
  6. import android.content.UriMatcher
  7. import android.database.Cursor
  8. import android.net.Uri
  9. import androidx.room.Room
  10. import com.example.roomproject.database.MusicDao
  11. import com.example.roomproject.database.MusicDatabase
  12.  
  13. class MusicProvider : ContentProvider {
  14.  
  15. internal var cursor: Cursor? = null
  16. internal var musicDao: MusicDao? = null
  17.  
  18. init {
  19. URI_MATCHER.addURI(AUTHORITY, TABLE_ALBUM, ALBUM_TABLE_CODE)
  20. URI_MATCHER.addURI(AUTHORITY, "$TABLE_ALBUM/*" , ALBUM_ROW_CODE)
  21. }
  22.  
  23. companion object {
  24. private val TAG = MusicProvider.javaClass.simpleName
  25. private val AUTHORITY = "com.example.roomproject.musicprovider"
  26. private val TABLE_ALBUM = "album"
  27. private val URI_MATCHER = UriMatcher(-1)
  28. private val ALBUM_TABLE_CODE = 101
  29. private val ALBUM_ROW_CODE = 102
  30.  
  31. }
  32.  
  33. constructor() {
  34.  
  35. }
  36.  
  37.  
  38. override fun onCreate(): Boolean {
  39. if (context != null) {
  40. val musicDao = Room
  41. .databaseBuilder(context.applicationContext, MusicDatabase::class.java, "music_database")
  42. .build()
  43. .getMusicDao()
  44. return true
  45. }
  46. return false
  47. }
  48.  
  49.  
  50. override fun getType(uri: Uri): String? {
  51. when (URI_MATCHER.match(uri)) {
  52. ALBUM_TABLE_CODE -> return "vnd.android.cursor.dir/$AUTHORITY.$TABLE_ALBUM"
  53. ALBUM_ROW_CODE -> return "vnd.android.cursor.item/$AUTHORITY.$TABLE_ALBUM"
  54. else -> throw UnsupportedOperationException("not yet implemented")
  55. }
  56. }
  57. override fun query(uri: Uri, projection: Array<String>?, selection: String?,
  58. selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
  59.  
  60. val code = URI_MATCHER.match(uri)
  61. if (code != ALBUM_ROW_CODE && code != ALBUM_TABLE_CODE) return null
  62. if (code == ALBUM_TABLE_CODE) {
  63. cursor = musicDao?.getAlbumsCursor()
  64. } else {
  65. cursor = musicDao?.getAlbumsWithIdCursor(ContentUris.parseId(uri).toInt())
  66. }
  67. return cursor
  68.  
  69. }
  70. override fun insert(uri: Uri, values: ContentValues?): Uri? {
  71. throw java.lang.UnsupportedOperationException("not yet implemented")
  72. }
  73.  
  74. override fun update(
  75. uri: Uri, values: ContentValues?, selection: String?,
  76. selectionArgs: Array<String>?): Int {
  77. throw java.lang.UnsupportedOperationException("not yet implemented")
  78. }
  79.  
  80. override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
  81. throw java.lang.UnsupportedOperationException("not yet implemented")
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement