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

332 lines
13 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-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
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
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
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 {
this.video = this.intent.extras.getSerializable("video") as VideoModel
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
2018-08-18 07:04:31 +02: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
2018-08-18 07:04:31 +02:00
videoView.loadUrl("https://"+ManagerSingleton.url+this.video.embedUrl)
2019-01-25 18:38:59 +01:00
2018-08-18 07:04:31 +02:00
}
catch (err:Exception){
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() }
}
private fun subscribe(){
2019-01-30 20:03:00 +01:00
val account = this.video.nameChannel+"@"+this.video.userHost
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
2018-10-10 19:24:35 +02:00
val res = this._actions.subscribe(ManagerSingleton.token.token, account)
if (res == 1) {
runOnUiThread {
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.subscribeMsg), this)
this.changeSubscribeBtn(true)
}
}
}
}
private fun unSubscribe(){
2019-01-30 20:03:00 +01:00
val account = this.video.nameChannel+"@"+this.video.userHost
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val res = this._actions.unSubscribe(ManagerSingleton.token.token, account)
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
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)
2018-10-10 19:24:35 +02: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))
2018-10-10 19:24:35 +02: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
}
}
}
}
}
private fun getRate(){
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val rate = this._actions.getRate(ManagerSingleton.token.token, this.video.id)
runOnUiThread {
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))
}
}
}
}
}
private fun getSubscription(){
2019-01-30 20:03:00 +01:00
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)
}
}
}
private fun changeSubscribeBtn(subscribed: Boolean){
if(subscribed){
subscribeBtn.text = getText(R.string.unSubscribeBtn)
subscribeBtn.setOnClickListener { this.unSubscribe() }
}
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() {
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()
}
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()
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
if(ManagerSingleton.user.avatar != ""){
Picasso.get().load("https://"+ManagerSingleton.url+ManagerSingleton.user.avatar).into(userImgCom)
}
2018-10-10 19:24:35 +02:00
}
}
2019-02-18 00:13:12 +01:00
fun getDescription(){
AsyncTask.execute {
val fullDescription = this.videos.fullDescription(this.video.id)
runOnUiThread {
descriptionVideoTxt.text = fullDescription
showMoreBtn.visibility = View.GONE
}
}
}
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? {
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
}