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
, boolean onlyOnDevice
) {
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
, false
);
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
= PreviewImageFragment
.newInstance(file
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
107 } else if (mDownloadErrors
.contains(Integer
.valueOf(i
))) {
108 fragment
= FileDownloadFragment
.newInstance(file
, mAccount
, true
);
109 ((FileDownloadFragment
)fragment
).setError(true
);
110 mDownloadErrors
.remove(Integer
.valueOf(i
));
113 fragment
= FileDownloadFragment
.newInstance(
114 file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
))
117 mObsoletePositions
.remove(Integer
.valueOf(i
));
121 public int getFilePosition(OCFile file
) {
122 return mImageFiles
.indexOf(file
);
126 public int getCount() {
127 return mImageFiles
.size();
131 public CharSequence
getPageTitle(int position
) {
132 return mImageFiles
.get(position
).getFileName();
136 public void updateFile(int position
, OCFile file
) {
137 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
138 if (fragmentToUpdate
!= null
) {
139 mObsoleteFragments
.add(fragmentToUpdate
);
141 mObsoletePositions
.add(Integer
.valueOf(position
));
142 mImageFiles
.set(position
, file
);
146 public void updateWithDownloadError(int position
) {
147 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
148 if (fragmentToUpdate
!= null
) {
149 mObsoleteFragments
.add(fragmentToUpdate
);
151 mDownloadErrors
.add(Integer
.valueOf(position
));
154 public void clearErrorAt(int position
) {
155 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
156 if (fragmentToUpdate
!= null
) {
157 mObsoleteFragments
.add(fragmentToUpdate
);
159 mDownloadErrors
.remove(Integer
.valueOf(position
));
164 public int getItemPosition(Object object
) {
165 if (mObsoleteFragments
.contains(object
)) {
166 mObsoleteFragments
.remove(object
);
167 return POSITION_NONE
;
169 return super.getItemPosition(object
);
174 public Object
instantiateItem(ViewGroup container
, int position
) {
175 Object fragment
= super.instantiateItem(container
, position
);
176 mCachedFragments
.put(Integer
.valueOf(position
), (FileFragment
)fragment
);
181 public void destroyItem(ViewGroup container
, int position
, Object object
) {
182 mCachedFragments
.remove(Integer
.valueOf(position
));
183 super.destroyItem(container
, position
, object
);
187 public boolean pendingErrorAt(int position
) {
188 return mDownloadErrors
.contains(Integer
.valueOf(position
));
192 * Reset the image zoom to default value for each CachedFragments
194 public void resetZoom() {
195 Iterator
<FileFragment
> entries
= mCachedFragments
.values().iterator();
196 while (entries
.hasNext()) {
197 FileFragment fileFragment
= (FileFragment
) entries
.next();
198 if (fileFragment
instanceof PreviewImageFragment
) {
199 ((PreviewImageFragment
) fileFragment
).getImageView().resetZoom();
205 * Called when a change in the shown pages is going to start being made.
207 * @param container The containing View which is displaying this adapter's page views.
210 public void startUpdate(ViewGroup container) {
211 Log_OC.e(TAG, "** startUpdate");
215 public Object instantiateItem(ViewGroup container, int position) {
216 Log_OC.e(TAG, "** instantiateItem " + position);
218 if (mFragments.size() > position) {
219 Fragment fragment = mFragments.get(position);
220 if (fragment != null) {
221 Log_OC.e(TAG, "** \t returning cached item");
226 if (mCurTransaction == null) {
227 mCurTransaction = mFragmentManager.beginTransaction();
230 Fragment fragment = getItem(position);
231 if (mSavedState.size() > position) {
232 Fragment.SavedState savedState = mSavedState.get(position);
233 if (savedState != null) {
235 // * The Fragment must currently be attached to the FragmentManager.
236 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
237 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
238 // to store a fragment reference
239 fragment.setInitialSavedState(savedState);
242 while (mFragments.size() <= position) {
243 mFragments.add(null);
245 fragment.setMenuVisibility(false);
246 mFragments.set(position, fragment);
247 //Log_OC.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
248 mCurTransaction.add(container.getId(), fragment);
254 public void destroyItem(ViewGroup container, int position, Object object) {
255 Log_OC.e(TAG, "** destroyItem " + position);
256 Fragment fragment = (Fragment)object;
258 if (mCurTransaction == null) {
259 mCurTransaction = mFragmentManager.beginTransaction();
261 Log_OC.e(TAG, "** \t removing fragment at position " + position);
262 while (mSavedState.size() <= position) {
263 mSavedState.add(null);
265 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
266 mFragments.set(position, null);
268 mCurTransaction.remove(fragment);
272 public void setPrimaryItem(ViewGroup container, int position, Object object) {
273 Fragment fragment = (Fragment)object;
274 if (fragment != mCurrentPrimaryItem) {
275 if (mCurrentPrimaryItem != null) {
276 mCurrentPrimaryItem.setMenuVisibility(false);
278 if (fragment != null) {
279 fragment.setMenuVisibility(true);
281 mCurrentPrimaryItem = fragment;
286 public void finishUpdate(ViewGroup container) {
287 Log_OC.e(TAG, "** finishUpdate (start)");
288 if (mCurTransaction != null) {
289 mCurTransaction.commitAllowingStateLoss();
290 mCurTransaction = null;
291 mFragmentManager.executePendingTransactions();
293 Log_OC.e(TAG, "** finishUpdate (end)");
297 public boolean isViewFromObject(View view, Object object) {
298 return ((Fragment)object).getView() == view;
302 public Parcelable saveState() {
304 if (mSavedState.size() > 0) {
305 state = new Bundle();
306 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
307 mSavedState.toArray(savedStates);
308 state.putParcelableArray("states", savedStates);
310 for (int i=0; i<mFragments.size(); i++) {
311 Fragment fragment = mFragments.get(i);
312 if (fragment != null) {
314 state = new Bundle();
316 String key = "f" + i;
317 mFragmentManager.putFragment(state, key, fragment);
324 public void restoreState(Parcelable state, ClassLoader loader) {
326 Bundle bundle = (Bundle)state;
327 bundle.setClassLoader(loader);
328 Parcelable[] states = bundle.getParcelableArray("states");
331 if (states != null) {
332 for (int i=0; i<states.length; i++) {
333 mSavedState.add((Fragment.SavedState)states[i]);
336 Iterable<String> keys = bundle.keySet();
337 for (String key: keys) {
338 if (key.startsWith("f")) {
339 int index = Integer.parseInt(key.substring(1));
340 Fragment f = mFragmentManager.getFragment(bundle, key);
342 while (mFragments.size() <= index) {
343 mFragments.add(null);
345 f.setMenuVisibility(false);
346 mFragments.set(index, f);
348 Log_OC.w(TAG, "Bad fragment at key " + key);