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
);
114 public int getCount() {
115 return mImageFiles
.size();
119 public CharSequence
getPageTitle(int position
) {
120 return mImageFiles
.get(position
).getFileName();
123 public void updateFile(int position
, OCFile file
) {
124 mImageFiles
.set(position
, file
);
125 mObsoleteFragments
.add(instantiateItem(null
, position
));
129 public int getItemPosition(Object object
) {
130 Log
.e(TAG
, "getItemPosition ");
131 if (mObsoleteFragments
.contains(object
)) {
132 return POSITION_NONE
;
134 return super.getItemPosition(object
);
138 * Called when a change in the shown pages is going to start being made.
140 * @param container The containing View which is displaying this adapter's page views.
143 public void startUpdate(ViewGroup container) {
144 Log.e(TAG, "** startUpdate");
148 public Object instantiateItem(ViewGroup container, int position) {
149 Log.e(TAG, "** instantiateItem " + position);
151 if (mFragments.size() > position) {
152 Fragment fragment = mFragments.get(position);
153 if (fragment != null) {
154 Log.e(TAG, "** \t returning cached item");
159 if (mCurTransaction == null) {
160 mCurTransaction = mFragmentManager.beginTransaction();
163 Fragment fragment = getItem(position);
164 if (mSavedState.size() > position) {
165 Fragment.SavedState savedState = mSavedState.get(position);
166 if (savedState != null) {
168 // * The Fragment must currently be attached to the FragmentManager.
169 // * A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
170 // * The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment)
171 // to store a fragment reference
172 fragment.setInitialSavedState(savedState);
175 while (mFragments.size() <= position) {
176 mFragments.add(null);
178 fragment.setMenuVisibility(false);
179 mFragments.set(position, fragment);
180 Log.e(TAG, "** \t adding fragment at position " + position + ", containerId " + container.getId());
181 mCurTransaction.add(container.getId(), fragment);
187 public void destroyItem(ViewGroup container, int position, Object object) {
188 Log.e(TAG, "** destroyItem " + position);
189 Fragment fragment = (Fragment)object;
191 if (mCurTransaction == null) {
192 mCurTransaction = mFragmentManager.beginTransaction();
194 Log.e(TAG, "** \t removing fragment at position " + position);
195 while (mSavedState.size() <= position) {
196 mSavedState.add(null);
198 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
199 mFragments.set(position, null);
201 mCurTransaction.remove(fragment);
205 public void setPrimaryItem(ViewGroup container, int position, Object object) {
206 Fragment fragment = (Fragment)object;
207 if (fragment != mCurrentPrimaryItem) {
208 if (mCurrentPrimaryItem != null) {
209 mCurrentPrimaryItem.setMenuVisibility(false);
211 if (fragment != null) {
212 fragment.setMenuVisibility(true);
214 mCurrentPrimaryItem = fragment;
219 public void finishUpdate(ViewGroup container) {
220 Log.e(TAG, "** finishUpdate (start)");
221 if (mCurTransaction != null) {
222 mCurTransaction.commitAllowingStateLoss();
223 mCurTransaction = null;
224 mFragmentManager.executePendingTransactions();
226 Log.e(TAG, "** finishUpdate (end)");
230 public boolean isViewFromObject(View view, Object object) {
231 return ((Fragment)object).getView() == view;
235 public Parcelable saveState() {
237 if (mSavedState.size() > 0) {
238 state = new Bundle();
239 Fragment.SavedState[] savedStates = new Fragment.SavedState[mSavedState.size()];
240 mSavedState.toArray(savedStates);
241 state.putParcelableArray("states", savedStates);
243 for (int i=0; i<mFragments.size(); i++) {
244 Fragment fragment = mFragments.get(i);
245 if (fragment != null) {
247 state = new Bundle();
249 String key = "f" + i;
250 mFragmentManager.putFragment(state, key, fragment);
257 public void restoreState(Parcelable state, ClassLoader loader) {
259 Bundle bundle = (Bundle)state;
260 bundle.setClassLoader(loader);
261 Parcelable[] states = bundle.getParcelableArray("states");
264 if (states != null) {
265 for (int i=0; i<states.length; i++) {
266 mSavedState.add((Fragment.SavedState)states[i]);
269 Iterable<String> keys = bundle.keySet();
270 for (String key: keys) {
271 if (key.startsWith("f")) {
272 int index = Integer.parseInt(key.substring(1));
273 Fragment f = mFragmentManager.getFragment(bundle, key);
275 while (mFragments.size() <= index) {
276 mFragments.add(null);
278 f.setMenuVisibility(false);
279 mFragments.set(index, f);
281 Log.w(TAG, "Bad fragment at key " + key);