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 private GridView imageView
;
70 private View fileView
;
73 public void setListAdapter(ListAdapter listAdapter
) {
74 imageView
.setAdapter(listAdapter
);
75 imageView
.invalidate();
78 public ListView
getListView() {
82 protected void switchImageView(){
83 // TODO berechnen, wieviele Spalten
84 imageView
.setNumColumns(3);
86 imageView
.invalidate();
89 protected void switchFileView(){
90 imageView
.setNumColumns(1);
92 imageView
.invalidate();
97 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
98 Log_OC
.e(TAG
, "onCreateView");
100 View v
= inflater
.inflate(R
.layout
.list_fragment
, null
);
102 imageView
= (GridView
) v
.findViewById(R
.id
.grid_list_view
);
103 imageView
.setOnItemClickListener(this);
105 // mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
106 mList
= (ExtendedListView
)(v
.findViewById(R
.id
.list_root
));
107 // mList.setOnItemClickListener(this);
109 mList
.setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
110 mList
.setDividerHeight(1);
112 if (savedInstanceState
!= null
) {
113 int referencePosition
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
);
114 setReferencePosition(referencePosition
);
118 mRefreshLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files
);
119 mRefreshEmptyLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files_emptyView
);
121 onCreateSwipeToRefresh(mRefreshLayout
);
122 onCreateSwipeToRefresh(mRefreshEmptyLayout
);
124 mList
.setEmptyView(mRefreshEmptyLayout
);
134 public void onActivityCreated(Bundle savedInstanceState
) {
135 super.onActivityCreated(savedInstanceState
);
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
));
145 mIndexes
= new ArrayList
<Integer
>();
146 mFirstPositions
= new ArrayList
<Integer
>();
147 mTops
= new ArrayList
<Integer
>();
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());
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.
170 * THe current policy is take as a reference the visible item in the center of the screen.
172 * @return The position in the list of the visible item in the center of the screen.
174 protected int getReferencePosition() {
176 return (mList
.getFirstVisiblePosition() + mList
.getLastVisiblePosition()) / 2;
184 * Sets the visible part of the list from the reference position.
186 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
188 protected void setReferencePosition(int position
) {
190 mList
.setAndCenterSelection(position
);
196 * Restore index and position
198 protected void restoreIndexAndTopPosition() {
199 if (mIndexes
.size() > 0) {
200 // needs to be checked; not every browse-up had a browse-down before
202 int index
= mIndexes
.remove(mIndexes
.size() - 1);
204 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
206 int top
= mTops
.remove(mTops
.size() - 1);
208 mList
.setSelectionFromTop(firstPosition
, top
);
210 // Move the scroll if the selection is not visible
211 int indexPosition
= mHeightCell
*index
;
212 int height
= mList
.getHeight();
214 if (indexPosition
> height
) {
215 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
217 mList
.smoothScrollToPosition(index
);
219 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
221 mList
.setSelectionFromTop(index
, 0);
229 * Save index and top position
231 protected void saveIndexAndTopPosition(int index
) {
235 int firstPosition
= mList
.getFirstVisiblePosition();
236 mFirstPositions
.add(firstPosition
);
238 View view
= mList
.getChildAt(0);
239 int top
= (view
== null
) ?
0 : view
.getTop() ;
243 // Save the height of a cell
244 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
249 public void onItemClick (AdapterView
<?
> parent
, View view
, int position
, long id
) {
254 public void onRefresh() {
256 mRefreshLayout
.setRefreshing(false
);
257 mRefreshEmptyLayout
.setRefreshing(false
);
259 if (mOnRefreshListener
!= null
) {
260 mOnRefreshListener
.onRefresh();
264 public void setOnRefreshListener(SwipeRefreshLayout
.OnRefreshListener listener
) {
265 mOnRefreshListener
= listener
;
270 * Enables swipe gesture
272 public void enableSwipe() {
273 mRefreshLayout
.setEnabled(true
);
277 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
278 * refreshing programmatically.
280 public void disableSwipe() {
281 mRefreshLayout
.setEnabled(false
);
285 * It shows the SwipeRefreshLayout progress
287 public void showSwipeProgress() {
288 mRefreshLayout
.setRefreshing(true
);
292 * It shows the SwipeRefreshLayout progress
294 public void hideSwipeProgress() {
295 mRefreshLayout
.setRefreshing(false
);
299 * Set message for empty list view
301 public void setMessageForEmptyList(String message
) {
302 if (mEmptyListMessage
!= null
) {
303 mEmptyListMessage
.setText(message
);
308 * Get the text of EmptyListMessage TextView
312 public String
getEmptyViewText() {
313 return (mEmptyListMessage
!= null
) ? mEmptyListMessage
.getText().toString() : "";
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
);
321 refreshLayout
.setOnRefreshListener(this);