Fixed automatic download for a not downloaded image file when previewing it
[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
40 /**
41 * Adapter class that provides Fragment instances
42 *
43 * @author David A. Velasco
44 */
45 //public class PreviewImagePagerAdapter extends PagerAdapter {
46 public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
47
48 private static final String TAG = PreviewImagePagerAdapter.class.getSimpleName();
49
50 private Vector<OCFile> mImageFiles;
51 private Account mAccount;
52 private Set<Object> mObsoleteFragments;
53 private DataStorageManager mStorageManager;
54
55 /*
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;
61 */
62
63 /**
64 * Constructor.
65 *
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.
69 */
70 public PreviewImagePagerAdapter(FragmentManager fragmentManager, OCFile parentFolder, Account account, DataStorageManager storageManager) {
71 super(fragmentManager);
72
73 if (fragmentManager == null) {
74 throw new IllegalArgumentException("NULL FragmentManager instance");
75 }
76 if (parentFolder == null) {
77 throw new IllegalArgumentException("NULL parent folder");
78 }
79 if (storageManager == null) {
80 throw new IllegalArgumentException("NULL storage manager");
81 }
82
83 mAccount = account;
84 mStorageManager = storageManager;
85 mImageFiles = mStorageManager.getDirectoryImages(parentFolder);
86 mObsoleteFragments = new HashSet<Object>();
87 }
88
89
90 /**
91 * Returns the image files handled by the adapter.
92 *
93 * @return A vector with the image files handled by the adapter.
94 */
95 protected OCFile getFileAt(int position) {
96 return mImageFiles.get(position);
97 }
98
99
100 public Fragment getItem(int i) {
101 Log.e(TAG, "GETTING PAGE " + i);
102 OCFile file = mImageFiles.get(i);
103 Fragment fragment = null;
104 if (file.isDown()) {
105 fragment = new PreviewImageFragment(file, mAccount);
106 } else {
107 fragment = new FileDownloadFragment(file, mAccount);
108 }
109 return fragment;
110 }
111
112 public int getFilePosition(OCFile file) {
113 return mImageFiles.indexOf(file);
114 }
115
116 @Override
117 public int getCount() {
118 return mImageFiles.size();
119 }
120
121 @Override
122 public CharSequence getPageTitle(int position) {
123 return mImageFiles.get(position).getFileName();
124 }
125
126 public void updateFile(int position, OCFile file) {
127 mImageFiles.set(position, file);
128 mObsoleteFragments.add(instantiateItem(null, position));
129 }
130
131 @Override
132 public int getItemPosition(Object object) {
133 Log.e(TAG, "getItemPosition ");
134 if (mObsoleteFragments.contains(object)) {
135 mObsoleteFragments.remove(object);
136 return POSITION_NONE;
137 }
138 return super.getItemPosition(object);
139 }
140
141
142 /* *
143 * Called when a change in the shown pages is going to start being made.
144 *
145 * @param container The containing View which is displaying this adapter's page views.
146 * -/
147 @Override
148 public void startUpdate(ViewGroup container) {
149 Log.e(TAG, "** startUpdate");
150 }
151
152 @Override
153 public Object instantiateItem(ViewGroup container, int position) {
154 Log.e(TAG, "** instantiateItem " + position);
155
156 if (mFragments.size() > position) {
157 Fragment fragment = mFragments.get(position);
158 if (fragment != null) {
159 Log.e(TAG, "** \t returning cached item");
160 return fragment;
161 }
162 }
163
164 if (mCurTransaction == null) {
165 mCurTransaction = mFragmentManager.beginTransaction();
166 }
167
168 Fragment fragment = getItem(position);
169 if (mSavedState.size() > position) {
170 Fragment.SavedState savedState = mSavedState.get(position);
171 if (savedState != null) {
172 // TODO WATCH OUT:
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);
178 }
179 }
180 while (mFragments.size() <= position) {
181 mFragments.add(null);
182 }
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);
187
188 return fragment;
189 }
190
191 @Override
192 public void destroyItem(ViewGroup container, int position, Object object) {
193 Log.e(TAG, "** destroyItem " + position);
194 Fragment fragment = (Fragment)object;
195
196 if (mCurTransaction == null) {
197 mCurTransaction = mFragmentManager.beginTransaction();
198 }
199 Log.e(TAG, "** \t removing fragment at position " + position);
200 while (mSavedState.size() <= position) {
201 mSavedState.add(null);
202 }
203 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
204 mFragments.set(position, null);
205
206 mCurTransaction.remove(fragment);
207 }
208
209 @Override
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);
215 }
216 if (fragment != null) {
217 fragment.setMenuVisibility(true);
218 }
219 mCurrentPrimaryItem = fragment;
220 }
221 }
222
223 @Override
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();
230 }
231 Log.e(TAG, "** finishUpdate (end)");
232 }
233
234 @Override
235 public boolean isViewFromObject(View view, Object object) {
236 return ((Fragment)object).getView() == view;
237 }
238
239 @Override
240 public Parcelable saveState() {
241 Bundle state = null;
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);
247 }
248 for (int i=0; i<mFragments.size(); i++) {
249 Fragment fragment = mFragments.get(i);
250 if (fragment != null) {
251 if (state == null) {
252 state = new Bundle();
253 }
254 String key = "f" + i;
255 mFragmentManager.putFragment(state, key, fragment);
256 }
257 }
258 return state;
259 }
260
261 @Override
262 public void restoreState(Parcelable state, ClassLoader loader) {
263 if (state != null) {
264 Bundle bundle = (Bundle)state;
265 bundle.setClassLoader(loader);
266 Parcelable[] states = bundle.getParcelableArray("states");
267 mSavedState.clear();
268 mFragments.clear();
269 if (states != null) {
270 for (int i=0; i<states.length; i++) {
271 mSavedState.add((Fragment.SavedState)states[i]);
272 }
273 }
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);
279 if (f != null) {
280 while (mFragments.size() <= index) {
281 mFragments.add(null);
282 }
283 f.setMenuVisibility(false);
284 mFragments.set(index, f);
285 } else {
286 Log.w(TAG, "Bad fragment at key " + key);
287 }
288 }
289 }
290 }
291 } */
292
293 }