abd53d3a5f4a4c0427af31ce0c0b8010510b7b04
[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.ui.ExtendedListView;
37 import com.owncloud.android.utils.Log_OC;
38
39 /**
40 * TODO extending SherlockListFragment instead of SherlockFragment
41 */
42 public class ExtendedListFragment extends SherlockFragment
43 implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
44
45 private static final String TAG = ExtendedListFragment.class.getSimpleName();
46
47 private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
48 private static final String KEY_INDEXES = "INDEXES";
49 private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
50 private static final String KEY_TOPS = "TOPS";
51 private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
52 private static final String KEY_EMPTY_LIST_MESSAGE = "EMPTY_LIST_MESSAGE";
53
54 protected ExtendedListView mList;
55
56 private SwipeRefreshLayout mRefreshLayout;
57 private SwipeRefreshLayout mRefreshEmptyLayout;
58 private TextView mEmptyListMessage;
59
60 // Save the state of the scroll in browsing
61 private ArrayList<Integer> mIndexes;
62 private ArrayList<Integer> mFirstPositions;
63 private ArrayList<Integer> mTops;
64 private int mHeightCell = 0;
65
66 private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = null;
67
68
69 public void setListAdapter(ListAdapter listAdapter) {
70 mList.setAdapter(listAdapter);
71 mList.invalidate();
72 }
73
74 public ListView getListView() {
75 return mList;
76 }
77
78
79 @Override
80 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
81 Log_OC.e(TAG, "onCreateView");
82
83 View v = inflater.inflate(R.layout.list_fragment, null);
84 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
85 mList = (ExtendedListView)(v.findViewById(R.id.list_root));
86 mList.setOnItemClickListener(this);
87
88 mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
89 mList.setDividerHeight(1);
90
91 if (savedInstanceState != null) {
92 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
93 setReferencePosition(referencePosition);
94 }
95
96 // Pull down refresh
97 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
98 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
99
100 onCreateSwipeToRefresh(mRefreshLayout);
101 onCreateSwipeToRefresh(mRefreshEmptyLayout);
102
103 mList.setEmptyView(mRefreshEmptyLayout);
104
105 return v;
106 }
107
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override
113 public void onActivityCreated(Bundle savedInstanceState) {
114 super.onActivityCreated(savedInstanceState);
115
116 if (savedInstanceState != null) {
117 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
118 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
119 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
120 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
121 setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
122
123 } else {
124 mIndexes = new ArrayList<Integer>();
125 mFirstPositions = new ArrayList<Integer>();
126 mTops = new ArrayList<Integer>();
127 mHeightCell = 0;
128 }
129 }
130
131
132 @Override
133 public void onSaveInstanceState(Bundle savedInstanceState) {
134 super.onSaveInstanceState(savedInstanceState);
135 Log_OC.e(TAG, "onSaveInstanceState()");
136 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
137 savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
138 savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
139 savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
140 savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
141 savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
142 }
143
144
145 /**
146 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
147 * the device is turned to other position.
148 *
149 * THe current policy is take as a reference the visible item in the center of the screen.
150 *
151 * @return The position in the list of the visible item in the center of the screen.
152 */
153 protected int getReferencePosition() {
154 if (mList != null) {
155 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
156 } else {
157 return 0;
158 }
159 }
160
161
162 /**
163 * Sets the visible part of the list from the reference position.
164 *
165 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
166 */
167 protected void setReferencePosition(int position) {
168 if (mList != null) {
169 mList.setAndCenterSelection(position);
170 }
171 }
172
173
174 /*
175 * Restore index and position
176 */
177 protected void restoreIndexAndTopPosition() {
178 if (mIndexes.size() > 0) {
179 // needs to be checked; not every browse-up had a browse-down before
180
181 int index = mIndexes.remove(mIndexes.size() - 1);
182
183 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
184
185 int top = mTops.remove(mTops.size() - 1);
186
187 mList.setSelectionFromTop(firstPosition, top);
188
189 // Move the scroll if the selection is not visible
190 int indexPosition = mHeightCell*index;
191 int height = mList.getHeight();
192
193 if (indexPosition > height) {
194 if (android.os.Build.VERSION.SDK_INT >= 11)
195 {
196 mList.smoothScrollToPosition(index);
197 }
198 else if (android.os.Build.VERSION.SDK_INT >= 8)
199 {
200 mList.setSelectionFromTop(index, 0);
201 }
202
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 = mList.getFirstVisiblePosition();
215 mFirstPositions.add(firstPosition);
216
217 View view = mList.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(SwipeRefreshLayout.OnRefreshListener 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 }