app/src/main/java/com/geekorum/ttrss/accounts/LoginActivity.kt
author Da Risk <da_risk@geekorum.com>
Wed, 12 Jan 2022 17:29:59 -0400
changeset 882 7a74abf66c49
parent 846 ac0863af5ef6
child 941 dd7a7a2adb1c
permissions -rw-r--r--
update license headers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
137
5464f07a306c Update copyright headers for 2019
Da Risk <da_risk@geekorum.com>
parents: 109
diff changeset
     1
/*
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     2
 * Geekttrss is a RSS feed reader application on the Android Platform.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     3
 *
882
7a74abf66c49 update license headers
Da Risk <da_risk@geekorum.com>
parents: 846
diff changeset
     4
 * Copyright (C) 2017-2022 by Frederic-Charles Barthelery.
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     5
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     6
 * This file is part of Geekttrss.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     7
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     8
 * Geekttrss is free software: you can redistribute it and/or modify
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     9
 * it under the terms of the GNU General Public License as published by
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    10
 * the Free Software Foundation, either version 3 of the License, or
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    11
 * (at your option) any later version.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    12
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    13
 * Geekttrss is distributed in the hope that it will be useful,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    16
 * GNU General Public License for more details.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    17
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    19
 * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    20
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    21
package com.geekorum.ttrss.accounts
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    22
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    23
import android.accounts.Account
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    24
import android.accounts.AccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    25
import android.app.Activity
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    26
import android.os.Bundle
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import android.transition.TransitionManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    28
import android.view.View
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    29
import android.view.ViewGroup
421
859638996c86 LoginActivity: fix a few warnings
Da Risk <da_risk@geekorum.com>
parents: 164
diff changeset
    30
import androidx.activity.viewModels
164
2d6e9f2063cd Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    31
import androidx.databinding.DataBindingUtil
2d6e9f2063cd Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    32
import androidx.lifecycle.Observer
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    33
import com.geekorum.geekdroid.accounts.AccountAuthenticatorAppCompatActivity
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    34
import com.geekorum.geekdroid.app.lifecycle.EventObserver
164
2d6e9f2063cd Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    35
import com.geekorum.geekdroid.dagger.DaggerDelegateViewModelsFactory
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    36
import com.geekorum.ttrss.R
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    37
import com.geekorum.ttrss.databinding.ActivityLoginAccountBinding
164
2d6e9f2063cd Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    38
import com.google.android.material.snackbar.Snackbar
107
3fa8423d4de4 LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents: 62
diff changeset
    39
import com.google.android.material.textfield.TextInputLayout
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    40
import dagger.hilt.android.AndroidEntryPoint
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    41
import javax.inject.Inject
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    42
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    43
/**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    44
 * A Login screen to a Tinytinyrss server.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    45
 */
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    46
@AndroidEntryPoint
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    47
class LoginActivity : AccountAuthenticatorAppCompatActivity() {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    48
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    49
    companion object {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    50
        const val ACTION_ADD_ACCOUNT = "add_account"
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    51
        const val ACTION_CONFIRM_CREDENTIALS = "confirm_credentials"
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    52
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
        const val EXTRA_ACCOUNT = "account"
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    54
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    55
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    56
    @Inject
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
    lateinit var accountManager: AndroidTinyrssAccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    59
    private lateinit var binding: ActivityLoginAccountBinding
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    60
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    61
    private val loginViewModel: LoginViewModel by viewModels()
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    62
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    63
    override fun onCreate(savedInstanceState: Bundle?) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    64
        super.onCreate(savedInstanceState)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    65
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login_account)
421
859638996c86 LoginActivity: fix a few warnings
Da Risk <da_risk@geekorum.com>
parents: 164
diff changeset
    66
        binding.lifecycleOwner = this
62
c9d5f3546fc1 LoginActivity: rework the layout to be better for large screen
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    67
        setSupportActionBar(binding.toolbar)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    68
        val account = intent.getParcelableExtra<Account>(EXTRA_ACCOUNT)?.let {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    69
            accountManager.fromAndroidAccount(it)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    70
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    71
        val action = requireNotNull(intent?.action) { "Invalid intent action passed to $this"}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    72
        loginViewModel.initialize(action, account)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    73
        binding.viewModel = loginViewModel
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    75
        loginViewModel.loginInProgress.observe(this, Observer { inProgress ->
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    76
            showProgress(inProgress!!)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    77
        })
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    78
        loginViewModel.loginFailedEvent.observe(this, EventObserver { event ->
109
eb12523cbc81 LoginActivity: use http authentication fields
Da Risk <da_risk@geekorum.com>
parents: 107
diff changeset
    79
            if (event.errorMsgId in listOf(R.string.error_http_forbidden, R.string.error_http_unauthorized)) {
eb12523cbc81 LoginActivity: use http authentication fields
Da Risk <da_risk@geekorum.com>
parents: 107
diff changeset
    80
                binding.form.useHttpAuth.isChecked = true
eb12523cbc81 LoginActivity: use http authentication fields
Da Risk <da_risk@geekorum.com>
parents: 107
diff changeset
    81
            }
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    82
            Snackbar.make(binding.root, event.errorMsgId, Snackbar.LENGTH_SHORT).show()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    83
        })
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    84
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    85
        loginViewModel.actionCompleteEvent.observe(this, EventObserver { event->
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    86
            when (event) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    87
                is LoginViewModel.ActionCompleteEvent.Failed -> handleActionFailed()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    88
                is LoginViewModel.ActionCompleteEvent.Success -> handleActionSuccess(event)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    89
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    90
            }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    91
        })
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    92
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    93
        loginViewModel.fieldErrors.observe(this, Observer { errorStatus ->
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    94
            val error = checkNotNull(errorStatus)
107
3fa8423d4de4 LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents: 62
diff changeset
    95
            setFieldError(binding.form.urlField, error.invalidUrlMsgId)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    96
            if (error.hasAttemptLogin) {
107
3fa8423d4de4 LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents: 62
diff changeset
    97
                setFieldError(binding.form.usernameField, error.invalidNameMsgId)
3fa8423d4de4 LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents: 62
diff changeset
    98
                setFieldError(binding.form.passwordField, error.invalidPasswordMsgId)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    99
            }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   100
        })
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   101
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   102
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   103
    private fun handleActionSuccess(event: LoginViewModel.ActionCompleteEvent.Success) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   104
        accountAuthenticatorResult = Bundle().apply {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   105
            when (intent.action) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   106
                ACTION_ADD_ACCOUNT -> {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   107
                    putString(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.TTRSS_ACCOUNT_TYPE)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   108
                    putString(AccountManager.KEY_ACCOUNT_NAME, event.account.username)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   109
                }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   110
                ACTION_CONFIRM_CREDENTIALS -> putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   111
            }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   112
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   113
        setResult(Activity.RESULT_OK)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   114
        finish()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   115
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   116
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   117
    private fun handleActionFailed() {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   118
        accountAuthenticatorResult = Bundle().apply {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   119
            when (intent.action) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   120
                ACTION_ADD_ACCOUNT -> {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   121
                    putInt(AccountManager.KEY_ERROR_CODE, AccountAuthenticator.ERROR_CODE_AUTHENTICATOR_FAILURE)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   122
                    putString(AccountManager.KEY_ERROR_MESSAGE, "Unable to add account")
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   123
                }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   124
                ACTION_CONFIRM_CREDENTIALS -> putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   125
            }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   126
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   127
        finish()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   128
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   129
107
3fa8423d4de4 LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents: 62
diff changeset
   130
    private fun setFieldError(view: TextInputLayout, errorId: Int?) {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   131
        val errorMsg = if (errorId != null) getString(errorId) else null
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   132
        view.error = errorMsg
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   133
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   134
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   135
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   136
    private fun showProgress(show: Boolean) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   137
        TransitionManager.beginDelayedTransition(binding.root as ViewGroup)
62
c9d5f3546fc1 LoginActivity: rework the layout to be better for large screen
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   138
        binding.form.loginForm.visibility = if (show) View.GONE else View.VISIBLE
c9d5f3546fc1 LoginActivity: rework the layout to be better for large screen
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   139
        binding.form.loginProgress.visibility = if (show) View.VISIBLE else View.GONE
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   140
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   141
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   142
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   143
}