diff --git a/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java b/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java index b7b75fae5..1fbf98a28 100644 --- a/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java +++ b/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java @@ -2,6 +2,8 @@ package fr.free.nrw.commons; import android.accounts.Account; import android.accounts.AccountManager; +import android.accounts.AccountManagerCallback; +import android.accounts.AccountManagerFuture; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.app.Application; @@ -11,6 +13,7 @@ import android.content.pm.PackageManager; import android.database.sqlite.SQLiteDatabase; import android.preference.PreferenceManager; import android.support.v4.util.LruCache; +import android.util.Log; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.stetho.Stetho; @@ -32,6 +35,7 @@ import fr.free.nrw.commons.modifications.ModifierSequence; import fr.free.nrw.commons.mwapi.ApacheHttpClientMediaWikiApi; import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.nearby.NearbyPlaces; +import fr.free.nrw.commons.theme.NavigationBaseActivity; import fr.free.nrw.commons.utils.FileUtils; import timber.log.Timber; @@ -186,7 +190,7 @@ public class CommonsApplication extends Application { pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT); } - public void clearApplicationData(Context context) { + public void clearApplicationData(Context context, NavigationBaseActivity.LogoutListener logoutListener) { File cacheDirectory = context.getCacheDir(); File applicationDirectory = new File(cacheDirectory.getParent()); if (applicationDirectory.exists()) { @@ -200,19 +204,53 @@ public class CommonsApplication extends Application { AccountManager accountManager = AccountManager.get(this); Account[] allAccounts = accountManager.getAccountsByType(AccountUtil.accountType()); - for (Account allAccount : allAccounts) { - accountManager.removeAccount(allAccount, null, null); - } - //TODO: fix preference manager - PreferenceManager.getDefaultSharedPreferences(getInstance()).edit().clear().commit(); - SharedPreferences preferences = context - .getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE); - preferences.edit().clear().commit(); - context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit(); - preferences.edit().putBoolean("firstrun", false).apply(); - updateAllDatabases(); - currentAccount = null; + AccountManagerCallback amCallback = new AccountManagerCallback() { + + private int index = 0; + + void setIndex(int index) { + this.index = index; + } + + int getIndex() { + return index; + } + + @Override + public void run(AccountManagerFuture accountManagerFuture) { + setIndex(getIndex() + 1); + + try { + if (accountManagerFuture != null) { + if (accountManagerFuture.getResult()) { + Timber.d("Account removed successfully."); + } + } + } catch (OperationCanceledException | IOException | AuthenticatorException e) { + e.printStackTrace(); + } + + if (getIndex() == allAccounts.length) { + Timber.d("All accounts have been removed"); + //TODO: fix preference manager + PreferenceManager.getDefaultSharedPreferences(getInstance()).edit().clear().commit(); + SharedPreferences preferences = context + .getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE); + preferences.edit().clear().commit(); + context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit(); + preferences.edit().putBoolean("firstrun", false).apply(); + updateAllDatabases(); + currentAccount = null; + + logoutListener.onLogoutComplete(); + } + } + }; + + for (Account account : allAccounts) { + accountManager.removeAccount(account, amCallback, null); + } } /** diff --git a/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java b/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java index d86090c91..4b4579d50 100644 --- a/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java @@ -22,6 +22,7 @@ import fr.free.nrw.commons.auth.LoginActivity; import fr.free.nrw.commons.contributions.ContributionsActivity; import fr.free.nrw.commons.nearby.NearbyActivity; import fr.free.nrw.commons.settings.SettingsActivity; +import timber.log.Timber; public class NavigationBaseActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener { @@ -121,13 +122,15 @@ public class NavigationBaseActivity extends BaseActivity .setCancelable(false) .setPositiveButton(R.string.yes, (dialog, which) -> { ((CommonsApplication) getApplicationContext()) - .clearApplicationData(NavigationBaseActivity.this); - Intent nearbyIntent = new Intent( - NavigationBaseActivity.this, LoginActivity.class); - nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); - nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(nearbyIntent); - finish(); + .clearApplicationData(NavigationBaseActivity.this, () -> { + Timber.d("Logout complete callback received."); + Intent nearbyIntent = new Intent( + NavigationBaseActivity.this, LoginActivity.class); + nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); + nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(nearbyIntent); + finish(); + }); }) .setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel()) .show(); @@ -136,4 +139,8 @@ public class NavigationBaseActivity extends BaseActivity return false; } } + + public interface LogoutListener { + void onLogoutComplete(); + } }