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
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.HashSet
;
24 import java
.util
.Vector
;
26 import android
.accounts
.Account
;
27 import android
.os
.Bundle
;
28 import android
.os
.Parcelable
;
29 import android
.support
.v4
.app
.Fragment
;
30 import android
.support
.v4
.app
.FragmentManager
;
31 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
32 import android
.support
.v4
.app
.FragmentTransaction
;
33 import android
.support
.v4
.view
.PagerAdapter
;
34 import android
.support
.v4
.view
.ViewPager
;
35 import android
.util
.Log
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
39 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
44 * Adapter class that provides Fragment instances
46 * @author David A. Velasco
48 //public class PreviewImagePagerAdapter extends PagerAdapter {
49 public class PreviewImagePagerAdapter
extends FragmentStatePagerAdapter
{
51 private static final String TAG
= PreviewImagePagerAdapter
.class.getSimpleName();
53 private Vector
<OCFile
> mImageFiles
;
54 private Account mAccount
;
55 private Set
<Object
> mObsoleteFragments
;
56 private Set
<Integer
> mObsoletePositions
;
57 private Set
<Integer
> mDownloadErrors
;
58 private DataStorageManager mStorageManager
;
60 private Map
<Integer
, FileFragment
> mCachedFragments
;
63 private final FragmentManager mFragmentManager;
64 private FragmentTransaction mCurTransaction = null;
65 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
66 private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
67 private Fragment mCurrentPrimaryItem = null;
73 * @param fragmentManager {@link FragmentManager} instance that will handle the {@link Fragment}s provided by the adapter.
74 * @param parentFolder Folder where images will be searched for.
75 * @param storageManager Bridge to database.
77 public PreviewImagePagerAdapter(FragmentManager fragmentManager
, OCFile parentFolder
, Account account
, DataStorageManager storageManager
) {
78 super(fragmentManager
);
80 if (fragmentManager
== null
) {
81 throw new IllegalArgumentException("NULL FragmentManager instance");
83 if (parentFolder
== null
) {
84 throw new IllegalArgumentException("NULL parent folder");
86 if (storageManager
== null
) {
87 throw new IllegalArgumentException("NULL storage manager");
91 mStorageManager
= storageManager
;
92 mImageFiles
= mStorageManager
.getDirectoryImages(parentFolder
);
93 mObsoleteFragments
= new HashSet
<Object
>();
94 mObsoletePositions
= new HashSet
<Integer
>();
95 mDownloadErrors
= new HashSet
<Integer
>();
96 //mFragmentManager = fragmentManager;
97 mCachedFragments
= new HashMap
<Integer
, FileFragment
>();
102 * Returns the image files handled by the adapter.
104 * @return A vector with the image files handled by the adapter.
106 protected OCFile
getFileAt(int position
) {
107 return mImageFiles
.get(position
);
111 public Fragment
getItem(int i
) {
112 OCFile file
= mImageFiles
.get(i
);
113 Fragment fragment
= null
;
115 fragment
= new PreviewImageFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
117 } else if (mDownloadErrors
.contains(Integer
.valueOf(i
))) {
118 fragment
= new FileDownloadFragment(file
, mAccount
, true
);
119 ((FileDownloadFragment
)fragment
).setError(true
);
120 mDownloadErrors
.remove(Integer
.valueOf(i
));
123 fragment
= new FileDownloadFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
125 mObsoletePositions
.remove(Integer
.valueOf(i
));
129 public int getFilePosition(OCFile file
) {
130 return mImageFiles
.indexOf(file
);
134 public int getCount() {
135 return mImageFiles
.size();
139 public CharSequence
getPageTitle(int position
) {
140 return mImageFiles
.get(position
).getFileName();
144 public void updateFile(int position
, OCFile file
) {
145 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
146 if (fragmentToUpdate
!= null
) {
147 mObsoleteFragments
.add(fragmentToUpdate
);
149 mObsoletePositions
.add(Integer
.valueOf(position
));
150 mImageFiles
.set(position
, file
);
154 public void updateWithDownloadError(int position
) {
155 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
156 if (fragmentToUpdate
!= null
) {
157 mObsoleteFragments
.add(fragmentToUpdate
);
159 mDownloadErrors
.add(Integer
.valueOf(position
));
162 public void clearErrorAt(int position
) {
163 FileFragment fragmentToUpdate
= mCachedFragments
.get(Integer
.valueOf(position
));
164 if (fragmentToUpdate
!= null
) {
165 mObsoleteFragments
.add(fragmentToUpdate
);
167 mDownloadErrors
.remove(Integer
.valueOf(position
));
172 public int getItemPosition(Object object
) {
173 if (mObsoleteFragments
.contains(object
)) {
174 mObsoleteFragments
.remove(object
);
175 return POSITION_NONE
;
177 return super.getItemPosition(object
);
182 public Object
instantiateItem(ViewGroup container
, int position
) {
183 Object fragment
= super.instantiateItem(container
, position
);
184 mCachedFragments
.put(Integer
.valueOf(position
), (FileFragment
)fragment
);
189 public void destroyItem(ViewGroup container
, int position
, Object object
) {
190 mCachedFragments
.remove(Integer
.valueOf(position
));
191 super.destroyItem(container
, position
, object
);
195 public boolean pendingErrorAt(int position
) {
196 return mDownloadErrors
.contains(Integer
.valueOf(position
));
202 * Called when a change in the shown pages is going to start being made.
204 * @param container The containing View which is displaying this adapter's page views.
207 public void startUpdate(ViewGroup container) {
208 Log.e(TAG, "** startUpdate");
212 public Object instantiateItem(ViewGroup container, int position) {
213 Log.e(TAG, "** instantiateItem " + position);
215 if (mFragments.size() > position) {
216 Fragment fragment = mFragments.get(position);
217 if (fragment != null) {
218 Log.e(TAG, "** \t returning cached item");
223 if (mCurTransaction == null) {
224 mCurTransaction = mFragmentManager.beginTransaction();
227 Fragment fragment = getItem(position);
228 if (mSavedState.size() > position) {
229 Fragment.SavedState savedState = mSavedState.get(position);
230 if (savedState != null) {
232 // * The Fragment must currently be attached to the FragmentManager.
233 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
234 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
235 // to store a fragment reference
236 fragment.setInitialSavedState(savedState);
239 while (mFragments.size() <= position) {
240 mFragments.add(null);
242 fragment.setMenuVisibility(false);
243 mFragments.set(position, fragment);
244 //Log.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
245 mCurTransaction.add(container.getId(), fragment);
251 public void destroyItem(ViewGroup container, int position, Object object) {
252 Log.e(TAG, "** destroyItem " + position);
253 Fragment fragment = (Fragment)object;
255 if (mCurTransaction == null) {
256 mCurTransaction = mFragmentManager.beginTransaction();
258 Log.e(TAG, "** \t removing fragment at position " + position);
259 while (mSavedState.size() <= position) {
260 mSavedState.add(null);
262 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
263 mFragments.set(position, null);
265 mCurTransaction.remove(fragment);
269 public void setPrimaryItem(ViewGroup container, int position, Object object) {
270 Fragment fragment = (Fragment)object;
271 if (fragment != mCurrentPrimaryItem) {
272 if (mCurrentPrimaryItem != null) {
273 mCurrentPrimaryItem.setMenuVisibility(false);
275 if (fragment != null) {
276 fragment.setMenuVisibility(true);
278 mCurrentPrimaryItem = fragment;
283 public void finishUpdate(ViewGroup container) {
284 Log.e(TAG, "** finishUpdate (start)");
285 if (mCurTransaction != null) {
286 mCurTransaction.commitAllowingStateLoss();
287 mCurTransaction = null;
288 mFragmentManager.executePendingTransactions();
290 Log.e(TAG, "** finishUpdate (end)");
294 public boolean isViewFromObject(View view, Object object) {
295 return ((Fragment)object).getView() == view;
299 public Parcelable saveState() {
301 if (mSavedState.size() > 0) {
302 state = new Bundle();
303 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
304 mSavedState.toArray(savedStates);
305 state.putParcelableArray("states", savedStates);
307 for (int i=0; i<mFragments.size(); i++) {
308 Fragment fragment = mFragments.get(i);
309 if (fragment != null) {
311 state = new Bundle();
313 String key = "f" + i;
314 mFragmentManager.putFragment(state, key, fragment);
321 public void restoreState(Parcelable state, ClassLoader loader) {
323 Bundle bundle = (Bundle)state;
324 bundle.setClassLoader(loader);
325 Parcelable[] states = bundle.getParcelableArray("states");
328 if (states != null) {
329 for (int i=0; i<states.length; i++) {
330 mSavedState.add((Fragment.SavedState)states[i]);
333 Iterable<String> keys = bundle.keySet();
334 for (String key: keys) {
335 if (key.startsWith("f")) {
336 int index = Integer.parseInt(key.substring(1));
337 Fragment f = mFragmentManager.getFragment(bundle, key);
339 while (mFragments.size() <= index) {
340 mFragments.add(null);
342 f.setMenuVisibility(false);
343 mFragments.set(index, f);
345 Log.w(TAG, "Bad fragment at key " + key);