cleanup
[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 in the list when
155 * 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 returned by {@link LocalFileListFragment#getReferencePosition()}
174 */
175 protected void setReferencePosition(int position) {
176 if (imageView != null) {
177 imageView.setSelection(position);
178 }
179 }
180
181
182 /*
183 * Restore index and position
184 */
185 protected void restoreIndexAndTopPosition() {
186 if (mIndexes.size() > 0) {
187 // needs to be checked; not every browse-up had a browse-down before
188
189 int index = mIndexes.remove(mIndexes.size() - 1);
190
191 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
192
193 int top = mTops.remove(mTops.size() - 1);
194
195 imageView.smoothScrollToPosition(firstPosition);
196
197 // Move the scroll if the selection is not visible
198 int indexPosition = mHeightCell*index;
199 int height = imageView.getHeight();
200
201 if (indexPosition > height) {
202 imageView.smoothScrollToPosition(index);
203 }
204 }
205 }
206
207 /*
208 * Save index and top position
209 */
210 protected void saveIndexAndTopPosition(int index) {
211
212 mIndexes.add(index);
213
214 int firstPosition = imageView.getFirstVisiblePosition();
215 mFirstPositions.add(firstPosition);
216
217 View view = imageView.getChildAt(0);
218 int top = (view == null) ? 0 : view.getTop() ;
219
220 mTops.add(top);
221
222 // Save the height of a cell
223 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
224 }
225
226
227 @Override
228 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
229 // to be @overriden
230 }
231
232 @Override
233 public void onRefresh() {
234 // to be @overriden
235 mRefreshLayout.setRefreshing(false);
236 mRefreshEmptyLayout.setRefreshing(false);
237
238 if (mOnRefreshListener != null) {
239 mOnRefreshListener.onRefresh();
240 }
241 }
242
243 public void setOnRefreshListener(OnEnforceableRefreshListener listener) {
244 mOnRefreshListener = listener;
245 }
246
247
248 /**
249 * Enables swipe gesture
250 */
251 public void enableSwipe() {
252 mRefreshLayout.setEnabled(true);
253 }
254
255 /**
256 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
257 * refreshing programmatically.
258 */
259 public void disableSwipe() {
260 mRefreshLayout.setEnabled(false);
261 }
262
263 /**
264 * It shows the SwipeRefreshLayout progress
265 */
266 public void showSwipeProgress() {
267 mRefreshLayout.setRefreshing(true);
268 }
269
270 /**
271 * It shows the SwipeRefreshLayout progress
272 */
273 public void hideSwipeProgress() {
274 mRefreshLayout.setRefreshing(false);
275 }
276
277 /**
278 * Set message for empty list view
279 */
280 public void setMessageForEmptyList(String message) {
281 if (mEmptyListMessage != null) {
282 mEmptyListMessage.setText(message);
283 }
284 }
285
286 /**
287 * Get the text of EmptyListMessage TextView
288 *
289 * @return String
290 */
291 public String getEmptyViewText() {
292 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
293 }
294
295 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
296 // Colors in animations: background
297 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
298 R.color.background_color);
299
300 refreshLayout.setOnRefreshListener(this);
301 }
302
303 @Override
304 public void onRefresh(boolean ignoreETag) {
305 mRefreshLayout.setRefreshing(false);
306 mRefreshEmptyLayout.setRefreshing(false);
307
308 if (mOnRefreshListener != null) {
309 mOnRefreshListener.onRefresh(ignoreETag);
310 }
311 }
312 }