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
.ui
.ExtendedListView
;
38 import com
.owncloud
.android
.utils
.Log_OC
;
41 * TODO extending SherlockListFragment instead of SherlockFragment
43 public class ExtendedListFragment
extends SherlockFragment
44 implements OnItemClickListener
, SwipeRefreshLayout
.OnRefreshListener
{
46 private static final String TAG
= ExtendedListFragment
.class.getSimpleName();
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";
55 // protected ExtendedListView mList;
57 private SwipeRefreshLayout mRefreshLayout
;
58 private SwipeRefreshLayout mRefreshEmptyLayout
;
59 private TextView mEmptyListMessage
;
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;
67 private SwipeRefreshLayout
.OnRefreshListener mOnRefreshListener
= null
;
69 protected GridView imageView
;
71 public void setListAdapter(ListAdapter listAdapter
) {
72 imageView
.setAdapter(listAdapter
);
73 imageView
.invalidate();
76 public GridView
getGridView() {
80 protected void switchImageView(){
81 imageView
.setNumColumns(GridView
.AUTO_FIT
);
82 imageView
.invalidate();
85 protected void switchFileView(){
86 imageView
.setNumColumns(1);
87 imageView
.invalidate();
92 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
93 Log_OC
.e(TAG
, "onCreateView");
95 View v
= inflater
.inflate(R
.layout
.list_fragment
, null
);
97 // imageView = (GridView) v.findViewById(R.id.grid_list_view);
98 // imageView.setOnItemClickListener(this);
100 // mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
101 imageView
= (ExtendedListView
)(v
.findViewById(R
.id
.list_root
));
102 imageView
.setOnItemClickListener(this);
105 //mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
106 //mList.setDividerHeight(1);
108 if (savedInstanceState
!= null
) {
109 int referencePosition
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
);
110 setReferencePosition(referencePosition
);
114 mRefreshLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files
);
115 mRefreshEmptyLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files_emptyView
);
117 onCreateSwipeToRefresh(mRefreshLayout
);
118 onCreateSwipeToRefresh(mRefreshEmptyLayout
);
120 // mList.setEmptyView(mRefreshEmptyLayout);
130 public void onActivityCreated(Bundle savedInstanceState
) {
131 super.onActivityCreated(savedInstanceState
);
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
));
141 mIndexes
= new ArrayList
<Integer
>();
142 mFirstPositions
= new ArrayList
<Integer
>();
143 mTops
= new ArrayList
<Integer
>();
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());
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.
166 * THe current policy is take as a reference the visible item in the center of the screen.
168 * @return The position in the list of the visible item in the center of the screen.
170 protected int getReferencePosition() {
171 if (imageView
!= null
) {
172 return (imageView
.getFirstVisiblePosition() + imageView
.getLastVisiblePosition()) / 2;
180 * Sets the visible part of the list from the reference position.
182 * @param position Reference position previously returned by {@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();
252 public void setOnRefreshListener(SwipeRefreshLayout
.OnRefreshListener listener
) {
253 mOnRefreshListener
= listener
;
258 * Enables swipe gesture
260 public void enableSwipe() {
261 mRefreshLayout
.setEnabled(true
);
265 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
266 * refreshing programmatically.
268 public void disableSwipe() {
269 mRefreshLayout
.setEnabled(false
);
273 * It shows the SwipeRefreshLayout progress
275 public void showSwipeProgress() {
276 mRefreshLayout
.setRefreshing(true
);
280 * It shows the SwipeRefreshLayout progress
282 public void hideSwipeProgress() {
283 mRefreshLayout
.setRefreshing(false
);
287 * Set message for empty list view
289 public void setMessageForEmptyList(String message
) {
290 if (mEmptyListMessage
!= null
) {
291 mEmptyListMessage
.setText(message
);
296 * Get the text of EmptyListMessage TextView
300 public String
getEmptyViewText() {
301 return (mEmptyListMessage
!= null
) ? mEmptyListMessage
.getText().toString() : "";
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
);
309 refreshLayout
.setOnRefreshListener(this);