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