Merge remote-tracking branch 'remotes/upstream/master' into switchListVsGridMaster
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewImagePagerAdapter.java
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
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.Vector;
28
29 import android.accounts.Account;
30 import android.graphics.Bitmap;
31 import android.support.v4.app.Fragment;
32 import android.support.v4.app.FragmentManager;
33 import android.support.v4.app.FragmentStatePagerAdapter;
34 import android.view.ViewGroup;
35
36 import com.owncloud.android.datamodel.FileDataStorageManager;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
39 import com.owncloud.android.ui.adapter.FileListListAdapter;
40 import com.owncloud.android.ui.fragment.FileFragment;
41 import com.owncloud.android.utils.FileStorageUtils;
42
43 /**
44 * Adapter class that provides Fragment instances
45 */
46 //public class PreviewImagePagerAdapter extends PagerAdapter {
47 public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
48
49 private Vector<OCFile> mImageFiles;
50 private Account mAccount;
51 private Set<Object> mObsoleteFragments;
52 private Set<Integer> mObsoletePositions;
53 private Set<Integer> mDownloadErrors;
54 private FileDataStorageManager mStorageManager;
55
56 private Map<Integer, FileFragment> mCachedFragments;
57
58 /**
59 * Constructor.
60 *
61 * @param fragmentManager {@link FragmentManager} instance that will handle
62 * the {@link Fragment}s provided by the adapter.
63 * @param parentFolder Folder where images will be searched for.
64 * @param storageManager Bridge to database.
65 */
66 public PreviewImagePagerAdapter(FragmentManager fragmentManager, OCFile parentFolder,
67 Account account, FileDataStorageManager storageManager /*,
68 boolean onlyOnDevice*/) {
69 super(fragmentManager);
70
71 if (fragmentManager == null) {
72 throw new IllegalArgumentException("NULL FragmentManager instance");
73 }
74 if (parentFolder == null) {
75 throw new IllegalArgumentException("NULL parent folder");
76 }
77 if (storageManager == null) {
78 throw new IllegalArgumentException("NULL storage manager");
79 }
80
81 mAccount = account;
82 mStorageManager = storageManager;
83 // TODO Enable when "On Device" is recovered ?
84 mImageFiles = mStorageManager.getFolderImages(parentFolder/*, false*/);
85
86 mImageFiles = FileStorageUtils.sortFolder(mImageFiles);
87
88 mObsoleteFragments = new HashSet<Object>();
89 mObsoletePositions = new HashSet<Integer>();
90 mDownloadErrors = new HashSet<Integer>();
91 //mFragmentManager = fragmentManager;
92 mCachedFragments = new HashMap<Integer, FileFragment>();
93 }
94
95 /**
96 * Returns the image files handled by the adapter.
97 *
98 * @return A vector with the image files handled by the adapter.
99 */
100 protected OCFile getFileAt(int position) {
101 return mImageFiles.get(position);
102 }
103
104
105 public Fragment getItem(int i) {
106 OCFile file = mImageFiles.get(i);
107 Fragment fragment = null;
108 if (file.isDown()) {
109 fragment = PreviewImageFragment.newInstance(file,
110 mObsoletePositions.contains(Integer.valueOf(i)), false);
111
112 } else if (mDownloadErrors.contains(Integer.valueOf(i))) {
113 fragment = FileDownloadFragment.newInstance(file, mAccount, true);
114 ((FileDownloadFragment)fragment).setError(true);
115 mDownloadErrors.remove(Integer.valueOf(i));
116 } else {
117 fragment = PreviewImageFragment.newInstance(file,
118 mObsoletePositions.contains(Integer.valueOf(i)), true);
119 }
120 mObsoletePositions.remove(Integer.valueOf(i));
121 return fragment;
122 }
123
124 public int getFilePosition(OCFile file) {
125 return mImageFiles.indexOf(file);
126 }
127
128 @Override
129 public int getCount() {
130 return mImageFiles.size();
131 }
132
133 @Override
134 public CharSequence getPageTitle(int position) {
135 return mImageFiles.get(position).getFileName();
136 }
137
138
139 public void updateFile(int position, OCFile file) {
140 FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
141 if (fragmentToUpdate != null) {
142 mObsoleteFragments.add(fragmentToUpdate);
143 }
144 mObsoletePositions.add(Integer.valueOf(position));
145 mImageFiles.set(position, file);
146 }
147
148
149 public void updateWithDownloadError(int position) {
150 FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
151 if (fragmentToUpdate != null) {
152 mObsoleteFragments.add(fragmentToUpdate);
153 }
154 mDownloadErrors.add(Integer.valueOf(position));
155 }
156
157 public void clearErrorAt(int position) {
158 FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
159 if (fragmentToUpdate != null) {
160 mObsoleteFragments.add(fragmentToUpdate);
161 }
162 mDownloadErrors.remove(Integer.valueOf(position));
163 }
164
165
166 @Override
167 public int getItemPosition(Object object) {
168 if (mObsoleteFragments.contains(object)) {
169 mObsoleteFragments.remove(object);
170 return POSITION_NONE;
171 }
172 return super.getItemPosition(object);
173 }
174
175
176 @Override
177 public Object instantiateItem(ViewGroup container, int position) {
178 Object fragment = super.instantiateItem(container, position);
179 mCachedFragments.put(Integer.valueOf(position), (FileFragment)fragment);
180 return fragment;
181 }
182
183 @Override
184 public void destroyItem(ViewGroup container, int position, Object object) {
185 mCachedFragments.remove(Integer.valueOf(position));
186 super.destroyItem(container, position, object);
187 }
188
189
190 public boolean pendingErrorAt(int position) {
191 return mDownloadErrors.contains(Integer.valueOf(position));
192 }
193
194 /**
195 * Reset the image zoom to default value for each CachedFragments
196 */
197 public void resetZoom() {
198 Iterator<FileFragment> entries = mCachedFragments.values().iterator();
199 while (entries.hasNext()) {
200 FileFragment fileFragment = (FileFragment) entries.next();
201 if (fileFragment instanceof PreviewImageFragment) {
202 ((PreviewImageFragment) fileFragment).getImageView().resetZoom();
203 }
204 }
205 }
206
207 /* -*
208 * Called when a change in the shown pages is going to start being made.
209 *
210 * @param container The containing View which is displaying this adapter's page views.
211 *- /
212 @Override
213 public void startUpdate(ViewGroup container) {
214 Log_OC.e(TAG, "** startUpdate");
215 }
216
217 @Override
218 public Object instantiateItem(ViewGroup container, int position) {
219 Log_OC.e(TAG, "** instantiateItem " + position);
220
221 if (mFragments.size() > position) {
222 Fragment fragment = mFragments.get(position);
223 if (fragment != null) {
224 Log_OC.e(TAG, "** \t returning cached item");
225 return fragment;
226 }
227 }
228
229 if (mCurTransaction == null) {
230 mCurTransaction = mFragmentManager.beginTransaction();
231 }
232
233 Fragment fragment = getItem(position);
234 if (mSavedState.size() > position) {
235 Fragment.SavedState savedState = mSavedState.get(position);
236 if (savedState != null) {
237 // TODO WATCH OUT:
238 // * The Fragment must currently be attached to the FragmentManager.
239 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
240 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
241 // to store a fragment reference
242 fragment.setInitialSavedState(savedState);
243 }
244 }
245 while (mFragments.size() <= position) {
246 mFragments.add(null);
247 }
248 fragment.setMenuVisibility(false);
249 mFragments.set(position, fragment);
250 //Log_OC.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
251 mCurTransaction.add(container.getId(), fragment);
252
253 return fragment;
254 }
255
256 @Override
257 public void destroyItem(ViewGroup container, int position, Object object) {
258 Log_OC.e(TAG, "** destroyItem " + position);
259 Fragment fragment = (Fragment)object;
260
261 if (mCurTransaction == null) {
262 mCurTransaction = mFragmentManager.beginTransaction();
263 }
264 Log_OC.e(TAG, "** \t removing fragment at position " + position);
265 while (mSavedState.size() <= position) {
266 mSavedState.add(null);
267 }
268 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
269 mFragments.set(position, null);
270
271 mCurTransaction.remove(fragment);
272 }
273
274 @Override
275 public void setPrimaryItem(ViewGroup container, int position, Object object) {
276 Fragment fragment = (Fragment)object;
277 if (fragment != mCurrentPrimaryItem) {
278 if (mCurrentPrimaryItem != null) {
279 mCurrentPrimaryItem.setMenuVisibility(false);
280 }
281 if (fragment != null) {
282 fragment.setMenuVisibility(true);
283 }
284 mCurrentPrimaryItem = fragment;
285 }
286 }
287
288 @Override
289 public void finishUpdate(ViewGroup container) {
290 Log_OC.e(TAG, "** finishUpdate (start)");
291 if (mCurTransaction != null) {
292 mCurTransaction.commitAllowingStateLoss();
293 mCurTransaction = null;
294 mFragmentManager.executePendingTransactions();
295 }
296 Log_OC.e(TAG, "** finishUpdate (end)");
297 }
298
299 @Override
300 public boolean isViewFromObject(View view, Object object) {
301 return ((Fragment)object).getView() == view;
302 }
303
304 @Override
305 public Parcelable saveState() {
306 Bundle state = null;
307 if (mSavedState.size() > 0) {
308 state = new Bundle();
309 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
310 mSavedState.toArray(savedStates);
311 state.putParcelableArray("states", savedStates);
312 }
313 for (int i=0; i<mFragments.size(); i++) {
314 Fragment fragment = mFragments.get(i);
315 if (fragment != null) {
316 if (state == null) {
317 state = new Bundle();
318 }
319 String key = "f" + i;
320 mFragmentManager.putFragment(state, key, fragment);
321 }
322 }
323 return state;
324 }
325
326 @Override
327 public void restoreState(Parcelable state, ClassLoader loader) {
328 if (state != null) {
329 Bundle bundle = (Bundle)state;
330 bundle.setClassLoader(loader);
331 Parcelable[] states = bundle.getParcelableArray("states");
332 mSavedState.clear();
333 mFragments.clear();
334 if (states != null) {
335 for (int i=0; i<states.length; i++) {
336 mSavedState.add((Fragment.SavedState)states[i]);
337 }
338 }
339 Iterable<String> keys = bundle.keySet();
340 for (String key: keys) {
341 if (key.startsWith("f")) {
342 int index = Integer.parseInt(key.substring(1));
343 Fragment f = mFragmentManager.getFragment(bundle, key);
344 if (f != null) {
345 while (mFragments.size() <= index) {
346 mFragments.add(null);
347 }
348 f.setMenuVisibility(false);
349 mFragments.set(index, f);
350 } else {
351 Log_OC.w(TAG, "Bad fragment at key " + key);
352 }
353 }
354 }
355 }
356 }
357 */
358 }