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 as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.preview
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashMap
;
22 import java
.util
.HashSet
;
25 import java
.util
.Vector
;
27 import android
.accounts
.Account
;
28 import android
.os
.Bundle
;
29 import android
.os
.Parcelable
;
30 import android
.support
.v4
.app
.Fragment
;
31 import android
.support
.v4
.app
.FragmentManager
;
32 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
33 import android
.support
.v4
.app
.FragmentTransaction
;
34 import android
.support
.v4
.view
.PagerAdapter
;
35 import android
.support
.v4
.view
.ViewPager
;
36 import android
.util
.Log
;
37 import android
.view
.View
;
38 import android
.view
.ViewGroup
;
40 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
41 import com
.owncloud
.android
.datamodel
.OCFile
;
42 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
45 * Adapter class that provides Fragment instances
47 * @author David A. Velasco
49 //public class PreviewImagePagerAdapter extends PagerAdapter {
50 public class PreviewImagePagerAdapter
extends FragmentStatePagerAdapter
{
52 private static final String TAG
= PreviewImagePagerAdapter
.class.getSimpleName();
54 private Vector
<OCFile
> mImageFiles
;
55 private Account mAccount
;
56 private Set
<Object
> mObsoleteFragments
;
57 private Set
<Integer
> mObsoletePositions
;
58 private Set
<Integer
> mDownloadErrors
;
59 private DataStorageManager mStorageManager
;
61 private Map
<Integer
, FileFragment
> mCachedFragments
;
64 private final FragmentManager mFragmentManager;
65 private FragmentTransaction mCurTransaction = null;
66 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
67 private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
68 private Fragment mCurrentPrimaryItem = null;
74 * @param fragmentManager {@link FragmentManager} instance that will handle the {@link Fragment}s provided by the adapter.
75 * @param parentFolder Folder where images will be searched for.
76 * @param storageManager Bridge to database.
78 public PreviewImagePagerAdapter(FragmentManager fragmentManager
, OCFile parentFolder
, Account account
, DataStorageManager storageManager
) {
79 super(fragmentManager
);
81 if (fragmentManager
== null
) {
82 throw new IllegalArgumentException("NULL FragmentManager instance");
84 if (parentFolder
== null
) {
85 throw new IllegalArgumentException("NULL parent folder");
87 if (storageManager
== null
) {
88 throw new IllegalArgumentException("NULL storage manager");
92 mStorageManager
= storageManager
;
93 mImageFiles
= mStorageManager
.getDirectoryImages(parentFolder
);
94 mObsoleteFragments
= new HashSet
<Object
>();
95 mObsoletePositions
= new HashSet
<Integer
>();
96 mDownloadErrors
= new HashSet
<Integer
>();
97 //mFragmentManager = fragmentManager;
98 mCachedFragments
= new HashMap
<Integer
, FileFragment
>();
103 * Returns the image files handled by the adapter.
105 * @return A vector with the image files handled by the adapter.
107 protected OCFile
getFileAt(int position
) {
108 return mImageFiles
.get(position
);
112 public Fragment
getItem(int i
) {
113 OCFile file
= mImageFiles
.get(i
);
114 Fragment fragment
= null
;
116 fragment
= new PreviewImageFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
118 } else if (mDownloadErrors
.contains(Integer
.valueOf(i
))) {
119 fragment
= new FileDownloadFragment(file
, mAccount
, true
);
120 ((FileDownloadFragment
)fragment
).setError(true
);
121 mDownloadErrors
.remove(Integer
.valueOf(i
));
124 fragment
= new FileDownloadFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
126 mObsoletePositions
.remove(Integer
.valueOf(i
));
130 public int getFilePosition(OCFile file
) {
131 return mImageFiles
.indexOf(file
);
135 public int getCount() {
136 return mImageFiles
.size();
140 public CharSequence
getPageTitle(int position
) {
141 return mImageFiles
.get(position
).getFileName();
145 public void updateFile(int position
, OCFile file
) {
146 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
147 if (fragmentToUpdate
!= null
) {
148 mObsoleteFragments
.add(fragmentToUpdate
);
150 mObsoletePositions
.add(Integer
.valueOf(position
));
151 mImageFiles
.set(position
, file
);
155 public void updateWithDownloadError(int position
) {
156 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
157 if (fragmentToUpdate
!= null
) {
158 mObsoleteFragments
.add(fragmentToUpdate
);
160 mDownloadErrors
.add(Integer
.valueOf(position
));
163 public void clearErrorAt(int position
) {
164 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
165 if (fragmentToUpdate
!= null
) {
166 mObsoleteFragments
.add(fragmentToUpdate
);
168 mDownloadErrors
.remove(Integer
.valueOf(position
));
173 public int getItemPosition(Object object
) {
174 if (mObsoleteFragments
.contains(object
)) {
175 mObsoleteFragments
.remove(object
);
176 return POSITION_NONE
;
178 return super.getItemPosition(object
);
183 public Object
instantiateItem(ViewGroup container
, int position
) {
184 Object fragment
= super.instantiateItem(container
, position
);
185 mCachedFragments
.put(Integer
.valueOf(position
), (FileFragment
)fragment
);
190 public void destroyItem(ViewGroup container
, int position
, Object object
) {
191 mCachedFragments
.remove(Integer
.valueOf(position
));
192 super.destroyItem(container
, position
, object
);
196 public boolean pendingErrorAt(int position
) {
197 return mDownloadErrors
.contains(Integer
.valueOf(position
));
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.e(TAG, "** startUpdate");
213 public Object instantiateItem(ViewGroup container, int position) {
214 Log.e(TAG, "** instantiateItem " + position);
216 if (mFragments.size() > position) {
217 Fragment fragment = mFragments.get(position);
218 if (fragment != null) {
219 Log.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.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.e(TAG, "** destroyItem " + position);
254 Fragment fragment = (Fragment)object;
256 if (mCurTransaction == null) {
257 mCurTransaction = mFragmentManager.beginTransaction();
259 Log.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.e(TAG, "** finishUpdate (start)");
286 if (mCurTransaction != null) {
287 mCurTransaction.commitAllowingStateLoss();
288 mCurTransaction = null;
289 mFragmentManager.executePendingTransactions();
291 Log.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.w(TAG, "Bad fragment at key " + key);