diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 23f17bd..d503a06 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -11,10 +11,13 @@
+
+
+
-
-
-
+
+
+
@@ -38,37 +41,34 @@
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -77,18 +77,28 @@
-
-
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
@@ -101,7 +111,16 @@
Looper.prepare()
setData
logout
+ TokenModel
+ TokenModel(
+ token.token
+ token
+ Async
+ logout(
+
+ $PROJECT_DIR$/app
+
@@ -2238,24 +2257,26 @@
-
-
-
-
+
+
+
+
+
+
-
-
-
-
+
+
+
+
@@ -2272,10 +2293,6 @@
-
-
-
-
@@ -2283,6 +2300,11 @@
+
+
+
+
+
@@ -2305,14 +2327,14 @@
-
-
-
-
+
+
+
+
@@ -2498,15 +2520,16 @@
-
+
+
-
+
@@ -2517,17 +2540,72 @@
-
-
+
+
+
+ file://$PROJECT_DIR$/app/src/main/java/org/libre/agosto/p2play/ajax/Auth.kt
+ 76
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -2611,30 +2689,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -2651,14 +2705,6 @@
-
-
-
-
-
-
-
-
@@ -2675,30 +2721,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -2710,16 +2732,88 @@
-
-
-
-
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/org/libre/agosto/p2play/Database.kt b/app/src/main/java/org/libre/agosto/p2play/Database.kt
index d4e9ebd..200a865 100644
--- a/app/src/main/java/org/libre/agosto/p2play/Database.kt
+++ b/app/src/main/java/org/libre/agosto/p2play/Database.kt
@@ -13,7 +13,7 @@ class Database(context:Context): SQLiteOpenHelper(context,"p2play",null,1) {
private val dbUsers = "CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT, uuid INTEGER, username varchar(30), " +
"nsfw INTEGER, email string, followers INTEGER, avatar string, status integer)"
- private val dbTokens = "CREATE TABLE tokens(id INTEGER PRIMARY KEY AUTOINCREMENT, token STRING, status INTEGER)"
+ private val dbTokens = "CREATE TABLE tokens(id INTEGER PRIMARY KEY AUTOINCREMENT, token STRING, refresh_token STRING, status INTEGER)"
override fun onCreate(db: SQLiteDatabase?) {
db?.execSQL(dbUsers)
db?.execSQL(dbTokens)
@@ -30,6 +30,7 @@ class Database(context:Context): SQLiteOpenHelper(context,"p2play",null,1) {
this.closeTokens()
val newToken=ContentValues()
newToken.put("token", token.token)
+ newToken.put("refresh_token", token.refresh_token)
newToken.put("status", token.status)
db.insert("tokens",null,newToken)
@@ -63,6 +64,7 @@ class Database(context:Context): SQLiteOpenHelper(context,"p2play",null,1) {
cursor.moveToFirst()
token.token = cursor.getString(cursor.getColumnIndex("token")).toString()
+ token.refresh_token = cursor.getString(cursor.getColumnIndex("refresh_token")).toString()
token.status = cursor.getString(cursor.getColumnIndex("status")).toInt()
cursor.close()
diff --git a/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt b/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt
index 4bd2205..210892d 100644
--- a/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt
+++ b/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt
@@ -9,12 +9,13 @@ import android.os.Looper
import android.preference.PreferenceManager
import android.util.Log
import kotlinx.android.synthetic.main.activity_host.*
+import org.libre.agosto.p2play.ajax.Auth
import org.libre.agosto.p2play.ajax.Client
class HostActivity : AppCompatActivity() {
lateinit var settings: SharedPreferences
lateinit var editor: SharedPreferences.Editor
- val client:Client = Client()
+ val client:Auth = Auth()
val _db = Database(this)
override fun onCreate(savedInstanceState: Bundle?) {
@@ -44,15 +45,30 @@ class HostActivity : AppCompatActivity() {
fun checkUser(){
val token = _db.getToken()
val user = _db.getUser()
- if(token.status == 1 && user.status==1){
- ManagerSingleton.user = user
- ManagerSingleton.token = token
- }
- else{
- _db.logout()
- }
+ AsyncTask.execute {
+ if (Looper.myLooper() == null)
+ Looper.prepare()
+ if (token.status == 1 && user.status == 1) {
+ startApp()
+ val client_id = settings.getString("client_id", "")
+ val client_secret = settings.getString("client_secret", "")
- startApp()
+ val newToken = client.refreshToken(token, client_id, client_secret)
+
+ when (token.status.toString()) {
+ "1" -> {
+ _db.newToken(newToken)
+ ManagerSingleton.token = newToken
+ ManagerSingleton.user = user
+ }
+ else -> _db.logout()
+ }
+ } else {
+ _db.logout()
+ }
+
+ this.finish()
+ }
}
fun saveHost(host: String){
@@ -95,8 +111,6 @@ class HostActivity : AppCompatActivity() {
runOnUiThread {
val intent = Intent(ManagerSingleton.context,MainActivity::class.java)
startActivity(intent)
-
- this.finish()
}
}
}
diff --git a/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt b/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt
index f36a696..11617e0 100644
--- a/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt
+++ b/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt
@@ -100,6 +100,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// Last videos
fun getLastVideos(){
+ swipeContainer.isRefreshing = true
section = "last"
setTitle(R.string.title_recent)
AsyncTask.execute {
@@ -112,6 +113,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
//
fun getPopularVideos(){
+ swipeContainer.isRefreshing = true
section = "popular"
setTitle(R.string.title_popular)
AsyncTask.execute {
@@ -123,6 +125,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
fun getLocalVideos(){
+ swipeContainer.isRefreshing = true
section = "local"
setTitle(R.string.title_local)
AsyncTask.execute {
@@ -134,6 +137,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
fun getMyVideos(){
+ swipeContainer.isRefreshing = true
section = "my_videos"
setTitle(R.string.title_myVideos)
AsyncTask.execute {
@@ -245,6 +249,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
myMenu.findItem(R.id.action_login).isVisible = true
myMenu.findItem(R.id.action_logout).isVisible = false
}
+
side_usernameTxt?.text = getString(R.string.nav_header_title)
side_emailTxt?.text = getString(R.string.nav_header_subtitle)
side_imageView?.setImageResource(R.mipmap.ic_launcher_round)
@@ -252,6 +257,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
_db.logout()
ManagerSingleton.logout()
+ this.refresh()
ManagerSingleton.Toast(getString(R.string.logout_msg))
}
diff --git a/app/src/main/java/org/libre/agosto/p2play/ajax/Auth.kt b/app/src/main/java/org/libre/agosto/p2play/ajax/Auth.kt
index bfd8525..6383f0d 100644
--- a/app/src/main/java/org/libre/agosto/p2play/ajax/Auth.kt
+++ b/app/src/main/java/org/libre/agosto/p2play/ajax/Auth.kt
@@ -27,7 +27,8 @@ class Auth: Client() {
while(data.hasNext()){
val k = data.nextName()
when(k.toString()){
- "access_token" -> token.token = "Bearer " + data.nextString()
+ "access_token" -> token.token = data.nextString()
+ "refresh_token" -> token.refresh_token = data.nextString()
else -> data.skipValue()
}
}
@@ -69,6 +70,43 @@ class Auth: Client() {
}
+ 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"
+ con.outputStream.write(params.toByteArray())
+ var token = TokenModel()
+
+ try {
+ if(con.responseCode==200){
+ var response = InputStreamReader(con.inputStream)
+ var data = JsonReader(response)
+ data.beginObject()
+
+ while(data.hasNext()){
+ val k = data.nextName()
+ when(k.toString()){
+ "access_token" -> token.token = data.nextString()
+ "refresh_token" -> token.refresh_token = data.nextString()
+ else -> data.skipValue()
+ }
+ }
+
+ data.endObject()
+ token.status = 1
+
+ }
+ else{
+ Log.d("Status", con.responseMessage)
+ }
+ }
+ catch (err: Exception){
+ err.printStackTrace()
+ token.status = 0
+ }
+
+ return token
+ }
+
fun me(token: String): UserModel{
var con = this._newCon("users/me","GET", token)
diff --git a/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt b/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt
index 3df2c71..475cd00 100644
--- a/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt
+++ b/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt
@@ -21,7 +21,7 @@ open class Client {
con.setRequestProperty("Accept", "*/*")
if(token != ""){
- con.setRequestProperty("Authorization", token)
+ con.setRequestProperty("Authorization", "Bearer ${token}")
}
con.requestMethod=method
diff --git a/app/src/main/java/org/libre/agosto/p2play/models/TokenModel.kt b/app/src/main/java/org/libre/agosto/p2play/models/TokenModel.kt
index dfe5dbc..c3e1c5d 100644
--- a/app/src/main/java/org/libre/agosto/p2play/models/TokenModel.kt
+++ b/app/src/main/java/org/libre/agosto/p2play/models/TokenModel.kt
@@ -2,5 +2,6 @@ package org.libre.agosto.p2play.models
class TokenModel (
var token: String = "",
+ var refresh_token: String = "",
var status: Int = -1
)
\ No newline at end of file