34b00fa84612a52be3b893099de2be9f932d2843
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ExtendedListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.ui.fragment;
20
21 import java.util.ArrayList;
22
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;
33
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;
38
39 /**
40 * TODO extending SherlockListFragment instead of SherlockFragment
41 */
42 public class ExtendedListFragment extends SherlockFragment implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{
43
44 private static final String TAG = ExtendedListFragment.class.getSimpleName();
45
46 private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
47 private static final String KEY_INDEXES = "INDEXES";
48 private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
49 private static final String KEY_TOPS = "TOPS";
50 private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
51 private static final String KEY_EMPTY_LIST_MESSAGE = "EMPTY_LIST_MESSAGE";
52
53 protected ExtendedListView mList;
54
55 private SwipeRefreshLayout mRefreshLayout;
56 private SwipeRefreshLayout mRefreshEmptyLayout;
57 private TextView mEmptyListMessage;
58
59 // Save the state of the scroll in browsing
60 private ArrayList<Integer> mIndexes;
61 private ArrayList<Integer> mFirstPositions;
62 private ArrayList<Integer> mTops;
63 private int mHeightCell = 0;
64
65
66 public void setListAdapter(ListAdapter listAdapter) {
67 mList.setAdapter(listAdapter);
68 mList.invalidate();
69 }
70
71 public ListView getListView() {
72 return mList;
73 }
74
75
76 @Override
77 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
78 Log_OC.e(TAG, "onCreateView");
79
80 View v = inflater.inflate(R.layout.list_fragment, null);
81 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
82 mList = (ExtendedListView)(v.findViewById(R.id.list_root));
83 mList.setOnItemClickListener(this);
84
85 mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
86 mList.setDividerHeight(1);
87
88 if (savedInstanceState != null) {
89 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
90 setReferencePosition(referencePosition);
91 }
92
93 // Pull down refresh
94 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
95 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
96
97 onCreateSwipeToRefresh(mRefreshLayout);
98 onCreateSwipeToRefresh(mRefreshEmptyLayout);
99
100 mList.setEmptyView(mRefreshEmptyLayout);
101
102 return v;
103 }
104
105
106 /**
107 * {@inheritDoc}
108 */
109 @Override
110 public void onActivityCreated(Bundle savedInstanceState) {
111 super.onActivityCreated(savedInstanceState);
112
113 if (savedInstanceState != null) {
114 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
115 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
116 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
117 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
118 setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
119
120 } else {
121 mIndexes = new ArrayList<Integer>();
122 mFirstPositions = new ArrayList<Integer>();
123 mTops = new ArrayList<Integer>();
124 mHeightCell = 0;
125 }
126 }
127
128
129 @Override
130 public void onSaveInstanceState(Bundle savedInstanceState) {
131 super.onSaveInstanceState(savedInstanceState);
132 Log_OC.e(TAG, "onSaveInstanceState()");
133 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
134 savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
135 savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
136 savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
137 savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
138 savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
139 }
140
141
142 /**
143 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
144 * the device is turned to other position.
145 *
146 * THe current policy is take as a reference the visible item in the center of the screen.
147 *
148 * @return The position in the list of the visible item in the center of the screen.
149 */
150 protected int getReferencePosition() {
151 if (mList != null) {
152 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
153 } else {
154 return 0;
155 }
156 }
157
158
159 /**
160 * Sets the visible part of the list from the reference position.
161 *
162 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
163 */
164 protected void setReferencePosition(int position) {
165 if (mList != null) {
166 mList.setAndCenterSelection(position);
167 }
168 }
169
170
171 /*
172 * Restore index and position
173 */
174 protected void restoreIndexAndTopPosition() {
175 if (mIndexes.size() > 0) {
176 // needs to be checked; not every browse-up had a browse-down before
177
178 int index = mIndexes.remove(mIndexes.size() - 1);
179
180 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
181
182 int top = mTops.remove(mTops.size() - 1);
183
184 mList.setSelectionFromTop(firstPosition, top);
185
186 // Move the scroll if the selection is not visible
187 int indexPosition = mHeightCell*index;
188 int height = mList.getHeight();
189
190 if (indexPosition > height) {
191 if (android.os.Build.VERSION.SDK_INT >= 11)
192 {
193 mList.smoothScrollToPosition(index);
194 }
195 else if (android.os.Build.VERSION.SDK_INT >= 8)
196 {
197 mList.setSelectionFromTop(index, 0);
198 }
199
200 }
201 }
202 }
203
204 /*
205 * Save index and top position
206 */
207 protected void saveIndexAndTopPosition(int index) {
208
209 mIndexes.add(index);
210
211 int firstPosition = mList.getFirstVisiblePosition();
212 mFirstPositions.add(firstPosition);
213
214 View view = mList.getChildAt(0);
215 int top = (view == null) ? 0 : view.getTop() ;
216
217 mTops.add(top);
218
219 // Save the height of a cell
220 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
221 }
222
223
224 @Override
225 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
226 // to be @overriden
227 }
228
229 @Override
230 public void onRefresh() {
231 // to be @overriden
232 mRefreshLayout.setRefreshing(false);
233 mRefreshEmptyLayout.setRefreshing(false);
234 }
235
236 /**
237 * Enables swipe gesture
238 */
239 public void enableSwipe() {
240 mRefreshLayout.setEnabled(true);
241 }
242
243 /**
244 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
245 * refreshing programmatically.
246 */
247 public void disableSwipe() {
248 mRefreshLayout.setEnabled(false);
249 }
250
251 /**
252 * It shows the SwipeRefreshLayout progress
253 */
254 public void showSwipeProgress() {
255 mRefreshLayout.setRefreshing(true);
256 }
257
258 /**
259 * It shows the SwipeRefreshLayout progress
260 */
261 public void hideSwipeProgress() {
262 mRefreshLayout.setRefreshing(false);
263 }
264
265 /**
266 * Set message for empty list view
267 */
268 public void setMessageForEmptyList(String message) {
269 if (mEmptyListMessage != null) {
270 mEmptyListMessage.setText(message);
271 }
272 }
273
274 /**
275 * Get the text of EmptyListMessage TextView
276 *
277 * @return String
278 */
279 public String getEmptyViewText() {
280 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
281 }
282
283 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
284 // Colors in animations: background
285 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
286 R.color.background_color);
287
288 refreshLayout.setOnRefreshListener(this);
289 }
290
291 }