Removed useless file
[pub/Android/ownCloud.git] / src / com / owncloud / android / 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 com.owncloud.android.ui.fragment;
19
20 import java.util.Vector;
21
22 import com.owncloud.android.datamodel.DataStorageManager;
23 import com.owncloud.android.datamodel.OCFile;
24 import com.owncloud.android.ui.FragmentListView;
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 FileListFragment extends FragmentListView {
43 private static final String TAG = "FileListFragment";
44
45 private FileListFragment.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 FileListFragment.ContainerActivity");
61 }
62 }
63
64
65 @Override
66 public View onCreateView(LayoutInflater inflater, ViewGroup container,
67 Bundle savedInstanceState) {
68 Log.i(getClass().toString(), "onCreateView() start");
69 super.onCreateView(inflater, container, savedInstanceState);
70 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
71 getListView().setDividerHeight(1);
72
73 Log.i(getClass().toString(), "onCreateView() end");
74 return getListView();
75 }
76
77
78 @Override
79 public void onActivityCreated(Bundle savedInstanceState) {
80 Log.i(getClass().toString(), "onActivityCreated() start");
81
82 super.onCreate(savedInstanceState);
83 //mAdapter = new FileListListAdapter();
84
85 Log.i(getClass().toString(), "onActivityCreated() stop");
86 }
87
88
89 @Override
90 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
91 OCFile file = (OCFile) mAdapter.getItem(position);
92 if (file != null) {
93 /// Click on a directory
94 if (file.getMimetype().equals("DIR")) {
95 // just local updates
96 mFile = file;
97 listDirectory(file);
98 // any other updates are let to the container Activity
99 mContainerActivity.onDirectoryClick(file);
100
101 } else { /// Click on a file
102 mContainerActivity.onFileClick(file);
103 }
104
105 } else {
106 Log.d(TAG, "Null object in ListAdapter!!");
107 }
108
109 }
110
111 /**
112 * Call this, when the user presses the up button
113 */
114 public void onNavigateUp() {
115 OCFile parentDir = null;
116
117 if(mFile != null){
118 DataStorageManager storageManager = mContainerActivity.getStorageManager();
119 parentDir = storageManager.getFileById(mFile.getParentId());
120 mFile = parentDir;
121 }
122 listDirectory(parentDir);
123 }
124
125 /**
126 * Use this to query the {@link OCFile} that is currently
127 * being displayed by this fragment
128 * @return The currently viewed OCFile
129 */
130 public OCFile getCurrentFile(){
131 return mFile;
132 }
133
134 /**
135 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
136 */
137 public void listDirectory(){
138 listDirectory(null);
139 }
140
141 /**
142 * Lists the given directory on the view. When the input parameter is null,
143 * it will either refresh the last known directory, or list the root
144 * if there never was a directory.
145 *
146 * @param directory File to be listed
147 */
148 public void listDirectory(OCFile directory) {
149
150 DataStorageManager storageManager = mContainerActivity.getStorageManager();
151
152 // Check input parameters for null
153 if(directory == null){
154 if(mFile != null){
155 directory = mFile;
156 } else {
157 directory = storageManager.getFileByPath("/");
158 if (directory == null) return; // no files, wait for sync
159 }
160 }
161
162
163 // If that's not a directory -> List its parent
164 if(!directory.isDirectory()){
165 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
166 directory = storageManager.getFileById(directory.getParentId());
167 }
168
169 mFile = directory;
170
171 mAdapter = new FileListListAdapter(directory, storageManager, getActivity());
172 setListAdapter(mAdapter);
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 }