|
5 | 5 | import android.accounts.AuthenticatorException;
|
6 | 6 | import android.accounts.OperationCanceledException;
|
7 | 7 | import android.app.Application;
|
| 8 | +import android.content.Context; |
| 9 | +import android.content.SharedPreferences; |
8 | 10 | import android.content.pm.PackageManager;
|
| 11 | +import android.database.sqlite.SQLiteDatabase; |
9 | 12 | import android.graphics.Bitmap;
|
10 | 13 | import android.os.Build;
|
| 14 | +import android.preference.PreferenceManager; |
11 | 15 | import android.support.v4.util.LruCache;
|
12 | 16 |
|
13 | 17 | import com.android.volley.RequestQueue;
|
|
35 | 39 | import org.apache.http.params.BasicHttpParams;
|
36 | 40 | import org.apache.http.params.CoreProtocolPNames;
|
37 | 41 |
|
| 42 | +import java.io.File; |
38 | 43 | import java.io.IOException;
|
39 | 44 |
|
40 | 45 | 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; |
41 | 50 | import timber.log.Timber;
|
42 | 51 |
|
43 | 52 | // TODO: Use ProGuard to rip out reporting when publishing
|
@@ -217,4 +226,59 @@ public boolean deviceHasCamera() {
|
217 | 226 | return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) ||
|
218 | 227 | pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
|
219 | 228 | }
|
| 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 | + } |
220 | 284 | }
|
0 commit comments