This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
/
Login.scala
137 lines (120 loc) · 4.76 KB
/
Login.scala
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
package com.zegoggles.gist
import android.graphics.Bitmap
import android.accounts.{Account, AccountManager, AccountAuthenticatorActivity}
import android.webkit._
import java.lang.String
import android.os.{Handler, Bundle}
import actors.Futures
import Implicits._
import android.webkit.WebSettings.ZoomDensity
import android.app.{AlertDialog, ProgressDialog}
import android.net.Uri
import android.text.TextUtils
import org.apache.http.HttpStatus
import java.io.IOException
import android.widget.Toast
class Login extends AccountAuthenticatorActivity with Logger with ApiActivity with TypedActivity {
val handler = new Handler()
lazy val view = findView(TR.webview)
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login)
view.getSettings.setDefaultZoom(ZoomDensity.FAR)
view.getSettings.setJavaScriptEnabled(true)
view.getSettings.setBlockNetworkImage(false)
view.getSettings.setLoadsImagesAutomatically(true)
val progress = ProgressDialog.show(this, null, getString(R.string.loading_login), false)
view.setWebViewClient(new WebViewClient() {
override def onPageStarted(view: WebView, url: String, favicon: Bitmap) {
super.onPageStarted(view, url, favicon)
progress.show()
if (android.os.Build.VERSION.SDK_INT <= 7) {
// in 2.1 shouldOverrideUrlLoading doesn't get called
if (shouldOverrideUrlLoading(view, url)) {
view.stopLoading()
}
}
}
override def shouldOverrideUrlLoading(view: WebView, url: String) = {
super.shouldOverrideUrlLoading(view, url)
if (url.startsWith(api.redirect_uri)) {
Uri.parse(url).getQueryParameter("code") match {
case code:String => exchangeToken(code)
case _ => warn("no code found in redirect uri")
}
true
} else false
}
override def onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
super.onReceivedError(view, errorCode, description, failingUrl)
showConnectionError(if (TextUtils.isEmpty(description)) None else Some(description))
}
override def onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
view.loadUrl("javascript:document.getElementById('login_field').focus();")
try { progress.dismiss() } catch { case e:IllegalArgumentException => warn("", e) }
}
})
if (isConnected) {
removeAllCookies()
view.loadUrl(api.authorizeUrl)
} else showConnectionError(None)
}
def exchangeToken(code: String) {
val progress = ProgressDialog.show(this, null, getString(R.string.loading_token), false)
Futures.future {
try {
val token = api.exchangeToken(code).getOrElse(throw new IOException("could not get token"))
val resp = api.get(Request("/user", "access_token"->token.access))
resp.getStatusLine.getStatusCode match {
case HttpStatus.SC_OK =>
User(resp.getEntity).map { user =>
handler.post {
setAccountAuthenticatorResult(
addAccount(user.login, token,
"id" -> user.id.toString,
"name" -> user.name,
"email" -> user.email))
}
}
case c =>
log("invalid status ("+c+") "+resp.getStatusLine)
Toast.makeText(this, R.string.loading_token_failed, Toast.LENGTH_LONG).show()
}
}
catch {
case e:IOException => warn("error", e)
Toast.makeText(this, R.string.loading_token_failed, Toast.LENGTH_LONG).show()
}
finally { handler.post { progress.dismiss(); finish() } }
}
}
def addAccount(name: String, token: Token, data: (String, String)*): Bundle = {
val account = new Account(name, accountType)
val am = AccountManager.get(this)
am.addAccountExplicitly(account, token.access, null)
am.setAuthToken(account, "access", token.access)
for ((k, v) <- data) am.setUserData(account, k, v)
val b = new Bundle()
b.putString(AccountManager.KEY_ACCOUNT_NAME, name)
b.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType)
b
}
def showConnectionError(message: Option[String]) {
var error = getString(R.string.login_error_no_connection_message)
message.map(m => error += " (" + m + ")")
new AlertDialog.Builder(this)
.setMessage(error)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.ok, () => finish())
.show()
}
def removeAllCookies() {
CookieSyncManager.createInstance(this)
CookieManager.getInstance().removeAllCookie()
}
override def onDestroy() {
view.stopLoading()
super.onDestroy()
}
}