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
;
41 * Adapter class that provides Fragment instances
43 * @author David A. Velasco
45 //public class PreviewImagePagerAdapter extends PagerAdapter {
46 public class PreviewImagePagerAdapter
extends FragmentStatePagerAdapter
{
48 private static final String TAG
= PreviewImagePagerAdapter
.class.getSimpleName();
50 private Vector
<OCFile
> mImageFiles
;
51 private Account mAccount
;
52 private Set
<Object
> mObsoleteFragments
;
53 private DataStorageManager mStorageManager
;
56 private final FragmentManager mFragmentManager;
57 private FragmentTransaction mCurTransaction = null;
58 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
59 private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
60 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
>();
91 * Returns the image files handled by the adapter.
93 * @return A vector with the image files handled by the adapter.
95 protected OCFile
getFileAt(int position
) {
96 return mImageFiles
.get(position
);
100 public Fragment
getItem(int i
) {
101 Log
.e(TAG
, "GETTING PAGE " + i
);
102 OCFile file
= mImageFiles
.get(i
);
103 Fragment fragment
= null
;
105 fragment
= new PreviewImageFragment(file
, mAccount
);
107 fragment
= new FileDownloadFragment(file
, mAccount
);
112 public int getFilePosition(OCFile file
) {
113 return mImageFiles
.indexOf(file
);
117 public int getCount() {
118 return mImageFiles
.size();
122 public CharSequence
getPageTitle(int position
) {
123 return mImageFiles
.get(position
).getFileName();
126 public void updateFile(int position
, OCFile file
) {
127 mImageFiles
.set(position
, file
);
128 mObsoleteFragments
.add(instantiateItem(null
, position
));
132 public int getItemPosition(Object object
) {
133 Log
.e(TAG
, "getItemPosition ");
134 if (mObsoleteFragments
.contains(object
)) {
135 mObsoleteFragments
.remove(object
);
136 return POSITION_NONE
;
138 return super.getItemPosition(object
);
143 * Called when a change in the shown pages is going to start being made.
145 * @param container The containing View which is displaying this adapter's page views.
148 public void startUpdate(ViewGroup container) {
149 Log.e(TAG, "** startUpdate");
153 public Object instantiateItem(ViewGroup container, int position) {
154 Log.e(TAG, "** instantiateItem " + position);
156 if (mFragments.size() > position) {
157 Fragment fragment = mFragments.get(position);
158 if (fragment != null) {
159 Log.e(TAG, "** \t returning cached item");
164 if (mCurTransaction == null) {
165 mCurTransaction = mFragmentManager.beginTransaction();
168 Fragment fragment = getItem(position);
169 if (mSavedState.size() > position) {
170 Fragment.SavedState savedState = mSavedState.get(position);
171 if (savedState != null) {
173 // * The Fragment must currently be attached to the FragmentManager.
174 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
175 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
176 // to store a fragment reference
177 fragment.setInitialSavedState(savedState);
180 while (mFragments.size() <= position) {
181 mFragments.add(null);
183 fragment.setMenuVisibility(false);
184 mFragments.set(position, fragment);
185 Log.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
186 mCurTransaction.add(container.getId(), fragment);
192 public void destroyItem(ViewGroup container, int position, Object object) {
193 Log.e(TAG, "** destroyItem " + position);
194 Fragment fragment = (Fragment)object;
196 if (mCurTransaction == null) {
197 mCurTransaction = mFragmentManager.beginTransaction();
199 Log.e(TAG, "** \t removing fragment at position " + position);
200 while (mSavedState.size() <= position) {
201 mSavedState.add(null);
203 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
204 mFragments.set(position, null);
206 mCurTransaction.remove(fragment);
210 public void setPrimaryItem(ViewGroup container, int position, Object object) {
211 Fragment fragment = (Fragment)object;
212 if (fragment != mCurrentPrimaryItem) {
213 if (mCurrentPrimaryItem != null) {
214 mCurrentPrimaryItem.setMenuVisibility(false);
216 if (fragment != null) {
217 fragment.setMenuVisibility(true);
219 mCurrentPrimaryItem = fragment;
224 public void finishUpdate(ViewGroup container) {
225 Log.e(TAG, "** finishUpdate (start)");
226 if (mCurTransaction != null) {
227 mCurTransaction.commitAllowingStateLoss();
228 mCurTransaction = null;
229 mFragmentManager.executePendingTransactions();
231 Log.e(TAG, "** finishUpdate (end)");
235 public boolean isViewFromObject(View view, Object object) {
236 return ((Fragment)object).getView() == view;
240 public Parcelable saveState() {
242 if (mSavedState.size() > 0) {
243 state = new Bundle();
244 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
245 mSavedState.toArray(savedStates);
246 state.putParcelableArray("states", savedStates);
248 for (int i=0; i<mFragments.size(); i++) {
249 Fragment fragment = mFragments.get(i);
250 if (fragment != null) {
252 state = new Bundle();
254 String key = "f" + i;
255 mFragmentManager.putFragment(state, key, fragment);
262 public void restoreState(Parcelable state, ClassLoader loader) {
264 Bundle bundle = (Bundle)state;
265 bundle.setClassLoader(loader);
266 Parcelable[] states = bundle.getParcelableArray("states");
269 if (states != null) {
270 for (int i=0; i<states.length; i++) {
271 mSavedState.add((Fragment.SavedState)states[i]);
274 Iterable<String> keys = bundle.keySet();
275 for (String key: keys) {
276 if (key.startsWith("f")) {
277 int index = Integer.parseInt(key.substring(1));
278 Fragment f = mFragmentManager.getFragment(bundle, key);
280 while (mFragments.size() <= index) {
281 mFragments.add(null);
283 f.setMenuVisibility(false);
284 mFragments.set(index, f);
286 Log.w(TAG, "Bad fragment at key " + key);