|
1 |
| -package ai.elimu.content_provider.provider; |
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.os.ParcelFileDescriptor; |
10 |
| -import android.util.Log; |
11 |
| - |
12 |
| -import java.io.File; |
13 |
| -import java.io.FileNotFoundException; |
14 |
| -import java.util.List; |
15 |
| - |
16 |
| -import ai.elimu.content_provider.BuildConfig; |
17 |
| -import ai.elimu.content_provider.room.dao.VideoDao; |
18 |
| -import ai.elimu.content_provider.room.db.RoomDb; |
19 |
| -import ai.elimu.content_provider.room.entity.Video; |
20 |
| -import ai.elimu.content_provider.util.FileHelper; |
21 |
| - |
22 |
| -public class VideoContentProvider extends ContentProvider { |
23 |
| - |
24 |
| - public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider.video_provider"; |
25 |
| - |
26 |
| - private static final String TABLE_VIDEOS = "videos"; |
27 |
| - private static final int CODE_VIDEOS = 1; |
28 |
| - private static final int CODE_VIDEO_ID = 2; |
29 |
| - private static final int CODE_VIDEO_TITLE = 4; |
30 |
| - |
31 |
| - private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
32 |
| - |
33 |
| - static { |
34 |
| - MATCHER.addURI(AUTHORITY, TABLE_VIDEOS, CODE_VIDEOS); |
35 |
| - MATCHER.addURI(AUTHORITY, TABLE_VIDEOS + "/#", CODE_VIDEO_ID); |
36 |
| - MATCHER.addURI(AUTHORITY, TABLE_VIDEOS + "/by-title/*", CODE_VIDEO_TITLE); |
37 |
| - } |
38 |
| - |
39 |
| - @Override |
40 |
| - public boolean onCreate() { |
41 |
| - Log.i(getClass().getName(), "onCreate"); |
42 |
| - |
43 |
| - return true; |
| 1 | +package ai.elimu.content_provider.provider |
| 2 | + |
| 3 | +import ai.elimu.content_provider.BuildConfig |
| 4 | +import ai.elimu.content_provider.room.db.RoomDb |
| 5 | +import ai.elimu.content_provider.util.FileHelper |
| 6 | +import android.content.ContentProvider |
| 7 | +import android.content.ContentValues |
| 8 | +import android.content.UriMatcher |
| 9 | +import android.database.Cursor |
| 10 | +import android.net.Uri |
| 11 | +import android.os.ParcelFileDescriptor |
| 12 | +import android.util.Log |
| 13 | +import java.io.FileNotFoundException |
| 14 | + |
| 15 | +class VideoContentProvider : ContentProvider() { |
| 16 | + |
| 17 | + private val TAG = javaClass.name |
| 18 | + |
| 19 | + override fun onCreate(): Boolean { |
| 20 | + Log.i(TAG, "onCreate") |
| 21 | + |
| 22 | + return true |
44 | 23 | }
|
45 | 24 |
|
46 | 25 | /**
|
47 | 26 | * Handles query requests from clients.
|
48 | 27 | */
|
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); |
| 28 | + override fun query( |
| 29 | + uri: Uri, |
| 30 | + projection: Array<String>?, |
| 31 | + selection: String?, |
| 32 | + selectionArgs: Array<String>?, |
| 33 | + sortOrder: String? |
| 34 | + ): Cursor? { |
| 35 | + Log.i(TAG, "query") |
| 36 | + |
| 37 | + Log.i(TAG, "uri: $uri") |
| 38 | + Log.i(TAG, "projection: $projection") |
| 39 | + Log.i(TAG, "selection: $selection") |
| 40 | + Log.i(TAG, "selectionArgs: $selectionArgs") |
| 41 | + Log.i(TAG, "sortOrder: $sortOrder") |
| 42 | + |
| 43 | + val context = context |
| 44 | + Log.i(TAG, "context: $context") |
61 | 45 | if (context == null) {
|
62 |
| - return null; |
| 46 | + return null |
63 | 47 | }
|
64 | 48 |
|
65 |
| - RoomDb roomDb = RoomDb.getDatabase(context); |
66 |
| - VideoDao videoDao = roomDb.videoDao(); |
67 |
| - |
68 |
| - final int code = MATCHER.match(uri); |
69 |
| - Log.i(getClass().getName(), "code: " + code); |
70 |
| - if (code == CODE_VIDEOS) { |
71 |
| - final Cursor cursor; |
72 |
| - |
73 |
| - // Get the Room Cursor |
74 |
| - cursor = videoDao.loadAllAsCursor(); |
75 |
| - Log.i(getClass().getName(), "cursor: " + cursor); |
76 |
| - |
77 |
| - cursor.setNotificationUri(context.getContentResolver(), uri); |
78 |
| - |
79 |
| - return cursor; |
80 |
| - } else if (code == CODE_VIDEO_ID) { |
81 |
| - // Extract the Video ID from the URI |
82 |
| - List<String> pathSegments = uri.getPathSegments(); |
83 |
| - Log.i(getClass().getName(), "pathSegments: " + pathSegments); |
84 |
| - String videoIdAsString = pathSegments.get(1); |
85 |
| - Long videoId = Long.valueOf(videoIdAsString); |
86 |
| - Log.i(getClass().getName(), "videoId: " + videoId); |
87 |
| - |
88 |
| - final Cursor cursor; |
89 |
| - |
90 |
| - // Get the Room Cursor |
91 |
| - cursor = videoDao.loadAsCursor(videoId); |
92 |
| - Log.i(getClass().getName(), "cursor: " + cursor); |
93 |
| - |
94 |
| - cursor.setNotificationUri(context.getContentResolver(), uri); |
95 |
| - |
96 |
| - return cursor; |
97 |
| - } else if (code == CODE_VIDEO_TITLE) { |
98 |
| - // Extract the transcription from the URI |
99 |
| - List<String> pathSegments = uri.getPathSegments(); |
100 |
| - Log.i(getClass().getName(), "pathSegments: " + pathSegments); |
101 |
| - String title = pathSegments.get(2); |
102 |
| - Log.i(getClass().getName(), "title: \"" + title + "\""); |
103 |
| - |
104 |
| - final Cursor cursor; |
105 |
| - |
106 |
| - // Get the Room Cursor |
107 |
| - cursor = videoDao.loadByTitleAsCursor(title); |
108 |
| - Log.i(getClass().getName(), "cursor: " + cursor); |
109 |
| - |
110 |
| - cursor.setNotificationUri(context.getContentResolver(), uri); |
111 |
| - |
112 |
| - return cursor; |
113 |
| - } else { |
114 |
| - throw new IllegalArgumentException("Unknown URI: " + uri); |
| 49 | + val roomDb = RoomDb.getDatabase(context) |
| 50 | + val videoDao = roomDb.videoDao() |
| 51 | + |
| 52 | + val code = MATCHER.match(uri) |
| 53 | + Log.i(TAG, "code: $code") |
| 54 | + when (code) { |
| 55 | + CODE_VIDEOS -> { |
| 56 | + // Get the Room Cursor |
| 57 | + val cursor = videoDao.loadAllAsCursor() |
| 58 | + Log.i(TAG, "cursor: $cursor") |
| 59 | + |
| 60 | + cursor.setNotificationUri(context.contentResolver, uri) |
| 61 | + |
| 62 | + return cursor |
| 63 | + } |
| 64 | + CODE_VIDEO_ID -> { |
| 65 | + // Extract the Video ID from the URI |
| 66 | + val pathSegments = uri.pathSegments |
| 67 | + Log.i(TAG, "pathSegments: $pathSegments") |
| 68 | + val videoIdAsString = pathSegments[1] |
| 69 | + val videoId = videoIdAsString.toLong() |
| 70 | + Log.i(TAG, "videoId: $videoId") |
| 71 | + |
| 72 | + // Get the Room Cursor |
| 73 | + val cursor = videoDao.loadAsCursor(videoId) |
| 74 | + Log.i(TAG, "cursor: $cursor") |
| 75 | + |
| 76 | + cursor.setNotificationUri(context.contentResolver, uri) |
| 77 | + |
| 78 | + return cursor |
| 79 | + } |
| 80 | + CODE_VIDEO_TITLE -> { |
| 81 | + // Extract the transcription from the URI |
| 82 | + val pathSegments = uri.pathSegments |
| 83 | + Log.i(TAG, "pathSegments: $pathSegments") |
| 84 | + val title = pathSegments[2] |
| 85 | + Log.i(TAG, "title: \"$title\"") |
| 86 | + |
| 87 | + // Get the Room Cursor |
| 88 | + val cursor = videoDao.loadByTitleAsCursor(title) |
| 89 | + Log.i(TAG, "cursor: $cursor") |
| 90 | + |
| 91 | + cursor.setNotificationUri(context.contentResolver, uri) |
| 92 | + |
| 93 | + return cursor |
| 94 | + } |
| 95 | + else -> { |
| 96 | + throw IllegalArgumentException("Unknown URI: $uri") |
| 97 | + } |
115 | 98 | }
|
116 | 99 | }
|
117 | 100 |
|
118 | 101 | /**
|
119 | 102 | * Handles requests for the MIME type of the data at the given URI.
|
120 | 103 | */
|
121 |
| - @Override |
122 |
| - public String getType(Uri uri) { |
123 |
| - Log.i(getClass().getName(), "getType"); |
| 104 | + override fun getType(uri: Uri): String? { |
| 105 | + Log.i(TAG, "getType") |
124 | 106 |
|
125 |
| - throw new UnsupportedOperationException("Not yet implemented"); |
| 107 | + throw UnsupportedOperationException("Not yet implemented") |
126 | 108 | }
|
127 | 109 |
|
128 | 110 | /**
|
129 | 111 | * Handles requests to insert a new row.
|
130 | 112 | */
|
131 |
| - @Override |
132 |
| - public Uri insert(Uri uri, ContentValues values) { |
133 |
| - Log.i(getClass().getName(), "insert"); |
| 113 | + override fun insert(uri: Uri, values: ContentValues?): Uri? { |
| 114 | + Log.i(TAG, "insert") |
134 | 115 |
|
135 |
| - throw new UnsupportedOperationException("Not yet implemented"); |
| 116 | + throw UnsupportedOperationException("Not yet implemented") |
136 | 117 | }
|
137 | 118 |
|
138 | 119 | /**
|
139 | 120 | * Handles requests to update one or more rows.
|
140 | 121 | */
|
141 |
| - @Override |
142 |
| - public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
143 |
| - Log.i(getClass().getName(), "update"); |
144 |
| - |
145 |
| - throw new UnsupportedOperationException("Not yet implemented"); |
| 122 | + override fun update( |
| 123 | + uri: Uri, |
| 124 | + values: ContentValues?, |
| 125 | + selection: String?, |
| 126 | + selectionArgs: Array<String>? |
| 127 | + ): Int { |
| 128 | + Log.i(TAG, "update") |
| 129 | + |
| 130 | + throw UnsupportedOperationException("Not yet implemented") |
146 | 131 | }
|
147 | 132 |
|
148 | 133 | /**
|
149 | 134 | * Handle requests to delete one or more rows.
|
150 | 135 | */
|
151 |
| - @Override |
152 |
| - public int delete(Uri uri, String selection, String[] selectionArgs) { |
153 |
| - Log.i(getClass().getName(), "delete"); |
| 136 | + override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int { |
| 137 | + Log.i(TAG, "delete") |
154 | 138 |
|
155 |
| - throw new UnsupportedOperationException("Not yet implemented"); |
| 139 | + throw UnsupportedOperationException("Not yet implemented") |
156 | 140 | }
|
157 | 141 |
|
158 |
| - @Override |
159 |
| - public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { |
160 |
| - List<String> segments = uri.getPathSegments(); |
161 |
| - if (segments.size() < 2) { |
162 |
| - throw new FileNotFoundException("Invalid URI: " + uri); |
| 142 | + @Throws(FileNotFoundException::class) |
| 143 | + override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? { |
| 144 | + val segments = uri.pathSegments |
| 145 | + if (segments.size < 2) { |
| 146 | + throw FileNotFoundException("Invalid URI: $uri") |
163 | 147 | }
|
164 |
| - String fileId = segments.get(1); |
| 148 | + val fileId = segments[1] |
165 | 149 |
|
166 |
| - RoomDb roomDb = RoomDb.getDatabase(getContext()); |
167 |
| - VideoDao videoDao = roomDb.videoDao(); |
168 |
| - Video video = videoDao.load(Long.parseLong(fileId)); |
| 150 | + val roomDb = RoomDb.getDatabase(context) |
| 151 | + val videoDao = roomDb.videoDao() |
| 152 | + val video = videoDao.load(fileId.toLong()) |
169 | 153 |
|
170 |
| - File videoFile = FileHelper.getVideoFile(video, getContext()); |
171 |
| - if (videoFile == null) { |
172 |
| - throw new FileNotFoundException("File not found!"); |
173 |
| - } |
| 154 | + val videoFile = FileHelper.getVideoFile(video, context) |
| 155 | + ?: throw FileNotFoundException("File not found!") |
174 | 156 | if (!videoFile.exists()) {
|
175 |
| - Log.e("VideoContentProvider", "videoFile doesn't exist: " + videoFile.getAbsolutePath()); |
176 |
| - throw new FileNotFoundException("File not found: " + videoFile.getAbsolutePath()); |
| 157 | + Log.e("VideoContentProvider", "videoFile doesn't exist: " + videoFile.absolutePath) |
| 158 | + throw FileNotFoundException("File not found: " + videoFile.absolutePath) |
| 159 | + } |
| 160 | + return ParcelFileDescriptor.open(videoFile, ParcelFileDescriptor.MODE_READ_ONLY) |
| 161 | + } |
| 162 | + |
| 163 | + companion object { |
| 164 | + private const val AUTHORITY: String = BuildConfig.APPLICATION_ID + ".provider.video_provider" |
| 165 | + |
| 166 | + private const val TABLE_VIDEOS = "videos" |
| 167 | + private const val CODE_VIDEOS = 1 |
| 168 | + private const val CODE_VIDEO_ID = 2 |
| 169 | + private const val CODE_VIDEO_TITLE = 4 |
| 170 | + |
| 171 | + private val MATCHER = UriMatcher(UriMatcher.NO_MATCH) |
| 172 | + |
| 173 | + init { |
| 174 | + MATCHER.addURI(AUTHORITY, TABLE_VIDEOS, CODE_VIDEOS) |
| 175 | + MATCHER.addURI(AUTHORITY, TABLE_VIDEOS + "/#", CODE_VIDEO_ID) |
| 176 | + MATCHER.addURI(AUTHORITY, TABLE_VIDEOS + "/by-title/*", CODE_VIDEO_TITLE) |
177 | 177 | }
|
178 |
| - return ParcelFileDescriptor.open(videoFile, ParcelFileDescriptor.MODE_READ_ONLY); |
179 | 178 | }
|
180 | 179 | }
|
0 commit comments