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
.ui
.ExtendedListView
;
37 import com
.owncloud
.android
.utils
.Log_OC
;
40 * TODO extending SherlockListFragment instead of SherlockFragment
42 public class ExtendedListFragment
extends SherlockFragment
43 implements OnItemClickListener
, SwipeRefreshLayout
.OnRefreshListener
{
45 private static final String TAG
= ExtendedListFragment
.class.getSimpleName();
47 private static final String KEY_SAVED_LIST_POSITION
= "SAVED_LIST_POSITION";
48 private static final String KEY_INDEXES
= "INDEXES";
49 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
50 private static final String KEY_TOPS
= "TOPS";
51 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
52 private static final String KEY_EMPTY_LIST_MESSAGE
= "EMPTY_LIST_MESSAGE";
54 protected ExtendedListView mList
;
56 private SwipeRefreshLayout mRefreshLayout
;
57 private SwipeRefreshLayout mRefreshEmptyLayout
;
58 private TextView mEmptyListMessage
;
60 // Save the state of the scroll in browsing
61 private ArrayList
<Integer
> mIndexes
;
62 private ArrayList
<Integer
> mFirstPositions
;
63 private ArrayList
<Integer
> mTops
;
64 private int mHeightCell
= 0;
66 private SwipeRefreshLayout
.OnRefreshListener mOnRefreshListener
= null
;
69 public void setListAdapter(ListAdapter listAdapter
) {
70 mList
.setAdapter(listAdapter
);
74 public ListView
getListView() {
80 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
81 Log_OC
.e(TAG
, "onCreateView");
83 View v
= inflater
.inflate(R
.layout
.list_fragment
, null
);
84 mEmptyListMessage
= (TextView
) v
.findViewById(R
.id
.empty_list_view
);
85 mList
= (ExtendedListView
)(v
.findViewById(R
.id
.list_root
));
86 mList
.setOnItemClickListener(this);
88 mList
.setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
89 mList
.setDividerHeight(1);
91 if (savedInstanceState
!= null
) {
92 int referencePosition
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
);
93 setReferencePosition(referencePosition
);
97 mRefreshLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files
);
98 mRefreshEmptyLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files_emptyView
);
100 onCreateSwipeToRefresh(mRefreshLayout
);
101 onCreateSwipeToRefresh(mRefreshEmptyLayout
);
103 mList
.setEmptyView(mRefreshEmptyLayout
);
113 public void onActivityCreated(Bundle savedInstanceState
) {
114 super.onActivityCreated(savedInstanceState
);
116 if (savedInstanceState
!= null
) {
117 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
118 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
119 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
120 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
121 setMessageForEmptyList(savedInstanceState
.getString(KEY_EMPTY_LIST_MESSAGE
));
124 mIndexes
= new ArrayList
<Integer
>();
125 mFirstPositions
= new ArrayList
<Integer
>();
126 mTops
= new ArrayList
<Integer
>();
133 public void onSaveInstanceState(Bundle savedInstanceState
) {
134 super.onSaveInstanceState(savedInstanceState
);
135 Log_OC
.e(TAG
, "onSaveInstanceState()");
136 savedInstanceState
.putInt(KEY_SAVED_LIST_POSITION
, getReferencePosition());
137 savedInstanceState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
138 savedInstanceState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
139 savedInstanceState
.putIntegerArrayList(KEY_TOPS
, mTops
);
140 savedInstanceState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
141 savedInstanceState
.putString(KEY_EMPTY_LIST_MESSAGE
, getEmptyViewText());
146 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
147 * the device is turned to other position.
149 * THe current policy is take as a reference the visible item in the center of the screen.
151 * @return The position in the list of the visible item in the center of the screen.
153 protected int getReferencePosition() {
155 return (mList
.getFirstVisiblePosition() + mList
.getLastVisiblePosition()) / 2;
163 * Sets the visible part of the list from the reference position.
165 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
167 protected void setReferencePosition(int position
) {
169 mList
.setAndCenterSelection(position
);
175 * Restore index and position
177 protected void restoreIndexAndTopPosition() {
178 if (mIndexes
.size() > 0) {
179 // needs to be checked; not every browse-up had a browse-down before
181 int index
= mIndexes
.remove(mIndexes
.size() - 1);
183 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
185 int top
= mTops
.remove(mTops
.size() - 1);
187 mList
.setSelectionFromTop(firstPosition
, top
);
189 // Move the scroll if the selection is not visible
190 int indexPosition
= mHeightCell
*index
;
191 int height
= mList
.getHeight();
193 if (indexPosition
> height
) {
194 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
196 mList
.smoothScrollToPosition(index
);
198 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
200 mList
.setSelectionFromTop(index
, 0);
208 * Save index and top position
210 protected void saveIndexAndTopPosition(int index
) {
214 int firstPosition
= mList
.getFirstVisiblePosition();
215 mFirstPositions
.add(firstPosition
);
217 View view
= mList
.getChildAt(0);
218 int top
= (view
== null
) ?
0 : view
.getTop() ;
222 // Save the height of a cell
223 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
228 public void onItemClick (AdapterView
<?
> parent
, View view
, int position
, long id
) {
233 public void onRefresh() {
235 mRefreshLayout
.setRefreshing(false
);
236 mRefreshEmptyLayout
.setRefreshing(false
);
238 if (mOnRefreshListener
!= null
) {
239 mOnRefreshListener
.onRefresh();
243 public void setOnRefreshListener(SwipeRefreshLayout
.OnRefreshListener listener
) {
244 mOnRefreshListener
= listener
;
249 * Enables swipe gesture
251 public void enableSwipe() {
252 mRefreshLayout
.setEnabled(true
);
256 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
257 * refreshing programmatically.
259 public void disableSwipe() {
260 mRefreshLayout
.setEnabled(false
);
264 * It shows the SwipeRefreshLayout progress
266 public void showSwipeProgress() {
267 mRefreshLayout
.setRefreshing(true
);
271 * It shows the SwipeRefreshLayout progress
273 public void hideSwipeProgress() {
274 mRefreshLayout
.setRefreshing(false
);
278 * Set message for empty list view
280 public void setMessageForEmptyList(String message
) {
281 if (mEmptyListMessage
!= null
) {
282 mEmptyListMessage
.setText(message
);
287 * Get the text of EmptyListMessage TextView
291 public String
getEmptyViewText() {
292 return (mEmptyListMessage
!= null
) ? mEmptyListMessage
.getText().toString() : "";
295 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout
) {
296 // Colors in animations: background
297 refreshLayout
.setColorScheme(R
.color
.background_color
, R
.color
.background_color
, R
.color
.background_color
,
298 R
.color
.background_color
);
300 refreshLayout
.setOnRefreshListener(this);