p2play-app-android/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt

387 lines
15 KiB
Kotlin
Raw Normal View History

2018-08-18 07:04:31 +02:00
package org.libre.agosto.p2play
2019-02-17 21:29:33 +01:00
import android.annotation.SuppressLint
import android.app.Activity
2019-02-25 03:53:40 +01:00
import android.content.DialogInterface
2019-02-24 17:34:12 +01:00
import android.content.Intent
2019-02-17 21:29:33 +01:00
import android.content.pm.ActivityInfo
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.AsyncTask
2018-08-18 07:04:31 +02:00
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
2018-10-10 19:24:35 +02:00
import android.support.v4.content.ContextCompat
2019-02-25 03:53:40 +01:00
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
2018-08-18 07:04:31 +02:00
import android.util.Log
2018-10-10 19:24:35 +02:00
import android.view.View
2019-02-17 21:29:33 +01:00
import android.view.WindowManager
import android.webkit.WebChromeClient
2019-02-25 03:53:40 +01:00
import android.widget.EditText
import android.widget.FrameLayout
2018-08-18 07:04:31 +02:00
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_reproductor.*
import org.libre.agosto.p2play.adapters.CommentariesAdapter
2018-10-10 19:24:35 +02:00
import org.libre.agosto.p2play.ajax.Actions
2019-01-25 18:38:59 +01:00
import org.libre.agosto.p2play.ajax.Comments
2019-02-18 00:13:12 +01:00
import org.libre.agosto.p2play.ajax.Videos
2019-01-25 18:38:59 +01:00
import org.libre.agosto.p2play.models.CommentaryModel
2018-08-18 07:04:31 +02:00
import org.libre.agosto.p2play.models.VideoModel
2019-02-17 21:29:33 +01:00
2019-12-13 16:53:10 +01:00
@Suppress("NAME_SHADOWING")
2018-08-18 07:04:31 +02:00
class ReproductorActivity : AppCompatActivity() {
2019-02-18 00:13:12 +01:00
lateinit var video: VideoModel
2018-10-10 19:24:35 +02:00
private val _actions: Actions = Actions()
2019-01-25 18:38:59 +01:00
private val client: Comments = Comments()
2019-02-18 00:13:12 +01:00
private val videos: Videos = Videos()
2018-08-18 07:04:31 +02:00
// Commentaries adapter values
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
2019-02-17 21:29:33 +01:00
@SuppressLint("SetJavaScriptEnabled", "SetTextI18n")
2018-08-18 07:04:31 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_reproductor)
videoView.webChromeClient = WebClient()
2018-08-18 07:04:31 +02:00
videoView.settings.javaScriptEnabled = true
videoView.settings.allowContentAccess = true
videoView.settings.javaScriptCanOpenWindowsAutomatically = true
videoView.settings.allowFileAccess = true
videoView.settings.allowFileAccessFromFileURLs = true
videoView.settings.allowUniversalAccessFromFileURLs = true
videoView.settings.domStorageEnabled = true
2018-08-18 07:04:31 +02:00
try {
2024-03-19 01:45:04 +01:00
this.video = this.intent.extras?.getSerializable("video") as VideoModel
2018-08-18 07:04:31 +02:00
tittleVideoTxt.text = this.video.name
2019-02-17 21:29:33 +01:00
viewsTxt.text = "${this.video.views} ${getString(R.string.view_text)}"
2018-08-18 07:04:31 +02:00
userTxt.text = this.video.username
2019-02-17 21:29:33 +01:00
descriptionVideoTxt.text = this.video.description
2019-02-18 01:42:08 +01:00
val haveDescription = this.video.description.endsWith("...")
2019-02-18 00:13:12 +01:00
if (haveDescription) {
showMoreBtn.visibility = View.VISIBLE
}
2019-02-17 21:29:33 +01:00
hostTxt.text = this.video.userHost
2018-08-18 07:04:31 +02:00
2019-01-30 20:03:00 +01:00
// Check if user had profile image
2019-02-25 03:53:40 +01:00
if (this.video.userImageUrl != "")
Picasso.get().load("https://" + ManagerSingleton.url + this.video.userImageUrl).into(userImg)
2019-01-30 20:03:00 +01:00
// Load the video
2019-02-25 03:53:40 +01:00
videoView.loadUrl("https://" + ManagerSingleton.url + this.video.embedUrl)
2019-01-25 18:38:59 +01:00
2019-02-25 03:53:40 +01:00
} catch (err: Exception) {
2018-08-18 07:04:31 +02:00
err.printStackTrace()
}
2019-01-25 18:38:59 +01:00
viewManager = LinearLayoutManager(this)
this.setDataComments(arrayListOf())
this.getComments()
subscribeBtn.setOnClickListener { subscribe() }
2018-10-10 19:24:35 +02:00
likeLayout.setOnClickListener { rate("like") }
dislikeLayout.setOnClickListener { rate("dislike") }
2019-01-25 19:30:48 +01:00
commentaryBtn.setOnClickListener { makeComment() }
2019-02-18 00:13:12 +01:00
showMoreBtn.setOnClickListener { getDescription() }
2019-02-25 03:53:40 +01:00
shareLayout.setOnClickListener { shareIntent() }
reportLayout.setOnClickListener { reportIntent() }
2019-02-24 17:34:12 +01:00
userImg.setOnClickListener {
val intent = Intent(this, ChannelActivity::class.java)
intent.putExtra("channel", video.getAccount())
startActivity(intent)
}
}
2019-02-25 03:53:40 +01:00
private fun subscribe() {
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
2019-02-24 17:34:12 +01:00
val res = this._actions.subscribe(ManagerSingleton.token.token, video.getAccount())
if (res == 1) {
runOnUiThread {
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.subscribeMsg), this)
this.changeSubscribeBtn(true)
}
}
}
}
2019-02-25 03:53:40 +01:00
private fun unSubscribe() {
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
2019-02-24 17:34:12 +01:00
val res = this._actions.unSubscribe(ManagerSingleton.token.token, video.getAccount())
if (res == 1) {
runOnUiThread {
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.unSubscribeMsg), this)
this.changeSubscribeBtn(false)
}
}
}
2018-08-18 07:04:31 +02:00
}
2018-10-10 19:24:35 +02:00
2019-02-25 03:53:40 +01:00
private fun rate(rate: String) {
2018-10-10 19:24:35 +02:00
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val res = this._actions.rate(ManagerSingleton.token.token, this.video.id, rate)
if (res == 1) {
runOnUiThread {
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.rateMsg), this)
2019-02-25 03:53:40 +01:00
if (rate == "like") {
2019-02-10 20:42:29 +01:00
textViewLike.setTextColor(ContextCompat.getColor(this, R.color.colorLike))
textViewDislike.setTextColor(ContextCompat.getColor(this, R.color.primary_dark_material_light))
2019-02-25 03:53:40 +01:00
} else if (rate == "dislike") {
2019-02-10 20:42:29 +01:00
textViewDislike.setTextColor(ContextCompat.getColor(this, R.color.colorDislike))
textViewLike.setTextColor(ContextCompat.getColor(this, R.color.primary_dark_material_light))
2018-10-10 19:24:35 +02:00
}
}
}
}
}
2019-02-25 03:53:40 +01:00
private fun getRate() {
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val rate = this._actions.getRate(ManagerSingleton.token.token, this.video.id)
runOnUiThread {
2019-02-25 03:53:40 +01:00
when (rate) {
"like" -> {
2019-02-10 20:42:29 +01:00
textViewLike.setTextColor(ContextCompat.getColor(this, R.color.colorLike))
textViewDislike.setTextColor(ContextCompat.getColor(this, R.color.primary_dark_material_light))
}
"dislike" -> {
2019-02-10 20:42:29 +01:00
textViewDislike.setTextColor(ContextCompat.getColor(this, R.color.colorDislike))
textViewLike.setTextColor(ContextCompat.getColor(this, R.color.primary_dark_material_light))
}
else -> {
2019-02-10 20:42:29 +01:00
textViewLike.setTextColor(ContextCompat.getColor(this, R.color.primary_dark_material_light))
textViewDislike.setTextColor(ContextCompat.getColor(this, R.color.primary_dark_material_light))
}
}
}
}
}
2019-02-25 03:53:40 +01:00
private fun getSubscription() {
val account = this.video.nameChannel + "@" + this.video.userHost
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val isSubscribed = this._actions.getSubscription(ManagerSingleton.token.token, account)
runOnUiThread {
this.changeSubscribeBtn(isSubscribed)
}
}
}
2019-02-25 03:53:40 +01:00
private fun changeSubscribeBtn(subscribed: Boolean) {
if (subscribed) {
subscribeBtn.text = getText(R.string.unSubscribeBtn)
subscribeBtn.setOnClickListener { this.unSubscribe() }
2019-02-25 03:53:40 +01:00
} else {
subscribeBtn.text = getText(R.string.subscribeBtn)
subscribeBtn.setOnClickListener { this.subscribe() }
}
}
2019-01-25 18:38:59 +01:00
private fun setDataComments(data: ArrayList<CommentaryModel>) {
// Set data for RecyclerView
viewAdapter = CommentariesAdapter(data)
recyclerView = findViewById<RecyclerView>(R.id.listCommentaries).apply {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
setHasFixedSize(true)
// use a linear layout manager
layoutManager = viewManager
// specify an viewAdapter (see also next example)
adapter = viewAdapter
}
}
private fun getComments() {
AsyncTask.execute {
val data = this.client.getCommentaries(this.video.id)
runOnUiThread {
this.setDataComments(data)
}
}
}
2019-01-25 19:30:48 +01:00
private fun makeComment() {
2019-02-25 03:53:40 +01:00
if (commentaryText.text.toString() == "") {
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.emptyCommentaryMsg), this)
2019-01-25 19:30:48 +01:00
return
}
val text = commentaryText.text.toString()
AsyncTask.execute {
val res = this.client.makeCommentary(ManagerSingleton.token.token, this.video.id, text)
runOnUiThread {
if (res) {
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.makedCommentaryMsg), this)
2019-01-25 19:30:48 +01:00
commentaryText.text.clear()
this.getComments()
2019-02-25 03:53:40 +01:00
} else {
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.errorCommentaryMsg), this)
2019-01-25 19:30:48 +01:00
}
}
}
}
2018-10-10 19:24:35 +02:00
override fun onResume() {
super.onResume()
2019-02-25 03:53:40 +01:00
if (ManagerSingleton.user.status == 1) {
this.getRate()
this.getSubscription()
2018-10-10 19:24:35 +02:00
actionsLayout.visibility = View.VISIBLE
subscribeBtn.visibility = View.VISIBLE
2019-01-25 19:30:48 +01:00
commentaryLayout.visibility = View.VISIBLE
2019-02-25 03:53:40 +01:00
if (ManagerSingleton.user.avatar != "") {
Picasso.get().load("https://" + ManagerSingleton.url + ManagerSingleton.user.avatar).into(userImgCom)
2019-01-25 19:30:48 +01:00
}
2018-10-10 19:24:35 +02:00
}
}
2019-02-25 03:53:40 +01:00
fun getDescription() {
2019-02-18 00:13:12 +01:00
AsyncTask.execute {
val fullDescription = this.videos.fullDescription(this.video.id)
runOnUiThread {
descriptionVideoTxt.text = fullDescription
showMoreBtn.visibility = View.GONE
}
}
}
2019-02-25 03:53:40 +01:00
private fun shareIntent() {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "${video.name} ${video.getVideoUrl()}")
type = "text/plain"
}
startActivity(Intent.createChooser(sendIntent, resources.getText(R.string.shareBtn)))
}
private fun reportIntent() {
val builder = AlertDialog.Builder(this)
// Get the layout inflater
val dialog = layoutInflater.inflate(R.layout.report_dialog, null)
val inputReason = dialog.findViewById<EditText>(R.id.reportText)
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(dialog)
// Add action buttons
2019-10-24 21:34:14 +02:00
.setPositiveButton(R.string.reportBtn) { _, _ ->
2019-02-25 03:53:40 +01:00
val reason = inputReason.text.toString()
reportVideo(reason)
}
2019-10-24 21:34:14 +02:00
.setNegativeButton("Cancel") { dialog, _ ->
dialog.run { cancel() }
2019-02-25 03:53:40 +01:00
}
val alertDialog = builder.create()
alertDialog.show()
}
private fun reportVideo(reason: String){
AsyncTask.execute {
val res = _actions.reportVideo(video.id, reason, ManagerSingleton.token.token)
runOnUiThread {
if(res) {
ManagerSingleton.Toast(getText(R.string.reportDialogMsg).toString(), this)
}
else {
ManagerSingleton.Toast(getText(R.string.errorMsg).toString(), this)
}
}
}
}
2019-02-13 05:40:35 +01:00
internal inner class WebClient: WebChromeClient() {
private var mCustomView: View? = null
private var mCustomViewCallback: WebChromeClient.CustomViewCallback? = null
private var mOriginalOrientation: Int = 0
private var mOriginalSystemUiVisibility: Int = 0
override fun getDefaultVideoPoster(): Bitmap? {
2019-10-24 20:46:23 +02:00
AsyncTask.execute {
this@ReproductorActivity._actions.watchVideo(this@ReproductorActivity.video.id, ManagerSingleton.token.token)
}
return if (mCustomView == null) {
null
} else BitmapFactory.decodeResource(this@ReproductorActivity.resources, 2130837573)
}
override fun onHideCustomView() {
try {
2019-02-17 21:29:33 +01:00
this@ReproductorActivity.nonFullScreen.visibility = View.VISIBLE
this@ReproductorActivity.fullScreen.visibility = View.GONE
this@ReproductorActivity.fullScreen.removeView(this.mCustomView)
this.mCustomView = null
2019-02-17 21:29:33 +01:00
this.mCustomViewCallback!!.onCustomViewHidden()
this.mCustomViewCallback = null
2019-02-17 21:29:33 +01:00
this@ReproductorActivity.requestedOrientation = this.mOriginalOrientation
// this@ReproductorActivity.window.decorView.systemUiVisibility = this.mOriginalSystemUiVisibility
val attrs = this@ReproductorActivity.window.attributes
attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON.inv()
window.attributes = attrs
this@ReproductorActivity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
}
catch (err: Exception){
err.printStackTrace()
}
}
override fun onShowCustomView(paramView: View, paramCustomViewCallback: WebChromeClient.CustomViewCallback) {
if (this.mCustomView != null) {
this.onHideCustomView()
return
}
try {
this.mCustomView = paramView
this.mOriginalSystemUiVisibility = this@ReproductorActivity.window.decorView.systemUiVisibility
this.mOriginalOrientation = this@ReproductorActivity.requestedOrientation
this.mCustomViewCallback = paramCustomViewCallback
2019-02-17 21:29:33 +01:00
val match_parent = WindowManager.LayoutParams.MATCH_PARENT
this@ReproductorActivity.nonFullScreen.visibility = View.GONE
this@ReproductorActivity.fullScreen.visibility = View.VISIBLE
this@ReproductorActivity.fullScreen.addView(paramView, FrameLayout.LayoutParams(match_parent, match_parent))
val attrs = this@ReproductorActivity.window.attributes
attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
window.attributes = attrs
this@ReproductorActivity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
this@ReproductorActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
catch (err: Exception){
err.printStackTrace()
}
}
}
2018-08-18 07:04:31 +02:00
}