1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2015 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.ui
.fragment
;
21 import java
.util
.ArrayList
;
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
;
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
;
43 import third_parties
.in.srain
.cube
.GridViewWithHeaderAndFooter
;
46 * TODO extending SherlockListFragment instead of SherlockFragment
48 public class ExtendedListFragment
extends SherlockFragment
49 implements OnItemClickListener
, OnEnforceableRefreshListener
{
51 private static final String TAG
= ExtendedListFragment
.class.getSimpleName();
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";
60 private SwipeRefreshLayout mRefreshListLayout
;
61 private SwipeRefreshLayout mRefreshGridLayout
;
62 private SwipeRefreshLayout mRefreshEmptyLayout
;
63 private TextView mEmptyListMessage
;
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;
71 private OnEnforceableRefreshListener mOnRefreshListener
= null
;
73 protected AbsListView mCurrentListView
;
74 private ExtendedListView mListView
;
75 private View mListFooterView
;
76 private GridViewWithHeaderAndFooter mGridView
;
77 private View mGridFooterView
;
79 private ListAdapter mAdapter
;
82 protected void setListAdapter(ListAdapter listAdapter
) {
83 mAdapter
= listAdapter
;
84 mCurrentListView
.setAdapter(listAdapter
);
85 mCurrentListView
.invalidate();
88 protected AbsListView
getListView() {
89 return mCurrentListView
;
93 protected void switchToGridView() {
94 if ((mCurrentListView
== mListView
)) {
96 mListView
.setAdapter(null
);
97 mRefreshListLayout
.setVisibility(View
.GONE
);
99 if (mAdapter
instanceof FileListListAdapter
) {
100 ((FileListListAdapter
) mAdapter
).setGridMode(true
);
102 mGridView
.setAdapter(mAdapter
);
103 mRefreshGridLayout
.setVisibility(View
.VISIBLE
);
105 mCurrentListView
= mGridView
;
109 protected void switchToListView() {
110 if (mCurrentListView
== mGridView
) {
111 mGridView
.setAdapter(null
);
112 mRefreshGridLayout
.setVisibility(View
.GONE
);
114 if (mAdapter
instanceof FileListListAdapter
) {
115 ((FileListListAdapter
) mAdapter
).setGridMode(false
);
117 mListView
.setAdapter(mAdapter
);
118 mRefreshListLayout
.setVisibility(View
.VISIBLE
);
120 mCurrentListView
= mListView
;
126 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
127 Log_OC
.d(TAG
, "onCreateView");
129 View v
= inflater
.inflate(R
.layout
.list_fragment
, null
);
131 mListView
= (ExtendedListView
)(v
.findViewById(R
.id
.list_root
));
132 mListView
.setOnItemClickListener(this);
133 mListFooterView
= inflater
.inflate(R
.layout
.list_footer
, null
, false
);
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
);
140 if (savedInstanceState
!= null
) {
141 int referencePosition
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
);
142 mListView
.setAndCenterSelection(referencePosition
);
143 mGridView
.setSelection(referencePosition
);
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
);
152 onCreateSwipeToRefresh(mRefreshListLayout
);
153 onCreateSwipeToRefresh(mRefreshGridLayout
);
154 onCreateSwipeToRefresh(mRefreshEmptyLayout
);
156 mListView
.setEmptyView(mRefreshEmptyLayout
);
157 mGridView
.setEmptyView(mRefreshEmptyLayout
);
159 mCurrentListView
= mListView
; // list as default
168 public void onActivityCreated(Bundle savedInstanceState
) {
169 super.onActivityCreated(savedInstanceState
);
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
));
179 mIndexes
= new ArrayList
<Integer
>();
180 mFirstPositions
= new ArrayList
<Integer
>();
181 mTops
= new ArrayList
<Integer
>();
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());
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
204 * The current policy is take as a reference the visible item in the center
207 * @return The position in the list of the visible item in the center of the
210 protected int getReferencePosition() {
211 if (mCurrentListView
!= null
) {
212 return (mCurrentListView
.getFirstVisiblePosition() + mCurrentListView
.getLastVisiblePosition()) / 2;
220 * Restore index and position
222 protected void restoreIndexAndTopPosition() {
223 if (mIndexes
.size() > 0) {
224 // needs to be checked; not every browse-up had a browse-down before
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);
230 Log_OC
.d(TAG
, "Setting selection to position: " + firstPosition
+ "; top: " + top
+ "; index: " + index
);
232 if (mCurrentListView
== mListView
) {
233 if (mHeightCell
*index
<= mListView
.getHeight()) {
234 mListView
.setSelectionFromTop(firstPosition
, top
);
236 mListView
.setSelectionFromTop(index
, 0);
240 if (mHeightCell
*index
<= mGridView
.getHeight()) {
241 mGridView
.setSelection(firstPosition
);
242 //mGridView.smoothScrollToPosition(firstPosition);
244 mGridView
.setSelection(index
);
245 //mGridView.smoothScrollToPosition(index);
253 * Save index and top position
255 protected void saveIndexAndTopPosition(int index
) {
259 int firstPosition
= mCurrentListView
.getFirstVisiblePosition();
260 mFirstPositions
.add(firstPosition
);
262 View view
= mCurrentListView
.getChildAt(0);
263 int top
= (view
== null
) ?
0 : view
.getTop() ;
267 // Save the height of a cell
268 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
273 public void onItemClick (AdapterView
<?
> parent
, View view
, int position
, long id
) {
278 public void onRefresh() {
279 mRefreshListLayout
.setRefreshing(false
);
280 mRefreshGridLayout
.setRefreshing(false
);
281 mRefreshEmptyLayout
.setRefreshing(false
);
283 if (mOnRefreshListener
!= null
) {
284 mOnRefreshListener
.onRefresh();
287 public void setOnRefreshListener(OnEnforceableRefreshListener listener
) {
288 mOnRefreshListener
= listener
;
293 * Disables swipe gesture.
295 * Sets the 'enabled' state of the refresh layouts contained in the fragment.
297 * When 'false' is set, prevents user gestures but keeps the option to refresh programatically,
299 * @param enabled Desired state for capturing swipe gesture.
301 public void setSwipeEnabled(boolean enabled
) {
302 mRefreshListLayout
.setEnabled(enabled
);
303 mRefreshGridLayout
.setEnabled(enabled
);
304 mRefreshEmptyLayout
.setEnabled(enabled
);
308 * Set message for empty list view
310 public void setMessageForEmptyList(String message
) {
311 if (mEmptyListMessage
!= null
) {
312 mEmptyListMessage
.setText(message
);
317 * Get the text of EmptyListMessage TextView
321 public String
getEmptyViewText() {
322 return (mEmptyListMessage
!= null
) ? mEmptyListMessage
.getText().toString() : "";
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
);
330 refreshLayout
.setOnRefreshListener(this);
334 public void onRefresh(boolean ignoreETag
) {
335 mRefreshListLayout
.setRefreshing(false
);
336 mRefreshGridLayout
.setRefreshing(false
);
337 mRefreshEmptyLayout
.setRefreshing(false
);
339 if (mOnRefreshListener
!= null
) {
340 mOnRefreshListener
.onRefresh(ignoreETag
);
345 protected void setChoiceMode(int choiceMode
) {
346 mListView
.setChoiceMode(choiceMode
);
347 mGridView
.setChoiceMode(choiceMode
);
350 protected void registerForContextMenu() {
351 registerForContextMenu(mListView
);
352 registerForContextMenu(mGridView
);
353 mListView
.setOnCreateContextMenuListener(this);
354 mGridView
.setOnCreateContextMenuListener(this);
359 * To be called before setAdapter, or GridViewWithHeaderAndFooter will throw an exception
363 protected void setFooterEnabled(boolean enabled
) {
365 if (mGridView
.getFooterViewCount() == 0) {
366 if (mGridFooterView
.getParent() != null
) {
367 ((ViewGroup
) mGridFooterView
.getParent()).removeView(mGridFooterView
);
369 mGridView
.addFooterView(mGridFooterView
, null
, false
);
371 mGridFooterView
.invalidate();
373 if (mListView
.getFooterViewsCount() == 0) {
374 if (mListFooterView
.getParent() != null
) {
375 ((ViewGroup
) mListFooterView
.getParent()).removeView(mListFooterView
);
377 mListView
.addFooterView(mListFooterView
, null
, false
);
379 mListFooterView
.invalidate();
382 mGridView
.removeFooterView(mGridFooterView
);
383 mListView
.removeFooterView(mListFooterView
);
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
);
398 setFooterEnabled(false
);