aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt23
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/activity/OAuthActivity.kt84
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/twitter/OAuthSession.kt72
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/twitter/task/Accounts.kt12
4 files changed, 72 insertions, 119 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt
index 37eec009..21b518d7 100644
--- a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt
+++ b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt
@@ -42,10 +42,12 @@ import net.lacolaco.smileessence.World
import net.lacolaco.smileessence.data.Account
import net.lacolaco.smileessence.data.ImageCache
import net.lacolaco.smileessence.logging.Logger
-import net.lacolaco.smileessence.twitter.OAuthSession
import net.lacolaco.smileessence.view.dialog.ConfirmDialogFragment
import kotlinx.android.synthetic.main.layout_edit_list.*
import kotlinx.android.synthetic.main.list_item_account.view.*
+import kotlinx.coroutines.experimental.android.UI
+import kotlinx.coroutines.experimental.async
+import org.jetbrains.anko.coroutines.experimental.bg
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.startActivityForResult
@@ -156,7 +158,7 @@ class ManageAccountsActivity : Activity(), AdapterView.OnItemClickListener, Adap
}
}
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REQUEST_OAUTH -> {
receiveOAuth(requestCode, resultCode, data)
@@ -167,13 +169,18 @@ class ManageAccountsActivity : Activity(), AdapterView.OnItemClickListener, Adap
}
}
- private fun receiveOAuth(requestCode: Int, resultCode: Int, data: Intent) {
+ private fun receiveOAuth(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK) {
- val account = Account.register(data.getStringExtra(OAuthSession.KEY_TOKEN),
- data.getStringExtra(OAuthSession.KEY_TOKEN_SECRET),
- data.getLongExtra(OAuthSession.KEY_USER_ID, -1L),
- data.getStringExtra(OAuthSession.KEY_SCREEN_NAME))
- adapter.add(account)
+ async(UI) {
+ data!!
+ adapter.add(bg {
+ Account.register(
+ data.getStringExtra(OAuthActivity.KEY_TOKEN),
+ data.getStringExtra(OAuthActivity.KEY_TOKEN_SECRET),
+ data.getLongExtra(OAuthActivity.KEY_USER_ID, -1L),
+ data.getStringExtra(OAuthActivity.KEY_SCREEN_NAME))
+ }.await())
+ }
} else {
Logger.error(requestCode)
Application.toast(R.string.notice_error_authenticate)
diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/OAuthActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/OAuthActivity.kt
index 52f9adf8..346d4880 100644
--- a/app/src/main/java/net/lacolaco/smileessence/activity/OAuthActivity.kt
+++ b/app/src/main/java/net/lacolaco/smileessence/activity/OAuthActivity.kt
@@ -28,22 +28,22 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Editable
-import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
-import android.widget.Button
-import android.widget.EditText
-import android.widget.TextView
import kotlinx.android.synthetic.main.layout_oauth.*
+import kotlinx.coroutines.experimental.android.UI
+import kotlinx.coroutines.experimental.async
import net.lacolaco.smileessence.Application
import net.lacolaco.smileessence.R
-import net.lacolaco.smileessence.twitter.OAuthSession
-import twitter4j.auth.AccessToken
+import org.jetbrains.anko.coroutines.experimental.bg
+import twitter4j.TwitterFactory
+import twitter4j.auth.RequestToken
class OAuthActivity : Activity(), View.OnClickListener, TextWatcher {
- private lateinit var oauthSession: OAuthSession
+ private var token: String? = null
+ private var tokenSecret: String? = null
- public override fun onCreate(savedInstanceState: Bundle?) {
+ override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_oauth)
@@ -51,32 +51,53 @@ class OAuthActivity : Activity(), View.OnClickListener, TextWatcher {
button_oauth_auth.setOnClickListener(this)
button_oauth_auth.isEnabled = false
- oauthSession = OAuthSession()
- val url = oauthSession.authorizationURL
- if (!TextUtils.isEmpty(url)) {
- textView_oauth_link.text = url
- } else {
- Application.toast(R.string.notice_error_authenticate_request)
- finish()
- }
+ if (savedInstanceState != null) {
+ token = savedInstanceState.getString(KEY_REQUEST_TOKEN)
+ tokenSecret = savedInstanceState.getString(KEY_REQUEST_TOKEN_SECRET)
+ } else
+ async(UI) {
+ val reqt = bg { TwitterFactory().instance.getOAuthRequestToken("oob") }
+
+ try {
+ val requestToken = reqt.await()
+ token = requestToken.token
+ tokenSecret = requestToken.tokenSecret
+ textView_oauth_link.text = requestToken.authorizationURL
+ } catch (e: Exception) {
+ Application.toast(R.string.notice_error_authenticate_request)
+ finish()
+ }
+ }
+ }
+
+ override fun onSaveInstanceState(outState: Bundle) {
+ super.onSaveInstanceState(outState)
+ outState.putString(KEY_REQUEST_TOKEN, token)
+ outState.putString(KEY_REQUEST_TOKEN_SECRET, tokenSecret)
}
override fun onClick(v: View) {
when (v.id) {
- R.id.button_oauth_auth -> {
- val accessToken = oauthSession.getAccessToken(editText_oauth_pin.text.toString())
- if (accessToken != null) {
+ R.id.button_oauth_auth -> async(UI) {
+ val pin = editText_oauth_pin.text.toString()
+ val acct = bg {
+ val requestToken = RequestToken(token, tokenSecret)
+ TwitterFactory().instance.getOAuthAccessToken(requestToken, pin)
+ }
+
+ try {
+ val accessToken = acct.await()
val intent = Intent()
- intent.putExtra(OAuthSession.KEY_TOKEN, accessToken.token)
- intent.putExtra(OAuthSession.KEY_TOKEN_SECRET, accessToken.tokenSecret)
- intent.putExtra(OAuthSession.KEY_USER_ID, accessToken.userId)
- intent.putExtra(OAuthSession.KEY_SCREEN_NAME, accessToken.screenName)
+ intent.putExtra(KEY_TOKEN, accessToken.token)
+ intent.putExtra(KEY_TOKEN_SECRET, accessToken.tokenSecret)
+ intent.putExtra(KEY_USER_ID, accessToken.userId)
+ intent.putExtra(KEY_SCREEN_NAME, accessToken.screenName)
setResult(Activity.RESULT_OK, intent)
- finish()
- } else {
+ } catch (e: Exception) {
+ Application.toast(R.string.notice_error_authenticate)
setResult(Activity.RESULT_CANCELED)
- finish()
}
+ finish()
}
}
}
@@ -84,8 +105,17 @@ class OAuthActivity : Activity(), View.OnClickListener, TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
- button_oauth_auth.isEnabled = s.length > 0
+ button_oauth_auth.isEnabled = s.isNotEmpty()
}
override fun afterTextChanged(s: Editable) {}
+
+ companion object {
+ val KEY_TOKEN = "token"
+ val KEY_TOKEN_SECRET = "tokenSecret"
+ val KEY_SCREEN_NAME = "screenName"
+ val KEY_USER_ID = "userID"
+ private val KEY_REQUEST_TOKEN = "reqToken"
+ private val KEY_REQUEST_TOKEN_SECRET = "reqTokenSecret"
+ }
} \ No newline at end of file
diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/OAuthSession.kt b/app/src/main/java/net/lacolaco/smileessence/twitter/OAuthSession.kt
deleted file mode 100644
index 63a0c484..00000000
--- a/app/src/main/java/net/lacolaco/smileessence/twitter/OAuthSession.kt
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * The MIT License (MIT)
- *
- * Copyright (c) 2012-2014 lacolaco.net
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package net.lacolaco.smileessence.twitter
-
-import net.lacolaco.smileessence.twitter.task.Accounts
-import twitter4j.Twitter
-import twitter4j.TwitterFactory
-import twitter4j.auth.AccessToken
-import twitter4j.auth.RequestToken
-
-class OAuthSession {
- private var requestToken: RequestToken? = null
-
- // --------------------- GETTER / SETTER METHODS ---------------------
-
- val authorizationURL: String?
- get() {
- val twitter = TwitterFactory().instance
- val task = Accounts.RequestTokenTask(twitter)
- task.execute()
- try {
- requestToken = task.immediately
- return requestToken!!.authorizationURL
- } catch (e: Exception) {
- return null
- }
-
- }
-
- // -------------------------- OTHER METHODS --------------------------
-
- fun getAccessToken(pinCode: String): AccessToken? {
- val twitter = TwitterFactory().instance
- val task = Accounts.AccessTokenTask(twitter, requestToken!!, pinCode)
- task.execute()
- try {
- return task.immediately
- } catch (e: Exception) {
- return null
- }
-
- }
-
- companion object {
- val KEY_TOKEN = "token"
- val KEY_TOKEN_SECRET = "tokenSecret"
- val KEY_SCREEN_NAME = "screenName"
- val KEY_USER_ID = "userID"
- }
-}
diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/Accounts.kt b/app/src/main/java/net/lacolaco/smileessence/twitter/task/Accounts.kt
index fc76784f..91165753 100644
--- a/app/src/main/java/net/lacolaco/smileessence/twitter/task/Accounts.kt
+++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/Accounts.kt
@@ -11,18 +11,6 @@ import twitter4j.auth.RequestToken
import java.util.ArrayList
class Accounts {
- class AccessTokenTask(private val twitter: Twitter, private val requestToken: RequestToken, private val pinCode: String) : BackgroundTask<AccessToken, Void>() {
- override fun doInBackground(): AccessToken {
- return twitter.getOAuthAccessToken(requestToken, pinCode)
- }
- }
-
- class RequestTokenTask(private val twitter: Twitter) : BackgroundTask<RequestToken, Void>() {
- override fun doInBackground(): RequestToken {
- return twitter.getOAuthRequestToken("oob")
- }
- }
-
class BlockIDsTask(private val account: Account) : BackgroundTask<List<Long>, Void>() {
override fun doInBackground(): List<Long> {
val idList = ArrayList<Long>()