Skip to content

Commit

Permalink
choose sources if there is multiple one
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanparai committed Apr 29, 2019
1 parent b92056a commit e909384
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

<activity android:name=".ui.PlaybackActivity"/>
<activity android:name=".ui.MovieDetailsActivity"/>
<activity android:name=".ui.SelectMovieSourceDialogActivity"/>

<provider android:name=".provider.VideoProvider"
android:authorities="com.ratanparai.moviedog"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.ratanparai.moviedog.db.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey

@Entity(tableName = "movieUrl")
@Entity(tableName = "movieUrl", indices = [Index(value = ["movieUrl"], unique = true)])
data class MovieUrl(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Int = 0,
val movieId: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class MovieService(private val context: Context) {
val scrappedDao = AppDatabase.getInstance(context).scrappedDao()
val movieUrlDao = AppDatabase.getInstance(context).movieUrlDao()

scrapMovies(wowMovieZoneScrapper, query, searchHashDao, movieDao, scrappedDao, movieUrlDao, "WoW Movie")
scrapMovies(bdPlexScrapper, query, searchHashDao, movieDao, scrappedDao, movieUrlDao, "BDPlex")
scrapMovies(dekhvhaiScrapper, query, searchHashDao, movieDao, scrappedDao, movieUrlDao, "Dekhvhai")
scrapMovies(wowMovieZoneScrapper, query, searchHashDao, movieDao, scrappedDao, movieUrlDao, "WoW Movie")

return movieDao.searchByTitle(query)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.bumptech.glide.load.resource.drawable.GlideDrawable
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
import com.ratanparai.moviedog.R
import com.ratanparai.moviedog.db.AppDatabase
import com.ratanparai.moviedog.db.entity.Movie
import com.ratanparai.moviedog.presenter.DetailsDescriptionPresenter
import com.ratanparai.moviedog.service.MovieService
Expand Down Expand Up @@ -83,12 +84,17 @@ class MovieDetailsFragment: DetailsFragment() {
)
)

actionAdapter.add(
Action(
ACTION_SELECT_SOURCE,
resources.getString(R.string.select_movie_source)
val movieUrlDao = AppDatabase.getInstance(context).movieUrlDao()
val movieUrlsByMovieId = movieUrlDao.getMovieUrlsByMovieId(movie!!.id)

if (movieUrlsByMovieId.size > 1) {
actionAdapter.add(
Action(
ACTION_SELECT_SOURCE,
resources.getString(R.string.select_movie_source)
)
)
)
}

row.actionsAdapter = actionAdapter

Expand All @@ -106,8 +112,12 @@ class MovieDetailsFragment: DetailsFragment() {
detailsPresenter.onActionClickedListener = OnActionClickedListener { action ->
when {
action.id == ACTION_PLAY_MOVIE -> startActivity(PlaybackActivity.createIntent(context, movie!!.id ))
action.id == ACTION_SELECT_SOURCE -> Toast.makeText(context, "Select Source Selected",
Toast.LENGTH_SHORT).show()
action.id == ACTION_SELECT_SOURCE -> {
val intent = Intent(context, SelectMovieSourceDialogActivity::class.java)
intent.putExtra(EXTRA_MOVIE_ID, movie!!.id)
startActivity(intent)
}

else -> Toast.makeText(context, action.toString(), Toast.LENGTH_SHORT).show()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.KeyEvent
import androidx.fragment.app.FragmentActivity
import com.ratanparai.moviedog.R
import com.ratanparai.moviedog.utilities.EXTRA_MOVIE_ID
import com.ratanparai.moviedog.utilities.EXTRA_MOVIE_URL

class PlaybackActivity: FragmentActivity() {

Expand Down Expand Up @@ -42,5 +43,12 @@ class PlaybackActivity: FragmentActivity() {
intent.putExtra(EXTRA_MOVIE_ID, movieId)
return intent
}

fun createIntent(context: Context, movieId: Int, movieUrl: String): Intent {
val intent = Intent(context, PlaybackActivity::class.java)
intent.putExtra(EXTRA_MOVIE_ID, movieId)
intent.putExtra(EXTRA_MOVIE_URL, movieUrl)
return intent
}
}
}
18 changes: 15 additions & 3 deletions app/src/main/java/com/ratanparai/moviedog/ui/PlaybackFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.ratanparai.moviedog.db.entity.Movie
import com.ratanparai.moviedog.player.VideoPlayerGlue
import com.ratanparai.moviedog.service.MovieService
import com.ratanparai.moviedog.utilities.EXTRA_MOVIE_ID
import com.ratanparai.moviedog.utilities.EXTRA_MOVIE_URL

class PlaybackFragment: VideoSupportFragment() {
private val TAG = "PlaybackFragment"
Expand All @@ -34,12 +35,15 @@ class PlaybackFragment: VideoSupportFragment() {

private var movieIdToPlay: Int = -1

private var movieUrl: String? = null


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG, "Fragment loaded")

val movieId = activity?.intent?.getIntExtra(EXTRA_MOVIE_ID, -1)
movieUrl = activity?.intent?.getStringExtra(EXTRA_MOVIE_URL)

movieIdToPlay = movieId!!

Expand Down Expand Up @@ -109,9 +113,17 @@ class PlaybackFragment: VideoSupportFragment() {

private fun prepareMediaForPlaying(movie: Movie) {
val userAgent = Util.getUserAgent(context, "MovieDog")
val mediaSource = ExtractorMediaSource
.Factory(DefaultDataSourceFactory(context, userAgent))
.createMediaSource(Uri.parse(movie.videoUrl))

val mediaSource = if (movieUrl == null) {
ExtractorMediaSource
.Factory(DefaultDataSourceFactory(context, userAgent))
.createMediaSource(Uri.parse(movie.videoUrl))
} else {
ExtractorMediaSource
.Factory(DefaultDataSourceFactory(context, userAgent))
.createMediaSource(Uri.parse(movieUrl))
}


exoPlayer.prepare(mediaSource)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ratanparai.moviedog.ui

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.leanback.app.GuidedStepFragment

class SelectMovieSourceDialogActivity: Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

window.setBackgroundDrawable(ColorDrawable(Color.parseColor("#21272A")))

if (savedInstanceState == null) {
val fragment = SelectMovieSourceDialogFragment()
GuidedStepFragment.addAsRoot(this, fragment, android.R.id.content)
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.ratanparai.moviedog.ui

import android.os.Bundle
import android.widget.Toast
import androidx.leanback.app.GuidedStepFragment
import androidx.leanback.widget.GuidanceStylist
import androidx.leanback.widget.GuidedAction
import com.ratanparai.moviedog.R
import com.ratanparai.moviedog.db.AppDatabase
import com.ratanparai.moviedog.db.entity.MovieUrl
import com.ratanparai.moviedog.utilities.EXTRA_MOVIE_ID

class SelectMovieSourceDialogFragment: GuidedStepFragment() {

private var movieUrls: List<MovieUrl>? = null

override fun onCreate(savedInstanceState: Bundle?) {

val movieId = activity?.intent?.getIntExtra(EXTRA_MOVIE_ID, -1)

if (movieId == -1) {
activity.finish()
}

movieUrls = AppDatabase.getInstance(context).movieUrlDao().getMovieUrlsByMovieId(movieId!!)

super.onCreate(savedInstanceState)
}

override fun onCreateGuidance(savedInstanceState: Bundle?): GuidanceStylist.Guidance {


val guidance = GuidanceStylist.Guidance(
getString(R.string.dialog_movie_select_title),
getString(R.string.dialog_movie_select_description),
"", null
)

return guidance
}



override fun onCreateActions(actions: MutableList<GuidedAction>, savedInstanceState: Bundle?) {

for (movieUrl in movieUrls!!) {
actions.add(
GuidedAction.Builder()
.id(movieUrl.id.toLong())
.description(movieUrl.movieUrl)
.title(movieUrl.serviceName).build()
)
}

// var action = GuidedAction.Builder()
// .id(ACTION_ID_POSITIVE)
// .title("Positive").build()
// actions.add(action)
// action = GuidedAction.Builder()
// .id(ACTION_ID_NEGATIVE)
// .title("negative").build()
// actions.add(action)
}

override fun onGuidedActionClicked(action: GuidedAction) {
// if (ACTION_ID_POSITIVE == action.id) {
// Toast.makeText(
// activity, "Positive Clicked",
// Toast.LENGTH_SHORT
// ).show()
// } else {
// Toast.makeText(
// activity, "Negative clicked",
// Toast.LENGTH_SHORT
// ).show()
// }

for (movieUrl in movieUrls!!) {
if(movieUrl.id == action.id.toInt())
startActivity(PlaybackActivity.createIntent(context, movieUrl.movieId, movieUrl.movieUrl))
}
}

companion object {
private const val ACTION_ID_POSITIVE = 1L
private const val ACTION_ID_NEGATIVE = 2L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ package com.ratanparai.moviedog.utilities
* Constants used throughout the app.
*/
const val DATABASE_NAME = "moviedog-db"
const val EXTRA_MOVIE_ID = "com.ratanparai.moviedog.extra.MOVIE_ID"
const val EXTRA_MOVIE_ID = "com.ratanparai.moviedog.extra.MOVIE_ID"
const val EXTRA_MOVIE_URL = "com.ratanparai.moviedog.extra.MOVIE_URL"
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
<string name="not_implemented">Not Implemented Yet!</string>
<string name="play_movie">Play</string>
<string name="select_movie_source">Select Source</string>
<string name="dialog_movie_select_title">Select movie source to play</string>
<string name="dialog_movie_select_description">You can choose any source to play movies. If one source failed,
please try another source
</string>
</resources>

0 comments on commit e909384

Please sign in to comment.