c6fd7ca9d68012d7fc54556a1bdbbf4c16afa57b
[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 public int getFilePosition(OCFile file) {
114 return mImageFiles.indexOf(file);
115 }
116
117 @Override
118 public int getCount() {
119 return mImageFiles.size();
120 }
121
122 @Override
123 public CharSequence getPageTitle(int position) {
124 return mImageFiles.get(position).getFileName();
125 }
126
127 public void updateFile(int position, OCFile file) {
128 mImageFiles.set(position, file);
129 mObsoleteFragments.add(instantiateItem(null, position));
130 }
131
132 @Override
133 public int getItemPosition(Object object) {
134 Log.e(TAG, "getItemPosition ");
135 if (mObsoleteFragments.contains(object)) {
136 mObsoleteFragments.remove(object);
137 return POSITION_NONE;
138 }
139 return super.getItemPosition(object);
140 }
141
142
143 /* *
144 * Called when a change in the shown pages is going to start being made.
145 *
146 * @param container The containing View which is displaying this adapter's page views.
147 * -/
148 @Override
149 public void startUpdate(ViewGroup container) {
150 Log.e(TAG, "** startUpdate");
151 }
152
153 @Override
154 public Object instantiateItem(ViewGroup container, int position) {
155 Log.e(TAG, "** instantiateItem " + position);
156
157 if (mFragments.size() > position) {
158 Fragment fragment = mFragments.get(position);
159 if (fragment != null) {
160 Log.e(TAG, "** \t returning cached item");
161 return fragment;
162 }
163 }
164
165 if (mCurTransaction == null) {
166 mCurTransaction = mFragmentManager.beginTransaction();
167 }
168
169 Fragment fragment = getItem(position);
170 if (mSavedState.size() > position) {
171 Fragment.SavedState savedState = mSavedState.get(position);
172 if (savedState != null) {
173 // TODO WATCH OUT:
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);
179 }
180 }
181 while (mFragments.size() <= position) {
182 mFragments.add(null);
183 }
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);
188
189 return fragment;
190 }
191
192 @Override
193 public void destroyItem(ViewGroup container, int position, Object object) {
194 Log.e(TAG, "** destroyItem " + position);
195 Fragment fragment = (Fragment)object;
196
197 if (mCurTransaction == null) {
198 mCurTransaction = mFragmentManager.beginTransaction();
199 }
200 Log.e(TAG, "** \t removing fragment at position " + position);
201 while (mSavedState.size() <= position) {
202 mSavedState.add(null);
203 }
204 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
205 mFragments.set(position, null);
206
207 mCurTransaction.remove(fragment);
208 }
209
210 @Override
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);
216 }
217 if (fragment != null) {
218 fragment.setMenuVisibility(true);
219 }
220 mCurrentPrimaryItem = fragment;
221 }
222 }
223
224 @Override
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();
231 }
232 Log.e(TAG, "** finishUpdate (end)");
233 }
234
235 @Override
236 public boolean isViewFromObject(View view, Object object) {
237 return ((Fragment)object).getView() == view;
238 }
239
240 @Override
241 public Parcelable saveState() {
242 Bundle state = null;
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);
248 }
249 for (int i=0; i<mFragments.size(); i++) {
250 Fragment fragment = mFragments.get(i);
251 if (fragment != null) {
252 if (state == null) {
253 state = new Bundle();
254 }
255 String key = "f" + i;
256 mFragmentManager.putFragment(state, key, fragment);
257 }
258 }
259 return state;
260 }
261
262 @Override
263 public void restoreState(Parcelable state, ClassLoader loader) {
264 if (state != null) {
265 Bundle bundle = (Bundle)state;
266 bundle.setClassLoader(loader);
267 Parcelable[] states = bundle.getParcelableArray("states");
268 mSavedState.clear();
269 mFragments.clear();
270 if (states != null) {
271 for (int i=0; i<states.length; i++) {
272 mSavedState.add((Fragment.SavedState)states[i]);
273 }
274 }
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);
280 if (f != null) {
281 while (mFragments.size() <= index) {
282 mFragments.add(null);
283 }
284 f.setMenuVisibility(false);
285 mFragments.set(index, f);
286 } else {
287 Log.w(TAG, "Bad fragment at key " + key);
288 }
289 }
290 }
291 }
292 } */
293
294 }