1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.ui
.fragment
;
21 import java
.util
.ArrayList
;
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
;
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
;
42 * TODO extending SherlockListFragment instead of SherlockFragment
44 public class ExtendedListFragment
extends SherlockFragment
45 implements OnItemClickListener
, OnEnforceableRefreshListener
{
47 private static final String TAG
= ExtendedListFragment
.class.getSimpleName();
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";
56 protected ExtendedListView mList
;
58 private SwipeRefreshLayout mRefreshLayout
;
59 private SwipeRefreshLayout mRefreshEmptyLayout
;
60 private TextView mEmptyListMessage
;
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;
68 private OnEnforceableRefreshListener mOnRefreshListener
= null
;
70 protected GridView imageView
;
72 public void setListAdapter(ListAdapter listAdapter
) {
73 imageView
.setAdapter(listAdapter
);
74 imageView
.invalidate();
77 public GridView
getGridView() {
81 public void setFooterView(View footer
) {
83 // mList.addFooterView(footer, null, false);
84 // mList.invalidate();
89 protected void switchImageView(){
90 imageView
.setNumColumns(GridView
.AUTO_FIT
);
91 imageView
.invalidate();
94 protected void switchFileView(){
95 imageView
.setNumColumns(1);
96 imageView
.invalidate();
101 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
102 Log_OC
.e(TAG
, "onCreateView");
104 View v
= inflater
.inflate(R
.layout
.list_fragment
, null
);
106 imageView
= (ExtendedListView
)(v
.findViewById(R
.id
.list_root
));
107 imageView
.setOnItemClickListener(this);
109 if (savedInstanceState
!= null
) {
110 int referencePosition
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
);
111 setReferencePosition(referencePosition
);
115 mRefreshLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files
);
116 mRefreshEmptyLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files_emptyView
);
118 onCreateSwipeToRefresh(mRefreshLayout
);
119 onCreateSwipeToRefresh(mRefreshEmptyLayout
);
128 public void onActivityCreated(Bundle savedInstanceState
) {
129 super.onActivityCreated(savedInstanceState
);
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
));
139 mIndexes
= new ArrayList
<Integer
>();
140 mFirstPositions
= new ArrayList
<Integer
>();
141 mTops
= new ArrayList
<Integer
>();
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());
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
164 * THe current policy is take as a reference the visible item in the center
167 * @return The position in the list of the visible item in the center of the
170 protected int getReferencePosition() {
171 if (imageView
!= null
) {
172 return (imageView
.getFirstVisiblePosition() + imageView
.getLastVisiblePosition()) / 2;
179 * Sets the visible part of the list from the reference position.
181 * @param position Reference position previously returned by
182 * {@link LocalFileListFragment#getReferencePosition()}
184 protected void setReferencePosition(int position
) {
185 if (imageView
!= null
) {
186 imageView
.setSelection(position
);
192 * Restore index and position
194 protected void restoreIndexAndTopPosition() {
195 if (mIndexes
.size() > 0) {
196 // needs to be checked; not every browse-up had a browse-down before
198 int index
= mIndexes
.remove(mIndexes
.size() - 1);
200 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
202 int top
= mTops
.remove(mTops
.size() - 1);
204 imageView
.smoothScrollToPosition(firstPosition
);
206 // Move the scroll if the selection is not visible
207 int indexPosition
= mHeightCell
*index
;
208 int height
= imageView
.getHeight();
210 if (indexPosition
> height
) {
211 imageView
.smoothScrollToPosition(index
);
217 * Save index and top position
219 protected void saveIndexAndTopPosition(int index
) {
223 int firstPosition
= imageView
.getFirstVisiblePosition();
224 mFirstPositions
.add(firstPosition
);
226 View view
= imageView
.getChildAt(0);
227 int top
= (view
== null
) ?
0 : view
.getTop() ;
231 // Save the height of a cell
232 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
237 public void onItemClick (AdapterView
<?
> parent
, View view
, int position
, long id
) {
242 public void onRefresh() {
244 mRefreshLayout
.setRefreshing(false
);
245 mRefreshEmptyLayout
.setRefreshing(false
);
247 if (mOnRefreshListener
!= null
) {
248 mOnRefreshListener
.onRefresh();
251 public void setOnRefreshListener(OnEnforceableRefreshListener listener
) {
252 mOnRefreshListener
= listener
;
257 * Enables swipe gesture
259 public void enableSwipe() {
260 mRefreshLayout
.setEnabled(true
);
264 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
265 * refreshing programmatically.
267 public void disableSwipe() {
268 mRefreshLayout
.setEnabled(false
);
272 * It shows the SwipeRefreshLayout progress
274 public void showSwipeProgress() {
275 mRefreshLayout
.setRefreshing(true
);
279 * It shows the SwipeRefreshLayout progress
281 public void hideSwipeProgress() {
282 mRefreshLayout
.setRefreshing(false
);
286 * Set message for empty list view
288 public void setMessageForEmptyList(String message
) {
289 if (mEmptyListMessage
!= null
) {
290 mEmptyListMessage
.setText(message
);
295 * Get the text of EmptyListMessage TextView
299 public String
getEmptyViewText() {
300 return (mEmptyListMessage
!= null
) ? mEmptyListMessage
.getText().toString() : "";
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
);
308 refreshLayout
.setOnRefreshListener(this);
312 public void onRefresh(boolean ignoreETag
) {
313 mRefreshLayout
.setRefreshing(false
);
314 mRefreshEmptyLayout
.setRefreshing(false
);
316 if (mOnRefreshListener
!= null
) {
317 mOnRefreshListener
.onRefresh(ignoreETag
);