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
.HashSet
;
23 import java
.util
.Vector
;
25 import android
.accounts
.Account
;
26 import android
.os
.Bundle
;
27 import android
.os
.Parcelable
;
28 import android
.support
.v4
.app
.Fragment
;
29 import android
.support
.v4
.app
.FragmentManager
;
30 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
31 import android
.support
.v4
.app
.FragmentTransaction
;
32 import android
.support
.v4
.view
.PagerAdapter
;
33 import android
.util
.Log
;
34 import android
.view
.View
;
35 import android
.view
.ViewGroup
;
37 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
42 * Adapter class that provides Fragment instances
44 * @author David A. Velasco
46 public class PreviewImagePagerAdapter
extends PagerAdapter
{
47 //public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
49 private static final String TAG
= PreviewImagePagerAdapter
.class.getSimpleName();
51 private Vector
<OCFile
> mImageFiles
;
52 private Account mAccount
;
53 private Set
<Object
> mObsoleteFragments
;
54 private Set
<Integer
> mObsoletePositions
;
55 private DataStorageManager mStorageManager
;
57 private final FragmentManager mFragmentManager
;
58 private FragmentTransaction mCurTransaction
= null
;
59 private ArrayList
<Fragment
.SavedState
> mSavedState
= new ArrayList
<Fragment
.SavedState
>();
60 private ArrayList
<Fragment
> mFragments
= new ArrayList
<Fragment
>();
61 private Fragment mCurrentPrimaryItem
= null
;
66 * @param fragmentManager {@link FragmentManager} instance that will handle the {@link Fragment}s provided by the adapter.
67 * @param parentFolder Folder where images will be searched for.
68 * @param storageManager Bridge to database.
70 public PreviewImagePagerAdapter(FragmentManager fragmentManager
, OCFile parentFolder
, Account account
, DataStorageManager storageManager
) {
71 //super(fragmentManager);
73 if (fragmentManager
== null
) {
74 throw new IllegalArgumentException("NULL FragmentManager instance");
76 if (parentFolder
== null
) {
77 throw new IllegalArgumentException("NULL parent folder");
79 if (storageManager
== null
) {
80 throw new IllegalArgumentException("NULL storage manager");
84 mStorageManager
= storageManager
;
85 mImageFiles
= mStorageManager
.getDirectoryImages(parentFolder
);
86 mObsoleteFragments
= new HashSet
<Object
>();
87 mObsoletePositions
= new HashSet
<Integer
>();
88 mFragmentManager
= fragmentManager
;
93 * Returns the image files handled by the adapter.
95 * @return A vector with the image files handled by the adapter.
97 protected OCFile
getFileAt(int position
) {
98 return mImageFiles
.get(position
);
102 public Fragment
getItem(int i
) {
103 Log
.e(TAG
, "GETTING PAGE " + i
);
104 OCFile file
= mImageFiles
.get(i
);
105 Fragment fragment
= null
;
107 fragment
= new PreviewImageFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
109 fragment
= new FileDownloadFragment(file
, mAccount
, mObsoletePositions
.contains(Integer
.valueOf(i
)));
114 public int getFilePosition(OCFile file
) {
115 return mImageFiles
.indexOf(file
);
119 public int getCount() {
120 return mImageFiles
.size();
124 public CharSequence
getPageTitle(int position
) {
125 return mImageFiles
.get(position
).getFileName();
128 public void updateFile(int position
, OCFile file
) {
129 mObsoleteFragments
.add(instantiateItem(null
, position
));
130 mObsoletePositions
.add(Integer
.valueOf(position
));
131 mImageFiles
.set(position
, file
);
135 public int getItemPosition(Object object
) {
136 Log
.e(TAG
, "getItemPosition ");
137 if (mObsoleteFragments
.contains(object
)) {
138 mObsoleteFragments
.remove(object
);
139 return POSITION_NONE
;
141 return super.getItemPosition(object
);
146 * Should not be used for not already started fragments...
150 protected FileFragment
getFragmentAt(int position
) {
152 return (FileFragment
) instantiateItem(null
, position
);
154 } catch (Exception e
) {
155 Log
.e(TAG
, "Wrong access to fragment in gallery ", e
);
162 * Called when a change in the shown pages is going to start being made.
164 * @param container The containing View which is displaying this adapter's page views.
167 public void startUpdate(ViewGroup container
) {
168 Log
.e(TAG
, "** startUpdate");
172 public Object
instantiateItem(ViewGroup container
, int position
) {
173 Log
.e(TAG
, "** instantiateItem " + position
);
175 if (mFragments
.size() > position
) {
176 Fragment fragment
= mFragments
.get(position
);
177 if (fragment
!= null
) {
178 Log
.e(TAG
, "** \t returning cached item");
183 if (mCurTransaction
== null
) {
184 mCurTransaction
= mFragmentManager
.beginTransaction();
187 Fragment fragment
= getItem(position
);
188 if (mSavedState
.size() > position
) {
189 Fragment
.SavedState savedState
= mSavedState
.get(position
);
190 if (savedState
!= null
) {
192 // * The Fragment must currently be attached to the FragmentManager.
193 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
194 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
195 // to store a fragment reference
196 fragment
.setInitialSavedState(savedState
);
199 while (mFragments
.size() <= position
) {
200 mFragments
.add(null
);
202 fragment
.setMenuVisibility(false
);
203 mFragments
.set(position
, fragment
);
204 //Log.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
205 mCurTransaction
.add(container
.getId(), fragment
);
211 public void destroyItem(ViewGroup container
, int position
, Object object
) {
212 Log
.e(TAG
, "** destroyItem " + position
);
213 Fragment fragment
= (Fragment
)object
;
215 if (mCurTransaction
== null
) {
216 mCurTransaction
= mFragmentManager
.beginTransaction();
218 Log
.e(TAG
, "** \t removing fragment at position " + position
);
219 while (mSavedState
.size() <= position
) {
220 mSavedState
.add(null
);
222 mSavedState
.set(position
, mFragmentManager
.saveFragmentInstanceState(fragment
));
223 mFragments
.set(position
, null
);
225 mCurTransaction
.remove(fragment
);
229 public void setPrimaryItem(ViewGroup container
, int position
, Object object
) {
230 Fragment fragment
= (Fragment
)object
;
231 if (fragment
!= mCurrentPrimaryItem
) {
232 if (mCurrentPrimaryItem
!= null
) {
233 mCurrentPrimaryItem
.setMenuVisibility(false
);
235 if (fragment
!= null
) {
236 fragment
.setMenuVisibility(true
);
238 mCurrentPrimaryItem
= fragment
;
243 public void finishUpdate(ViewGroup container
) {
244 Log
.e(TAG
, "** finishUpdate (start)");
245 if (mCurTransaction
!= null
) {
246 mCurTransaction
.commitAllowingStateLoss();
247 mCurTransaction
= null
;
248 mFragmentManager
.executePendingTransactions();
250 Log
.e(TAG
, "** finishUpdate (end)");
254 public boolean isViewFromObject(View view
, Object object
) {
255 return ((Fragment
)object
).getView() == view
;
259 public Parcelable
saveState() {
261 if (mSavedState
.size() > 0) {
262 state
= new Bundle();
263 Fragment
.SavedState
[] savedStates
= new Fragment
.SavedState
[mSavedState
.size()];
264 mSavedState
.toArray(savedStates
);
265 state
.putParcelableArray("states", savedStates
);
267 for (int i
=0; i
<mFragments
.size(); i
++) {
268 Fragment fragment
= mFragments
.get(i
);
269 if (fragment
!= null
) {
271 state
= new Bundle();
273 String key
= "f" + i
;
274 mFragmentManager
.putFragment(state
, key
, fragment
);
281 public void restoreState(Parcelable state
, ClassLoader loader
) {
283 Bundle bundle
= (Bundle
)state
;
284 bundle
.setClassLoader(loader
);
285 Parcelable
[] states
= bundle
.getParcelableArray("states");
288 if (states
!= null
) {
289 for (int i
=0; i
<states
.length
; i
++) {
290 mSavedState
.add((Fragment
.SavedState
)states
[i
]);
293 Iterable
<String
> keys
= bundle
.keySet();
294 for (String key
: keys
) {
295 if (key
.startsWith("f")) {
296 int index
= Integer
.parseInt(key
.substring(1));
297 Fragment f
= mFragmentManager
.getFragment(bundle
, key
);
299 while (mFragments
.size() <= index
) {
300 mFragments
.add(null
);
302 f
.setMenuVisibility(false
);
303 mFragments
.set(index
, f
);
305 Log
.w(TAG
, "Bad fragment at key " + key
);