CheckAvailableSpaceTask.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2019 Tobias Kaminsky
  6. * Copyright (C) 2019 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU 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 General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.asynctasks;
  22. import android.os.AsyncTask;
  23. import com.owncloud.android.utils.FileStorageUtils;
  24. import java.io.File;
  25. /**
  26. * Asynchronous task checking if there is space enough to copy all the files chosen to upload into the ownCloud local
  27. * folder. Maybe an AsyncTask is not strictly necessary, but who really knows.
  28. */
  29. public class CheckAvailableSpaceTask extends AsyncTask<Boolean, Void, Boolean> {
  30. private String[] paths;
  31. private CheckAvailableSpaceListener callback;
  32. public CheckAvailableSpaceTask(CheckAvailableSpaceListener callback, String[] paths) {
  33. this.paths = paths;
  34. this.callback = callback;
  35. }
  36. /**
  37. * Updates the UI before trying the movement.
  38. */
  39. @Override
  40. protected void onPreExecute() {
  41. callback.onCheckAvailableSpaceStart();
  42. }
  43. /**
  44. * Checks the available space.
  45. *
  46. * @param params boolean flag if storage calculation should be done.
  47. * @return 'True' if there is space enough or doesn't have to be calculated
  48. */
  49. @Override
  50. protected Boolean doInBackground(Boolean... params) {
  51. long total = 0;
  52. for (int i = 0; paths != null && i < paths.length; i++) {
  53. String localPath = paths[i];
  54. File localFile = new File(localPath);
  55. total += localFile.length();
  56. }
  57. return FileStorageUtils.getUsableSpace() >= total;
  58. }
  59. @Override
  60. protected void onPostExecute(Boolean result) {
  61. callback.onCheckAvailableSpaceFinish(result, paths);
  62. }
  63. public interface CheckAvailableSpaceListener {
  64. void onCheckAvailableSpaceStart();
  65. void onCheckAvailableSpaceFinish(boolean hasEnoughSpaceAvailable, String[] filesToUpload);
  66. }
  67. }