- imageview gets updated when thumbnail generation is finished
[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 protected void switchImageView(){
82 imageView.setNumColumns(GridView.AUTO_FIT);
83 imageView.invalidate();
84 }
85
86 protected void switchFileView(){
87 imageView.setNumColumns(1);
88 imageView.invalidate();
89 }
90
91
92 @Override
93 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
94 Log_OC.e(TAG, "onCreateView");
95
96 View v = inflater.inflate(R.layout.list_fragment, null);
97
98 // imageView = (GridView) v.findViewById(R.id.grid_list_view);
99 // imageView.setOnItemClickListener(this);
100
101 // mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
102 imageView = (ExtendedListView)(v.findViewById(R.id.list_root));
103 imageView.setOnItemClickListener(this);
104
105 //mList.set
106 //mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
107 //mList.setDividerHeight(1);
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 // mList.setEmptyView(mRefreshEmptyLayout);
122
123 return v;
124 }
125
126
127 /**
128 * {@inheritDoc}
129 */
130 @Override
131 public void onActivityCreated(Bundle savedInstanceState) {
132 super.onActivityCreated(savedInstanceState);
133
134 if (savedInstanceState != null) {
135 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
136 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
137 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
138 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
139 setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
140
141 } else {
142 mIndexes = new ArrayList<Integer>();
143 mFirstPositions = new ArrayList<Integer>();
144 mTops = new ArrayList<Integer>();
145 mHeightCell = 0;
146 }
147 }
148
149
150 @Override
151 public void onSaveInstanceState(Bundle savedInstanceState) {
152 super.onSaveInstanceState(savedInstanceState);
153 Log_OC.e(TAG, "onSaveInstanceState()");
154 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
155 savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
156 savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
157 savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
158 savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
159 savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
160 }
161
162
163 /**
164 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
165 * the device is turned to other position.
166 *
167 * THe current policy is take as a reference the visible item in the center of the screen.
168 *
169 * @return The position in the list of the visible item in the center of the screen.
170 */
171 protected int getReferencePosition() {
172 if (imageView != null) {
173 return (imageView.getFirstVisiblePosition() + imageView.getLastVisiblePosition()) / 2;
174 } else {
175 return 0;
176 }
177 }
178
179
180 /**
181 * Sets the visible part of the list from the reference position.
182 *
183 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
184 */
185 protected void setReferencePosition(int position) {
186 if (imageView != null) {
187 imageView.setSelection(position);
188 }
189 }
190
191
192 /*
193 * Restore index and position
194 */
195 protected void restoreIndexAndTopPosition() {
196 if (mIndexes.size() > 0) {
197 // needs to be checked; not every browse-up had a browse-down before
198
199 int index = mIndexes.remove(mIndexes.size() - 1);
200
201 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
202
203 int top = mTops.remove(mTops.size() - 1);
204
205 imageView.smoothScrollToPosition(firstPosition);
206
207 // Move the scroll if the selection is not visible
208 int indexPosition = mHeightCell*index;
209 int height = imageView.getHeight();
210
211 if (indexPosition > height) {
212 imageView.smoothScrollToPosition(index);
213 }
214 }
215 }
216
217 /*
218 * Save index and top position
219 */
220 protected void saveIndexAndTopPosition(int index) {
221
222 mIndexes.add(index);
223
224 int firstPosition = imageView.getFirstVisiblePosition();
225 mFirstPositions.add(firstPosition);
226
227 View view = imageView.getChildAt(0);
228 int top = (view == null) ? 0 : view.getTop() ;
229
230 mTops.add(top);
231
232 // Save the height of a cell
233 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
234 }
235
236
237 @Override
238 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
239 // to be @overriden
240 }
241
242 @Override
243 public void onRefresh() {
244 // to be @overriden
245 mRefreshLayout.setRefreshing(false);
246 mRefreshEmptyLayout.setRefreshing(false);
247
248 if (mOnRefreshListener != null) {
249 mOnRefreshListener.onRefresh();
250 }
251 }
252
253 public void setOnRefreshListener(OnEnforceableRefreshListener listener) {
254 mOnRefreshListener = listener;
255 }
256
257
258 /**
259 * Enables swipe gesture
260 */
261 public void enableSwipe() {
262 mRefreshLayout.setEnabled(true);
263 }
264
265 /**
266 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
267 * refreshing programmatically.
268 */
269 public void disableSwipe() {
270 mRefreshLayout.setEnabled(false);
271 }
272
273 /**
274 * It shows the SwipeRefreshLayout progress
275 */
276 public void showSwipeProgress() {
277 mRefreshLayout.setRefreshing(true);
278 }
279
280 /**
281 * It shows the SwipeRefreshLayout progress
282 */
283 public void hideSwipeProgress() {
284 mRefreshLayout.setRefreshing(false);
285 }
286
287 /**
288 * Set message for empty list view
289 */
290 public void setMessageForEmptyList(String message) {
291 if (mEmptyListMessage != null) {
292 mEmptyListMessage.setText(message);
293 }
294 }
295
296 /**
297 * Get the text of EmptyListMessage TextView
298 *
299 * @return String
300 */
301 public String getEmptyViewText() {
302 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
303 }
304
305 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
306 // Colors in animations: background
307 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
308 R.color.background_color);
309
310 refreshLayout.setOnRefreshListener(this);
311 }
312
313 @Override
314 public void onRefresh(boolean ignoreETag) {
315 mRefreshLayout.setRefreshing(false);
316 mRefreshEmptyLayout.setRefreshing(false);
317
318 if (mOnRefreshListener != null) {
319 mOnRefreshListener.onRefresh(ignoreETag);
320 }
321 }
322 }