|
1 | | -package ai.elimu.content_provider.provider; |
| 1 | +package ai.elimu.content_provider.provider |
2 | 2 |
|
3 | | -import android.content.ContentProvider; |
4 | | -import android.content.ContentValues; |
5 | | -import android.content.Context; |
6 | | -import android.content.UriMatcher; |
7 | | -import android.database.Cursor; |
8 | | -import android.net.Uri; |
9 | | -import android.util.Log; |
| 3 | +import ai.elimu.content_provider.BuildConfig |
| 4 | +import ai.elimu.content_provider.room.db.RoomDb |
| 5 | +import android.content.ContentProvider |
| 6 | +import android.content.ContentValues |
| 7 | +import android.content.UriMatcher |
| 8 | +import android.database.Cursor |
| 9 | +import android.net.Uri |
| 10 | +import android.util.Log |
10 | 11 |
|
11 | | -import java.util.List; |
| 12 | +class WordContentProvider : ContentProvider() { |
| 13 | + override fun onCreate(): Boolean { |
| 14 | + Log.i(javaClass.name, "onCreate") |
12 | 15 |
|
13 | | -import ai.elimu.content_provider.BuildConfig; |
14 | | -import ai.elimu.content_provider.room.dao.WordDao; |
15 | | -import ai.elimu.content_provider.room.db.RoomDb; |
| 16 | + Log.i(javaClass.name, "URI_WORD: " + URI_WORD) |
16 | 17 |
|
17 | | -public class WordContentProvider extends ContentProvider { |
18 | | - |
19 | | - // The authority of this content provider |
20 | | - public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider.word_provider"; |
21 | | - |
22 | | - private static final String TABLE_WORDS = "words"; |
23 | | - private static final int CODE_WORDS = 1; |
24 | | - private static final int CODE_WORD_ID = 2; |
25 | | - private static final int CODE_WORDS_BY_STORYBOOK_PARAGRAPH_ID = 3; |
26 | | - public static final Uri URI_WORD = Uri.parse("content://" + AUTHORITY + "/" + TABLE_WORDS); |
27 | | - |
28 | | - // The URI matcher |
29 | | - private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
30 | | - |
31 | | - static { |
32 | | - MATCHER.addURI(AUTHORITY, TABLE_WORDS, CODE_WORDS); |
33 | | - MATCHER.addURI(AUTHORITY, TABLE_WORDS + "/#", CODE_WORD_ID); |
34 | | - MATCHER.addURI(AUTHORITY, TABLE_WORDS + "/by-paragraph-id/#", CODE_WORDS_BY_STORYBOOK_PARAGRAPH_ID); |
35 | | - } |
36 | | - |
37 | | - @Override |
38 | | - public boolean onCreate() { |
39 | | - Log.i(getClass().getName(), "onCreate"); |
40 | | - |
41 | | - Log.i(getClass().getName(), "URI_WORD: " + URI_WORD); |
42 | | - |
43 | | - return true; |
| 18 | + return true |
44 | 19 | } |
45 | 20 |
|
46 | 21 | /** |
47 | 22 | * Handles query requests from clients. |
48 | 23 | */ |
49 | | - @Override |
50 | | - public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { |
51 | | - Log.i(getClass().getName(), "query"); |
52 | | - |
53 | | - Log.i(getClass().getName(), "uri: " + uri); |
54 | | - Log.i(getClass().getName(), "projection: " + projection); |
55 | | - Log.i(getClass().getName(), "selection: " + selection); |
56 | | - Log.i(getClass().getName(), "selectionArgs: " + selectionArgs); |
57 | | - Log.i(getClass().getName(), "sortOrder: " + sortOrder); |
58 | | - |
59 | | - Context context = getContext(); |
60 | | - Log.i(getClass().getName(), "context: " + context); |
| 24 | + override fun query( |
| 25 | + uri: Uri, |
| 26 | + projection: Array<String>?, |
| 27 | + selection: String?, |
| 28 | + selectionArgs: Array<String>?, |
| 29 | + sortOrder: String? |
| 30 | + ): Cursor? { |
| 31 | + Log.i(javaClass.name, "query") |
| 32 | + |
| 33 | + Log.i(javaClass.name, "uri: $uri") |
| 34 | + Log.i(javaClass.name, "projection: $projection") |
| 35 | + Log.i(javaClass.name, "selection: $selection") |
| 36 | + Log.i(javaClass.name, "selectionArgs: $selectionArgs") |
| 37 | + Log.i(javaClass.name, "sortOrder: $sortOrder") |
| 38 | + |
| 39 | + val context = context |
| 40 | + Log.i(javaClass.name, "context: $context") |
61 | 41 | if (context == null) { |
62 | | - return null; |
| 42 | + return null |
63 | 43 | } |
64 | 44 |
|
65 | | - RoomDb roomDb = RoomDb.getDatabase(context); |
66 | | - WordDao wordDao = roomDb.wordDao(); |
| 45 | + val roomDb = RoomDb.getDatabase(context) |
| 46 | + val wordDao = roomDb.wordDao() |
67 | 47 |
|
68 | | - final int code = MATCHER.match(uri); |
69 | | - Log.i(getClass().getName(), "code: " + code); |
| 48 | + val code = MATCHER.match(uri) |
| 49 | + Log.i(javaClass.name, "code: $code") |
70 | 50 | if (code == CODE_WORDS) { |
71 | | - final Cursor cursor; |
72 | | - |
73 | 51 | // Get the Room Cursor |
74 | | - cursor = wordDao.loadAllOrderedByUsageCountAsCursor(); |
75 | | - Log.i(getClass().getName(), "cursor: " + cursor); |
| 52 | + val cursor = wordDao.loadAllOrderedByUsageCountAsCursor() |
| 53 | + Log.i(javaClass.name, "cursor: $cursor") |
76 | 54 |
|
77 | | - cursor.setNotificationUri(context.getContentResolver(), uri); |
| 55 | + cursor.setNotificationUri(context.contentResolver, uri) |
78 | 56 |
|
79 | | - return cursor; |
| 57 | + return cursor |
80 | 58 | } else if (code == CODE_WORDS_BY_STORYBOOK_PARAGRAPH_ID) { |
81 | 59 | // Extract the StoryBookParagraph ID from the URI |
82 | | - List<String> pathSegments = uri.getPathSegments(); |
83 | | - Log.i(getClass().getName(), "pathSegments: " + pathSegments); |
84 | | - String storyBookParagraphIdAsString = pathSegments.get(2); |
85 | | - Long storyBookParagraphId = Long.valueOf(storyBookParagraphIdAsString); |
86 | | - Log.i(getClass().getName(), "storyBookParagraphId: " + storyBookParagraphId); |
87 | | - |
88 | | - final Cursor cursor; |
| 60 | + val pathSegments = uri.pathSegments |
| 61 | + Log.i(javaClass.name, "pathSegments: $pathSegments") |
| 62 | + val storyBookParagraphIdAsString = pathSegments[2] |
| 63 | + val storyBookParagraphId = storyBookParagraphIdAsString.toLong() |
| 64 | + Log.i(javaClass.name, "storyBookParagraphId: $storyBookParagraphId") |
89 | 65 |
|
90 | 66 | // Get the Room Cursor |
91 | | - cursor = wordDao.loadAllAsCursor(storyBookParagraphId); |
92 | | - Log.i(getClass().getName(), "cursor: " + cursor); |
| 67 | + val cursor = wordDao.loadAllAsCursor(storyBookParagraphId) |
| 68 | + Log.i(javaClass.name, "cursor: $cursor") |
93 | 69 |
|
94 | | - cursor.setNotificationUri(context.getContentResolver(), uri); |
| 70 | + cursor.setNotificationUri(context.contentResolver, uri) |
95 | 71 |
|
96 | | - return cursor; |
| 72 | + return cursor |
97 | 73 | } else if (code == CODE_WORD_ID) { |
98 | 74 | // Extract the Word ID from the URI |
99 | | - List<String> pathSegments = uri.getPathSegments(); |
100 | | - Log.i(getClass().getName(), "pathSegments: " + pathSegments); |
101 | | - String wordIdAsString = pathSegments.get(1); |
102 | | - Long wordId = Long.valueOf(wordIdAsString); |
103 | | - Log.i(getClass().getName(), "wordId: " + wordId); |
104 | | - |
105 | | - final Cursor cursor; |
| 75 | + val pathSegments = uri.pathSegments |
| 76 | + Log.i(javaClass.name, "pathSegments: $pathSegments") |
| 77 | + val wordIdAsString = pathSegments[1] |
| 78 | + val wordId = wordIdAsString.toLong() |
| 79 | + Log.i(javaClass.name, "wordId: $wordId") |
106 | 80 |
|
107 | 81 | // Get the Room Cursor |
108 | | - cursor = wordDao.loadAsCursor(wordId); |
109 | | - Log.i(getClass().getName(), "cursor: " + cursor); |
| 82 | + val cursor = wordDao.loadAsCursor(wordId) |
| 83 | + Log.i(javaClass.name, "cursor: $cursor") |
110 | 84 |
|
111 | | - cursor.setNotificationUri(context.getContentResolver(), uri); |
| 85 | + cursor.setNotificationUri(context.contentResolver, uri) |
112 | 86 |
|
113 | | - return cursor; |
| 87 | + return cursor |
114 | 88 | } else { |
115 | | - throw new IllegalArgumentException("Unknown URI: " + uri); |
| 89 | + throw IllegalArgumentException("Unknown URI: $uri") |
116 | 90 | } |
117 | 91 | } |
118 | 92 |
|
119 | 93 | /** |
120 | 94 | * Handles requests for the MIME type of the data at the given URI. |
121 | 95 | */ |
122 | | - @Override |
123 | | - public String getType(Uri uri) { |
124 | | - Log.i(getClass().getName(), "getType"); |
| 96 | + override fun getType(uri: Uri): String? { |
| 97 | + Log.i(javaClass.name, "getType") |
125 | 98 |
|
126 | | - throw new UnsupportedOperationException("Not yet implemented"); |
| 99 | + throw UnsupportedOperationException("Not yet implemented") |
127 | 100 | } |
128 | 101 |
|
129 | 102 | /** |
130 | 103 | * Handles requests to insert a new row. |
131 | 104 | */ |
132 | | - @Override |
133 | | - public Uri insert(Uri uri, ContentValues values) { |
134 | | - Log.i(getClass().getName(), "insert"); |
| 105 | + override fun insert(uri: Uri, values: ContentValues?): Uri? { |
| 106 | + Log.i(javaClass.name, "insert") |
135 | 107 |
|
136 | | - throw new UnsupportedOperationException("Not yet implemented"); |
| 108 | + throw UnsupportedOperationException("Not yet implemented") |
137 | 109 | } |
138 | 110 |
|
139 | 111 | /** |
140 | 112 | * Handles requests to update one or more rows. |
141 | 113 | */ |
142 | | - @Override |
143 | | - public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
144 | | - Log.i(getClass().getName(), "update"); |
145 | | - |
146 | | - throw new UnsupportedOperationException("Not yet implemented"); |
| 114 | + override fun update( |
| 115 | + uri: Uri, |
| 116 | + values: ContentValues?, |
| 117 | + selection: String?, |
| 118 | + selectionArgs: Array<String>? |
| 119 | + ): Int { |
| 120 | + Log.i(javaClass.name, "update") |
| 121 | + |
| 122 | + throw UnsupportedOperationException("Not yet implemented") |
147 | 123 | } |
148 | 124 |
|
149 | 125 | /** |
150 | 126 | * Handle requests to delete one or more rows. |
151 | 127 | */ |
152 | | - @Override |
153 | | - public int delete(Uri uri, String selection, String[] selectionArgs) { |
154 | | - Log.i(getClass().getName(), "delete"); |
| 128 | + override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int { |
| 129 | + Log.i(javaClass.name, "delete") |
155 | 130 |
|
156 | | - throw new UnsupportedOperationException("Not yet implemented"); |
| 131 | + throw UnsupportedOperationException("Not yet implemented") |
| 132 | + } |
| 133 | + |
| 134 | + companion object { |
| 135 | + // The authority of this content provider |
| 136 | + const val AUTHORITY: String = BuildConfig.APPLICATION_ID + ".provider.word_provider" |
| 137 | + |
| 138 | + private const val TABLE_WORDS = "words" |
| 139 | + private const val CODE_WORDS = 1 |
| 140 | + private const val CODE_WORD_ID = 2 |
| 141 | + private const val CODE_WORDS_BY_STORYBOOK_PARAGRAPH_ID = 3 |
| 142 | + val URI_WORD: Uri = Uri.parse("content://" + AUTHORITY + "/" + TABLE_WORDS) |
| 143 | + |
| 144 | + // The URI matcher |
| 145 | + private val MATCHER = UriMatcher(UriMatcher.NO_MATCH) |
| 146 | + |
| 147 | + init { |
| 148 | + MATCHER.addURI(AUTHORITY, TABLE_WORDS, CODE_WORDS) |
| 149 | + MATCHER.addURI(AUTHORITY, TABLE_WORDS + "/#", CODE_WORD_ID) |
| 150 | + MATCHER.addURI( |
| 151 | + AUTHORITY, |
| 152 | + TABLE_WORDS + "/by-paragraph-id/#", |
| 153 | + CODE_WORDS_BY_STORYBOOK_PARAGRAPH_ID |
| 154 | + ) |
| 155 | + } |
157 | 156 | } |
158 | 157 | } |
0 commit comments