Fixed. Some message dissapear from list view as 'loading' and 'nothing in here...'
[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.invalidate();
95 }
96
97 protected void switchFileView(){
98 imageView.setNumColumns(1);
99 imageView.invalidate();
100 }
101
102
103 @Override
104 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
105 Log_OC.e(TAG, "onCreateView");
106
107 View v = inflater.inflate(R.layout.list_fragment, null);
108
109 imageView = (GridViewWithHeaderAndFooter)(v.findViewById(R.id.list_root));
110 imageView.setOnItemClickListener(this);
111
112 if (savedInstanceState != null) {
113 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
114 setReferencePosition(referencePosition);
115 }
116
117 // Pull down refresh
118 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
119 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
120 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
121
122 onCreateSwipeToRefresh(mRefreshLayout);
123 onCreateSwipeToRefresh(mRefreshEmptyLayout);
124
125 imageView.setEmptyView(mRefreshEmptyLayout);
126
127 return v;
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 @Override
134 public void onActivityCreated(Bundle savedInstanceState) {
135 super.onActivityCreated(savedInstanceState);
136
137 if (savedInstanceState != null) {
138 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
139 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
140 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
141 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
142 setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
143
144 } else {
145 mIndexes = new ArrayList<Integer>();
146 mFirstPositions = new ArrayList<Integer>();
147 mTops = new ArrayList<Integer>();
148 mHeightCell = 0;
149 }
150 }
151
152
153 @Override
154 public void onSaveInstanceState(Bundle savedInstanceState) {
155 super.onSaveInstanceState(savedInstanceState);
156 Log_OC.e(TAG, "onSaveInstanceState()");
157 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
158 savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
159 savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
160 savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
161 savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
162 savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
163 }
164
165 /**
166 * Calculates the position of the item that will be used as a reference to
167 * reposition the visible items in the list when the device is turned to
168 * other position.
169 *
170 * THe current policy is take as a reference the visible item in the center
171 * of the screen.
172 *
173 * @return The position in the list of the visible item in the center of the
174 * screen.
175 */
176 protected int getReferencePosition() {
177 if (imageView != null) {
178 return (imageView.getFirstVisiblePosition() + imageView.getLastVisiblePosition()) / 2;
179 } else {
180 return 0;
181 }
182 }
183
184 /**
185 * Sets the visible part of the list from the reference position.
186 *
187 * @param position Reference position previously returned by
188 * {@link LocalFileListFragment#getReferencePosition()}
189 */
190 protected void setReferencePosition(int position) {
191 if (imageView != null) {
192 imageView.setSelection(position);
193 }
194 }
195
196
197 /*
198 * Restore index and position
199 */
200 protected void restoreIndexAndTopPosition() {
201 if (mIndexes.size() > 0) {
202 // needs to be checked; not every browse-up had a browse-down before
203
204 int index = mIndexes.remove(mIndexes.size() - 1);
205
206 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
207
208 int top = mTops.remove(mTops.size() - 1);
209
210 imageView.smoothScrollToPosition(firstPosition);
211
212 // Move the scroll if the selection is not visible
213 int indexPosition = mHeightCell*index;
214 int height = imageView.getHeight();
215
216 if (indexPosition > height) {
217 imageView.smoothScrollToPosition(index);
218 }
219 }
220 }
221
222 /*
223 * Save index and top position
224 */
225 protected void saveIndexAndTopPosition(int index) {
226
227 mIndexes.add(index);
228
229 int firstPosition = imageView.getFirstVisiblePosition();
230 mFirstPositions.add(firstPosition);
231
232 View view = imageView.getChildAt(0);
233 int top = (view == null) ? 0 : view.getTop() ;
234
235 mTops.add(top);
236
237 // Save the height of a cell
238 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
239 }
240
241
242 @Override
243 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
244 // to be @overriden
245 }
246
247 @Override
248 public void onRefresh() {
249 // to be @overriden
250 mRefreshLayout.setRefreshing(false);
251 mRefreshEmptyLayout.setRefreshing(false);
252
253 if (mOnRefreshListener != null) {
254 mOnRefreshListener.onRefresh();
255 }
256 }
257 public void setOnRefreshListener(OnEnforceableRefreshListener listener) {
258 mOnRefreshListener = listener;
259 }
260
261
262 /**
263 * Enables swipe gesture
264 */
265 public void enableSwipe() {
266 mRefreshLayout.setEnabled(true);
267 }
268
269 /**
270 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
271 * refreshing programmatically.
272 */
273 public void disableSwipe() {
274 mRefreshLayout.setEnabled(false);
275 }
276
277 /**
278 * It shows the SwipeRefreshLayout progress
279 */
280 public void showSwipeProgress() {
281 mRefreshLayout.setRefreshing(true);
282 }
283
284 /**
285 * It shows the SwipeRefreshLayout progress
286 */
287 public void hideSwipeProgress() {
288 mRefreshLayout.setRefreshing(false);
289 }
290
291 /**
292 * Set message for empty list view
293 */
294 public void setMessageForEmptyList(String message) {
295 if (mEmptyListMessage != null) {
296 mEmptyListMessage.setText(message);
297 }
298 }
299
300 /**
301 * Get the text of EmptyListMessage TextView
302 *
303 * @return String
304 */
305 public String getEmptyViewText() {
306 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
307 }
308
309 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
310 // Colors in animations: background
311 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
312 R.color.background_color);
313
314 refreshLayout.setOnRefreshListener(this);
315 }
316
317 @Override
318 public void onRefresh(boolean ignoreETag) {
319 mRefreshLayout.setRefreshing(false);
320 mRefreshEmptyLayout.setRefreshing(false);
321
322 if (mOnRefreshListener != null) {
323 mOnRefreshListener.onRefresh(ignoreETag);
324 }
325 }
326 }