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