22aa495328bcce4dc6f71ab5333f71480f6e949e
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewImagePagerAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.preview;
19
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.Vector;
24
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;
36
37 import com.owncloud.android.datamodel.DataStorageManager;
38 import com.owncloud.android.datamodel.OCFile;
39 import com.owncloud.android.ui.fragment.FileDownloadFragment;
40
41 /**
42 * Adapter class that provides Fragment instances
43 *
44 * @author David A. Velasco
45 */
46 //public class PreviewImagePagerAdapter extends PagerAdapter {
47 public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
48
49 private static final String TAG = PreviewImagePagerAdapter.class.getSimpleName();
50
51 private Vector<OCFile> mImageFiles;
52 private Account mAccount;
53 private Set<Object> mObsoleteFragments;
54 private DataStorageManager mStorageManager;
55
56 /*
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;
62 */
63
64 /**
65 * Constructor.
66 *
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.
70 */
71 public PreviewImagePagerAdapter(FragmentManager fragmentManager, OCFile parentFolder, Account account, DataStorageManager storageManager) {
72 super(fragmentManager);
73
74 if (fragmentManager == null) {
75 throw new IllegalArgumentException("NULL FragmentManager instance");
76 }
77 if (parentFolder == null) {
78 throw new IllegalArgumentException("NULL parent folder");
79 }
80 if (storageManager == null) {
81 throw new IllegalArgumentException("NULL storage manager");
82 }
83
84 mAccount = account;
85 mStorageManager = storageManager;
86 mImageFiles = mStorageManager.getDirectoryImages(parentFolder);
87 mObsoleteFragments = new HashSet<Object>();
88 }
89
90
91 /**
92 * Returns the image files handled by the adapter.
93 *
94 * @return A vector with the image files handled by the adapter.
95 */
96 protected OCFile getFileAt(int position) {
97 return mImageFiles.get(position);
98 }
99
100
101 public Fragment getItem(int i) {
102 Log.e(TAG, "GETTING PAGE " + i);
103 OCFile file = mImageFiles.get(i);
104 Fragment fragment = null;
105 if (file.isDown()) {
106 fragment = new PreviewImageFragment(file, mAccount);
107 } else {
108 fragment = new FileDownloadFragment(file, mAccount);
109 }
110 return fragment;
111 }
112
113 @Override
114 public int getCount() {
115 return mImageFiles.size();
116 }
117
118 @Override
119 public CharSequence getPageTitle(int position) {
120 return mImageFiles.get(position).getFileName();
121 }
122
123 public void updateFile(int position, OCFile file) {
124 mImageFiles.set(position, file);
125 mObsoleteFragments.add(instantiateItem(null, position));
126 }
127
128 @Override
129 public int getItemPosition(Object object) {
130 Log.e(TAG, "getItemPosition ");
131 if (mObsoleteFragments.contains(object)) {
132 return POSITION_NONE;
133 }
134 return super.getItemPosition(object);
135 }
136
137 /* *
138 * Called when a change in the shown pages is going to start being made.
139 *
140 * @param container The containing View which is displaying this adapter's page views.
141 * -/
142 @Override
143 public void startUpdate(ViewGroup container) {
144 Log.e(TAG, "** startUpdate");
145 }
146
147 @Override
148 public Object instantiateItem(ViewGroup container, int position) {
149 Log.e(TAG, "** instantiateItem " + position);
150
151 if (mFragments.size() > position) {
152 Fragment fragment = mFragments.get(position);
153 if (fragment != null) {
154 Log.e(TAG, "** \t returning cached item");
155 return fragment;
156 }
157 }
158
159 if (mCurTransaction == null) {
160 mCurTransaction = mFragmentManager.beginTransaction();
161 }
162
163 Fragment fragment = getItem(position);
164 if (mSavedState.size() > position) {
165 Fragment.SavedState savedState = mSavedState.get(position);
166 if (savedState != null) {
167 // TODO WATCH OUT:
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);
173 }
174 }
175 while (mFragments.size() <= position) {
176 mFragments.add(null);
177 }
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);
182
183 return fragment;
184 }
185
186 @Override
187 public void destroyItem(ViewGroup container, int position, Object object) {
188 Log.e(TAG, "** destroyItem " + position);
189 Fragment fragment = (Fragment)object;
190
191 if (mCurTransaction == null) {
192 mCurTransaction = mFragmentManager.beginTransaction();
193 }
194 Log.e(TAG, "** \t removing fragment at position " + position);
195 while (mSavedState.size() <= position) {
196 mSavedState.add(null);
197 }
198 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
199 mFragments.set(position, null);
200
201 mCurTransaction.remove(fragment);
202 }
203
204 @Override
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);
210 }
211 if (fragment != null) {
212 fragment.setMenuVisibility(true);
213 }
214 mCurrentPrimaryItem = fragment;
215 }
216 }
217
218 @Override
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();
225 }
226 Log.e(TAG, "** finishUpdate (end)");
227 }
228
229 @Override
230 public boolean isViewFromObject(View view, Object object) {
231 return ((Fragment)object).getView() == view;
232 }
233
234 @Override
235 public Parcelable saveState() {
236 Bundle state = null;
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);
242 }
243 for (int i=0; i<mFragments.size(); i++) {
244 Fragment fragment = mFragments.get(i);
245 if (fragment != null) {
246 if (state == null) {
247 state = new Bundle();
248 }
249 String key = "f" + i;
250 mFragmentManager.putFragment(state, key, fragment);
251 }
252 }
253 return state;
254 }
255
256 @Override
257 public void restoreState(Parcelable state, ClassLoader loader) {
258 if (state != null) {
259 Bundle bundle = (Bundle)state;
260 bundle.setClassLoader(loader);
261 Parcelable[] states = bundle.getParcelableArray("states");
262 mSavedState.clear();
263 mFragments.clear();
264 if (states != null) {
265 for (int i=0; i<states.length; i++) {
266 mSavedState.add((Fragment.SavedState)states[i]);
267 }
268 }
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);
274 if (f != null) {
275 while (mFragments.size() <= index) {
276 mFragments.add(null);
277 }
278 f.setMenuVisibility(false);
279 mFragments.set(index, f);
280 } else {
281 Log.w(TAG, "Bad fragment at key " + key);
282 }
283 }
284 }
285 }
286 } */
287
288 }