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 @@
package com.keylesspalace.tusky;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
......@@ -258,9 +260,8 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
@Override
protected void onResume() {
super.onResume();
NotificationHelper.clearNotificationsForActiveAccount(this, accountManager);
syncLibremOneAccounts();
}
@Override
......@@ -654,10 +655,50 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
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) {
Log.e(TAG, "Failed to fetch user info. " + throwable.getMessage());
}
@Nullable
@Override
public FloatingActionButton getActionButton() {
......
......@@ -52,7 +52,7 @@ class AccountManager(db: AppDatabase) {
* @param accessToken the access token for the new account
* @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 {
it.isActive = false
......@@ -69,6 +69,7 @@ class AccountManager(db: AppDatabase) {
accounts.add(it)
}
return newAccountId
}
/**
......@@ -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