- GridLayout
[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.setSelection(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 if (android.os.Build.VERSION.SDK_INT >= 11)
212 {
213 imageView.smoothScrollToPosition(index);
214 }
215 else if (android.os.Build.VERSION.SDK_INT >= 8)
216 {
217 imageView.setSelection(index);
218 }
219
220 }
221 }
222 }
223
224 /*
225 * Save index and top position
226 */
227 protected void saveIndexAndTopPosition(int index) {
228
229 mIndexes.add(index);
230
231 int firstPosition = imageView.getFirstVisiblePosition();
232 mFirstPositions.add(firstPosition);
233
234 View view = imageView.getChildAt(0);
235 int top = (view == null) ? 0 : view.getTop() ;
236
237 mTops.add(top);
238
239 // Save the height of a cell
240 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
241 }
242
243
244 @Override
245 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
246 // to be @overriden
247 }
248
249 @Override
250 public void onRefresh() {
251 // to be @overriden
252 mRefreshLayout.setRefreshing(false);
253 mRefreshEmptyLayout.setRefreshing(false);
254
255 if (mOnRefreshListener != null) {
256 mOnRefreshListener.onRefresh();
257 }
258 }
259
260 public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
261 mOnRefreshListener = listener;
262 }
263
264
265 /**
266 * Enables swipe gesture
267 */
268 public void enableSwipe() {
269 mRefreshLayout.setEnabled(true);
270 }
271
272 /**
273 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
274 * refreshing programmatically.
275 */
276 public void disableSwipe() {
277 mRefreshLayout.setEnabled(false);
278 }
279
280 /**
281 * It shows the SwipeRefreshLayout progress
282 */
283 public void showSwipeProgress() {
284 mRefreshLayout.setRefreshing(true);
285 }
286
287 /**
288 * It shows the SwipeRefreshLayout progress
289 */
290 public void hideSwipeProgress() {
291 mRefreshLayout.setRefreshing(false);
292 }
293
294 /**
295 * Set message for empty list view
296 */
297 public void setMessageForEmptyList(String message) {
298 if (mEmptyListMessage != null) {
299 mEmptyListMessage.setText(message);
300 }
301 }
302
303 /**
304 * Get the text of EmptyListMessage TextView
305 *
306 * @return String
307 */
308 public String getEmptyViewText() {
309 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
310 }
311
312 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
313 // Colors in animations: background
314 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
315 R.color.background_color);
316
317 refreshLayout.setOnRefreshListener(this);
318 }
319
320 }