917f7a55b6f3d1f4701de83b2ee8f92c8b8ee412
[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 TextView mEmptyListMessage;
50
51 public void setListAdapter(ListAdapter listAdapter) {
52 mList.setAdapter(listAdapter);
53 mList.invalidate();
54 }
55
56 public ListView getListView() {
57 return mList;
58 }
59
60
61 @Override
62 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
63 Log_OC.e(TAG, "onCreateView");
64 //mList = new ExtendedListView(getActivity());
65
66 View v = inflater.inflate(R.layout.list_fragment, null);
67 mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
68 mList = (ExtendedListView)(v.findViewById(R.id.list_root));
69 mList.setOnItemClickListener(this);
70 mList.setEmptyView(mEmptyListMessage); // looks like it's not a cool idea
71 mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
72 mList.setDividerHeight(1);
73
74 if (savedInstanceState != null) {
75 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
76 setReferencePosition(referencePosition);
77 }
78
79 // Pull down refresh
80 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
81 // Colors in animations: background
82 mRefreshLayout.setColorScheme(R.color.background_color, R.color.background_color,
83 R.color.background_color, R.color.background_color);
84
85 mRefreshLayout.setOnRefreshListener(this);
86
87 return v;
88 }
89
90
91 @Override
92 public void onSaveInstanceState(Bundle savedInstanceState) {
93 super.onSaveInstanceState(savedInstanceState);
94 Log_OC.e(TAG, "onSaveInstanceState()");
95 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
96 }
97
98
99 /**
100 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
101 * the device is turned to other position.
102 *
103 * THe current policy is take as a reference the visible item in the center of the screen.
104 *
105 * @return The position in the list of the visible item in the center of the screen.
106 */
107 protected int getReferencePosition() {
108 if (mList != null) {
109 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
110 } else {
111 return 0;
112 }
113 }
114
115
116 /**
117 * Sets the visible part of the list from the reference position.
118 *
119 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
120 */
121 protected void setReferencePosition(int position) {
122 if (mList != null) {
123 mList.setAndCenterSelection(position);
124 }
125 }
126
127 @Override
128 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
129 // to be @overriden
130 }
131
132 @Override
133 public void onRefresh() {
134 // to be @overriden
135 mRefreshLayout.setRefreshing(false);
136 }
137
138 /**
139 * Enables swipe gesture
140 */
141 public void enableSwipe() {
142 mRefreshLayout.setEnabled(true);
143 }
144
145 /**
146 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
147 * refreshing programmatically.
148 */
149 public void disableSwipe() {
150 mRefreshLayout.setEnabled(false);
151 }
152
153 /**
154 * It shows the SwipeRefreshLayout progress
155 */
156 public void showSwipeProgress() {
157 mRefreshLayout.setRefreshing(true);
158 }
159
160 /**
161 * It shows the SwipeRefreshLayout progress
162 */
163 public void hideSwipeProgress() {
164 mRefreshLayout.setRefreshing(false);
165 }
166
167 /**
168 * Set message for empty list view
169 */
170 public void setMessageforEmptyView(String message) {
171 if (mEmptyListMessage != null) {
172 mEmptyListMessage.setText(message);
173 }
174 }
175
176 /**
177 * Get the text of EmptyListMessage TextView
178 *
179 * @return String
180 */
181 public String getEmptyViewText() {
182 return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
183 }
184
185 }