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