Skip to content

Commit cce8c27

Browse files
committed
Add logout action
1 parent 1a1fc14 commit cce8c27

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import android.accounts.AuthenticatorException;
66
import android.accounts.OperationCanceledException;
77
import android.app.Application;
8+
import android.content.Context;
9+
import android.content.SharedPreferences;
810
import android.content.pm.PackageManager;
11+
import android.database.sqlite.SQLiteDatabase;
912
import android.graphics.Bitmap;
1013
import android.os.Build;
14+
import android.preference.PreferenceManager;
1115
import android.support.v4.util.LruCache;
1216

1317
import com.android.volley.RequestQueue;
@@ -35,9 +39,14 @@
3539
import org.apache.http.params.BasicHttpParams;
3640
import org.apache.http.params.CoreProtocolPNames;
3741

42+
import java.io.File;
3843
import java.io.IOException;
3944

4045
import fr.free.nrw.commons.caching.CacheController;
46+
import fr.free.nrw.commons.category.Category;
47+
import fr.free.nrw.commons.contributions.Contribution;
48+
import fr.free.nrw.commons.data.DBOpenHelper;
49+
import fr.free.nrw.commons.modifications.ModifierSequence;
4150
import timber.log.Timber;
4251

4352
// TODO: Use ProGuard to rip out reporting when publishing
@@ -217,4 +226,59 @@ public boolean deviceHasCamera() {
217226
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) ||
218227
pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
219228
}
229+
230+
public void clearApplicationData(Context context) {
231+
File cacheDirectory = context.getCacheDir();
232+
File applicationDirectory = new File(cacheDirectory.getParent());
233+
if (applicationDirectory.exists()) {
234+
String[] fileNames = applicationDirectory.list();
235+
for (String fileName : fileNames) {
236+
if (!fileName.equals("lib")) {
237+
deleteFile(new File(applicationDirectory, fileName));
238+
}
239+
}
240+
}
241+
242+
AccountManager accountManager = AccountManager.get(this);
243+
Account[] allAccounts = accountManager.getAccountsByType(AccountUtil.accountType());
244+
for (int index = 0; index < allAccounts.length; index++) {
245+
accountManager.removeAccount(allAccounts[index], null, null);
246+
}
247+
248+
//TODO: fix preference manager
249+
PreferenceManager.getDefaultSharedPreferences(app).edit().clear().commit();
250+
SharedPreferences preferences = context.getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE);
251+
preferences.edit().clear().commit();
252+
context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit();;
253+
preferences.edit().putBoolean("firstrun", false).apply();
254+
updateAllDatabases(context);
255+
currentAccount = null;
256+
}
257+
258+
public void updateAllDatabases(Context context)
259+
{
260+
DBOpenHelper dbOpenHelper = DBOpenHelper.getInstance(context);
261+
dbOpenHelper.getReadableDatabase().close();
262+
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
263+
264+
ModifierSequence.Table.onDelete(db);
265+
Category.Table.onDelete(db);
266+
Contribution.Table.onDelete(db);
267+
}
268+
269+
public static boolean deleteFile(File file) {
270+
boolean deletedAll = true;
271+
if (file != null) {
272+
if (file.isDirectory()) {
273+
String[] children = file.list();
274+
for (int i = 0; i < children.length; i++) {
275+
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
276+
}
277+
} else {
278+
deletedAll = file.delete();
279+
}
280+
}
281+
282+
return deletedAll;
283+
}
220284
}

app/src/main/java/fr/free/nrw/commons/hamburger/NavigationBaseFragment.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package fr.free.nrw.commons.hamburger;
22

33
import android.content.ActivityNotFoundException;
4+
import android.content.DialogInterface;
45
import android.content.Intent;
56
import android.os.Bundle;
67
import android.support.v4.app.Fragment;
78
import android.support.v4.widget.DrawerLayout;
89
import android.support.v7.app.ActionBarDrawerToggle;
10+
import android.support.v7.app.AlertDialog;
911
import android.view.LayoutInflater;
1012
import android.view.View;
1113
import android.view.ViewGroup;
@@ -21,6 +23,7 @@
2123
import fr.free.nrw.commons.BuildConfig;
2224
import fr.free.nrw.commons.CommonsApplication;
2325
import fr.free.nrw.commons.R;
26+
import fr.free.nrw.commons.auth.LoginActivity;
2427
import fr.free.nrw.commons.contributions.ContributionsActivity;
2528
import fr.free.nrw.commons.nearby.NearbyActivity;
2629
import fr.free.nrw.commons.settings.SettingsActivity;
@@ -120,6 +123,32 @@ protected void onFeedbackItemClicked() {
120123
}
121124
}
122125

126+
@OnClick(R.id.logout_item)
127+
protected void onLogoutItemClicked() {
128+
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
129+
alertDialogBuilder.setMessage(R.string.logout_verification)
130+
.setCancelable(false)
131+
.setPositiveButton(R.string.yes,
132+
new DialogInterface.OnClickListener() {
133+
public void onClick(DialogInterface dialog, int id) {
134+
((CommonsApplication)getActivity().getApplicationContext()).clearApplicationData(getContext());
135+
Intent nearbyIntent = new Intent(getActivity(), LoginActivity.class);
136+
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
137+
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
138+
startActivity(nearbyIntent);
139+
getActivity().finish();
140+
}
141+
});
142+
alertDialogBuilder.setNegativeButton(R.string.no,
143+
new DialogInterface.OnClickListener() {
144+
public void onClick(DialogInterface dialog, int id) {
145+
dialog.cancel();
146+
}
147+
});
148+
AlertDialog alert = alertDialogBuilder.create();
149+
alert.show();
150+
}
151+
123152
private void closeDrawer() {
124153
if (drawerLayout != null) {
125154
drawerLayout.closeDrawer(drawerPane);

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Tap this message (or hit back) to skip this step.</string>
183183
<string name="maximum_limit_alert">Unable to display more than 500</string>
184184
<string name="set_limit">Set Recent Upload Limit</string>
185185
<string name="login_failed_2fa_not_supported">Two factor authentication is currently not supported.</string>
186+
<string name="logout_verification">Do you really want to logout?</string>
186187

187188
<string name="cancel">Cancel</string>
188189
<string name="navigation_drawer_open">Open</string>

0 commit comments

Comments
 (0)