Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/CommonsApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.free.nrw.commons;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
Expand Down Expand Up @@ -128,6 +129,7 @@ public static RefWatcher getRefWatcher(Context context) {
* @param context Application context
* @param logoutListener Implementation of interface LogoutListener
*/
@SuppressLint("CheckResult")
public void clearApplicationData(Context context, LogoutListener logoutListener) {
File cacheDirectory = context.getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
Expand All @@ -140,7 +142,7 @@ public void clearApplicationData(Context context, LogoutListener logoutListener)
}
}

sessionManager.clearAllAccounts()
sessionManager.logout()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
Expand All @@ -151,7 +153,6 @@ public void clearApplicationData(Context context, LogoutListener logoutListener)
applicationPrefs.edit().putBoolean("firstrun", false).apply();
otherPrefs.edit().clear().apply();
updateAllDatabases();

logoutListener.onLogoutComplete();
});
}
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/auth/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,19 @@ public void forceLogin(Context context) {
}
}

public Completable clearAllAccounts() {
/**
* 1. Clears existing accounts from account manager
* 2. Calls MediaWikiApi's logout function to clear cookies
* @return
*/
public Completable logout() {
AccountManager accountManager = AccountManager.get(context);
Account[] allAccounts = accountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE);
return Completable.fromObservable(Observable.fromArray(allAccounts)
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
.doOnComplete(() -> currentAccount = null);
.doOnComplete(() -> {
mediaWikiApi.logout();
currentAccount = null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1032,4 +1032,15 @@ private Date parseMWDate(String mwDate) {
}
}

/**
* Calls media wiki's logout API
*/
public void logout() {
try {
api.logout();
} catch (IOException e) {
Timber.e(e, "Error occurred while logging out");
}
}

}
2 changes: 2 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/mwapi/CustomMwApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public void logout() throws IOException {
// I should be doing more validation here, but meh
isLoggedIn = false;
this.action("logout").post();
removeAllCookies();
authCookie = null;
}

private CustomApiResult makeRequest(String method, HashMap<String, Object> params) throws IOException {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public interface MediaWikiApi {
@NonNull
Single<JSONObject> getAchievements(String userName);

void logout();

interface ProgressListener {
void onProgress(long transferred, long total);
}
Expand Down