Merge branch 'develop' into refresh_folder_contents_when_browsed_into
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewImagePagerAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17 package com.owncloud.android.ui.preview;
18
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.Vector;
24
25 import android.accounts.Account;
26 import android.support.v4.app.Fragment;
27 import android.support.v4.app.FragmentManager;
28 import android.support.v4.app.FragmentStatePagerAdapter;
29 import android.view.ViewGroup;
30
31 import com.owncloud.android.datamodel.FileDataStorageManager;
32 import com.owncloud.android.datamodel.OCFile;
33 import com.owncloud.android.ui.fragment.FileFragment;
34
35 /**
36 * Adapter class that provides Fragment instances
37 *
38 * @author David A. Velasco
39 */
40 //public class PreviewImagePagerAdapter extends PagerAdapter {
41 public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
42
43 private Vector<OCFile> mImageFiles;
44 private Account mAccount;
45 private Set<Object> mObsoleteFragments;
46 private Set<Integer> mObsoletePositions;
47 private Set<Integer> mDownloadErrors;
48 private FileDataStorageManager mStorageManager;
49
50 private Map<Integer, FileFragment> mCachedFragments;
51
52 /**
53 * Constructor.
54 *
55 * @param fragmentManager {@link FragmentManager} instance that will handle the {@link Fragment}s provided by the adapter.
56 * @param parentFolder Folder where images will be searched for.
57 * @param storageManager Bridge to database.
58 */
59 public PreviewImagePagerAdapter(FragmentManager fragmentManager, OCFile parentFolder, Account account, FileDataStorageManager storageManager) {
60 super(fragmentManager);
61
62 if (fragmentManager == null) {
63 throw new IllegalArgumentException("NULL FragmentManager instance");
64 }
65 if (parentFolder == null) {
66 throw new IllegalArgumentException("NULL parent folder");
67 }
68 if (storageManager == null) {
69 throw new IllegalArgumentException("NULL storage manager");
70 }
71
72 mAccount = account;
73 mStorageManager = storageManager;
74 mImageFiles = mStorageManager.getFolderImages(parentFolder);
75 mObsoleteFragments = new HashSet<Object>();
76 mObsoletePositions = new HashSet<Integer>();
77 mDownloadErrors = new HashSet<Integer>();
78 //mFragmentManager = fragmentManager;
79 mCachedFragments = new HashMap<Integer, FileFragment>();
80 }
81
82
83 /**
84 * Returns the image files handled by the adapter.
85 *
86 * @return A vector with the image files handled by the adapter.
87 */
88 protected OCFile getFileAt(int position) {
89 return mImageFiles.get(position);
90 }
91
92
93 public Fragment getItem(int i) {
94 OCFile file = mImageFiles.get(i);
95 Fragment fragment = null;
96 if (file.isDown()) {
97 fragment = new PreviewImageFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i)));
98
99 } else if (mDownloadErrors.contains(Integer.valueOf(i))) {
100 fragment = new FileDownloadFragment(file, mAccount, true);
101 ((FileDownloadFragment)fragment).setError(true);
102 mDownloadErrors.remove(Integer.valueOf(i));
103
104 } else {
105 fragment = new FileDownloadFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i)));
106 }
107 mObsoletePositions.remove(Integer.valueOf(i));
108 return fragment;
109 }
110
111 public int getFilePosition(OCFile file) {
112 return mImageFiles.indexOf(file);
113 }
114
115 @Override
116 public int getCount() {
117 return mImageFiles.size();
118 }
119
120 @Override
121 public CharSequence getPageTitle(int position) {
122 return mImageFiles.get(position).getFileName();
123 }
124
125
126 public void updateFile(int position, OCFile file) {
127 FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
128 if (fragmentToUpdate != null) {
129 mObsoleteFragments.add(fragmentToUpdate);
130 }
131 mObsoletePositions.add(Integer.valueOf(position));
132 mImageFiles.set(position, file);
133 }
134
135
136 public void updateWithDownloadError(int position) {
137 FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
138 if (fragmentToUpdate != null) {
139 mObsoleteFragments.add(fragmentToUpdate);
140 }
141 mDownloadErrors.add(Integer.valueOf(position));
142 }
143
144 public void clearErrorAt(int position) {
145 FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
146 if (fragmentToUpdate != null) {
147 mObsoleteFragments.add(fragmentToUpdate);
148 }
149 mDownloadErrors.remove(Integer.valueOf(position));
150 }
151
152
153 @Override
154 public int getItemPosition(Object object) {
155 if (mObsoleteFragments.contains(object)) {
156 mObsoleteFragments.remove(object);
157 return POSITION_NONE;
158 }
159 return super.getItemPosition(object);
160 }
161
162
163 @Override
164 public Object instantiateItem(ViewGroup container, int position) {
165 Object fragment = super.instantiateItem(container, position);
166 mCachedFragments.put(Integer.valueOf(position), (FileFragment)fragment);
167 return fragment;
168 }
169
170 @Override
171 public void destroyItem(ViewGroup container, int position, Object object) {
172 mCachedFragments.remove(Integer.valueOf(position));
173 super.destroyItem(container, position, object);
174 }
175
176
177 public boolean pendingErrorAt(int position) {
178 return mDownloadErrors.contains(Integer.valueOf(position));
179 }
180
181 /* -*
182 * Called when a change in the shown pages is going to start being made.
183 *
184 * @param container The containing View which is displaying this adapter's page views.
185 *- /
186 @Override
187 public void startUpdate(ViewGroup container) {
188 Log.e(TAG, "** startUpdate");
189 }
190
191 @Override
192 public Object instantiateItem(ViewGroup container, int position) {
193 Log.e(TAG, "** instantiateItem " + position);
194
195 if (mFragments.size() > position) {
196 Fragment fragment = mFragments.get(position);
197 if (fragment != null) {
198 Log.e(TAG, "** \t returning cached item");
199 return fragment;
200 }
201 }
202
203 if (mCurTransaction == null) {
204 mCurTransaction = mFragmentManager.beginTransaction();
205 }
206
207 Fragment fragment = getItem(position);
208 if (mSavedState.size() > position) {
209 Fragment.SavedState savedState = mSavedState.get(position);
210 if (savedState != null) {
211 // TODO WATCH OUT:
212 // * The Fragment must currently be attached to the FragmentManager.
213 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
214 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
215 // to store a fragment reference
216 fragment.setInitialSavedState(savedState);
217 }
218 }
219 while (mFragments.size() <= position) {
220 mFragments.add(null);
221 }
222 fragment.setMenuVisibility(false);
223 mFragments.set(position, fragment);
224 //Log.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
225 mCurTransaction.add(container.getId(), fragment);
226
227 return fragment;
228 }
229
230 @Override
231 public void destroyItem(ViewGroup container, int position, Object object) {
232 Log.e(TAG, "** destroyItem " + position);
233 Fragment fragment = (Fragment)object;
234
235 if (mCurTransaction == null) {
236 mCurTransaction = mFragmentManager.beginTransaction();
237 }
238 Log.e(TAG, "** \t removing fragment at position " + position);
239 while (mSavedState.size() <= position) {
240 mSavedState.add(null);
241 }
242 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
243 mFragments.set(position, null);
244
245 mCurTransaction.remove(fragment);
246 }
247
248 @Override
249 public void setPrimaryItem(ViewGroup container, int position, Object object) {
250 Fragment fragment = (Fragment)object;
251 if (fragment != mCurrentPrimaryItem) {
252 if (mCurrentPrimaryItem != null) {
253 mCurrentPrimaryItem.setMenuVisibility(false);
254 }
255 if (fragment != null) {
256 fragment.setMenuVisibility(true);
257 }
258 mCurrentPrimaryItem = fragment;
259 }
260 }
261
262 @Override
263 public void finishUpdate(ViewGroup container) {
264 Log.e(TAG, "** finishUpdate (start)");
265 if (mCurTransaction != null) {
266 mCurTransaction.commitAllowingStateLoss();
267 mCurTransaction = null;
268 mFragmentManager.executePendingTransactions();
269 }
270 Log.e(TAG, "** finishUpdate (end)");
271 }
272
273 @Override
274 public boolean isViewFromObject(View view, Object object) {
275 return ((Fragment)object).getView() == view;
276 }
277
278 @Override
279 public Parcelable saveState() {
280 Bundle state = null;
281 if (mSavedState.size() > 0) {
282 state = new Bundle();
283 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
284 mSavedState.toArray(savedStates);
285 state.putParcelableArray("states", savedStates);
286 }
287 for (int i=0; i<mFragments.size(); i++) {
288 Fragment fragment = mFragments.get(i);
289 if (fragment != null) {
290 if (state == null) {
291 state = new Bundle();
292 }
293 String key = "f" + i;
294 mFragmentManager.putFragment(state, key, fragment);
295 }
296 }
297 return state;
298 }
299
300 @Override
301 public void restoreState(Parcelable state, ClassLoader loader) {
302 if (state != null) {
303 Bundle bundle = (Bundle)state;
304 bundle.setClassLoader(loader);
305 Parcelable[] states = bundle.getParcelableArray("states");
306 mSavedState.clear();
307 mFragments.clear();
308 if (states != null) {
309 for (int i=0; i<states.length; i++) {
310 mSavedState.add((Fragment.SavedState)states[i]);
311 }
312 }
313 Iterable<String> keys = bundle.keySet();
314 for (String key: keys) {
315 if (key.startsWith("f")) {
316 int index = Integer.parseInt(key.substring(1));
317 Fragment f = mFragmentManager.getFragment(bundle, key);
318 if (f != null) {
319 while (mFragments.size() <= index) {
320 mFragments.add(null);
321 }
322 f.setMenuVisibility(false);
323 mFragments.set(index, f);
324 } else {
325 Log.w(TAG, "Bad fragment at key " + key);
326 }
327 }
328 }
329 }
330 }
331 */
332 }