069a24d62572f497cd20016829e493fc0dcbf7cb
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileListFragment.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 eu.alefzero.owncloud.ui.fragment;
19
20 import java.util.Vector;
21
22 import android.app.Activity;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.AdapterView;
29 import eu.alefzero.owncloud.R;
30 import eu.alefzero.owncloud.datamodel.DataStorageManager;
31 import eu.alefzero.owncloud.datamodel.OCFile;
32 import eu.alefzero.owncloud.ui.FragmentListView;
33 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
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 FileListFragment extends FragmentListView {
42 private static final String TAG = "FileListFragment";
43
44 private FileListFragment.ContainerActivity mContainerActivity;
45
46 private Vector<OCFile> mFiles;
47 private OCFile mFile = null;
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 FileListFragment.ContainerActivity");
60 }
61 }
62
63
64 @Override
65 public View onCreateView(LayoutInflater inflater, ViewGroup container,
66 Bundle savedInstanceState) {
67 Log.i(getClass().toString(), "onCreateView() start");
68 super.onCreateView(inflater, container, savedInstanceState);
69 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
70 getListView().setDividerHeight(1);
71
72 Log.i(getClass().toString(), "onCreateView() end");
73 return getListView();
74 }
75
76
77 @Override
78 public void onActivityCreated(Bundle savedInstanceState) {
79 Log.i(getClass().toString(), "onActivityCreated() start");
80
81 super.onCreate(savedInstanceState);
82
83 Log.i(getClass().toString(), "onActivityCreated() stop");
84 }
85
86
87 @Override
88 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
89 if (mFiles.size() <= position) {
90 throw new IndexOutOfBoundsException("Incorrect item selected");
91 }
92 OCFile file = mFiles.get(position);
93
94 /// Click on a directory
95 if (file.getMimetype().equals("DIR")) {
96 // just local updates
97 mFile = file;
98 listDirectory(file);
99 // any other updates are let to the container Activity
100 mContainerActivity.onDirectoryClick(file);
101
102 } else { /// Click on a file
103 mContainerActivity.onFileClick(file);
104 }
105
106 }
107
108 /**
109 * Call this, when the user presses the up button
110 */
111 public void onNavigateUp() {
112 OCFile parentDir = null;
113
114 if(mFile != null){
115 DataStorageManager storageManager = mContainerActivity.getStorageManager();
116 parentDir = storageManager.getFileById(mFile.getParentId());
117 mFile = parentDir;
118 }
119 listDirectory(parentDir);
120 }
121
122 /**
123 * Use this to query the {@link OCFile} that is currently
124 * being displayed by this fragment
125 * @return The currently viewed OCFile
126 */
127 public OCFile getCurrentFile(){
128 return mFile;
129 }
130
131 /**
132 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
133 */
134 public void listDirectory(){
135 listDirectory(null);
136 }
137
138 /**
139 * Lists the given directory on the view. When the input parameter is null,
140 * it will either refresh the last known directory, or list the root
141 * if there never was a directory.
142 *
143 * @param directory File to be listed
144 */
145 public void listDirectory(OCFile directory) {
146
147 DataStorageManager storageManager = mContainerActivity.getStorageManager();
148
149 // Check input parameters for null
150 if(directory == null){
151 if(mFile != null){
152 directory = mFile;
153 } else {
154 directory = storageManager.getFileByPath("/");
155 if (directory == null) return; // no files, wait for sync
156 }
157 }
158
159
160 // If that's not a directory -> List its parent
161 if(!directory.isDirectory()){
162 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
163 directory = storageManager.getFileById(directory.getParentId());
164 }
165
166 mFile = directory;
167 mFiles = storageManager.getDirectoryContent(directory);
168
169 /*if (mFiles == null || mFiles.size() == 0) {
170 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
171 }*/
172 setListAdapter(new FileListListAdapter(directory, storageManager, getActivity()));
173 }
174
175
176
177 /**
178 * Interface to implement by any Activity that includes some instance of FileListFragment
179 *
180 * @author David A. Velasco
181 */
182 public interface ContainerActivity {
183
184 /**
185 * Callback method invoked when a directory is clicked by the user on the files list
186 *
187 * @param file
188 */
189 public void onDirectoryClick(OCFile file);
190
191 /**
192 * Callback method invoked when a file (non directory) is clicked by the user on the files list
193 *
194 * @param file
195 */
196 public void onFileClick(OCFile file);
197
198 /**
199 * Getter for the current DataStorageManager in the container activity
200 */
201 public DataStorageManager getStorageManager();
202
203 }
204
205 }