Keep the same item in the CENTER of the files list when the device is turned to other...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 package com.owncloud.android.ui.fragment;
19
20 import com.owncloud.android.datamodel.DataStorageManager;
21 import com.owncloud.android.datamodel.OCFile;
22 import com.owncloud.android.ui.ExtendedListView;
23 import com.owncloud.android.ui.FragmentListView;
24 import com.owncloud.android.ui.activity.TransferServiceGetter;
25 import com.owncloud.android.ui.adapter.FileListListAdapter;
26
27 import android.app.Activity;
28 import android.os.Bundle;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.AdapterView;
34 import com.owncloud.android.R;
35
36 /**
37 * A Fragment that lists all files and folders in a given path.
38 *
39 * @author Bartek Przybylski
40 *
41 */
42 public class OCFileListFragment extends FragmentListView {
43 private static final String TAG = "FileListFragment";
44 private static final String SAVED_LIST_POSITION = "LIST_POSITION";
45
46 private OCFileListFragment.ContainerActivity mContainerActivity;
47
48 private OCFile mFile = null;
49 private FileListListAdapter mAdapter;
50
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public void onAttach(Activity activity) {
57 super.onAttach(activity);
58 try {
59 mContainerActivity = (ContainerActivity) activity;
60 } catch (ClassCastException e) {
61 throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getCanonicalName());
62 }
63 }
64
65
66 /**
67 * {@inheritDoc}
68 */
69 @Override
70 public View onCreateView(LayoutInflater inflater, ViewGroup container,
71 Bundle savedInstanceState) {
72 Log.i(TAG, "onCreateView() start");
73 super.onCreateView(inflater, container, savedInstanceState);
74 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
75 getListView().setDividerHeight(1);
76
77 Log.i(TAG, "onCreateView() end");
78 return getListView();
79 }
80
81
82 /**
83 * {@inheritDoc}
84 */
85 @Override
86 public void onActivityCreated(Bundle savedInstanceState) {
87 Log.i(TAG, "onActivityCreated() start");
88
89 super.onActivityCreated(savedInstanceState);
90 mAdapter = new FileListListAdapter(mContainerActivity.getInitialDirectory(), mContainerActivity.getStorageManager(), getActivity(), mContainerActivity);
91 setListAdapter(mAdapter);
92
93 if (savedInstanceState != null) {
94 Log.i(TAG, "savedInstanceState is not null");
95 int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
96 setReferencePosition(position);
97 }
98
99 Log.i(TAG, "onActivityCreated() stop");
100 }
101
102
103
104 @Override
105 public void onSaveInstanceState(Bundle savedInstanceState) {
106 Log.i(TAG, "onSaveInstanceState() start");
107
108 savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
109
110 Log.i(TAG, "onSaveInstanceState() stop");
111 }
112
113
114 /**
115 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
116 * the device is turned to other position.
117 *
118 * THe current policy is take as a reference the visible item in the center of the screen.
119 *
120 * @return The position in the list of the visible item in the center of the screen.
121 */
122 private int getReferencePosition() {
123 return (getListView().getFirstVisiblePosition() + getListView().getLastVisiblePosition()) / 2;
124 }
125
126
127 /**
128 * Sets the visible part of the list from the reference position.
129 *
130 * @param position Reference position previously returned by {@link OCFileListFragment#getReferencePosition()}
131 */
132 private void setReferencePosition(int position) {
133 ((ExtendedListView)getListView()).setAndCenterSelection(position);
134 }
135
136
137 @Override
138 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
139 OCFile file = (OCFile) mAdapter.getItem(position);
140 if (file != null) {
141 /// Click on a directory
142 if (file.getMimetype().equals("DIR")) {
143 // just local updates
144 mFile = file;
145 listDirectory(file);
146 // any other updates are let to the container Activity
147 mContainerActivity.onDirectoryClick(file);
148
149 } else { /// Click on a file
150 mContainerActivity.onFileClick(file);
151 }
152
153 } else {
154 Log.d(TAG, "Null object in ListAdapter!!");
155 }
156
157 }
158
159 /**
160 * Call this, when the user presses the up button
161 */
162 public void onNavigateUp() {
163 OCFile parentDir = null;
164
165 if(mFile != null){
166 DataStorageManager storageManager = mContainerActivity.getStorageManager();
167 parentDir = storageManager.getFileById(mFile.getParentId());
168 mFile = parentDir;
169 }
170 listDirectory(parentDir);
171 }
172
173 /**
174 * Use this to query the {@link OCFile} that is currently
175 * being displayed by this fragment
176 * @return The currently viewed OCFile
177 */
178 public OCFile getCurrentFile(){
179 return mFile;
180 }
181
182 /**
183 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
184 */
185 public void listDirectory(){
186 listDirectory(null);
187 }
188
189 /**
190 * Lists the given directory on the view. When the input parameter is null,
191 * it will either refresh the last known directory, or list the root
192 * if there never was a directory.
193 *
194 * @param directory File to be listed
195 */
196 public void listDirectory(OCFile directory) {
197 DataStorageManager storageManager = mContainerActivity.getStorageManager();
198 if (storageManager != null) {
199
200 // Check input parameters for null
201 if(directory == null){
202 if(mFile != null){
203 directory = mFile;
204 } else {
205 directory = storageManager.getFileByPath("/");
206 if (directory == null) return; // no files, wait for sync
207 }
208 }
209
210
211 // If that's not a directory -> List its parent
212 if(!directory.isDirectory()){
213 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
214 directory = storageManager.getFileById(directory.getParentId());
215 }
216
217 mAdapter.swapDirectory(mFile, storageManager);
218 if (mFile == null || !mFile.equals(directory)) {
219 mList.setSelectionFromTop(0, 0);
220 }
221 mFile = directory;
222 }
223 }
224
225
226
227 /**
228 * Interface to implement by any Activity that includes some instance of FileListFragment
229 *
230 * @author David A. Velasco
231 */
232 public interface ContainerActivity extends TransferServiceGetter {
233
234 /**
235 * Callback method invoked when a directory is clicked by the user on the files list
236 *
237 * @param file
238 */
239 public void onDirectoryClick(OCFile file);
240
241 /**
242 * Callback method invoked when a file (non directory) is clicked by the user on the files list
243 *
244 * @param file
245 */
246 public void onFileClick(OCFile file);
247
248 /**
249 * Getter for the current DataStorageManager in the container activity
250 */
251 public DataStorageManager getStorageManager();
252
253
254 /**
255 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
256 *
257 * @return Directory to list firstly. Can be NULL.
258 */
259 public OCFile getInitialDirectory();
260
261
262 }
263
264 }