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

Sync onResume with any added LibremOne accounts

parent bbad9fcf
Pipeline #52052 failed with stages
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package com.keylesspalace.tusky; package com.keylesspalace.tusky;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
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;
...@@ -258,9 +260,8 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut ...@@ -258,9 +260,8 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
NotificationHelper.clearNotificationsForActiveAccount(this, accountManager); NotificationHelper.clearNotificationsForActiveAccount(this, accountManager);
syncLibremOneAccounts();
} }
@Override @Override
...@@ -654,10 +655,50 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut ...@@ -654,10 +655,50 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
headerResult.setActiveProfile(accountManager.getActiveAccount().getId()); headerResult.setActiveProfile(accountManager.getActiveAccount().getId());
} }
private void syncLibremOneAccounts() {
AccountManager am = AccountManager.get(MainActivity.this);
android.accounts.Account[] libremOneAccounts = am.getAccountsByType("librem.one");
for (android.accounts.Account libremOneAccount : libremOneAccounts) {
AccountEntity acc = accountManager.getAccountByUsername(
libremOneAccount.name.split("@")[0],
"social.librem.one");
if( acc == null) {
// need to add libremOneAccount to social
// but first get its token
am.getAuthToken(
libremOneAccount,
"social",
null,
true,
new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle result = future.getResult();
String username = result.getString(AccountManager.KEY_ACCOUNT_NAME).split("@")[0];
String token = result.getString(AccountManager.KEY_AUTHTOKEN);
long id = accountManager.addAccount(token, "social.librem.one", username);
changeAccount(id, null);
} catch (Exception e) {
e.printStackTrace();
}
}
},
null);
}
}
}
private static void onFetchUserInfoFailure(Throwable throwable) { private static void onFetchUserInfoFailure(Throwable throwable) {
Log.e(TAG, "Failed to fetch user info. " + throwable.getMessage()); Log.e(TAG, "Failed to fetch user info. " + throwable.getMessage());
} }
@Nullable @Nullable
@Override @Override
public FloatingActionButton getActionButton() { public FloatingActionButton getActionButton() {
......
...@@ -52,7 +52,7 @@ class AccountManager(db: AppDatabase) { ...@@ -52,7 +52,7 @@ class AccountManager(db: AppDatabase) {
* @param accessToken the access token for the new account * @param accessToken the access token for the new account
* @param domain the domain of the accounts Mastodon instance * @param domain the domain of the accounts Mastodon instance
*/ */
fun addAccount(accessToken: String, domain: String, username: String = "") { fun addAccount(accessToken: String, domain: String, username: String = ""): Long {
activeAccount?.let { activeAccount?.let {
it.isActive = false it.isActive = false
...@@ -69,6 +69,7 @@ class AccountManager(db: AppDatabase) { ...@@ -69,6 +69,7 @@ class AccountManager(db: AppDatabase) {
accounts.add(it) accounts.add(it)
} }
return newAccountId
} }
/** /**
...@@ -199,4 +200,16 @@ class AccountManager(db: AppDatabase) { ...@@ -199,4 +200,16 @@ class AccountManager(db: AppDatabase) {
} }
} }
/**
* Finds an account by its username and domain
* @param username the username of the account
* @param domain the domain of the account
* @return the requested account or null if it was not found
*/
fun getAccountByUsername(username: String, domain: String): AccountEntity? {
return accounts.find { acc ->
acc.username == username && acc.domain == domain
}
}
} }
\ No newline at end of file
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