Commit dfc8d0a7 authored by Thomas Markiewicz's avatar Thomas Markiewicz
Browse files

Moved integration with system-wide AccountManager to Onboarding screen

parent 1e0a5017
Pipeline #50944 failed with stages
in 1 minute and 38 seconds
......@@ -94,49 +94,6 @@ class LoginActivity : BaseActivity(), Injectable {
supportActionBar?.setDisplayShowTitleEnabled(false)
} else {
toolbar.visibility = View.GONE
// TODO: use system Librem One account
// SEE: https://developer.android.com/reference/android/accounts/AccountManager
val am = AccountManager.get(this)
val accounts = am.getAccountsByType("librem.one")
if( accounts.isNotEmpty()) {
for (a in accounts) {
// hardcoding domain "social.librem.one" for now
// also assuming this token IS in fact for social
// TODO: figure out how to tell from code
/*
var token = am.getAuthToken(a, "social", true)
am.getAuthToken(a, "social", null, this, )
accountManager.addAccount(token,"social.librem.one", a.name)
*/
}
startMainActivity()
} else {
/* see: https://www.zoftino.com/how-to-get-auth-token-from-accountmanager-in-android
// start system-wide Librem One login activity
val intent = Intent(this, ZoftinoAccountActivity::class.java)
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, "com.zoftino")
intent.putExtra("addAccount", true)
startActivityForResult(intent, 22)
Toast.makeText(this, "You'll get notification once latest coupon data is downloaded", Toast.LENGTH_LONG).show()
return
*/
/*
NOTE:
remove from the account manager by calling invalidateAuthToken() method.
After removing the token from account manager, you need to call getAuthToken method passing account, account type, and account manager callback to get new token.
*/
}
}
}
......
package com.keylesspalace.tusky
import android.accounts.AccountManager
import android.accounts.AccountManagerCallback
import android.accounts.AccountManagerFuture
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.webkit.WebView
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import com.keylesspalace.tusky.di.Injectable
import kotlinx.coroutines.*
class OnboardingActivity : BaseActivity(), Injectable {
class OnboardingActivity : BaseActivity(), CoroutineScope by MainScope(), Injectable {
var numLibremOneAccounts = 0
var processedLibremOneAccounts = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_onboarding)
findViewById<Button>(R.id.buttonRegister).setOnClickListener {
displayInWebView(R.string.register_url)
}
findViewById<Button>(R.id.buttonSupport).setOnClickListener {
displayInWebView(R.string.support_url)
}
findViewById<Button>(R.id.buttonLoginLibremOne).setOnClickListener {
// TODO: use system Librem One account
// SEE: https://developer.android.com/reference/android/accounts/AccountManager
val am = AccountManager.get(this)
val accounts = am.getAccountsByType("librem.one")
val options = Bundle()
findViewById<Button>(R.id.buttonPolicy).setOnClickListener {
displayInWebView(R.string.policy_url)
}
numLibremOneAccounts = accounts.size
findViewById<Button>(R.id.buttonPolicy).setOnClickListener {
displayInWebView(R.string.policy_url)
}
if( accounts.isNotEmpty()) {
for (a in accounts) {
am.getAuthToken(
a,
"social",
options,
this,
OnTokenAcquired(),
Handler{
message ->
true
}
)
}
}
//else {
/* see: https://www.zoftino.com/how-to-get-auth-token-from-accountmanager-in-android
// start system-wide Librem One login activity
val intent = Intent(this, ZoftinoAccountActivity::class.java)
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, "com.zoftino")
intent.putExtra("addAccount", true)
startActivityForResult(intent, 22)
Toast.makeText(this, "You'll get notification once latest coupon data is downloaded", Toast.LENGTH_LONG).show()
return
*/
/*
NOTE:
remove from the account manager by calling invalidateAuthToken() method.
findViewById<Button>(R.id.buttonStaySafe).setOnClickListener {
displayInWebView(R.string.stay_safe_url)
After removing the token from account manager, you need to call getAuthToken method passing account, account type, and account manager callback to get new token.
*/
}
findViewById<Button>(R.id.buttonLogin).setOnClickListener {
val intent = Intent(this, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
findViewById<Button>(R.id.buttonLoginOther).setOnClickListener {
startLoginActivity();
}
}
......@@ -47,14 +87,49 @@ class OnboardingActivity : BaseActivity(), Injectable {
return false
}
private fun displayInWebView(id: Int) {
val url = this.getString(id)
val wv = WebView(this)
wv.loadUrl(url)
AlertDialog.Builder(this)
.setView(wv)
.setPositiveButton(android.R.string.ok, null)
.show()
private fun startLoginActivity() {
val intent = Intent(this, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}
private fun startMainActivity() {
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
finish()
overridePendingTransition(R.anim.explode, R.anim.explode)
}
private inner class OnTokenAcquired : AccountManagerCallback<Bundle> {
override fun run(result: AccountManagerFuture<Bundle>) {
// Get the result of the operation from the AccountManagerFuture.
val bundle = result.result
// Launch Librem One account login activity if requested
val launch = bundle.get(AccountManager.KEY_INTENT) as? Intent
if (launch != null) {
startActivityForResult(launch, 0)
}
// Otherwise the token is a named value in the bundle. The name of the value
// is stored in the constant AccountManager.KEY_AUTHTOKEN.
val token: String = bundle.getString(AccountManager.KEY_AUTHTOKEN)
val username: String = bundle.getString(AccountManager.KEY_ACCOUNT_NAME);
// accountManager in this case comes from app's BaseActivity()
accountManager.addAccount(token, "social.librem.one", username)
// if this is the last account token we've added,
// move on to main activity
processedLibremOneAccounts++
if(processedLibremOneAccounts == numLibremOneAccounts) {
startMainActivity()
}
}
}
companion object {
......
......@@ -34,44 +34,33 @@
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="32dp"
android:text="@string/onboarding_message" />
<Button
android:id="@+id/buttonRegister"
android:id="@+id/buttonInstallHub"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:text="@string/register" />
<Button
android:id="@+id/buttonSupport"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:visibility="gone"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:text="@string/support" />
android:text="@string/install_librem_hub" />
<Button
android:id="@+id/buttonPolicy"
android:id="@+id/buttonLoginLibremOne"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:text="@string/policy" />
android:layout_width="match_parent"
android:text="@string/action_login_librem_one" />
<Button
android:id="@+id/buttonStaySafe"
android:id="@+id/buttonLoginOther"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
......@@ -79,17 +68,9 @@
android:layout_marginEnd="16dp"
android:layout_width="match_parent"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:text="@string/stay_safe" />
android:text="@string/action_login_other" />
<Button
android:id="@+id/buttonLogin"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_width="match_parent"
android:text="@string/action_login" />
</LinearLayout>
......
......@@ -75,6 +75,8 @@
<string name="action_compose">Compose</string>
<string name="action_login">Login</string>
<string name="action_login_with_browser">Login with browser</string>
<string name="action_login_librem_one">Login to Librem One</string>
<string name="action_login_other">Login to another instance</string>
<string name="action_forgot_passphrase">Forgot passphrase?</string>
<string name="action_logout">Logout</string>
<string name="action_logout_confirm">Are you sure you want to log out of the account %1$s?</string>
......@@ -284,6 +286,7 @@
<string name="support">Support</string>
<string name="policy">Policy</string>
<string name="stay_safe">Stay safe</string>
<string name="install_librem_hub">Install Librem Hub</string>
<string name="about_title_activity">About</string>
<string name="about_tusky_version">Librem Social %s</string>
<string name="about_tusky_license">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment