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
.FileDownloadFragment
;
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 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;
67 * @param fragmentManager {@link FragmentManager} instance that will handle the {@link Fragment}s provided by the adapter.
68 * @param parentFolder Folder where images will be searched for.
69 * @param storageManager Bridge to database.
71 public PreviewImagePagerAdapter(FragmentManager fragmentManager
, OCFile parentFolder
, Account account
, DataStorageManager storageManager
) {
72 super(fragmentManager
);
74 if (fragmentManager
== null
) {
75 throw new IllegalArgumentException("NULL FragmentManager instance");
77 if (parentFolder
== null
) {
78 throw new IllegalArgumentException("NULL parent folder");
80 if (storageManager
== null
) {
81 throw new IllegalArgumentException("NULL storage manager");
85 mStorageManager
= storageManager
;
86 mImageFiles
= mStorageManager
.getDirectoryImages(parentFolder
);
87 mObsoleteFragments
= new HashSet
<Object
>();
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 Log
.e(TAG
, "GETTING PAGE " + i
);
103 OCFile file
= mImageFiles
.get(i
);
104 Fragment fragment
= null
;
106 fragment
= new PreviewImageFragment(file
, mAccount
);
108 fragment
= new FileDownloadFragment(file
, mAccount
);
113 public int getFilePosition(OCFile file
) {
114 return mImageFiles
.indexOf(file
);
118 public int getCount() {
119 return mImageFiles
.size();
123 public CharSequence
getPageTitle(int position
) {
124 return mImageFiles
.get(position
).getFileName();
127 public void updateFile(int position
, OCFile file
) {
128 mImageFiles
.set(position
, file
);
129 mObsoleteFragments
.add(instantiateItem(null
, position
));
133 public int getItemPosition(Object object
) {
134 Log
.e(TAG
, "getItemPosition ");
135 if (mObsoleteFragments
.contains(object
)) {
136 mObsoleteFragments
.remove(object
);
137 return POSITION_NONE
;
139 return super.getItemPosition(object
);
144 * Called when a change in the shown pages is going to start being made.
146 * @param container The containing View which is displaying this adapter's page views.
149 public void startUpdate(ViewGroup container) {
150 Log.e(TAG, "** startUpdate");
154 public Object instantiateItem(ViewGroup container, int position) {
155 Log.e(TAG, "** instantiateItem " + position);
157 if (mFragments.size() > position) {
158 Fragment fragment = mFragments.get(position);
159 if (fragment != null) {
160 Log.e(TAG, "** \t returning cached item");
165 if (mCurTransaction == null) {
166 mCurTransaction = mFragmentManager.beginTransaction();
169 Fragment fragment = getItem(position);
170 if (mSavedState.size() > position) {
171 Fragment.SavedState savedState = mSavedState.get(position);
172 if (savedState != null) {
174 // * The Fragment must currently be attached to the FragmentManager.
175 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
176 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
177 // to store a fragment reference
178 fragment.setInitialSavedState(savedState);
181 while (mFragments.size() <= position) {
182 mFragments.add(null);
184 fragment.setMenuVisibility(false);
185 mFragments.set(position, fragment);
186 Log.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
187 mCurTransaction.add(container.getId(), fragment);
193 public void destroyItem(ViewGroup container, int position, Object object) {
194 Log.e(TAG, "** destroyItem " + position);
195 Fragment fragment = (Fragment)object;
197 if (mCurTransaction == null) {
198 mCurTransaction = mFragmentManager.beginTransaction();
200 Log.e(TAG, "** \t removing fragment at position " + position);
201 while (mSavedState.size() <= position) {
202 mSavedState.add(null);
204 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
205 mFragments.set(position, null);
207 mCurTransaction.remove(fragment);
211 public void setPrimaryItem(ViewGroup container, int position, Object object) {
212 Fragment fragment = (Fragment)object;
213 if (fragment != mCurrentPrimaryItem) {
214 if (mCurrentPrimaryItem != null) {
215 mCurrentPrimaryItem.setMenuVisibility(false);
217 if (fragment != null) {
218 fragment.setMenuVisibility(true);
220 mCurrentPrimaryItem = fragment;
225 public void finishUpdate(ViewGroup container) {
226 Log.e(TAG, "** finishUpdate (start)");
227 if (mCurTransaction != null) {
228 mCurTransaction.commitAllowingStateLoss();
229 mCurTransaction = null;
230 mFragmentManager.executePendingTransactions();
232 Log.e(TAG, "** finishUpdate (end)");
236 public boolean isViewFromObject(View view, Object object) {
237 return ((Fragment)object).getView() == view;
241 public Parcelable saveState() {
243 if (mSavedState.size() > 0) {
244 state = new Bundle();
245 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
246 mSavedState.toArray(savedStates);
247 state.putParcelableArray("states", savedStates);
249 for (int i=0; i<mFragments.size(); i++) {
250 Fragment fragment = mFragments.get(i);
251 if (fragment != null) {
253 state = new Bundle();
255 String key = "f" + i;
256 mFragmentManager.putFragment(state, key, fragment);
263 public void restoreState(Parcelable state, ClassLoader loader) {
265 Bundle bundle = (Bundle)state;
266 bundle.setClassLoader(loader);
267 Parcelable[] states = bundle.getParcelableArray("states");
270 if (states != null) {
271 for (int i=0; i<states.length; i++) {
272 mSavedState.add((Fragment.SavedState)states[i]);
275 Iterable<String> keys = bundle.keySet();
276 for (String key: keys) {
277 if (key.startsWith("f")) {
278 int index = Integer.parseInt(key.substring(1));
279 Fragment f = mFragmentManager.getFragment(bundle, key);
281 while (mFragments.size() <= index) {
282 mFragments.add(null);
284 f.setMenuVisibility(false);
285 mFragments.set(index, f);
287 Log.w(TAG, "Bad fragment at key " + key);