aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/activity/OAuthActivity.kt
blob: 52f9adf8c10c92c1c2d6412c8ae1a8cec2c8fe03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * 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.activity

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 net.lacolaco.smileessence.Application
import net.lacolaco.smileessence.R
import net.lacolaco.smileessence.twitter.OAuthSession
import twitter4j.auth.AccessToken

class OAuthActivity : Activity(), View.OnClickListener, TextWatcher {
    private lateinit var oauthSession: OAuthSession

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_oauth)

        editText_oauth_pin.addTextChangedListener(this)
        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()
        }
    }

    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) {
                    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)
                    setResult(Activity.RESULT_OK, intent)
                    finish()
                } else {
                    setResult(Activity.RESULT_CANCELED)
                    finish()
                }
            }
        }
    }

    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
    }

    override fun afterTextChanged(s: Editable) {}
}