AccountRemovalJob.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.services;
  22. import android.accounts.Account;
  23. import android.accounts.AccountManager;
  24. import android.accounts.AccountManagerCallback;
  25. import android.accounts.AccountManagerFuture;
  26. import android.content.Context;
  27. import android.support.annotation.NonNull;
  28. import com.evernote.android.job.Job;
  29. import com.evernote.android.job.util.support.PersistableBundleCompat;
  30. import com.owncloud.android.MainApp;
  31. import com.owncloud.android.authentication.AccountUtils;
  32. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  33. import com.owncloud.android.datamodel.FileDataStorageManager;
  34. import com.owncloud.android.ui.events.AccountRemovedEvent;
  35. import com.owncloud.android.utils.FileStorageUtils;
  36. import org.greenrobot.eventbus.EventBus;
  37. import java.io.File;
  38. import static android.content.Context.ACCOUNT_SERVICE;
  39. import static com.owncloud.android.ui.activity.ManageAccountsActivity.PENDING_FOR_REMOVAL;
  40. /**
  41. * Removes account and all local files
  42. */
  43. public class AccountRemovalJob extends Job implements AccountManagerCallback<Boolean> {
  44. public static final String TAG = "AccountRemovalJob";
  45. public static final String ACCOUNT = "account";
  46. @NonNull
  47. @Override
  48. protected Result onRunJob(Params params) {
  49. Context context = MainApp.getAppContext();
  50. PersistableBundleCompat bundle = params.getExtras();
  51. Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT, ""));
  52. if (account != null ) {
  53. AccountManager am = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
  54. am.removeAccount(account, this, null);
  55. FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
  56. File tempDir = new File(FileStorageUtils.getTemporalPath(account.name));
  57. File saveDir = new File(FileStorageUtils.getSavePath(account.name));
  58. FileStorageUtils.deleteRecursively(tempDir, storageManager);
  59. FileStorageUtils.deleteRecursively(saveDir, storageManager);
  60. // delete all database entries
  61. storageManager.deleteAllFiles();
  62. // remove pending account removal
  63. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
  64. arbitraryDataProvider.deleteKeyForAccount(account, PENDING_FOR_REMOVAL);
  65. return Result.SUCCESS;
  66. } else {
  67. return Result.FAILURE;
  68. }
  69. }
  70. @Override
  71. public void run(AccountManagerFuture<Boolean> future) {
  72. if (future.isDone()) {
  73. EventBus.getDefault().post(new AccountRemovedEvent());
  74. }
  75. }
  76. }