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