FileFragment.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2015 ownCloud Inc.
  7. * Copyright (C) 2018 Andy Scherzinger
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  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 <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.fragment;
  22. import android.accounts.Account;
  23. import android.app.Activity;
  24. import android.os.Bundle;
  25. import com.owncloud.android.datamodel.OCFile;
  26. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  27. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  28. import com.owncloud.android.ui.activity.ComponentsGetter;
  29. import androidx.annotation.Nullable;
  30. import androidx.fragment.app.Fragment;
  31. import static com.owncloud.android.ui.activity.FileActivity.EXTRA_FILE;
  32. /**
  33. * Common methods for {@link Fragment}s containing {@link OCFile}s
  34. */
  35. public class FileFragment extends Fragment {
  36. private OCFile mFile;
  37. protected ContainerActivity mContainerActivity;
  38. /**
  39. * Creates an empty fragment.
  40. *
  41. * It's necessary to keep a public constructor without parameters; the system uses it when
  42. * tries to reinstantiate a fragment automatically.
  43. */
  44. public FileFragment() {
  45. mFile = null;
  46. }
  47. @Override
  48. public void onCreate(@Nullable Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. Bundle bundle = getArguments();
  51. setFile(bundle.getParcelable(EXTRA_FILE));
  52. }
  53. /**
  54. * Creates an instance for a given {@OCFile}.
  55. *
  56. * @param file
  57. */
  58. public static FileFragment newInstance(OCFile file) {
  59. FileFragment fileFragment = new FileFragment();
  60. Bundle bundle = new Bundle();
  61. bundle.putParcelable(EXTRA_FILE, file);
  62. fileFragment.setArguments(bundle);
  63. return fileFragment;
  64. }
  65. /**
  66. * Getter for the hold {@link OCFile}
  67. *
  68. * @return The {@link OCFile} hold
  69. */
  70. public OCFile getFile() {
  71. return mFile;
  72. }
  73. protected void setFile(OCFile file) {
  74. mFile = file;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. @Override
  80. public void onAttach(Activity activity) {
  81. super.onAttach(activity);
  82. try {
  83. mContainerActivity = (ContainerActivity) activity;
  84. } catch (ClassCastException e) {
  85. throw new IllegalArgumentException(activity.toString() + " must implement " +
  86. ContainerActivity.class.getSimpleName(), e);
  87. }
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. @Override
  93. public void onDetach() {
  94. mContainerActivity = null;
  95. super.onDetach();
  96. }
  97. /**
  98. * Interface to implement by any Activity that includes some instance of FileListFragment
  99. * Interface to implement by any Activity that includes some instance of FileFragment
  100. */
  101. public interface ContainerActivity extends ComponentsGetter {
  102. /**
  103. * Request the parent activity to show the details of an {@link OCFile}.
  104. *
  105. * @param file File to show details
  106. */
  107. void showDetails(OCFile file);
  108. /**
  109. * Request the parent activity to show the details of an {@link OCFile}.
  110. *
  111. * @param file File to show details
  112. * @param activeTab the active tab
  113. */
  114. void showDetails(OCFile file, int activeTab);
  115. ///// TO UNIFY IN A SINGLE CALLBACK METHOD - EVENT NOTIFICATIONs -> something happened
  116. // inside the fragment, MAYBE activity is interested --> unify in notification method
  117. /**
  118. * Callback method invoked when a the user browsed into a different folder through the
  119. * list of files
  120. *
  121. * @param folder
  122. */
  123. void onBrowsedDownTo(OCFile folder);
  124. /**
  125. * Callback method invoked when a the 'transfer state' of a file changes.
  126. *
  127. * This happens when a download or upload is started or ended for a file.
  128. *
  129. * This method is necessary by now to update the user interface of the double-pane layout
  130. * in tablets because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)}
  131. * and {@link FileUploaderBinder#isUploading(Account, OCFile)}
  132. * won't provide the needed response before the method where this is called finishes.
  133. *
  134. * TODO Remove this when the transfer state of a file is kept in the database
  135. * (other thing TODO)
  136. *
  137. * @param file OCFile which state changed.
  138. * @param downloading Flag signaling if the file is now downloading.
  139. * @param uploading Flag signaling if the file is now uploading.
  140. */
  141. void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading);
  142. }
  143. }