diff options
-rw-r--r-- | .idea/gradle.xml | 1 | ||||
-rw-r--r-- | app/app.iml | 4 | ||||
-rw-r--r-- | app/build.gradle | 7 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt | 23 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/activity/OAuthActivity.kt | 84 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/twitter/OAuthSession.kt | 72 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/twitter/task/Accounts.kt | 12 |
7 files changed, 83 insertions, 120 deletions
diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 4c65fbe6..7ac24c77 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -12,7 +12,6 @@ </set> </option> <option name="resolveModulePerSourceSet" value="false" /> - <option name="useAutoImport" value="true" /> </GradleProjectSettings> </option> </component> diff --git a/app/app.iml b/app/app.iml index d1ec1b8f..9a62dc71 100644 --- a/app/app.iml +++ b/app/app.iml @@ -38,6 +38,7 @@ <option name="pluginClasspaths"> <array /> </option> + <option name="coroutinesState" value="enable" /> </compilerArguments> </configuration> </facet> @@ -130,14 +131,17 @@ <orderEntry type="library" exported="" name="orma-migration-4.2.5" level="project" /> <orderEntry type="library" exported="" name="haha-2.0.3" level="project" /> <orderEntry type="library" exported="" name="library-3.3.0-SNAPSHOT" level="project" /> + <orderEntry type="library" exported="" name="kotlinx-coroutines-core-0.15" level="project" /> <orderEntry type="library" exported="" name="support-core-utils-26.1.0" level="project" /> <orderEntry type="library" exported="" name="crashlytics-core-2.3.17" level="project" /> <orderEntry type="library" exported="" name="antlr4-runtime-4.6" level="project" /> <orderEntry type="library" exported="" name="kotlin-stdlib-jre7-1.1.51" level="project" /> + <orderEntry type="library" exported="" name="anko-coroutines-0.10.1" level="project" /> <orderEntry type="library" exported="" name="rxandroid-2.0.1" level="project" /> <orderEntry type="library" exported="" name="twitter4j-stream-4.0.6" level="project" /> <orderEntry type="library" exported="" name="rxjava-2.0.9" level="project" /> <orderEntry type="library" exported="" name="leakcanary-analyzer-1.5.4" level="project" /> + <orderEntry type="library" exported="" name="kotlinx-coroutines-android-0.15" level="project" /> <orderEntry type="library" exported="" name="reactive-streams-1.0.0" level="project" /> <orderEntry type="library" exported="" name="support-compat-26.1.0" level="project" /> <orderEntry type="library" exported="" name="fabric-1.3.17" level="project" /> diff --git a/app/build.gradle b/app/build.gradle index f21b0c23..098c122f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -68,6 +68,7 @@ dependencies { // Anko; not using Anko Layouts and Anko SQLite for now compile "org.jetbrains.anko:anko-commons:$anko_version" + compile "org.jetbrains.anko:anko-coroutines:$anko_version" // volley compile 'com.android.volley:volley:1.0.0' @@ -98,3 +99,9 @@ dependencies { releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' release2Compile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' } + +kotlin { + experimental { + coroutines "enable" + } +} 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>() |