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