Commit 23dfb98a authored by Ivan Agosto's avatar Ivan Agosto

Merge branch 'development' into 'master'

Development

See merge request !5
parents d15ad100 22a2772a
......@@ -23,7 +23,7 @@ Comming soon!
## Features
- Show recent, popular and local list of videos.
- Reproduce videos (very simple)
- Reproduce videos
- Login and register in your instance
- Pull to refresh
- Show uploaded videos
......@@ -34,12 +34,12 @@ Comming soon!
- Splash screen
- Search videos
- Infinite scroll
## What to do? (in next version)
- Share videos
- Report videos
- Peertube profiles
## What to do? (in next version)
- History of videos watched
- Notifications
......
......@@ -7,11 +7,12 @@ apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "mx.agosto182.p2play"
applicationId "org.libre.agosto.p2play"
minSdkVersion 21
//noinspection OldTargetApi
targetSdkVersion 27
versionCode 4
versionName "0.3"
versionCode 5
versionName "0.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
......
......@@ -12,6 +12,8 @@
android:roundIcon="@mipmap/ic_p2play"
android:supportsRtl="true"
android:theme="@style/P2playTheme">
<activity android:name=".ChannelActivity"
android:theme="@style/P2playTheme.noBar"></activity>
<activity
android:name=".SplashActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
......@@ -27,8 +29,9 @@
android:theme="@style/P2playTheme.NoActionBar" />
<activity
android:name=".ReproductorActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
android:configChanges="orientation|screenSize"/>
android:theme="@style/P2playTheme.noBar" />
<activity android:name=".LoginActivity" />
<activity android:name=".RegisterActivity" />
<activity android:name=".AboutActivity" />
......
package org.libre.agosto.p2play
import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_channel.*
import org.libre.agosto.p2play.adapters.VideosAdapter
import org.libre.agosto.p2play.ajax.Actions
import org.libre.agosto.p2play.ajax.Channels
import org.libre.agosto.p2play.ajax.Videos
import org.libre.agosto.p2play.models.ChannelModel
import org.libre.agosto.p2play.models.VideoModel
class ChannelActivity : AppCompatActivity() {
private lateinit var channelId: String
private lateinit var channel: ChannelModel
private var isSubcribed: Boolean = false
private val _channel = Channels()
private val _videos = Videos()
private val _actions = Actions()
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<VideosAdapter.ViewHolder>
private lateinit var viewManager: RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_channel)
channelId = this.intent.extras.getString("channel")
viewManager = LinearLayoutManager(this)
subcriptionBtn.setOnClickListener {
subscribeAction()
}
}
override fun onResume() {
super.onResume()
getChannel()
getSubscription()
getVideos()
if(ManagerSingleton.user.status == 1) {
subcriptionBtn.visibility = View.VISIBLE
getSubscription()
}
}
private fun getChannel() {
AsyncTask.execute {
channel = _channel.getChannelInfo(channelId)
runOnUiThread {
usernameProfile.text = channel.name
hostTxt.text = channel.host
subcriptionsTxt.text = channel.followers.toString()
if(channel.channelImg != "")
Picasso.get().load("https://${ManagerSingleton.url}${channel.channelImg}").into(channelImg)
}
}
}
private fun subscribe() {
AsyncTask.execute {
val res = _actions.subscribe(ManagerSingleton.token.token, channel.getAccount())
runOnUiThread {
if(res == 1){
subcriptionBtn.text = getString(R.string.unSubscribeBtn)
ManagerSingleton.Toast(getString(R.string.subscribeMsg), this)
getSubscription()
}
else {
ManagerSingleton.Toast(getString(R.string.errorMsg), this)
}
}
}
}
private fun unSubscribe() {
AsyncTask.execute {
val res = _actions.unSubscribe(ManagerSingleton.token.token, channel.getAccount())
runOnUiThread {
if(res == 1){
subcriptionBtn.text = getString(R.string.subscribeBtn)
ManagerSingleton.Toast(getString(R.string.unSubscribeMsg), this)
getSubscription()
}
else {
ManagerSingleton.Toast(getString(R.string.errorMsg), this)
}
}
}
}
private fun subscribeAction() {
if(isSubcribed)
unSubscribe()
else
subscribe()
}
private fun getSubscription() {
AsyncTask.execute {
isSubcribed = _actions.getSubscription(ManagerSingleton.token.token, channel.getAccount())
runOnUiThread {
if(isSubcribed){
subcriptionBtn.text = getText(R.string.unSubscribeBtn)
}
else {
subcriptionBtn.text = getText(R.string.subscribeBtn)
}
}
}
}
private fun getVideos() {
AsyncTask.execute {
val videos = _videos.channelVideos(channel.getAccount(), 0)
runOnUiThread {
initRecycler(videos)
}
}
}
// Generic function for set data to RecyclerView
private fun initRecycler(data: ArrayList<VideoModel>){
// val data = arrayListOf<VideoModel>()
viewAdapter = VideosAdapter(data)
recyclerView = findViewById<RecyclerView>(R.id.listVideosChannel).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
}
// swipeContainer.isRefreshing = false
}
}
\ No newline at end of file
......@@ -60,7 +60,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// Init RecyclerView
this.initRecycler()
this.getLastVideos()
this.getTrengindVideos()
swipeContainer.setOnRefreshListener {
this.refresh()
......@@ -127,6 +127,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
when(section){
"local" -> this.getLocalVideos()
"popular" -> this.getPopularVideos()
"trending" -> this.getTrengindVideos()
"last" -> this.getLastVideos()
"sub" -> this.getSubscriptionVideos()
"search" -> this.searchVideos()
......@@ -182,6 +183,19 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
}
// Trending videos
private fun getTrengindVideos(){
swipeContainer.isRefreshing = true
section = "trending"
setTitle(R.string.title_trending)
AsyncTask.execute {
val videos = client.getTrendingVideos(this.pagination)
runOnUiThread {
this.addVideos(videos)
}
}
}
// Local videos
private fun getLocalVideos(){
swipeContainer.isRefreshing = true
......@@ -286,19 +300,12 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// item.isChecked = true
when (item.itemId) {
R.id.nav_subscriptions->{
getSubscriptionVideos()
}
R.id.nav_popular-> {
getPopularVideos()
}
R.id.nav_recent-> {
getLastVideos()
}
R.id.nav_local-> {
getLocalVideos()
}
R.id.nav_about-> {
R.id.nav_subscriptions -> getSubscriptionVideos()
R.id.nav_popular -> getPopularVideos()
R.id.nav_trending -> getTrengindVideos()
R.id.nav_recent -> getLastVideos()
R.id.nav_local -> getLocalVideos()
R.id.nav_about -> {
val intent = Intent(this, AboutActivity::class.java)
startActivity(intent)
}
......@@ -320,6 +327,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
if(ManagerSingleton.user.avatar!="" && side_imageView != null)
Picasso.get().load("https://"+ManagerSingleton.url+ManagerSingleton.user.avatar).into(side_imageView)
side_imageView?.setOnClickListener {
pagination = 0
getMyVideos()
drawer_layout.closeDrawer(GravityCompat.START)
}
......@@ -355,6 +363,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
when(section){
"local" -> this.getLocalVideos()
"popular" -> this.getPopularVideos()
"trending" -> this.getTrengindVideos()
"last" -> this.getLastVideos()
"sub" -> this.getSubscriptionVideos()
"search" -> this.searchVideos()
......
......@@ -6,6 +6,7 @@ import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.AsyncTask
import android.support.v7.widget.RecyclerView
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -13,10 +14,7 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.squareup.picasso.Picasso
import org.libre.agosto.p2play.MainActivity
import org.libre.agosto.p2play.ManagerSingleton
import org.libre.agosto.p2play.R
import org.libre.agosto.p2play.ReproductorActivity
import org.libre.agosto.p2play.*
import org.libre.agosto.p2play.models.CommentaryModel
import org.libre.agosto.p2play.models.VideoModel
import java.io.InputStream
......@@ -62,10 +60,16 @@ class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
// - replace the contents of the view with that element
holder.username.text = myDataset[position].username
// holder.userImg.setOnClickListener {
// val intent = Intent(holder.context, ChannelActivity::class.java)
// intent.putExtra("channel", myDataset[position])
// holder.context.startActivity(intent)
// }
if(myDataset[position].userImageUrl!="")
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg);
holder.commentary.text = myDataset[position].commentary
holder.commentary.text = Html.fromHtml(myDataset[position].commentary)
}
......
......@@ -13,10 +13,7 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.squareup.picasso.Picasso
import org.libre.agosto.p2play.MainActivity
import org.libre.agosto.p2play.ManagerSingleton
import org.libre.agosto.p2play.R
import org.libre.agosto.p2play.ReproductorActivity
import org.libre.agosto.p2play.*
import org.libre.agosto.p2play.models.VideoModel
import java.io.InputStream
import java.io.Serializable
......@@ -69,6 +66,13 @@ class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
intent.putExtra("video", myDataset[position] as Serializable)
holder.context.startActivity(intent)
}
holder.userImg.setOnClickListener {
val intent = Intent(holder.context, ChannelActivity::class.java)
intent.putExtra("channel", myDataset[position].getAccount())
holder.context.startActivity(intent)
}
if(myDataset[position].userImageUrl!="")
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg)
else
......
......@@ -6,7 +6,7 @@ import java.io.InputStreamReader
class Actions: Client() {
fun subscribe(token: String, account: String):Int{
var con=this._newCon("users/me/subscriptions","POST", token)
val con = this._newCon("users/me/subscriptions","POST", token)
val params:String= "uri=$account"
con.outputStream.write(params.toByteArray())
var response = 0
......@@ -21,11 +21,12 @@ class Actions: Client() {
response = -1
}
con.disconnect()
return response
}
fun unSubscribe(token: String, account: String):Int{
var con=this._newCon("users/me/subscriptions/$account","DELETE", token)
val con = this._newCon("users/me/subscriptions/$account","DELETE", token)
var response = 0
try {
......@@ -38,17 +39,18 @@ class Actions: Client() {
response = -1
}
con.disconnect()
return response
}
fun getSubscription(token: String, account: String): Boolean{
var con=this._newCon("users/me/subscriptions/exist?uris=$account","GET", token)
val con = this._newCon("users/me/subscriptions/exist?uris=$account","GET", token)
var isSubscribed = false
try {
if (con.responseCode == 200) {
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
data.beginObject()
while (data.hasNext()){
val key = data.nextName()
......@@ -61,6 +63,7 @@ class Actions: Client() {
}
}
}
data.close()
}
}
catch (err: Exception){
......@@ -68,12 +71,13 @@ class Actions: Client() {
isSubscribed = false
}
con.disconnect()
return isSubscribed
}
fun rate(token: String, id_video: Int, rate: String):Int{
var con=this._newCon("videos/$id_video/rate","PUT", token)
val params:String= "rating=$rate"
val con = this._newCon("videos/$id_video/rate","PUT", token)
val params = "rating=$rate"
con.outputStream.write(params.toByteArray())
var response = 0
......@@ -87,17 +91,18 @@ class Actions: Client() {
response = -1
}
con.disconnect()
return response
}
fun getRate(token: String, id_video: Int):String{
var con=this._newCon("users/me/videos/$id_video/rating","GET", token)
val con = this._newCon("users/me/videos/$id_video/rating","GET", token)
var rating = "none"
try {
if (con.responseCode == 200) {
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
data.beginObject()
while (data.hasNext()){
val key = data.nextName()
......@@ -110,6 +115,7 @@ class Actions: Client() {
}
}
}
con.disconnect()
}
}
catch (err: Exception){
......@@ -117,7 +123,25 @@ class Actions: Client() {
rating = "none"
}
con.disconnect()
return rating
}
fun reportVideo(videoId: Int, reason: String, token: String): Boolean {
val con = this._newCon("videos/$videoId/abuse", "POST", token)
val params = "reason=$reason"
con.outputStream.write(params.toByteArray())
var response = false
try {
if(con.responseCode == 200){
response = true
}
} catch (err: Exception) {
err.printStackTrace()
response = false
}
return response
}
}
\ No newline at end of file
......@@ -10,19 +10,19 @@ import org.libre.agosto.p2play.models.UserModel
import java.io.InputStreamReader
class Auth: Client() {
val stockParams = "grant_type=password"
private val stockParams = "grant_type=password"
fun login(username: String, password: String, client_id: String, client_secret: String): TokenModel{
var con = this._newCon("users/token","POST")
val params:String= "$stockParams&username=$username&password=$password&client_id=$client_id&client_secret=$client_secret"
val con = this._newCon("users/token","POST")
val params = "$stockParams&username=$username&password=$password&client_id=$client_id&client_secret=$client_secret"
con.outputStream.write(params.toByteArray())
var token = TokenModel()
val token = TokenModel()
try {
if(con.responseCode==200){
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
data.beginObject()
while(data.hasNext()){
......@@ -35,6 +35,7 @@ class Auth: Client() {
}
data.endObject()
data.close()
token.status = 1
}
......@@ -47,12 +48,13 @@ class Auth: Client() {
token.status = 0
}
con.disconnect()
return token
}
fun register(username: String, password: String, email: String): Int{
var con = this._newCon("users/register","POST")
val params:String= "username=$username&password=$password&email=$email"
val con = this._newCon("users/register","POST")
val params = "username=$username&password=$password&email=$email"
con.outputStream.write(params.toByteArray())
var response = 0
......@@ -67,20 +69,21 @@ class Auth: Client() {
response = -1
}
con.disconnect()
return response
}
fun refreshToken(token: TokenModel, client_id: String, client_secret: String): TokenModel{
var con = this._newCon("users/token", "POST", token.token)
val params:String = "refresh_token=${token.refresh_token}&response_type=code&grant_type=refresh_token&client_id=$client_id&client_secret=$client_secret"
val con = this._newCon("users/token", "POST", token.token)
val params = "refresh_token=${token.refresh_token}&response_type=code&grant_type=refresh_token&client_id=$client_id&client_secret=$client_secret"
con.outputStream.write(params.toByteArray())
var token = TokenModel()
// val token = TokenModel()
try {
if(con.responseCode==200){
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
data.beginObject()
while(data.hasNext()){
......@@ -93,6 +96,7 @@ class Auth: Client() {
}
data.endObject()
data.close()
token.status = 1
}
......@@ -105,19 +109,19 @@ class Auth: Client() {
token.status = 0
}
con.disconnect()
return token
}
fun me(token: String): UserModel{
var con = this._newCon("users/me","GET", token)
var user = UserModel()
val con = this._newCon("users/me","GET", token)
val user = UserModel()
try {
if(con.responseCode==200){
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
data.beginObject()
while(data.hasNext()){
......@@ -159,6 +163,7 @@ class Auth: Client() {
}
data.endObject()
data.close()
user.status = 1
}
......@@ -171,6 +176,7 @@ class Auth: Client() {
user.status = 0
}
con.disconnect()
return user
}
......
package org.libre.agosto.p2play.ajax
import android.util.JsonReader
import android.util.JsonToken
import org.libre.agosto.p2play.models.ChannelModel
import org.libre.agosto.p2play.models.CommentaryModel
import java.io.InputStreamReader
class Channels: Client() {
private fun parseChannel(data: JsonReader): ChannelModel{
val channel = ChannelModel()
data.close()
return channel
}
fun getChannelInfo(account: String): ChannelModel {
val con = this._newCon("video-channels/$account", "GET")
var channel = ChannelModel()
try {
if(con.responseCode == 200){
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
channel.parseChannel(data)
data.close()
}
}catch (err: Exception) {
err.printStackTrace()
}
return channel
}
}
\ No newline at end of file
package org.libre.agosto.p2play.ajax
import android.content.SharedPreferences
import android.util.JsonReader
import android.util.Log
import org.libre.agosto.p2play.ManagerSingleton
import org.libre.agosto.p2play.models.HostModel
import java.io.InputStreamReader
import java.io.Reader
import java.net.HttpURLConnection
import java.net.URL
......@@ -13,22 +13,22 @@ open class Client {
protected fun _newCon(uri: String, method: String, token: String = ""): HttpURLConnection {
var url = URL("https://"+ManagerSingleton.url+"/api/v1/"+uri)
var con = url.openConnection() as HttpURLConnection
val url = URL("https://${ManagerSingleton.url}/api/v1/$uri")
val con = url.openConnection() as HttpURLConnection
con.setRequestProperty("User-Agent", "P2play/0.1")
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
con.setRequestProperty("Accept", "*/*")
if(token != ""){
con.setRequestProperty("Authorization", "Bearer ${token}")
con.setRequestProperty("Authorization", "Bearer $token")
}
con.requestMethod=method
con.connectTimeout=10000
con.readTimeout=10000
if(method.equals("POST"))
if(method == "POST")
con.doOutput=true
Log.d("Petition", url.toString())
......@@ -36,12 +36,12 @@ open class Client {
}
fun getKeys():HostModel{
var con=this._newCon("oauth-clients/local","GET")
var keys = HostModel("","")
val con = this._newCon("oauth-clients/local","GET")
val keys = HostModel("","")
try {
if (con.responseCode == 200) {
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
data.beginObject()
while (data.hasNext()) {
val key = data.nextName()
......@@ -57,13 +57,14 @@ open class Client {
}
}
}
data.close()
}
Log.d("Key",keys.client_id)
return keys