Skip to content

Commit 7c956e8

Browse files
authored
Merge pull request commons-app#856 from Bluesir9/issue_848_logout_contributions_redirect_issue
Added callback when removing accounts on logout
2 parents ae0e051 + 35fdb43 commit 7c956e8

File tree

2 files changed

+65
-20
lines changed

2 files changed

+65
-20
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.accounts.Account;
44
import android.accounts.AccountManager;
5+
import android.accounts.AccountManagerCallback;
6+
import android.accounts.AccountManagerFuture;
57
import android.accounts.AuthenticatorException;
68
import android.accounts.OperationCanceledException;
79
import android.app.Application;
@@ -11,6 +13,7 @@
1113
import android.database.sqlite.SQLiteDatabase;
1214
import android.preference.PreferenceManager;
1315
import android.support.v4.util.LruCache;
16+
import android.util.Log;
1417

1518
import com.facebook.drawee.backends.pipeline.Fresco;
1619
import com.facebook.stetho.Stetho;
@@ -32,6 +35,7 @@
3235
import fr.free.nrw.commons.mwapi.ApacheHttpClientMediaWikiApi;
3336
import fr.free.nrw.commons.mwapi.MediaWikiApi;
3437
import fr.free.nrw.commons.nearby.NearbyPlaces;
38+
import fr.free.nrw.commons.theme.NavigationBaseActivity;
3539
import fr.free.nrw.commons.utils.FileUtils;
3640
import timber.log.Timber;
3741

@@ -186,7 +190,7 @@ public boolean deviceHasCamera() {
186190
pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
187191
}
188192

189-
public void clearApplicationData(Context context) {
193+
public void clearApplicationData(Context context, NavigationBaseActivity.LogoutListener logoutListener) {
190194
File cacheDirectory = context.getCacheDir();
191195
File applicationDirectory = new File(cacheDirectory.getParent());
192196
if (applicationDirectory.exists()) {
@@ -200,19 +204,53 @@ public void clearApplicationData(Context context) {
200204

201205
AccountManager accountManager = AccountManager.get(this);
202206
Account[] allAccounts = accountManager.getAccountsByType(AccountUtil.accountType());
203-
for (Account allAccount : allAccounts) {
204-
accountManager.removeAccount(allAccount, null, null);
205-
}
206207

207-
//TODO: fix preference manager
208-
PreferenceManager.getDefaultSharedPreferences(getInstance()).edit().clear().commit();
209-
SharedPreferences preferences = context
210-
.getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE);
211-
preferences.edit().clear().commit();
212-
context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit();
213-
preferences.edit().putBoolean("firstrun", false).apply();
214-
updateAllDatabases();
215-
currentAccount = null;
208+
AccountManagerCallback<Boolean> amCallback = new AccountManagerCallback<Boolean>() {
209+
210+
private int index = 0;
211+
212+
void setIndex(int index) {
213+
this.index = index;
214+
}
215+
216+
int getIndex() {
217+
return index;
218+
}
219+
220+
@Override
221+
public void run(AccountManagerFuture<Boolean> accountManagerFuture) {
222+
setIndex(getIndex() + 1);
223+
224+
try {
225+
if (accountManagerFuture != null) {
226+
if (accountManagerFuture.getResult()) {
227+
Timber.d("Account removed successfully.");
228+
}
229+
}
230+
} catch (OperationCanceledException | IOException | AuthenticatorException e) {
231+
e.printStackTrace();
232+
}
233+
234+
if (getIndex() == allAccounts.length) {
235+
Timber.d("All accounts have been removed");
236+
//TODO: fix preference manager
237+
PreferenceManager.getDefaultSharedPreferences(getInstance()).edit().clear().commit();
238+
SharedPreferences preferences = context
239+
.getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE);
240+
preferences.edit().clear().commit();
241+
context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit();
242+
preferences.edit().putBoolean("firstrun", false).apply();
243+
updateAllDatabases();
244+
currentAccount = null;
245+
246+
logoutListener.onLogoutComplete();
247+
}
248+
}
249+
};
250+
251+
for (Account account : allAccounts) {
252+
accountManager.removeAccount(account, amCallback, null);
253+
}
216254
}
217255

218256
/**

app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import fr.free.nrw.commons.contributions.ContributionsActivity;
2323
import fr.free.nrw.commons.nearby.NearbyActivity;
2424
import fr.free.nrw.commons.settings.SettingsActivity;
25+
import timber.log.Timber;
2526

2627
public class NavigationBaseActivity extends BaseActivity
2728
implements NavigationView.OnNavigationItemSelectedListener {
@@ -121,13 +122,15 @@ public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
121122
.setCancelable(false)
122123
.setPositiveButton(R.string.yes, (dialog, which) -> {
123124
((CommonsApplication) getApplicationContext())
124-
.clearApplicationData(NavigationBaseActivity.this);
125-
Intent nearbyIntent = new Intent(
126-
NavigationBaseActivity.this, LoginActivity.class);
127-
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
128-
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
129-
startActivity(nearbyIntent);
130-
finish();
125+
.clearApplicationData(NavigationBaseActivity.this, () -> {
126+
Timber.d("Logout complete callback received.");
127+
Intent nearbyIntent = new Intent(
128+
NavigationBaseActivity.this, LoginActivity.class);
129+
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
130+
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
131+
startActivity(nearbyIntent);
132+
finish();
133+
});
131134
})
132135
.setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel())
133136
.show();
@@ -136,4 +139,8 @@ public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
136139
return false;
137140
}
138141
}
142+
143+
public interface LogoutListener {
144+
void onLogoutComplete();
145+
}
139146
}

0 commit comments

Comments
 (0)