3row Grid
[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.ui.ExtendedListView;
38 import com.owncloud.android.utils.Log_OC;
39
40 /**
41 * TODO extending SherlockListFragment instead of SherlockFragment
42 */
43 public class ExtendedListFragment extends SherlockFragment
44 implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
45
46 private static final String TAG = ExtendedListFragment.class.getSimpleName();
47
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";
54
55 // protected ExtendedListView mList;
56
57 private SwipeRefreshLayout mRefreshLayout;
58 private SwipeRefreshLayout mRefreshEmptyLayout;
59 private TextView mEmptyListMessage;
60
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;
66
67 private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = null;
68
69 protected GridView imageView;
70
71 public void setListAdapter(ListAdapter listAdapter) {
72 imageView.setAdapter(listAdapter);
73 imageView.invalidate();
74 }
75
76 public GridView getGridView() {
77 return imageView;
78 }
79
80 protected void switchImageView(){
81 imageView.setNumColumns(GridView.AUTO_FIT);
82 imageView.invalidate();
83 }
84
85 protected void switchFileView(){
86 imageView.setNumColumns(1);
87 imageView.invalidate();
88 }
89
90
91 @Override
92 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
93 Log_OC.e(TAG, "onCreateView");
94
95 View v = inflater.inflate(R.layout.list_fragment, null);
96
97 // imageView = (GridView) v.findViewById(R.id.grid_list_view);
98 // imageView.setOnItemClickListener(this);
99
100 // mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
101 imageView = (ExtendedListView)(v.findViewById(R.id.list_root));
102 imageView.setOnItemClickListener(this);
103
104 //mList.set
105 //mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
106 //mList.setDividerHeight(1);
107
108 if (savedInstanceState != null) {
109 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
110 setReferencePosition(referencePosition);
111 }
112
113 // Pull down refresh
114 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
115 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
116
117 onCreateSwipeToRefresh(mRefreshLayout);
118 onCreateSwipeToRefresh(mRefreshEmptyLayout);
119
120 // mList.setEmptyView(mRefreshEmptyLayout);
121
122 return v;
123 }
124
125
126 /**
127 * {@inheritDoc}
128 */
129 @Override
130 public void onActivityCreated(Bundle savedInstanceState) {
131 super.onActivityCreated(savedInstanceState);
132
133 if (savedInstanceState != null) {
134 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
135 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
136 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
137 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
138 setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
139
140 } else {
141 mIndexes = new ArrayList<Integer>();
142 mFirstPositions = new ArrayList<Integer>();
143 mTops = new ArrayList<Integer>();
144 mHeightCell = 0;
145 }
146 }
147
148
149 @Override
150 public void onSaveInstanceState(Bundle savedInstanceState) {
151 super.onSaveInstanceState(savedInstanceState);
152 Log_OC.e(TAG, "onSaveInstanceState()");
153 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
154 savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
155 savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
156 savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
157 savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
158 savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
159 }
160
161
162 /**
163 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
164 * the device is turned to other position.
165 *
166 * THe current policy is take as a reference the visible item in the center of the screen.
167 *
168 * @return The position in the list of the visible item in the center of the 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 /**
180 * Sets the visible part of the list from the reference position.
181 *
182 * @param position Reference position previously returned by {@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
252 public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
253 mOnRefreshListener = listener;
254 }
255
256
257 /**
258 * Enables swipe gesture
259 */
260 public void enableSwipe() {
261 mRefreshLayout.setEnabled(true);
262 }
263
264 /**
265 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
266 * refreshing programmatically.
267 */
268 public void disableSwipe() {
269 mRefreshLayout.setEnabled(false);
270 }
271
272 /**
273 * It shows the SwipeRefreshLayout progress
274 */
275 public void showSwipeProgress() {
276 mRefreshLayout.setRefreshing(true);
277 }
278
279 /**
280 * It shows the SwipeRefreshLayout progress
281 */
282 public void hideSwipeProgress() {
283 mRefreshLayout.setRefreshing(false);
284 }
285
286 /**
287 * Set message for empty list view
288 */
289 public void setMessageForEmptyList(String message) {
290 if (mEmptyListMessage != null) {
291 mEmptyListMessage.setText(message);
292 }
293 }
294
295 /**
296 * Get the text of EmptyListMessage TextView
297 *
298 * @return String
299 */
300 public String getEmptyViewText() {
301 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
302 }
303
304 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
305 // Colors in animations: background
306 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
307 R.color.background_color);
308
309 refreshLayout.setOnRefreshListener(this);
310 }
311
312 }