diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 9570e82b2..7e43d4d0f 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -758,6 +758,57 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } }).start(); } + //Fetch recent used accounts + new Thread(() -> { + try { + List accounts = new Account(BaseMainActivity.this).getLastUsedAccounts(); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + if (accounts != null && accounts.size() > 0) { + Helper.loadPP(this, headerMainBinding.otherAccount1, accounts.get(0)); + headerMainBinding.otherAccount1.setVisibility(View.VISIBLE); + headerMainBinding.otherAccount1.setOnClickListener(v -> { + headerMenuOpen = false; + Toasty.info(BaseMainActivity.this, getString(R.string.toast_account_changed, "@" + accounts.get(0).mastodon_account.acct + "@" + accounts.get(0).instance), Toasty.LENGTH_LONG).show(); + BaseMainActivity.currentToken = accounts.get(0).token; + BaseMainActivity.currentUserID = accounts.get(0).user_id; + api = accounts.get(0).api; + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_TOKEN, accounts.get(0).token); + editor.commit(); + //The user is now aut + //The user is now authenticated, it will be redirected to MainActivity + Intent mainActivity = new Intent(this, MainActivity.class); + startActivity(mainActivity); + finish(); + }); + if (accounts.size() > 1) { + Helper.loadPP(this, headerMainBinding.otherAccount2, accounts.get(1)); + headerMainBinding.otherAccount2.setVisibility(View.VISIBLE); + headerMainBinding.otherAccount2.setOnClickListener(v -> { + headerMenuOpen = false; + Toasty.info(BaseMainActivity.this, getString(R.string.toast_account_changed, "@" + accounts.get(1).mastodon_account.acct + "@" + accounts.get(1).instance), Toasty.LENGTH_LONG).show(); + BaseMainActivity.currentToken = accounts.get(1).token; + BaseMainActivity.currentUserID = accounts.get(1).user_id; + api = accounts.get(1).api; + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_TOKEN, accounts.get(1).token); + editor.commit(); + //The user is now aut + //The user is now authenticated, it will be redirected to MainActivity + Intent mainActivity = new Intent(this, MainActivity.class); + startActivity(mainActivity); + finish(); + }); + } + } + }; + mainHandler.post(myRunnable); + + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); } protected abstract void rateThisApp(); diff --git a/app/src/main/java/app/fedilab/android/client/entities/app/Account.java b/app/src/main/java/app/fedilab/android/client/entities/app/Account.java index 7564cc362..090c1b3dd 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/app/Account.java +++ b/app/src/main/java/app/fedilab/android/client/entities/app/Account.java @@ -318,6 +318,23 @@ public class Account extends BaseAccount implements Serializable { } } + /** + * Returns last used account + * + * @return BaseAccount {@link BaseAccount} + */ + public List getLastUsedAccounts() throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_UPDATED_AT + " DESC", null); + return cursorToListUser(c); + } catch (Exception e) { + return null; + } + } + /** * Remove an account from db * diff --git a/app/src/main/res/layout/nav_header_main.xml b/app/src/main/res/layout/nav_header_main.xml index c9c28f1ba..fc0ed4648 100644 --- a/app/src/main/res/layout/nav_header_main.xml +++ b/app/src/main/res/layout/nav_header_main.xml @@ -37,15 +37,48 @@ android:paddingRight="@dimen/activity_horizontal_margin" android:paddingBottom="5dp"> - + + + + + + + + + + +