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

Social now creates new Librem One account if one didn't exist and opens...

Social now creates new Librem One account if one didn't exist and opens timeline for this new account
parent ef1160b2
Pipeline #51281 failed with stages
in 12 minutes and 52 seconds
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
android:name="android.permission.GET_ACCOUNTS" android:name="android.permission.GET_ACCOUNTS"
android:maxSdkVersion="22" /> android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.MANAGE_ACCOUNTS"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission <uses-permission
android:name="android.permission.USE_CREDENTIALS" android:name="android.permission.USE_CREDENTIALS"
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
package com.keylesspalace.tusky; package com.keylesspalace.tusky;
import android.accounts.AccountManager;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
...@@ -551,6 +552,13 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut ...@@ -551,6 +552,13 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
.setMessage(getString(R.string.action_logout_confirm, activeAccount.getFullName())) .setMessage(getString(R.string.action_logout_confirm, activeAccount.getFullName()))
.setPositiveButton(android.R.string.yes, (dialog, which) -> { .setPositiveButton(android.R.string.yes, (dialog, which) -> {
android.accounts.AccountManager
.get(this)
.invalidateAuthToken(
"librem.one",
activeAccount.getAccessToken()
);
NotificationHelper.deleteNotificationChannelsForAccount(accountManager.getActiveAccount(), MainActivity.this); NotificationHelper.deleteNotificationChannelsForAccount(accountManager.getActiveAccount(), MainActivity.this);
cacheUpdater.clearForUser(activeAccount.getId()); cacheUpdater.clearForUser(activeAccount.getId());
conversationRepository.deleteCacheForAccount(activeAccount.getId()); conversationRepository.deleteCacheForAccount(activeAccount.getId());
......
...@@ -3,12 +3,16 @@ package com.keylesspalace.tusky ...@@ -3,12 +3,16 @@ package com.keylesspalace.tusky
import android.accounts.AccountManager import android.accounts.AccountManager
import android.accounts.AccountManagerCallback import android.accounts.AccountManagerCallback
import android.accounts.AccountManagerFuture import android.accounts.AccountManagerFuture
import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Message
import android.widget.Button import android.widget.Button
import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.di.Injectable
import javax.security.auth.callback.Callback
class OnboardingActivity : BaseActivity(), Injectable { class OnboardingActivity : BaseActivity(), Injectable {
...@@ -21,6 +25,7 @@ class OnboardingActivity : BaseActivity(), Injectable { ...@@ -21,6 +25,7 @@ class OnboardingActivity : BaseActivity(), Injectable {
findViewById<Button>(R.id.buttonLoginLibremOne).setOnClickListener { findViewById<Button>(R.id.buttonLoginLibremOne).setOnClickListener {
// TODO: use system Librem One account // TODO: use system Librem One account
// SEE: https://developer.android.com/reference/android/accounts/AccountManager // SEE: https://developer.android.com/reference/android/accounts/AccountManager
val am = AccountManager.get(this) val am = AccountManager.get(this)
...@@ -31,21 +36,29 @@ class OnboardingActivity : BaseActivity(), Injectable { ...@@ -31,21 +36,29 @@ class OnboardingActivity : BaseActivity(), Injectable {
if( accounts.isNotEmpty()) { if( accounts.isNotEmpty()) {
for (a in accounts) { for (a in accounts) {
am.getAuthToken( am.getAuthToken(
a, a,
"social", "social",
options, options,
this, true,
OnTokenAcquired(), OnTokenAcquired(),
Handler{ Handler {
message -> false
true
} }
) )
}
} else {
} am.addAccount(
"librem.one",
"social",
null,
null,
this,
OnTokenAcquired(),
null
)
} }
...@@ -97,16 +110,41 @@ class OnboardingActivity : BaseActivity(), Injectable { ...@@ -97,16 +110,41 @@ class OnboardingActivity : BaseActivity(), Injectable {
// Launch Librem One account login activity if requested // Launch Librem One account login activity if requested
val launch = bundle.get(AccountManager.KEY_INTENT) as? Intent val launch = bundle.get(AccountManager.KEY_INTENT) as? Intent
if (launch != null) { if (launch != null) {
startActivityForResult(launch, 0) startActivityForResult(launch, 123)
return
} }
// Otherwise the token is a named value in the bundle. The name of the value // Otherwise the token is a named value in the bundle. The name of the value
// is stored in the constant AccountManager.KEY_AUTHTOKEN. // is stored in the constant AccountManager.KEY_AUTHTOKEN.
val token: String = bundle.getString(AccountManager.KEY_AUTHTOKEN) val token: String? = bundle.getString(AccountManager.KEY_AUTHTOKEN)
val username: String = bundle.getString(AccountManager.KEY_ACCOUNT_NAME).split('@')[0]
if(token == null) {
// new account?
val am = AccountManager.get(this@OnboardingActivity)
val accounts = am.getAccountsByType("librem.one")
for (a in accounts) {
am.getAuthToken(
a,
"social",
null,
true,
OnTokenAcquired(),
Handler {
false
}
)
numLibremOneAccounts++;
}
} else {
val username: String = bundle.getString(AccountManager.KEY_ACCOUNT_NAME).split('@')[0]
// accountManager in this case comes from app's BaseActivity()
accountManager.addAccount(token, "social.librem.one", username)
}
// 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, // if this is the last account token we've added,
// move on to main activity // move on to main activity
...@@ -117,6 +155,21 @@ class OnboardingActivity : BaseActivity(), Injectable { ...@@ -117,6 +155,21 @@ class OnboardingActivity : BaseActivity(), Injectable {
} }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Check which request we're responding to
if (requestCode == 123) {
// Make sure the request was successful
if (resultCode == Activity.RESULT_OK) {
val token = data?.extras?.getString(AccountManager.KEY_AUTHTOKEN)
if(token!=null) {
accountManager.addAccount(token, "social.librem.one")
startMainActivity()
}
}
}
}
companion object { companion object {
@JvmStatic @JvmStatic
fun getIntent(context: Context): Intent { fun getIntent(context: Context): Intent {
......
...@@ -95,7 +95,7 @@ ...@@ -95,7 +95,7 @@
<string name="action_retry">Retry</string> <string name="action_retry">Retry</string>
<string name="action_close">Close</string> <string name="action_close">Close</string>
<string name="action_view_profile">Profile</string> <string name="action_view_profile">Profile</string>
<string name="action_view_preferences">Preferences</string> <string name="action_view_preferences">App Preferences</string>
<string name="action_view_account_preferences">Account Preferences</string> <string name="action_view_account_preferences">Account Preferences</string>
<string name="action_view_favourites">Favorites</string> <string name="action_view_favourites">Favorites</string>
<string name="action_view_mutes">Muted users</string> <string name="action_view_mutes">Muted users</string>
......
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