Recovered ExtendedListView; ExtendedListFragment updated to handle to possible layouts
[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-2015 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.content.Context;
24 import android.os.Bundle;
25 import android.support.v4.widget.SwipeRefreshLayout;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.AbsListView;
30 import android.widget.AdapterView;
31 import android.widget.AdapterView.OnItemClickListener;
32 import android.widget.GridView;
33 import android.widget.ListAdapter;
34 import android.widget.TextView;
35
36 import com.actionbarsherlock.app.SherlockFragment;
37 import com.owncloud.android.R;
38 import com.owncloud.android.lib.common.utils.Log_OC;
39 import com.owncloud.android.ui.ExtendedListView;
40 import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
41 import com.owncloud.android.ui.adapter.FileListListAdapter;
42
43 import third_parties.in.srain.cube.GridViewWithHeaderAndFooter;
44
45 /**
46 * TODO extending SherlockListFragment instead of SherlockFragment
47 */
48 public class ExtendedListFragment extends SherlockFragment
49 implements OnItemClickListener, OnEnforceableRefreshListener {
50
51 private static final String TAG = ExtendedListFragment.class.getSimpleName();
52
53 private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
54 private static final String KEY_INDEXES = "INDEXES";
55 private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
56 private static final String KEY_TOPS = "TOPS";
57 private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
58 private static final String KEY_EMPTY_LIST_MESSAGE = "EMPTY_LIST_MESSAGE";
59
60 private SwipeRefreshLayout mRefreshListLayout;
61 private SwipeRefreshLayout mRefreshGridLayout;
62 private SwipeRefreshLayout mRefreshEmptyLayout;
63 private TextView mEmptyListMessage;
64
65 // Save the state of the scroll in browsing
66 private ArrayList<Integer> mIndexes;
67 private ArrayList<Integer> mFirstPositions;
68 private ArrayList<Integer> mTops;
69 private int mHeightCell = 0;
70
71 private OnEnforceableRefreshListener mOnRefreshListener = null;
72
73 protected AbsListView mCurrentListView;
74 private ExtendedListView mListView;
75 private View mListFooterView;
76 private GridViewWithHeaderAndFooter mGridView;
77 private View mGridFooterView;
78
79 private ListAdapter mAdapter;
80
81
82 protected void setListAdapter(ListAdapter listAdapter) {
83 mAdapter = listAdapter;
84 mCurrentListView.setAdapter(listAdapter);
85 mCurrentListView.invalidate();
86 }
87
88 protected AbsListView getListView() {
89 return mCurrentListView;
90 }
91
92
93 protected void switchToGridView() {
94 if ((mCurrentListView == mListView)) {
95
96 mListView.setAdapter(null);
97 mRefreshListLayout.setVisibility(View.GONE);
98
99 if (mAdapter instanceof FileListListAdapter) {
100 ((FileListListAdapter) mAdapter).setGridMode(true);
101 }
102 mGridView.setAdapter(mAdapter);
103 mRefreshGridLayout.setVisibility(View.VISIBLE);
104
105 mCurrentListView = mGridView;
106 }
107 }
108
109 protected void switchToListView() {
110 if (mCurrentListView == mGridView) {
111 mGridView.setAdapter(null);
112 mRefreshGridLayout.setVisibility(View.GONE);
113
114 if (mAdapter instanceof FileListListAdapter) {
115 ((FileListListAdapter) mAdapter).setGridMode(false);
116 }
117 mListView.setAdapter(mAdapter);
118 mRefreshListLayout.setVisibility(View.VISIBLE);
119
120 mCurrentListView = mListView;
121 }
122 }
123
124
125 @Override
126 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
127 Log_OC.d(TAG, "onCreateView");
128
129 View v = inflater.inflate(R.layout.list_fragment, null);
130
131 mListView = (ExtendedListView)(v.findViewById(R.id.list_root));
132 mListView.setOnItemClickListener(this);
133 mListFooterView = inflater.inflate(R.layout.list_footer, null, false);
134
135 mGridView = (GridViewWithHeaderAndFooter) (v.findViewById(R.id.grid_root));
136 mGridView.setNumColumns(GridView.AUTO_FIT);
137 mGridView.setOnItemClickListener(this);
138 mGridFooterView = inflater.inflate(R.layout.list_footer, null, false);
139
140 if (savedInstanceState != null) {
141 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
142 mListView.setAndCenterSelection(referencePosition);
143 mGridView.setSelection(referencePosition);
144 }
145
146 // Pull-down to refresh layout
147 mRefreshListLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_list);
148 mRefreshGridLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_grid);
149 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_empty);
150 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
151
152 onCreateSwipeToRefresh(mRefreshListLayout);
153 onCreateSwipeToRefresh(mRefreshGridLayout);
154 onCreateSwipeToRefresh(mRefreshEmptyLayout);
155
156 mListView.setEmptyView(mRefreshEmptyLayout);
157 mGridView.setEmptyView(mRefreshEmptyLayout);
158
159 mCurrentListView = mListView; // list as default
160
161 return v;
162 }
163
164 /**
165 * {@inheritDoc}
166 */
167 @Override
168 public void onActivityCreated(Bundle savedInstanceState) {
169 super.onActivityCreated(savedInstanceState);
170
171 if (savedInstanceState != null) {
172 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
173 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
174 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
175 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
176 setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
177
178 } else {
179 mIndexes = new ArrayList<Integer>();
180 mFirstPositions = new ArrayList<Integer>();
181 mTops = new ArrayList<Integer>();
182 mHeightCell = 0;
183 }
184 }
185
186
187 @Override
188 public void onSaveInstanceState(Bundle savedInstanceState) {
189 super.onSaveInstanceState(savedInstanceState);
190 Log_OC.d(TAG, "onSaveInstanceState()");
191 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
192 savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
193 savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
194 savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
195 savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
196 savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
197 }
198
199 /**
200 * Calculates the position of the item that will be used as a reference to
201 * reposition the visible items in the list when the device is turned to
202 * other position.
203 *
204 * The current policy is take as a reference the visible item in the center
205 * of the screen.
206 *
207 * @return The position in the list of the visible item in the center of the
208 * screen.
209 */
210 protected int getReferencePosition() {
211 if (mCurrentListView != null) {
212 return (mCurrentListView.getFirstVisiblePosition() + mCurrentListView.getLastVisiblePosition()) / 2;
213 } else {
214 return 0;
215 }
216 }
217
218
219 /*
220 * Restore index and position
221 */
222 protected void restoreIndexAndTopPosition() {
223 if (mIndexes.size() > 0) {
224 // needs to be checked; not every browse-up had a browse-down before
225
226 int index = mIndexes.remove(mIndexes.size() - 1);
227 final int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
228 int top = mTops.remove(mTops.size() - 1);
229
230 Log_OC.d(TAG, "Setting selection to position: " + firstPosition + "; top: " + top + "; index: " + index);
231
232 if (mCurrentListView == mListView) {
233 if (mHeightCell*index <= mListView.getHeight()) {
234 mListView.setSelectionFromTop(firstPosition, top);
235 } else {
236 mListView.setSelectionFromTop(index, 0);
237 }
238
239 } else {
240 if (mHeightCell*index <= mGridView.getHeight()) {
241 mGridView.setSelection(firstPosition);
242 //mGridView.smoothScrollToPosition(firstPosition);
243 } else {
244 mGridView.setSelection(index);
245 //mGridView.smoothScrollToPosition(index);
246 }
247 }
248
249 }
250 }
251
252 /*
253 * Save index and top position
254 */
255 protected void saveIndexAndTopPosition(int index) {
256
257 mIndexes.add(index);
258
259 int firstPosition = mCurrentListView.getFirstVisiblePosition();
260 mFirstPositions.add(firstPosition);
261
262 View view = mCurrentListView.getChildAt(0);
263 int top = (view == null) ? 0 : view.getTop() ;
264
265 mTops.add(top);
266
267 // Save the height of a cell
268 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
269 }
270
271
272 @Override
273 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
274 // to be @overriden
275 }
276
277 @Override
278 public void onRefresh() {
279 mRefreshListLayout.setRefreshing(false);
280 mRefreshGridLayout.setRefreshing(false);
281 mRefreshEmptyLayout.setRefreshing(false);
282
283 if (mOnRefreshListener != null) {
284 mOnRefreshListener.onRefresh();
285 }
286 }
287 public void setOnRefreshListener(OnEnforceableRefreshListener listener) {
288 mOnRefreshListener = listener;
289 }
290
291
292 /**
293 * Disables swipe gesture.
294 *
295 * Sets the 'enabled' state of the refresh layouts contained in the fragment.
296 *
297 * When 'false' is set, prevents user gestures but keeps the option to refresh programatically,
298 *
299 * @param enabled Desired state for capturing swipe gesture.
300 */
301 public void setSwipeEnabled(boolean enabled) {
302 mRefreshListLayout.setEnabled(enabled);
303 mRefreshGridLayout.setEnabled(enabled);
304 mRefreshEmptyLayout.setEnabled(enabled);
305 }
306
307 /**
308 * Set message for empty list view
309 */
310 public void setMessageForEmptyList(String message) {
311 if (mEmptyListMessage != null) {
312 mEmptyListMessage.setText(message);
313 }
314 }
315
316 /**
317 * Get the text of EmptyListMessage TextView
318 *
319 * @return String
320 */
321 public String getEmptyViewText() {
322 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
323 }
324
325 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
326 // Colors in animations: background
327 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
328 R.color.background_color);
329
330 refreshLayout.setOnRefreshListener(this);
331 }
332
333 @Override
334 public void onRefresh(boolean ignoreETag) {
335 mRefreshListLayout.setRefreshing(false);
336 mRefreshGridLayout.setRefreshing(false);
337 mRefreshEmptyLayout.setRefreshing(false);
338
339 if (mOnRefreshListener != null) {
340 mOnRefreshListener.onRefresh(ignoreETag);
341 }
342 }
343
344
345 protected void setChoiceMode(int choiceMode) {
346 mListView.setChoiceMode(choiceMode);
347 mGridView.setChoiceMode(choiceMode);
348 }
349
350 protected void registerForContextMenu() {
351 registerForContextMenu(mListView);
352 registerForContextMenu(mGridView);
353 mListView.setOnCreateContextMenuListener(this);
354 mGridView.setOnCreateContextMenuListener(this);
355 }
356
357 /**
358 * TODO doc
359 * To be called before setAdapter, or GridViewWithHeaderAndFooter will throw an exception
360 *
361 * @param enabled
362 */
363 protected void setFooterEnabled(boolean enabled) {
364 if (enabled) {
365 if (mGridView.getFooterViewCount() == 0) {
366 if (mGridFooterView.getParent() != null ) {
367 ((ViewGroup) mGridFooterView.getParent()).removeView(mGridFooterView);
368 }
369 mGridView.addFooterView(mGridFooterView, null, false);
370 }
371 mGridFooterView.invalidate();
372
373 if (mListView.getFooterViewsCount() == 0) {
374 if (mListFooterView.getParent() != null ) {
375 ((ViewGroup) mListFooterView.getParent()).removeView(mListFooterView);
376 }
377 mListView.addFooterView(mListFooterView, null, false);
378 }
379 mListFooterView.invalidate();
380
381 } else {
382 mGridView.removeFooterView(mGridFooterView);
383 mListView.removeFooterView(mListFooterView);
384 }
385 }
386
387 /**
388 * TODO doc
389 * @param text
390 */
391 protected void setFooterText(String text) {
392 if (text != null && text.length() > 0) {
393 ((TextView)mListFooterView.findViewById(R.id.footerText)).setText(text);
394 ((TextView)mGridFooterView.findViewById(R.id.footerText)).setText(text);
395 setFooterEnabled(true);
396
397 } else {
398 setFooterEnabled(false);
399 }
400 }
401
402 }