6c52c6e00a3a5011e3f3452000b86c1c47571b70
[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 android.os.Bundle;
22 import android.support.v4.widget.SwipeRefreshLayout;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.AdapterView;
27 import android.widget.AdapterView.OnItemClickListener;
28 import android.widget.ListAdapter;
29 import android.widget.ListView;
30 import android.widget.TextView;
31
32 import com.actionbarsherlock.app.SherlockFragment;
33 import com.owncloud.android.R;
34 import com.owncloud.android.ui.ExtendedListView;
35 import com.owncloud.android.utils.Log_OC;
36
37 /**
38 * TODO extending SherlockListFragment instead of SherlockFragment
39 */
40 public class ExtendedListFragment extends SherlockFragment implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{
41
42 private static final String TAG = ExtendedListFragment.class.getSimpleName();
43
44 private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
45
46 protected ExtendedListView mList;
47
48 private SwipeRefreshLayout mRefreshLayout;
49 private SwipeRefreshLayout mRefreshEmptyLayout;
50 private TextView mEmptyListMessage;
51
52 public void setListAdapter(ListAdapter listAdapter) {
53 mList.setAdapter(listAdapter);
54 mList.invalidate();
55 }
56
57 public ListView getListView() {
58 return mList;
59 }
60
61
62 @Override
63 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
64 Log_OC.e(TAG, "onCreateView");
65 //mList = new ExtendedListView(getActivity());
66
67 View v = inflater.inflate(R.layout.list_fragment, null);
68 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
69 mList = (ExtendedListView)(v.findViewById(R.id.list_root));
70 mList.setOnItemClickListener(this);
71
72 mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
73 mList.setDividerHeight(1);
74
75 if (savedInstanceState != null) {
76 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
77 setReferencePosition(referencePosition);
78 }
79
80 // Pull down refresh
81 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
82 mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
83
84 onCreateSwipeToRefresh(mRefreshLayout);
85 onCreateSwipeToRefresh(mRefreshEmptyLayout);
86
87 mList.setEmptyView(mRefreshEmptyLayout);
88
89 return v;
90 }
91
92
93 @Override
94 public void onSaveInstanceState(Bundle savedInstanceState) {
95 super.onSaveInstanceState(savedInstanceState);
96 Log_OC.e(TAG, "onSaveInstanceState()");
97 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
98 }
99
100
101 /**
102 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
103 * the device is turned to other position.
104 *
105 * THe current policy is take as a reference the visible item in the center of the screen.
106 *
107 * @return The position in the list of the visible item in the center of the screen.
108 */
109 protected int getReferencePosition() {
110 if (mList != null) {
111 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
112 } else {
113 return 0;
114 }
115 }
116
117
118 /**
119 * Sets the visible part of the list from the reference position.
120 *
121 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
122 */
123 protected void setReferencePosition(int position) {
124 if (mList != null) {
125 mList.setAndCenterSelection(position);
126 }
127 }
128
129 @Override
130 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
131 // to be @overriden
132 }
133
134 @Override
135 public void onRefresh() {
136 // to be @overriden
137 mRefreshLayout.setRefreshing(false);
138 mRefreshEmptyLayout.setRefreshing(false);
139 }
140
141 /**
142 * Enables swipe gesture
143 */
144 public void enableSwipe() {
145 mRefreshLayout.setEnabled(true);
146 }
147
148 /**
149 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
150 * refreshing programmatically.
151 */
152 public void disableSwipe() {
153 mRefreshLayout.setEnabled(false);
154 }
155
156 /**
157 * It shows the SwipeRefreshLayout progress
158 */
159 public void showSwipeProgress() {
160 mRefreshLayout.setRefreshing(true);
161 }
162
163 /**
164 * It shows the SwipeRefreshLayout progress
165 */
166 public void hideSwipeProgress() {
167 mRefreshLayout.setRefreshing(false);
168 }
169
170 /**
171 * Set message for empty list view
172 */
173 public void setMessageForEmptyList(String message) {
174 if (mEmptyListMessage != null) {
175 mEmptyListMessage.setText(message);
176 }
177 }
178
179 /**
180 * Get the text of EmptyListMessage TextView
181 *
182 * @return String
183 */
184 public String getEmptyViewText() {
185 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
186 }
187
188 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
189 // Colors in animations: background
190 refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
191 R.color.background_color);
192
193 refreshLayout.setOnRefreshListener(this);
194 }
195
196 }