-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ratanparai/feature/content-provider
Content provider to receive and publish search suggestion
- Loading branch information
Showing
9 changed files
with
1,397 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/ratanparai/moviedog/SearchableActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.ratanparai.moviedog | ||
|
||
import android.app.Activity | ||
|
||
class SearchableActivity: Activity() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
app/src/main/java/com/ratanparai/moviedog/provider/VideoProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.ratanparai.moviedog.provider | ||
|
||
import android.app.SearchManager | ||
import android.content.ContentProvider | ||
import android.content.ContentValues | ||
import android.content.UriMatcher | ||
import android.database.Cursor | ||
import android.database.MatrixCursor | ||
import android.net.Uri | ||
import android.provider.BaseColumns | ||
import android.util.Log | ||
import com.ratanparai.moviedog.db.AppDatabase | ||
import com.ratanparai.moviedog.db.dao.MovieDao | ||
import com.ratanparai.moviedog.db.entity.Movie | ||
import com.ratanparai.moviedog.scrapper.DekhvhaiScrapper | ||
|
||
class VideoProvider : ContentProvider() { | ||
|
||
private val TAG = "VideoContentProvider" | ||
|
||
|
||
private lateinit var movieDao: MovieDao | ||
|
||
private lateinit var dekhvhaiScrapper: DekhvhaiScrapper | ||
|
||
private lateinit var uriMatcher: UriMatcher | ||
|
||
private val AUTHORITY = "com.ratanparai.moviedog" | ||
private val SEARCH_SUGGEST = 1 | ||
|
||
private val queryProjection = arrayOf( | ||
BaseColumns._ID, | ||
SearchManager.SUGGEST_COLUMN_TEXT_1, | ||
SearchManager.SUGGEST_COLUMN_TEXT_2, | ||
SearchManager.SUGGEST_COLUMN_RESULT_CARD_IMAGE, | ||
SearchManager.SUGGEST_COLUMN_PRODUCTION_YEAR, | ||
SearchManager.SUGGEST_COLUMN_DURATION, | ||
SearchManager.SUGGEST_COLUMN_INTENT_ACTION, | ||
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID | ||
) | ||
|
||
override fun onCreate(): Boolean { | ||
movieDao = AppDatabase.getInstance(context).movieDao() | ||
uriMatcher = buildUriMatcher() | ||
dekhvhaiScrapper = DekhvhaiScrapper() | ||
return true | ||
} | ||
|
||
|
||
override fun query( | ||
uri: Uri, | ||
projection: Array<String>?, | ||
selection: String?, | ||
selectionArgs: Array<String>?, | ||
sortOrder: String? | ||
): Cursor? { | ||
Log.d(TAG, uri.toString()) | ||
|
||
if (uriMatcher.match(uri) == SEARCH_SUGGEST) { | ||
Log.d(TAG, "Search suggestions requested.") | ||
|
||
return search(uri.lastPathSegment) | ||
|
||
} else { | ||
Log.d(TAG, "Unknown uri to query: $uri") | ||
throw IllegalArgumentException("Unknown Uri: $uri") | ||
} | ||
} | ||
|
||
private fun search(query: String?): Cursor? { | ||
if(query != null) { | ||
val movies = dekhvhaiScrapper.search(query) | ||
val matrixCursor = MatrixCursor(queryProjection) | ||
|
||
for (movie in movies) { | ||
matrixCursor.addRow(convertMovieIntoRow(movie)) | ||
} | ||
return matrixCursor | ||
} | ||
return null | ||
} | ||
|
||
private fun convertMovieIntoRow(movie: Movie): Array<Any> { | ||
return arrayOf( | ||
movie.id, | ||
movie.title, | ||
movie.description, | ||
movie.cardImage, | ||
movie.productionYear, | ||
movie.duration, | ||
"GLOBALSEARCH", | ||
movie.id | ||
) | ||
} | ||
|
||
private fun buildUriMatcher(): UriMatcher { | ||
val uriMatcher = UriMatcher(UriMatcher.NO_MATCH) | ||
uriMatcher.addURI( | ||
AUTHORITY, "/search/" + SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST | ||
) | ||
uriMatcher.addURI( | ||
AUTHORITY, | ||
"/search/" + SearchManager.SUGGEST_URI_PATH_QUERY + "/*", | ||
SEARCH_SUGGEST | ||
) | ||
return uriMatcher | ||
} | ||
|
||
override fun insert(uri: Uri, values: ContentValues?): Uri? { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun getType(uri: Uri): String? { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- The attributes below configure the Android search box appearance | ||
and the search suggestions settings. | ||
See the Developer Guide for more information | ||
http://developer.android.com/guide/topics/search/ | ||
The Assistant's priority order for intent actions are as follows: | ||
1) Intent specified in cursor(SUGGEST_COLUMN_INTENT_ACTION) | ||
2) Intent specific in this file (searchSuggestIntentAction) | ||
3) ACTION_SEARCH(default) | ||
It is recommended to set it to ACTION_VIEW in searchable.xml and only override in code if you | ||
want to handle certain pieces of content differently. | ||
--> | ||
<searchable xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:hint="@string/search_hint" | ||
android:includeInGlobalSearch="true" | ||
android:label="@string/search_label" | ||
android:icon="@drawable/lb_ic_in_app_search" | ||
android:searchSettingsDescription="@string/settings_description" | ||
android:searchSuggestAuthority="com.ratanparai.moviedog" | ||
android:searchSuggestIntentAction="android.intent.action.VIEW" | ||
android:searchSuggestIntentData="content://com.rataparai.moviedog/video" | ||
android:queryAfterZeroResults="true" | ||
android:searchSuggestPath="search" | ||
android:searchSuggestThreshold="0" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.