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