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
.Collections
;
23 import java
.util
.Comparator
;
24 import java
.util
.HashMap
;
25 import java
.util
.HashSet
;
26 import java
.util
.Iterator
;
29 import java
.util
.Vector
;
31 import android
.accounts
.Account
;
32 import android
.support
.v4
.app
.Fragment
;
33 import android
.support
.v4
.app
.FragmentManager
;
34 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
35 import android
.view
.ViewGroup
;
37 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
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 the {@link Fragment}s provided by the adapter.
62 * @param parentFolder Folder where images will be searched for.
63 * @param storageManager Bridge to database.
65 public PreviewImagePagerAdapter(FragmentManager fragmentManager
, OCFile parentFolder
, Account account
, FileDataStorageManager storageManager
) {
66 super(fragmentManager
);
68 if (fragmentManager
== null
) {
69 throw new IllegalArgumentException("NULL FragmentManager instance");
71 if (parentFolder
== null
) {
72 throw new IllegalArgumentException("NULL parent folder");
74 if (storageManager
== null
) {
75 throw new IllegalArgumentException("NULL storage manager");
79 mStorageManager
= storageManager
;
80 mImageFiles
= mStorageManager
.getFolderImages(parentFolder
);
82 mImageFiles
= FileStorageUtils
.sortFolder(mImageFiles
);
84 mObsoleteFragments
= new HashSet
<Object
>();
85 mObsoletePositions
= new HashSet
<Integer
>();
86 mDownloadErrors
= new HashSet
<Integer
>();
87 //mFragmentManager = fragmentManager;
88 mCachedFragments
= new HashMap
<Integer
, FileFragment
>();
92 * Returns the image files handled by the adapter.
94 * @return A vector with the image files handled by the adapter.
96 protected OCFile
getFileAt(int position
) {
97 return mImageFiles
.get(position
);
101 public Fragment
getItem(int i
) {
102 OCFile file
= mImageFiles
.get(i
);
103 Fragment fragment
= null
;
105 fragment
= new PreviewImageFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
107 } else if (mDownloadErrors
.contains(Integer
.valueOf(i
))) {
108 fragment
= new FileDownloadFragment(file
, mAccount
, true
);
109 ((FileDownloadFragment
)fragment
).setError(true
);
110 mDownloadErrors
.remove(Integer
.valueOf(i
));
113 fragment
= new FileDownloadFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
115 mObsoletePositions
.remove(Integer
.valueOf(i
));
119 public int getFilePosition(OCFile file
) {
120 return mImageFiles
.indexOf(file
);
124 public int getCount() {
125 return mImageFiles
.size();
129 public CharSequence
getPageTitle(int position
) {
130 return mImageFiles
.get(position
).getFileName();
134 public void updateFile(int position
, OCFile file
) {
135 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
136 if (fragmentToUpdate
!= null
) {
137 mObsoleteFragments
.add(fragmentToUpdate
);
139 mObsoletePositions
.add(Integer
.valueOf(position
));
140 mImageFiles
.set(position
, file
);
144 public void updateWithDownloadError(int position
) {
145 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
146 if (fragmentToUpdate
!= null
) {
147 mObsoleteFragments
.add(fragmentToUpdate
);
149 mDownloadErrors
.add(Integer
.valueOf(position
));
152 public void clearErrorAt(int position
) {
153 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
154 if (fragmentToUpdate
!= null
) {
155 mObsoleteFragments
.add(fragmentToUpdate
);
157 mDownloadErrors
.remove(Integer
.valueOf(position
));
162 public int getItemPosition(Object object
) {
163 if (mObsoleteFragments
.contains(object
)) {
164 mObsoleteFragments
.remove(object
);
165 return POSITION_NONE
;
167 return super.getItemPosition(object
);
172 public Object
instantiateItem(ViewGroup container
, int position
) {
173 Object fragment
= super.instantiateItem(container
, position
);
174 mCachedFragments
.put(Integer
.valueOf(position
), (FileFragment
)fragment
);
179 public void destroyItem(ViewGroup container
, int position
, Object object
) {
180 mCachedFragments
.remove(Integer
.valueOf(position
));
181 super.destroyItem(container
, position
, object
);
185 public boolean pendingErrorAt(int position
) {
186 return mDownloadErrors
.contains(Integer
.valueOf(position
));
190 * Reset the image zoom to default value for each CachedFragments
192 public void resetZoom() {
193 Iterator
<FileFragment
> entries
= mCachedFragments
.values().iterator();
194 while (entries
.hasNext()) {
195 FileFragment fileFragment
= (FileFragment
) entries
.next();
196 if (fileFragment
instanceof PreviewImageFragment
) {
197 ((PreviewImageFragment
) fileFragment
).getImageView().resetZoom();
203 * Called when a change in the shown pages is going to start being made.
205 * @param container The containing View which is displaying this adapter's page views.
208 public void startUpdate(ViewGroup container) {
209 Log_OC.e(TAG, "** startUpdate");
213 public Object instantiateItem(ViewGroup container, int position) {
214 Log_OC.e(TAG, "** instantiateItem " + position);
216 if (mFragments.size() > position) {
217 Fragment fragment = mFragments.get(position);
218 if (fragment != null) {
219 Log_OC.e(TAG, "** \t returning cached item");
224 if (mCurTransaction == null) {
225 mCurTransaction = mFragmentManager.beginTransaction();
228 Fragment fragment = getItem(position);
229 if (mSavedState.size() > position) {
230 Fragment.SavedState savedState = mSavedState.get(position);
231 if (savedState != null) {
233 // * The Fragment must currently be attached to the FragmentManager.
234 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
235 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
236 // to store a fragment reference
237 fragment.setInitialSavedState(savedState);
240 while (mFragments.size() <= position) {
241 mFragments.add(null);
243 fragment.setMenuVisibility(false);
244 mFragments.set(position, fragment);
245 //Log_OC.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
246 mCurTransaction.add(container.getId(), fragment);
252 public void destroyItem(ViewGroup container, int position, Object object) {
253 Log_OC.e(TAG, "** destroyItem " + position);
254 Fragment fragment = (Fragment)object;
256 if (mCurTransaction == null) {
257 mCurTransaction = mFragmentManager.beginTransaction();
259 Log_OC.e(TAG, "** \t removing fragment at position " + position);
260 while (mSavedState.size() <= position) {
261 mSavedState.add(null);
263 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
264 mFragments.set(position, null);
266 mCurTransaction.remove(fragment);
270 public void setPrimaryItem(ViewGroup container, int position, Object object) {
271 Fragment fragment = (Fragment)object;
272 if (fragment != mCurrentPrimaryItem) {
273 if (mCurrentPrimaryItem != null) {
274 mCurrentPrimaryItem.setMenuVisibility(false);
276 if (fragment != null) {
277 fragment.setMenuVisibility(true);
279 mCurrentPrimaryItem = fragment;
284 public void finishUpdate(ViewGroup container) {
285 Log_OC.e(TAG, "** finishUpdate (start)");
286 if (mCurTransaction != null) {
287 mCurTransaction.commitAllowingStateLoss();
288 mCurTransaction = null;
289 mFragmentManager.executePendingTransactions();
291 Log_OC.e(TAG, "** finishUpdate (end)");
295 public boolean isViewFromObject(View view, Object object) {
296 return ((Fragment)object).getView() == view;
300 public Parcelable saveState() {
302 if (mSavedState.size() > 0) {
303 state = new Bundle();
304 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
305 mSavedState.toArray(savedStates);
306 state.putParcelableArray("states", savedStates);
308 for (int i=0; i<mFragments.size(); i++) {
309 Fragment fragment = mFragments.get(i);
310 if (fragment != null) {
312 state = new Bundle();
314 String key = "f" + i;
315 mFragmentManager.putFragment(state, key, fragment);
322 public void restoreState(Parcelable state, ClassLoader loader) {
324 Bundle bundle = (Bundle)state;
325 bundle.setClassLoader(loader);
326 Parcelable[] states = bundle.getParcelableArray("states");
329 if (states != null) {
330 for (int i=0; i<states.length; i++) {
331 mSavedState.add((Fragment.SavedState)states[i]);
334 Iterable<String> keys = bundle.keySet();
335 for (String key: keys) {
336 if (key.startsWith("f")) {
337 int index = Integer.parseInt(key.substring(1));
338 Fragment f = mFragmentManager.getFragment(bundle, key);
340 while (mFragments.size() <= index) {
341 mFragments.add(null);
343 f.setMenuVisibility(false);
344 mFragments.set(index, f);
346 Log_OC.w(TAG, "Bad fragment at key " + key);