1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 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
.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
;
34 import com
.actionbarsherlock
.app
.SherlockFragment
;
35 import com
.owncloud
.android
.R
;
36 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
37 import com
.owncloud
.android
.ui
.ExtendedListView
;
38 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
41 * TODO extending SherlockListFragment instead of SherlockFragment
43 public class ExtendedListFragment
extends SherlockFragment
44 implements OnItemClickListener
, OnEnforceableRefreshListener
{
46 private static final String TAG
= ExtendedListFragment
.class.getSimpleName();
48 private static final String KEY_SAVED_LIST_POSITION
= "SAVED_LIST_POSITION";
49 private static final String KEY_INDEXES
= "INDEXES";
50 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
51 private static final String KEY_TOPS
= "TOPS";
52 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
53 private static final String KEY_EMPTY_LIST_MESSAGE
= "EMPTY_LIST_MESSAGE";
55 protected ExtendedListView mList
;
57 private SwipeRefreshLayout mRefreshLayout
;
58 private SwipeRefreshLayout mRefreshEmptyLayout
;
59 private TextView mEmptyListMessage
;
61 // Save the state of the scroll in browsing
62 private ArrayList
<Integer
> mIndexes
;
63 private ArrayList
<Integer
> mFirstPositions
;
64 private ArrayList
<Integer
> mTops
;
65 private int mHeightCell
= 0;
67 private OnEnforceableRefreshListener mOnRefreshListener
= null
;
70 public void setListAdapter(ListAdapter listAdapter
) {
71 mList
.setAdapter(listAdapter
);
75 public void setFooterView(View footer
) {
76 mList
.addFooterView(footer
, null
, false
);
80 public ListView
getListView() {
85 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
86 Log_OC
.e(TAG
, "onCreateView");
88 View v
= inflater
.inflate(R
.layout
.list_fragment
, null
);
89 mEmptyListMessage
= (TextView
) v
.findViewById(R
.id
.empty_list_view
);
90 mList
= (ExtendedListView
) (v
.findViewById(R
.id
.list_root
));
91 mList
.setOnItemClickListener(this);
93 mList
.setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
94 mList
.setDividerHeight(1);
96 if (savedInstanceState
!= null
) {
97 int referencePosition
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
);
98 setReferencePosition(referencePosition
);
102 mRefreshLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files
);
103 mRefreshEmptyLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files_emptyView
);
105 onCreateSwipeToRefresh(mRefreshLayout
);
106 onCreateSwipeToRefresh(mRefreshEmptyLayout
);
108 mList
.setEmptyView(mRefreshEmptyLayout
);
117 public void onActivityCreated(Bundle savedInstanceState
) {
118 super.onActivityCreated(savedInstanceState
);
120 if (savedInstanceState
!= null
) {
121 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
122 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
123 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
124 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
125 setMessageForEmptyList(savedInstanceState
.getString(KEY_EMPTY_LIST_MESSAGE
));
128 mIndexes
= new ArrayList
<Integer
>();
129 mFirstPositions
= new ArrayList
<Integer
>();
130 mTops
= new ArrayList
<Integer
>();
137 public void onSaveInstanceState(Bundle savedInstanceState
) {
138 super.onSaveInstanceState(savedInstanceState
);
139 Log_OC
.e(TAG
, "onSaveInstanceState()");
140 savedInstanceState
.putInt(KEY_SAVED_LIST_POSITION
, getReferencePosition());
141 savedInstanceState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
142 savedInstanceState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
143 savedInstanceState
.putIntegerArrayList(KEY_TOPS
, mTops
);
144 savedInstanceState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
145 savedInstanceState
.putString(KEY_EMPTY_LIST_MESSAGE
, getEmptyViewText());
149 * Calculates the position of the item that will be used as a reference to
150 * reposition the visible items in the list when the device is turned to
153 * THe current policy is take as a reference the visible item in the center
156 * @return The position in the list of the visible item in the center of the
159 protected int getReferencePosition() {
161 return (mList
.getFirstVisiblePosition() + mList
.getLastVisiblePosition()) / 2;
168 * Sets the visible part of the list from the reference position.
170 * @param position Reference position previously returned by
171 * {@link LocalFileListFragment#getReferencePosition()}
173 protected void setReferencePosition(int position
) {
175 mList
.setAndCenterSelection(position
);
181 * Restore index and position
183 protected void restoreIndexAndTopPosition() {
184 if (mIndexes
.size() > 0) {
185 // needs to be checked; not every browse-up had a browse-down before
187 int index
= mIndexes
.remove(mIndexes
.size() - 1);
189 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
191 int top
= mTops
.remove(mTops
.size() - 1);
193 mList
.setSelectionFromTop(firstPosition
, top
);
195 // Move the scroll if the selection is not visible
196 int indexPosition
= mHeightCell
*index
;
197 int height
= mList
.getHeight();
199 if (indexPosition
> height
) {
200 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
202 mList
.smoothScrollToPosition(index
);
204 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
206 mList
.setSelectionFromTop(index
, 0);
214 * Save index and top position
216 protected void saveIndexAndTopPosition(int index
) {
220 int firstPosition
= mList
.getFirstVisiblePosition();
221 mFirstPositions
.add(firstPosition
);
223 View view
= mList
.getChildAt(0);
224 int top
= (view
== null
) ?
0 : view
.getTop() ;
228 // Save the height of a cell
229 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
234 public void onItemClick (AdapterView
<?
> parent
, View view
, int position
, long id
) {
239 public void onRefresh() {
241 mRefreshLayout
.setRefreshing(false
);
242 mRefreshEmptyLayout
.setRefreshing(false
);
244 if (mOnRefreshListener
!= null
) {
245 mOnRefreshListener
.onRefresh();
248 public void setOnRefreshListener(OnEnforceableRefreshListener listener
) {
249 mOnRefreshListener
= listener
;
254 * Enables swipe gesture
256 public void enableSwipe() {
257 mRefreshLayout
.setEnabled(true
);
261 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
262 * refreshing programmatically.
264 public void disableSwipe() {
265 mRefreshLayout
.setEnabled(false
);
269 * It shows the SwipeRefreshLayout progress
271 public void showSwipeProgress() {
272 mRefreshLayout
.setRefreshing(true
);
276 * It shows the SwipeRefreshLayout progress
278 public void hideSwipeProgress() {
279 mRefreshLayout
.setRefreshing(false
);
283 * Set message for empty list view
285 public void setMessageForEmptyList(String message
) {
286 if (mEmptyListMessage
!= null
) {
287 mEmptyListMessage
.setText(message
);
292 * Get the text of EmptyListMessage TextView
296 public String
getEmptyViewText() {
297 return (mEmptyListMessage
!= null
) ? mEmptyListMessage
.getText().toString() : "";
300 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout
) {
301 // Colors in animations: background
302 refreshLayout
.setColorScheme(R
.color
.background_color
, R
.color
.background_color
, R
.color
.background_color
,
303 R
.color
.background_color
);
305 refreshLayout
.setOnRefreshListener(this);
309 public void onRefresh(boolean ignoreETag
) {
310 mRefreshLayout
.setRefreshing(false
);
311 mRefreshEmptyLayout
.setRefreshing(false
);
313 if (mOnRefreshListener
!= null
) {
314 mOnRefreshListener
.onRefresh(ignoreETag
);