Skip to content

Commit

Permalink
Remove dummy_account_name from sync, use real email. Also remove sync…
Browse files Browse the repository at this point in the history
… account when logging out
  • Loading branch information
yukuku committed Jul 18, 2023
1 parent b2c921d commit 167eb7e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
4 changes: 1 addition & 3 deletions Alkitab/src/main/java/yuku/alkitab/base/sync/Sync.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ public static synchronized void notifySyncNeeded(final String... syncSetNames) {
extraSyncSetNames.add(extraSyncSetName);
}

if (extraSyncSetNames.size() == 0) {
return;
}
extraSyncSetNames.size();

final Account account = SyncUtils.getOrCreateSyncAccount();
final String authority = App.context.getString(R.string.sync_provider_authority);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ void updateDisplay() {

FirebaseCrashlytics.getInstance().setUserId("");

SyncUtils.removeAllSyncAccounts();

updateDisplay();
return Unit.INSTANCE;
},
Expand Down
35 changes: 32 additions & 3 deletions Alkitab/src/main/java/yuku/alkitab/base/sync/SyncUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yuku.alkitab.base.sync
import android.accounts.Account
import android.accounts.AccountManager
import android.content.Context
import yuku.afw.storage.Preferences
import yuku.alkitab.base.App
import yuku.alkitab.debug.R

Expand All @@ -13,24 +14,52 @@ object SyncUtils {
@JvmStatic
fun getOrCreateSyncAccount(): Account {
val ACCOUNT_TYPE = App.context.getString(R.string.account_type)
val ACCOUNT_NAME = "dummy_account_name"
val placeholderAccountName = "dummy_account_name"

// Get an instance of the Android account manager
val accountManager = App.context.getSystemService(Context.ACCOUNT_SERVICE) as AccountManager

// Create the account type and default account
val newAccount = Account(ACCOUNT_NAME, ACCOUNT_TYPE)
// New account using user's email
val newAccountName = Preferences.getString(R.string.pref_syncAccountName_key) ?: placeholderAccountName

for (oldAccount in accountManager.getAccountsByTypeForPackage(ACCOUNT_TYPE, App.context.packageName)) {
when (oldAccount.name) {
placeholderAccountName -> if (newAccountName != placeholderAccountName) {
// we are seeing placeholder, but if the new account is also a placeholder, do not remove
accountManager.removeAccountExplicitly(oldAccount)
}

newAccountName -> {
// do not do anything to the account that already matches the new account
}

else -> {
accountManager.removeAccountExplicitly(oldAccount)
}
}
}

/*
* We do not know if this is success or not.
* If the account already exists, it returns false, but that is what we need.
* So we can't differentiate between error and already exists. Both returns false.
*/
val newAccount = Account(newAccountName, ACCOUNT_TYPE)
accountManager.addAccountExplicitly(newAccount, null, null)

return newAccount
}

@JvmStatic
fun removeAllSyncAccounts() {
val ACCOUNT_TYPE = App.context.getString(R.string.account_type)
val accountManager = App.context.getSystemService(Context.ACCOUNT_SERVICE) as AccountManager

for (oldAccount in accountManager.getAccountsByTypeForPackage(ACCOUNT_TYPE, App.context.packageName)) {
accountManager.removeAccountExplicitly(oldAccount)
}
}

@JvmStatic
fun <C> isSameContent(a: Sync.Entity<C>, b: Sync.Entity<C>): Boolean {
if (a.gid != b.gid) return false
Expand Down

0 comments on commit 167eb7e

Please sign in to comment.