22
33import android .accounts .Account ;
44import android .accounts .AccountManager ;
5+ import android .accounts .AccountManagerCallback ;
6+ import android .accounts .AccountManagerFuture ;
57import android .accounts .AuthenticatorException ;
68import android .accounts .OperationCanceledException ;
79import android .app .Application ;
1113import android .database .sqlite .SQLiteDatabase ;
1214import android .preference .PreferenceManager ;
1315import android .support .v4 .util .LruCache ;
16+ import android .util .Log ;
1417
1518import com .facebook .drawee .backends .pipeline .Fresco ;
1619import com .facebook .stetho .Stetho ;
3235import fr .free .nrw .commons .mwapi .ApacheHttpClientMediaWikiApi ;
3336import fr .free .nrw .commons .mwapi .MediaWikiApi ;
3437import fr .free .nrw .commons .nearby .NearbyPlaces ;
38+ import fr .free .nrw .commons .theme .NavigationBaseActivity ;
3539import fr .free .nrw .commons .utils .FileUtils ;
3640import 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 /**
0 commit comments