TaskRetainerFragment.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2016 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.fragment;
  20. import android.content.Context;
  21. import android.os.Bundle;
  22. import android.support.v4.app.Fragment;
  23. import com.owncloud.android.ui.activity.ReceiveExternalFilesActivity;
  24. import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask;
  25. /**
  26. * Fragment retaining a background task across configuration changes.
  27. */
  28. public class TaskRetainerFragment extends Fragment {
  29. public static final String FTAG_TASK_RETAINER_FRAGMENT = "TASK_RETAINER_FRAGMENT";
  30. private CopyAndUploadContentUrisTask mTask;
  31. /**
  32. * Updates the listener of the retained task whenever the parent
  33. * Activity is attached.
  34. *
  35. * Since its done in main thread, and provided the AsyncTask only accesses
  36. * the listener in the main thread (should so), no sync problem should occur.
  37. */
  38. @Override
  39. public void onAttach(Context context) {
  40. super.onAttach(context);
  41. if (mTask != null) {
  42. if (context instanceof ReceiveExternalFilesActivity) {
  43. mTask.setListener((CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener) context);
  44. } else {
  45. mTask.setListener(null);
  46. }
  47. }
  48. }
  49. /**
  50. * Only called once, since the instance is retained across configuration changes
  51. */
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setRetainInstance(true); // the key point
  56. }
  57. /**
  58. * Sets the task to retain across configuration changes
  59. *
  60. * @param task Task to retain
  61. */
  62. public void setTask(CopyAndUploadContentUrisTask task) {
  63. if (mTask != null) {
  64. mTask.setListener(null);
  65. }
  66. mTask = task;
  67. Context context = getContext();
  68. if (mTask != null && context != null) {
  69. if (context instanceof ReceiveExternalFilesActivity) {
  70. task.setListener((CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener) context);
  71. } else {
  72. task.setListener(null);
  73. }
  74. }
  75. }
  76. }