1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
17 package com
.owncloud
.android
.ui
.preview
;
19 import java
.util
.HashMap
;
20 import java
.util
.HashSet
;
23 import java
.util
.Vector
;
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
;
31 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
33 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
36 * Adapter class that provides Fragment instances
38 * @author David A. Velasco
40 //public class PreviewImagePagerAdapter extends PagerAdapter {
41 public class PreviewImagePagerAdapter
extends FragmentStatePagerAdapter
{
43 private static final String TAG
= PreviewImagePagerAdapter
.class.getSimpleName();
45 private Vector
<OCFile
> mImageFiles
;
46 private Account mAccount
;
47 private Set
<Object
> mObsoleteFragments
;
48 private Set
<Integer
> mObsoletePositions
;
49 private Set
<Integer
> mDownloadErrors
;
50 private DataStorageManager mStorageManager
;
52 private Map
<Integer
, FileFragment
> mCachedFragments
;
55 private final FragmentManager mFragmentManager;
56 private FragmentTransaction mCurTransaction = null;
57 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
58 private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
59 private Fragment mCurrentPrimaryItem = null;
65 * @param fragmentManager {@link FragmentManager} instance that will handle the {@link Fragment}s provided by the adapter.
66 * @param parentFolder Folder where images will be searched for.
67 * @param storageManager Bridge to database.
69 public PreviewImagePagerAdapter(FragmentManager fragmentManager
, OCFile parentFolder
, Account account
, DataStorageManager storageManager
) {
70 super(fragmentManager
);
72 if (fragmentManager
== null
) {
73 throw new IllegalArgumentException("NULL FragmentManager instance");
75 if (parentFolder
== null
) {
76 throw new IllegalArgumentException("NULL parent folder");
78 if (storageManager
== null
) {
79 throw new IllegalArgumentException("NULL storage manager");
83 mStorageManager
= storageManager
;
84 mImageFiles
= mStorageManager
.getDirectoryImages(parentFolder
);
85 mObsoleteFragments
= new HashSet
<Object
>();
86 mObsoletePositions
= new HashSet
<Integer
>();
87 mDownloadErrors
= new HashSet
<Integer
>();
88 //mFragmentManager = fragmentManager;
89 mCachedFragments
= new HashMap
<Integer
, FileFragment
>();
94 * Returns the image files handled by the adapter.
96 * @return A vector with the image files handled by the adapter.
98 protected OCFile
getFileAt(int position
) {
99 return mImageFiles
.get(position
);
103 public Fragment
getItem(int i
) {
104 OCFile file
= mImageFiles
.get(i
);
105 Fragment fragment
= null
;
107 fragment
= new PreviewImageFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
109 } else if (mDownloadErrors
.contains(Integer
.valueOf(i
))) {
110 fragment
= new FileDownloadFragment(file
, mAccount
, true
);
111 ((FileDownloadFragment
)fragment
).setError(true
);
112 mDownloadErrors
.remove(Integer
.valueOf(i
));
115 fragment
= new FileDownloadFragment(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
));
194 * Called when a change in the shown pages is going to start being made.
196 * @param container The containing View which is displaying this adapter's page views.
199 public void startUpdate(ViewGroup container) {
200 Log.e(TAG, "** startUpdate");
204 public Object instantiateItem(ViewGroup container, int position) {
205 Log.e(TAG, "** instantiateItem " + position);
207 if (mFragments.size() > position) {
208 Fragment fragment = mFragments.get(position);
209 if (fragment != null) {
210 Log.e(TAG, "** \t returning cached item");
215 if (mCurTransaction == null) {
216 mCurTransaction = mFragmentManager.beginTransaction();
219 Fragment fragment = getItem(position);
220 if (mSavedState.size() > position) {
221 Fragment.SavedState savedState = mSavedState.get(position);
222 if (savedState != null) {
224 // * The Fragment must currently be attached to the FragmentManager.
225 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
226 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
227 // to store a fragment reference
228 fragment.setInitialSavedState(savedState);
231 while (mFragments.size() <= position) {
232 mFragments.add(null);
234 fragment.setMenuVisibility(false);
235 mFragments.set(position, fragment);
236 //Log.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
237 mCurTransaction.add(container.getId(), fragment);
243 public void destroyItem(ViewGroup container, int position, Object object) {
244 Log.e(TAG, "** destroyItem " + position);
245 Fragment fragment = (Fragment)object;
247 if (mCurTransaction == null) {
248 mCurTransaction = mFragmentManager.beginTransaction();
250 Log.e(TAG, "** \t removing fragment at position " + position);
251 while (mSavedState.size() <= position) {
252 mSavedState.add(null);
254 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
255 mFragments.set(position, null);
257 mCurTransaction.remove(fragment);
261 public void setPrimaryItem(ViewGroup container, int position, Object object) {
262 Fragment fragment = (Fragment)object;
263 if (fragment != mCurrentPrimaryItem) {
264 if (mCurrentPrimaryItem != null) {
265 mCurrentPrimaryItem.setMenuVisibility(false);
267 if (fragment != null) {
268 fragment.setMenuVisibility(true);
270 mCurrentPrimaryItem = fragment;
275 public void finishUpdate(ViewGroup container) {
276 Log.e(TAG, "** finishUpdate (start)");
277 if (mCurTransaction != null) {
278 mCurTransaction.commitAllowingStateLoss();
279 mCurTransaction = null;
280 mFragmentManager.executePendingTransactions();
282 Log.e(TAG, "** finishUpdate (end)");
286 public boolean isViewFromObject(View view, Object object) {
287 return ((Fragment)object).getView() == view;
291 public Parcelable saveState() {
293 if (mSavedState.size() > 0) {
294 state = new Bundle();
295 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
296 mSavedState.toArray(savedStates);
297 state.putParcelableArray("states", savedStates);
299 for (int i=0; i<mFragments.size(); i++) {
300 Fragment fragment = mFragments.get(i);
301 if (fragment != null) {
303 state = new Bundle();
305 String key = "f" + i;
306 mFragmentManager.putFragment(state, key, fragment);
313 public void restoreState(Parcelable state, ClassLoader loader) {
315 Bundle bundle = (Bundle)state;
316 bundle.setClassLoader(loader);
317 Parcelable[] states = bundle.getParcelableArray("states");
320 if (states != null) {
321 for (int i=0; i<states.length; i++) {
322 mSavedState.add((Fragment.SavedState)states[i]);
325 Iterable<String> keys = bundle.keySet();
326 for (String key: keys) {
327 if (key.startsWith("f")) {
328 int index = Integer.parseInt(key.substring(1));
329 Fragment f = mFragmentManager.getFragment(bundle, key);
331 while (mFragments.size() <= index) {
332 mFragments.add(null);
334 f.setMenuVisibility(false);
335 mFragments.set(index, f);
337 Log.w(TAG, "Bad fragment at key " + key);