PreviewImagePagerAdapter.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.preview;
  21. import android.accounts.Account;
  22. import android.support.v4.app.Fragment;
  23. import android.support.v4.app.FragmentManager;
  24. import android.support.v4.app.FragmentStatePagerAdapter;
  25. import android.util.SparseArray;
  26. import android.view.ViewGroup;
  27. import com.owncloud.android.datamodel.FileDataStorageManager;
  28. import com.owncloud.android.datamodel.OCFile;
  29. import com.owncloud.android.datamodel.VirtualFolderType;
  30. import com.owncloud.android.ui.fragment.FileFragment;
  31. import com.owncloud.android.utils.FileSortOrderByName;
  32. import com.owncloud.android.utils.FileStorageUtils;
  33. import java.util.HashSet;
  34. import java.util.Set;
  35. import java.util.Vector;
  36. import javax.annotation.Nullable;
  37. /**
  38. * Adapter class that provides Fragment instances
  39. */
  40. public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
  41. private Vector<OCFile> mImageFiles;
  42. private Account mAccount;
  43. private Set<Object> mObsoleteFragments;
  44. private Set<Integer> mObsoletePositions;
  45. private Set<Integer> mDownloadErrors;
  46. private FileDataStorageManager mStorageManager;
  47. private SparseArray<FileFragment> mCachedFragments;
  48. /**
  49. * Constructor
  50. *
  51. * @param fragmentManager {@link FragmentManager} instance that will handle
  52. * the {@link Fragment}s provided by the adapter.
  53. * @param parentFolder Folder where images will be searched for.
  54. * @param storageManager Bridge to database.
  55. */
  56. public PreviewImagePagerAdapter(FragmentManager fragmentManager, OCFile parentFolder,
  57. Account account, FileDataStorageManager storageManager,
  58. boolean onlyOnDevice) {
  59. super(fragmentManager);
  60. if (fragmentManager == null) {
  61. throw new IllegalArgumentException("NULL FragmentManager instance");
  62. }
  63. if (parentFolder == null) {
  64. throw new IllegalArgumentException("NULL parent folder");
  65. }
  66. if (storageManager == null) {
  67. throw new IllegalArgumentException("NULL storage manager");
  68. }
  69. mAccount = account;
  70. mStorageManager = storageManager;
  71. mImageFiles = mStorageManager.getFolderImages(parentFolder, onlyOnDevice);
  72. mImageFiles = FileSortOrderByName.sort_a_to_z.sortCloudFiles(mImageFiles);
  73. mObsoleteFragments = new HashSet<>();
  74. mObsoletePositions = new HashSet<>();
  75. mDownloadErrors = new HashSet<>();
  76. mCachedFragments = new SparseArray<>();
  77. }
  78. /**
  79. * Constructor
  80. *
  81. * @param fragmentManager {@link FragmentManager} instance that will handle
  82. * the {@link Fragment}s provided by the adapter.
  83. * @param type Type of virtual folder, e.g. favorite or photos
  84. * @param storageManager Bridge to database.
  85. */
  86. public PreviewImagePagerAdapter(FragmentManager fragmentManager, VirtualFolderType type,
  87. Account account, FileDataStorageManager storageManager) {
  88. super(fragmentManager);
  89. if (fragmentManager == null) {
  90. throw new IllegalArgumentException("NULL FragmentManager instance");
  91. }
  92. if (type == null) {
  93. throw new IllegalArgumentException("NULL parent folder");
  94. }
  95. if (storageManager == null) {
  96. throw new IllegalArgumentException("NULL storage manager");
  97. }
  98. mAccount = account;
  99. mStorageManager = storageManager;
  100. mImageFiles = mStorageManager.getVirtualFolderContent(type, true);
  101. if (type == VirtualFolderType.PHOTOS) {
  102. mImageFiles = FileStorageUtils.sortOcFolderDescDateModified(mImageFiles);
  103. }
  104. mObsoleteFragments = new HashSet<>();
  105. mObsoletePositions = new HashSet<>();
  106. mDownloadErrors = new HashSet<>();
  107. mCachedFragments = new SparseArray<>();
  108. }
  109. /**
  110. * Returns the image files handled by the adapter.
  111. *
  112. * @return OCFile desired image or null if position is not in adapter
  113. */
  114. @Nullable
  115. public OCFile getFileAt(int position) {
  116. try {
  117. return mImageFiles.get(position);
  118. } catch (IndexOutOfBoundsException exception) {
  119. return null;
  120. }
  121. }
  122. public Fragment getItem(int i) {
  123. OCFile file = mImageFiles.get(i);
  124. Fragment fragment;
  125. if (file.isDown()) {
  126. fragment = PreviewImageFragment.newInstance(file, mObsoletePositions.contains(i), false);
  127. } else {
  128. if (mDownloadErrors.contains(i)) {
  129. fragment = FileDownloadFragment.newInstance(file, mAccount, true);
  130. ((FileDownloadFragment) fragment).setError(true);
  131. mDownloadErrors.remove(i);
  132. } else {
  133. if (file.isEncrypted()) {
  134. fragment = FileDownloadFragment.newInstance(file, mAccount, mObsoletePositions.contains(i));
  135. } else {
  136. fragment = PreviewImageFragment.newInstance(file, mObsoletePositions.contains(i), true);
  137. }
  138. }
  139. }
  140. mObsoletePositions.remove(i);
  141. return fragment;
  142. }
  143. public int getFilePosition(OCFile file) {
  144. return mImageFiles.indexOf(file);
  145. }
  146. @Override
  147. public int getCount() {
  148. return mImageFiles.size();
  149. }
  150. @Override
  151. public CharSequence getPageTitle(int position) {
  152. return mImageFiles.get(position).getFileName();
  153. }
  154. public void updateFile(int position, OCFile file) {
  155. FileFragment fragmentToUpdate = mCachedFragments.get(position);
  156. if (fragmentToUpdate != null) {
  157. mObsoleteFragments.add(fragmentToUpdate);
  158. }
  159. mObsoletePositions.add(position);
  160. mImageFiles.set(position, file);
  161. }
  162. public void updateWithDownloadError(int position) {
  163. FileFragment fragmentToUpdate = mCachedFragments.get(position);
  164. if (fragmentToUpdate != null) {
  165. mObsoleteFragments.add(fragmentToUpdate);
  166. }
  167. mDownloadErrors.add(position);
  168. }
  169. @Override
  170. public int getItemPosition(Object object) {
  171. if (mObsoleteFragments.contains(object)) {
  172. mObsoleteFragments.remove(object);
  173. return POSITION_NONE;
  174. }
  175. return super.getItemPosition(object);
  176. }
  177. @Override
  178. public Object instantiateItem(ViewGroup container, int position) {
  179. Object fragment = super.instantiateItem(container, position);
  180. mCachedFragments.put(position, (FileFragment) fragment);
  181. return fragment;
  182. }
  183. @Override
  184. public void destroyItem(ViewGroup container, int position, Object object) {
  185. mCachedFragments.remove(position);
  186. super.destroyItem(container, position, object);
  187. }
  188. public boolean pendingErrorAt(int position) {
  189. return mDownloadErrors.contains(position);
  190. }
  191. /**
  192. * Reset the image zoom to default value for each CachedFragments
  193. */
  194. public void resetZoom() {
  195. for (int i = 0; i < mCachedFragments.size(); i++) {
  196. FileFragment fileFragment = mCachedFragments.valueAt(i);
  197. if (fileFragment instanceof PreviewImageFragment) {
  198. ((PreviewImageFragment) fileFragment).getImageView().resetZoom();
  199. }
  200. }
  201. }
  202. }