2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.ui
.preview
;
22 import java
.util
.HashMap
;
23 import java
.util
.HashSet
;
24 import java
.util
.Iterator
;
27 import java
.util
.Vector
;
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
;
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
;
44 * Adapter class that provides Fragment instances
46 //public class PreviewImagePagerAdapter extends PagerAdapter {
47 public class PreviewImagePagerAdapter
extends FragmentStatePagerAdapter
{
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
;
56 private Map
<Integer
, FileFragment
> mCachedFragments
;
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.
66 public PreviewImagePagerAdapter(FragmentManager fragmentManager
, OCFile parentFolder
,
67 Account account
, FileDataStorageManager storageManager
/*,
68 boolean onlyOnDevice*/) {
69 super(fragmentManager
);
71 if (fragmentManager
== null
) {
72 throw new IllegalArgumentException("NULL FragmentManager instance");
74 if (parentFolder
== null
) {
75 throw new IllegalArgumentException("NULL parent folder");
77 if (storageManager
== null
) {
78 throw new IllegalArgumentException("NULL storage manager");
82 mStorageManager
= storageManager
;
83 // TODO Enable when "On Device" is recovered ?
84 mImageFiles
= mStorageManager
.getFolderImages(parentFolder
/*, false*/);
86 mImageFiles
= FileStorageUtils
.sortFolder(mImageFiles
);
88 mObsoleteFragments
= new HashSet
<Object
>();
89 mObsoletePositions
= new HashSet
<Integer
>();
90 mDownloadErrors
= new HashSet
<Integer
>();
91 //mFragmentManager = fragmentManager;
92 mCachedFragments
= new HashMap
<Integer
, FileFragment
>();
96 * Returns the image files handled by the adapter.
98 * @return A vector with the image files handled by the adapter.
100 protected OCFile
getFileAt(int position
) {
101 return mImageFiles
.get(position
);
105 public Fragment
getItem(int i
) {
106 OCFile file
= mImageFiles
.get(i
);
107 Fragment fragment
= null
;
109 fragment
= PreviewImageFragment
.newInstance(file
,
110 mObsoletePositions
.contains(Integer
.valueOf(i
)), false
);
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
));
117 fragment
= PreviewImageFragment
.newInstance(file
,
118 mObsoletePositions
.contains(Integer
.valueOf(i
)), true
);
120 mObsoletePositions
.remove(Integer
.valueOf(i
));
124 public int getFilePosition(OCFile file
) {
125 return mImageFiles
.indexOf(file
);
129 public int getCount() {
130 return mImageFiles
.size();
134 public CharSequence
getPageTitle(int position
) {
135 return mImageFiles
.get(position
).getFileName();
139 public void updateFile(int position
, OCFile file
) {
140 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
141 if (fragmentToUpdate
!= null
) {
142 mObsoleteFragments
.add(fragmentToUpdate
);
144 mObsoletePositions
.add(Integer
.valueOf(position
));
145 mImageFiles
.set(position
, file
);
149 public void updateWithDownloadError(int position
) {
150 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
151 if (fragmentToUpdate
!= null
) {
152 mObsoleteFragments
.add(fragmentToUpdate
);
154 mDownloadErrors
.add(Integer
.valueOf(position
));
157 public void clearErrorAt(int position
) {
158 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
159 if (fragmentToUpdate
!= null
) {
160 mObsoleteFragments
.add(fragmentToUpdate
);
162 mDownloadErrors
.remove(Integer
.valueOf(position
));
167 public int getItemPosition(Object object
) {
168 if (mObsoleteFragments
.contains(object
)) {
169 mObsoleteFragments
.remove(object
);
170 return POSITION_NONE
;
172 return super.getItemPosition(object
);
177 public Object
instantiateItem(ViewGroup container
, int position
) {
178 Object fragment
= super.instantiateItem(container
, position
);
179 mCachedFragments
.put(Integer
.valueOf(position
), (FileFragment
)fragment
);
184 public void destroyItem(ViewGroup container
, int position
, Object object
) {
185 mCachedFragments
.remove(Integer
.valueOf(position
));
186 super.destroyItem(container
, position
, object
);
190 public boolean pendingErrorAt(int position
) {
191 return mDownloadErrors
.contains(Integer
.valueOf(position
));
195 * Reset the image zoom to default value for each CachedFragments
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();
208 * Called when a change in the shown pages is going to start being made.
210 * @param container The containing View which is displaying this adapter's page views.
213 public void startUpdate(ViewGroup container) {
214 Log_OC.e(TAG, "** startUpdate");
218 public Object instantiateItem(ViewGroup container, int position) {
219 Log_OC.e(TAG, "** instantiateItem " + position);
221 if (mFragments.size() > position) {
222 Fragment fragment = mFragments.get(position);
223 if (fragment != null) {
224 Log_OC.e(TAG, "** \t returning cached item");
229 if (mCurTransaction == null) {
230 mCurTransaction = mFragmentManager.beginTransaction();
233 Fragment fragment = getItem(position);
234 if (mSavedState.size() > position) {
235 Fragment.SavedState savedState = mSavedState.get(position);
236 if (savedState != null) {
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);
245 while (mFragments.size() <= position) {
246 mFragments.add(null);
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);
257 public void destroyItem(ViewGroup container, int position, Object object) {
258 Log_OC.e(TAG, "** destroyItem " + position);
259 Fragment fragment = (Fragment)object;
261 if (mCurTransaction == null) {
262 mCurTransaction = mFragmentManager.beginTransaction();
264 Log_OC.e(TAG, "** \t removing fragment at position " + position);
265 while (mSavedState.size() <= position) {
266 mSavedState.add(null);
268 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
269 mFragments.set(position, null);
271 mCurTransaction.remove(fragment);
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);
281 if (fragment != null) {
282 fragment.setMenuVisibility(true);
284 mCurrentPrimaryItem = fragment;
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();
296 Log_OC.e(TAG, "** finishUpdate (end)");
300 public boolean isViewFromObject(View view, Object object) {
301 return ((Fragment)object).getView() == view;
305 public Parcelable saveState() {
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);
313 for (int i=0; i<mFragments.size(); i++) {
314 Fragment fragment = mFragments.get(i);
315 if (fragment != null) {
317 state = new Bundle();
319 String key = "f" + i;
320 mFragmentManager.putFragment(state, key, fragment);
327 public void restoreState(Parcelable state, ClassLoader loader) {
329 Bundle bundle = (Bundle)state;
330 bundle.setClassLoader(loader);
331 Parcelable[] states = bundle.getParcelableArray("states");
334 if (states != null) {
335 for (int i=0; i<states.length; i++) {
336 mSavedState.add((Fragment.SavedState)states[i]);
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);
345 while (mFragments.size() <= index) {
346 mFragments.add(null);
348 f.setMenuVisibility(false);
349 mFragments.set(index, f);
351 Log_OC.w(TAG, "Bad fragment at key " + key);