/
LoginActivity.kt
253 lines (225 loc) 路 10.7 KB
/
LoginActivity.kt
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright 2016-2020 Open Food Facts
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package openfoodfacts.github.scrachx.openfood.features
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContract
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import com.afollestad.materialdialogs.MaterialDialog
import com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_LONG
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import okhttp3.ResponseBody
import openfoodfacts.github.scrachx.openfood.AppFlavors.OFF
import openfoodfacts.github.scrachx.openfood.AppFlavors.isFlavors
import openfoodfacts.github.scrachx.openfood.BuildConfig
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.analytics.AnalyticsEvent
import openfoodfacts.github.scrachx.openfood.analytics.MatomoAnalytics
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabActivityHelper
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabsHelper
import openfoodfacts.github.scrachx.openfood.customtabs.WebViewFallback
import openfoodfacts.github.scrachx.openfood.databinding.ActivityLoginBinding
import openfoodfacts.github.scrachx.openfood.features.shared.BaseActivity
import openfoodfacts.github.scrachx.openfood.network.services.ProductsAPI
import openfoodfacts.github.scrachx.openfood.utils.Utils
import openfoodfacts.github.scrachx.openfood.utils.getLoginPreferences
import retrofit2.Response
import java.io.IOException
import java.net.HttpCookie
import javax.inject.Inject
/**
* A login screen that offers login via login/password.
* This Activity connect to the Chrome Custom Tabs Service on startup to prefetch the url.
*/
@AndroidEntryPoint
class LoginActivity : BaseActivity() {
private var _binding: ActivityLoginBinding? = null
private val binding get() = _binding!!
@Inject
lateinit var productsApi: ProductsAPI
@Inject
lateinit var matomoAnalytics: MatomoAnalytics
private lateinit var customTabActivityHelper: CustomTabActivityHelper
private val disp = CompositeDisposable()
private var userLoginUri: Uri? = null
private var resetPasswordUri: Uri? = null
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
android.R.id.home -> {
super.onBackPressed()
true
}
else -> false
}
private fun doAttemptLogin() {
Utils.hideKeyboard(this)
// Start checks
val login = binding.loginInput.text.toString()
val password = binding.passInput.text.toString()
if (login.isBlank()) {
binding.loginInput.error = getString(R.string.error_field_required)
binding.loginInput.requestFocus()
return
}
if (password.isBlank()) {
binding.passInput.error = getString(R.string.error_field_required)
binding.passInput.requestFocus()
return
} else if (password.length < 6) {
binding.passInput.error = getText(R.string.error_invalid_password)
binding.passInput.requestFocus()
return
}
// End checks
val snackbar = Snackbar.make(binding.loginLinearlayout, R.string.toast_retrieving, LENGTH_LONG)
.apply { show() }
binding.btnLogin.isClickable = false
productsApi.signIn(login, password, "Sign-in")
.observeOn(AndroidSchedulers.mainThread()) // We need to modify view
.doOnError {
Toast.makeText(this, this.getString(R.string.errorWeb), Toast.LENGTH_LONG).show()
Log.e(this::class.simpleName, "onFailure", it)
}
.subscribe { response: Response<ResponseBody> ->
if (!response.isSuccessful) {
Toast.makeText(this@LoginActivity, R.string.errorWeb, Toast.LENGTH_LONG).show()
return@subscribe
}
val htmlNoParsed = try {
response.body()?.string()
} catch (e: IOException) {
Log.e("LOGIN", "Unable to parse the login response page", e)
return@subscribe
}
val pref = this@LoginActivity.getSharedPreferences("login", 0)
if (isHtmlNotValid(htmlNoParsed)) {
Snackbar.make(binding.loginLinearlayout, R.string.errorLogin, LENGTH_LONG).show()
binding.passInput.setText("")
binding.txtInfoLogin.setTextColor(ContextCompat.getColor(this, R.color.red))
binding.txtInfoLogin.setText(R.string.txtInfoLoginNo)
snackbar.dismiss()
} else {
// store the user session id (user_session and user_id)
for (httpCookie in HttpCookie.parse(response.headers()["set-cookie"])) {
// Example format of set-cookie: session=user_session&S0MeR@nD0MSECRETk3Y&user_id&testuser; domain=.openfoodfacts.org; path=/
if (BuildConfig.HOST.contains(httpCookie.domain) && httpCookie.path == "/") {
val cookieValues = httpCookie.value.split("&")
var i = 0
while (i < cookieValues.size) {
pref.edit { putString(cookieValues[i], cookieValues[++i]) }
i++
}
break
}
}
Snackbar.make(binding.loginLinearlayout, R.string.connection, LENGTH_LONG).show()
pref.edit {
putString("user", login)
putString("pass", password)
}
binding.txtInfoLogin.setTextColor(ContextCompat.getColor(this, R.color.green_500))
binding.txtInfoLogin.setText(R.string.txtInfoLoginOk)
matomoAnalytics.trackEvent(AnalyticsEvent.UserLogin)
setResult(RESULT_OK)
finish()
}
}.addTo(disp)
binding.btnLogin.isClickable = true
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (resources.getBoolean(R.bool.portrait_only)) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
_binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
// check flavour and show helper text for user account
if (!isFlavors(OFF)) {
binding.txtLoginHelper.setText(R.string.txtloginHelper)
binding.txtLoginHelper.visibility = View.VISIBLE
}
// Setup view listeners
binding.btnLogin.setOnClickListener { doAttemptLogin() }
binding.btnCreateAccount.setOnClickListener { doRegister() }
binding.btnForgotPass.setOnClickListener { doForgotPassword() }
setSupportActionBar(binding.toolbarLayout.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title = getString(R.string.txtSignIn)
userLoginUri = Uri.parse(getString(R.string.website) + "cgi/user.pl")
resetPasswordUri = Uri.parse(getString(R.string.website) + "cgi/reset_password.pl")
// prefetch the uri
customTabActivityHelper = CustomTabActivityHelper()
customTabActivityHelper.setConnectionCallback(
onConnected = { binding.btnCreateAccount.isEnabled = true },
onDisconnected = { binding.btnCreateAccount.isEnabled = false }
)
customTabActivityHelper.mayLaunchUrl(userLoginUri, null, null)
binding.btnCreateAccount.isEnabled = true
val loginS = getLoginPreferences().getString(resources.getString(R.string.user), resources.getString(R.string.txt_anonymous))
if (loginS == resources.getString(R.string.user)) {
MaterialDialog.Builder(this).apply {
title(R.string.log_in)
content(R.string.login_true)
neutralText(R.string.ok_button)
onNeutral { _, _ -> finish() }
show()
}
}
}
private fun doRegister() {
val customTabsIntent = CustomTabsHelper.getCustomTabsIntent(baseContext, customTabActivityHelper.session)
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, userLoginUri!!, WebViewFallback())
}
private fun doForgotPassword() {
val customTabsIntent = CustomTabsHelper.getCustomTabsIntent(baseContext, customTabActivityHelper.session)
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, resetPasswordUri!!, WebViewFallback())
}
override fun onStart() {
super.onStart()
customTabActivityHelper.bindCustomTabsService(this)
}
override fun onStop() {
super.onStop()
customTabActivityHelper.unbindCustomTabsService(this)
binding.btnCreateAccount.isEnabled = false
}
override fun onDestroy() {
disp.dispose()
customTabActivityHelper.connectionCallback = null
_binding = null
super.onDestroy()
}
companion object {
class LoginContract : ActivityResultContract<Unit?, Boolean>() {
override fun createIntent(context: Context, input: Unit?) = Intent(context, LoginActivity::class.java)
override fun parseResult(resultCode: Int, intent: Intent?) = resultCode == RESULT_OK
}
internal fun isHtmlNotValid(html: String?) = (html == null
|| html.contains("Incorrect user name or password.")
|| html.contains("See you soon!"))
}
}