Download service refactoring: multiple downloads and cancellation support
[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.files.services.FileDownloader.FileDownloaderBinder;
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
45 private OCFileListFragment.ContainerActivity mContainerActivity;
46
47 private OCFile mFile = null;
48 private FileListListAdapter mAdapter;
49
50
51 /**
52 * {@inheritDoc}
53 */
54 @Override
55 public void onAttach(Activity activity) {
56 super.onAttach(activity);
57 try {
58 mContainerActivity = (ContainerActivity) activity;
59 } catch (ClassCastException e) {
60 throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getCanonicalName());
61 }
62 }
63
64
65 /**
66 * {@inheritDoc}
67 */
68 @Override
69 public View onCreateView(LayoutInflater inflater, ViewGroup container,
70 Bundle savedInstanceState) {
71 Log.i(TAG, "onCreateView() start");
72 super.onCreateView(inflater, container, savedInstanceState);
73 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
74 getListView().setDividerHeight(1);
75
76 Log.i(TAG, "onCreateView() end");
77 return getListView();
78 }
79
80
81 /**
82 * {@inheritDoc}
83 */
84 @Override
85 public void onActivityCreated(Bundle savedInstanceState) {
86 Log.i(TAG, "onActivityCreated() start");
87
88 super.onActivityCreated(savedInstanceState);
89 mAdapter = new FileListListAdapter(mContainerActivity.getInitialDirectory(), mContainerActivity.getStorageManager(), getActivity(), mContainerActivity);
90 setListAdapter(mAdapter);
91
92 if (savedInstanceState != null) {
93 Log.i(TAG, "savedInstanceState is not null");
94 int position = savedInstanceState.getInt("LIST_POSITION");
95 getListView().setSelectionFromTop(position, 0);
96 }
97 //mAdapter = new FileListListAdapter();
98
99 Log.i(TAG, "onActivityCreated() stop");
100 }
101
102
103 @Override
104 public void onSaveInstanceState(Bundle savedInstanceState) {
105 Log.i(TAG, "onSaveInstanceState() start");
106
107 savedInstanceState.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
108
109 Log.i(TAG, "onSaveInstanceState() stop");
110 }
111
112
113 @Override
114 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
115 OCFile file = (OCFile) mAdapter.getItem(position);
116 if (file != null) {
117 /// Click on a directory
118 if (file.getMimetype().equals("DIR")) {
119 // just local updates
120 mFile = file;
121 listDirectory(file);
122 // any other updates are let to the container Activity
123 mContainerActivity.onDirectoryClick(file);
124
125 } else { /// Click on a file
126 mContainerActivity.onFileClick(file);
127 }
128
129 } else {
130 Log.d(TAG, "Null object in ListAdapter!!");
131 }
132
133 }
134
135 /**
136 * Call this, when the user presses the up button
137 */
138 public void onNavigateUp() {
139 OCFile parentDir = null;
140
141 if(mFile != null){
142 DataStorageManager storageManager = mContainerActivity.getStorageManager();
143 parentDir = storageManager.getFileById(mFile.getParentId());
144 mFile = parentDir;
145 }
146 listDirectory(parentDir);
147 }
148
149 /**
150 * Use this to query the {@link OCFile} that is currently
151 * being displayed by this fragment
152 * @return The currently viewed OCFile
153 */
154 public OCFile getCurrentFile(){
155 return mFile;
156 }
157
158 /**
159 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
160 */
161 public void listDirectory(){
162 int position = mList.getFirstVisiblePosition();
163 listDirectory(null);
164 mList.setSelectionFromTop(position, 0);
165 }
166
167 /**
168 * Lists the given directory on the view. When the input parameter is null,
169 * it will either refresh the last known directory, or list the root
170 * if there never was a directory.
171 *
172 * @param directory File to be listed
173 */
174 public void listDirectory(OCFile directory) {
175 DataStorageManager storageManager = mContainerActivity.getStorageManager();
176
177 // Check input parameters for null
178 if(directory == null){
179 if(mFile != null){
180 directory = mFile;
181 } else {
182 directory = storageManager.getFileByPath("/");
183 if (directory == null) return; // no files, wait for sync
184 }
185 }
186
187
188 // If that's not a directory -> List its parent
189 if(!directory.isDirectory()){
190 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
191 directory = storageManager.getFileById(directory.getParentId());
192 }
193
194 mFile = directory;
195 mAdapter.swapDirectory(mFile);
196 mList.setSelectionFromTop(0, 0);
197 mList.invalidate();
198 }
199
200
201
202 /**
203 * Interface to implement by any Activity that includes some instance of FileListFragment
204 *
205 * @author David A. Velasco
206 */
207 public interface ContainerActivity extends TransferServiceGetter {
208
209 /**
210 * Callback method invoked when a directory is clicked by the user on the files list
211 *
212 * @param file
213 */
214 public void onDirectoryClick(OCFile file);
215
216 /**
217 * Callback method invoked when a file (non directory) is clicked by the user on the files list
218 *
219 * @param file
220 */
221 public void onFileClick(OCFile file);
222
223 /**
224 * Getter for the current DataStorageManager in the container activity
225 */
226 public DataStorageManager getStorageManager();
227
228
229 /**
230 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
231 *
232 * @return Directory to list firstly. Can be NULL.
233 */
234 public OCFile getInitialDirectory();
235
236
237 }
238
239 }