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