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