FileFragment.java 5.0 KB

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