Fix bug: Footer is not shown correct in the grid view
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ExtendedListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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
19 package com.owncloud.android.ui.fragment;
20
21 import java.util.ArrayList;
22
23 import android.os.Bundle;
24 import android.support.v4.widget.SwipeRefreshLayout;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.AdapterView;
29 import android.widget.AdapterView.OnItemClickListener;
30 import android.widget.GridView;
31 import android.widget.ListAdapter;
32 import android.widget.TextView;
33
34 import com.actionbarsherlock.app.SherlockFragment;
35 import com.owncloud.android.R;
36 import com.owncloud.android.lib.common.utils.Log_OC;
37 import third_parties.in.srain.cube.GridViewWithHeaderAndFooter;
38 import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
39
40 /**
41 * TODO extending SherlockListFragment instead of SherlockFragment
42 */
43 public class ExtendedListFragment extends SherlockFragment
44 implements OnItemClickListener, OnEnforceableRefreshListener {
45
46 private static final String TAG = ExtendedListFragment.class.getSimpleName();
47
48 private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
49 private static final String KEY_INDEXES = "INDEXES";
50 private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
51 private static final String KEY_TOPS = "TOPS";
52 private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
53 private static final String KEY_EMPTY_LIST_MESSAGE = "EMPTY_LIST_MESSAGE";
54
55 private SwipeRefreshLayout mRefreshLayout;
56 private SwipeRefreshLayout mRefreshEmptyLayout;
57 private TextView mEmptyListMessage;
58
59 // Save the state of the scroll in browsing
60 private ArrayList<Integer> mIndexes;
61 private ArrayList<Integer> mFirstPositions;
62 private ArrayList<Integer> mTops;
63 private int mHeightCell = 0;
64
65 private OnEnforceableRefreshListener mOnRefreshListener = null;
66
67 protected GridViewWithHeaderAndFooter imageView;
68
69 public void setListAdapter(ListAdapter listAdapter) {
70 imageView.setAdapter(listAdapter);
71 imageView.invalidate();
72 }
73
74 public GridView getGridView() {
75 return imageView;
76 }
77
78 public void setFooterView(View footer) {
79 imageView.addFooterView(footer, null, false);
80 imageView.invalidate();
81 }
82
83 public void removeFooterView(View footer) {
84 imageView.removeFooterView(footer);
85 imageView.invalidate();
86 }
87
88 public int getFooterViewCount() {
89 return imageView.getFooterViewCount();
90 }
91
92 protected void switchImageView(){
93 imageView.setNumColumns(GridView.AUTO_FIT);
94 imageView.invalidateRowHeight(); // Force to recalculate mRowHeight of imageView
95 imageView.invalidate();
96 }
97
98 protected void switchFileView(){
99 imageView.setNumColumns(1);
100 imageView.invalidate();
101 }
102
103
104 @Override
105 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
106 Log_OC.e(TAG, "onCreateView");
107
108 View v = inflater.inflate(R.layout.list_fragment, null);
109
110 imageView = (GridViewWithHeaderAndFooter)(v.findViewById(R.id.list_root));
111 imageView.setOnItemClickListener(this);
112
113 if (savedInstanceState != null) {
114 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
115 setReferencePosition(referencePosition);
116 }
117
118 // Pull down refresh
119 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
120 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
121 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
122
123 onCreateSwipeToRefresh(mRefreshLayout);
124 onCreateSwipeToRefresh(mRefreshEmptyLayout);
125
126 imageView.setEmptyView(mRefreshEmptyLayout);
127
128 return v;
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 @Override
135 public void onActivityCreated(Bundle savedInstanceState) {
136 super.onActivityCreated(savedInstanceState);
137
138 if (savedInstanceState != null) {
139 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
140 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
141 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
142 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
143 setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
144
145 } else {
146 mIndexes = new ArrayList<Integer>();
147 mFirstPositions = new ArrayList<Integer>();
148 mTops = new ArrayList<Integer>();
149 mHeightCell = 0;
150 }
151 }
152
153
154 @Override
155 public void onSaveInstanceState(Bundle savedInstanceState) {
156 super.onSaveInstanceState(savedInstanceState);
157 Log_OC.e(TAG, "onSaveInstanceState()");
158 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
159 savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
160 savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
161 savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
162 savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
163 savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
164 }
165
166 /**
167 * Calculates the position of the item that will be used as a reference to
168 * reposition the visible items in the list when the device is turned to
169 * other position.
170 *
171 * THe current policy is take as a reference the visible item in the center
172 * of the screen.
173 *
174 * @return The position in the list of the visible item in the center of the
175 * screen.
176 */
177 protected int getReferencePosition() {
178 if (imageView != null) {
179 return (imageView.getFirstVisiblePosition() + imageView.getLastVisiblePosition()) / 2;
180 } else {
181 return 0;
182 }
183 }
184
185 /**
186 * Sets the visible part of the list from the reference position.
187 *
188 * @param position Reference position previously returned by
189 * {@link LocalFileListFragment#getReferencePosition()}
190 */
191 protected void setReferencePosition(int position) {
192 if (imageView != null) {
193 imageView.setSelection(position);
194 }
195 }
196
197
198 /*
199 * Restore index and position
200 */
201 protected void restoreIndexAndTopPosition() {
202 if (mIndexes.size() > 0) {
203 // needs to be checked; not every browse-up had a browse-down before
204
205 int index = mIndexes.remove(mIndexes.size() - 1);
206
207 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
208
209 int top = mTops.remove(mTops.size() - 1);
210
211 imageView.smoothScrollToPosition(firstPosition);
212
213 // Move the scroll if the selection is not visible
214 int indexPosition = mHeightCell*index;
215 int height = imageView.getHeight();
216
217 if (indexPosition > height) {
218 imageView.smoothScrollToPosition(index);
219 }
220 }
221 }
222
223 /*
224 * Save index and top position
225 */
226 protected void saveIndexAndTopPosition(int index) {
227
228 mIndexes.add(index);
229
230 int firstPosition = imageView.getFirstVisiblePosition();
231 mFirstPositions.add(firstPosition);
232
233 View view = imageView.getChildAt(0);
234 int top = (view == null) ? 0 : view.getTop() ;
235
236 mTops.add(top);
237
238 // Save the height of a cell
239 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
240 }
241
242
243 @Override
244 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
245 // to be @overriden
246 }
247
248 @Override
249 public void onRefresh() {
250 // to be @overriden
251 mRefreshLayout.setRefreshing(false);
252 mRefreshEmptyLayout.setRefreshing(false);
253
254 if (mOnRefreshListener != null) {
255 mOnRefreshListener.onRefresh();
256 }
257 }
258 public void setOnRefreshListener(OnEnforceableRefreshListener listener) {
259 mOnRefreshListener = listener;
260 }
261
262
263 /**
264 * Enables swipe gesture
265 */
266 public void enableSwipe() {
267 mRefreshLayout.setEnabled(true);
268 }
269
270 /**
271 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
272 * refreshing programmatically.
273 */
274 public void disableSwipe() {
275 mRefreshLayout.setEnabled(false);
276 }
277
278 /**
279 * It shows the SwipeRefreshLayout progress
280 */
281 public void showSwipeProgress() {
282 mRefreshLayout.setRefreshing(true);
283 }
284
285 /**
286 * It shows the SwipeRefreshLayout progress
287 */
288 public void hideSwipeProgress() {
289 mRefreshLayout.setRefreshing(false);
290 }
291
292 /**
293 * Set message for empty list view
294 */
295 public void setMessageForEmptyList(String message) {
296 if (mEmptyListMessage != null) {
297 mEmptyListMessage.setText(message);
298 }
299 }
300
301 /**
302 * Get the text of EmptyListMessage TextView
303 *
304 * @return String
305 */
306 public String getEmptyViewText() {
307 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
308 }
309
310 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
311 // Colors in animations: background
312 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
313 R.color.background_color);
314
315 refreshLayout.setOnRefreshListener(this);
316 }
317
318 @Override
319 public void onRefresh(boolean ignoreETag) {
320 mRefreshLayout.setRefreshing(false);
321 mRefreshEmptyLayout.setRefreshing(false);
322
323 if (mOnRefreshListener != null) {
324 mOnRefreshListener.onRefresh(ignoreETag);
325 }
326 }
327 }