Merge pull request #669 from grecep/master
[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.ListAdapter;
31 import android.widget.ListView;
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 com.owncloud.android.ui.ExtendedListView;
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 protected ExtendedListView mList;
56
57 private SwipeRefreshLayout mRefreshLayout;
58 private SwipeRefreshLayout mRefreshEmptyLayout;
59 private TextView mEmptyListMessage;
60
61 // Save the state of the scroll in browsing
62 private ArrayList<Integer> mIndexes;
63 private ArrayList<Integer> mFirstPositions;
64 private ArrayList<Integer> mTops;
65 private int mHeightCell = 0;
66
67 private OnEnforceableRefreshListener mOnRefreshListener = null;
68
69
70 public void setListAdapter(ListAdapter listAdapter) {
71 mList.setAdapter(listAdapter);
72 mList.invalidate();
73 }
74
75 public ListView getListView() {
76 return mList;
77 }
78
79
80 @Override
81 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
82 Log_OC.e(TAG, "onCreateView");
83
84 View v = inflater.inflate(R.layout.list_fragment, null);
85 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
86 mList = (ExtendedListView)(v.findViewById(R.id.list_root));
87 mList.setOnItemClickListener(this);
88
89 mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
90 mList.setDividerHeight(1);
91
92 if (savedInstanceState != null) {
93 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
94 setReferencePosition(referencePosition);
95 }
96
97 // Pull down refresh
98 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
99 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
100
101 onCreateSwipeToRefresh(mRefreshLayout);
102 onCreateSwipeToRefresh(mRefreshEmptyLayout);
103
104 mList.setEmptyView(mRefreshEmptyLayout);
105
106 return v;
107 }
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 /**
147 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
148 * the device is turned to other position.
149 *
150 * THe current policy is take as a reference the visible item in the center of the screen.
151 *
152 * @return The position in the list of the visible item in the center of the screen.
153 */
154 protected int getReferencePosition() {
155 if (mList != null) {
156 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
157 } else {
158 return 0;
159 }
160 }
161
162
163 /**
164 * Sets the visible part of the list from the reference position.
165 *
166 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
167 */
168 protected void setReferencePosition(int position) {
169 if (mList != null) {
170 mList.setAndCenterSelection(position);
171 }
172 }
173
174
175 /*
176 * Restore index and position
177 */
178 protected void restoreIndexAndTopPosition() {
179 if (mIndexes.size() > 0) {
180 // needs to be checked; not every browse-up had a browse-down before
181
182 int index = mIndexes.remove(mIndexes.size() - 1);
183
184 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
185
186 int top = mTops.remove(mTops.size() - 1);
187
188 mList.setSelectionFromTop(firstPosition, top);
189
190 // Move the scroll if the selection is not visible
191 int indexPosition = mHeightCell*index;
192 int height = mList.getHeight();
193
194 if (indexPosition > height) {
195 if (android.os.Build.VERSION.SDK_INT >= 11)
196 {
197 mList.smoothScrollToPosition(index);
198 }
199 else if (android.os.Build.VERSION.SDK_INT >= 8)
200 {
201 mList.setSelectionFromTop(index, 0);
202 }
203
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 = mList.getFirstVisiblePosition();
216 mFirstPositions.add(firstPosition);
217
218 View view = mList.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 }