Avoid double query to the ContentResolver when listing the contents of a folder
[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 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 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 //mAdapter = new FileListListAdapter();
83
84 Log.i(getClass().toString(), "onActivityCreated() stop");
85 }
86
87
88 @Override
89 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
90 OCFile file = (OCFile) mAdapter.getItem(position);
91 if (file != null) {
92 /// Click on a directory
93 if (file.getMimetype().equals("DIR")) {
94 // just local updates
95 mFile = file;
96 listDirectory(file);
97 // any other updates are let to the container Activity
98 mContainerActivity.onDirectoryClick(file);
99
100 } else { /// Click on a file
101 mContainerActivity.onFileClick(file);
102 }
103
104 } else {
105 Log.d(TAG, "Null object in ListAdapter!!");
106 }
107
108 }
109
110 /**
111 * Call this, when the user presses the up button
112 */
113 public void onNavigateUp() {
114 OCFile parentDir = null;
115
116 if(mFile != null){
117 DataStorageManager storageManager = mContainerActivity.getStorageManager();
118 parentDir = storageManager.getFileById(mFile.getParentId());
119 mFile = parentDir;
120 }
121 listDirectory(parentDir);
122 }
123
124 /**
125 * Use this to query the {@link OCFile} that is currently
126 * being displayed by this fragment
127 * @return The currently viewed OCFile
128 */
129 public OCFile getCurrentFile(){
130 return mFile;
131 }
132
133 /**
134 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
135 */
136 public void listDirectory(){
137 listDirectory(null);
138 }
139
140 /**
141 * Lists the given directory on the view. When the input parameter is null,
142 * it will either refresh the last known directory, or list the root
143 * if there never was a directory.
144 *
145 * @param directory File to be listed
146 */
147 public void listDirectory(OCFile directory) {
148
149 DataStorageManager storageManager = mContainerActivity.getStorageManager();
150
151 // Check input parameters for null
152 if(directory == null){
153 if(mFile != null){
154 directory = mFile;
155 } else {
156 directory = storageManager.getFileByPath("/");
157 if (directory == null) return; // no files, wait for sync
158 }
159 }
160
161
162 // If that's not a directory -> List its parent
163 if(!directory.isDirectory()){
164 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
165 directory = storageManager.getFileById(directory.getParentId());
166 }
167
168 mFile = directory;
169
170 mAdapter = new FileListListAdapter(directory, storageManager, getActivity());
171 setListAdapter(mAdapter);
172 }
173
174
175
176 /**
177 * Interface to implement by any Activity that includes some instance of FileListFragment
178 *
179 * @author David A. Velasco
180 */
181 public interface ContainerActivity {
182
183 /**
184 * Callback method invoked when a directory is clicked by the user on the files list
185 *
186 * @param file
187 */
188 public void onDirectoryClick(OCFile file);
189
190 /**
191 * Callback method invoked when a file (non directory) is clicked by the user on the files list
192 *
193 * @param file
194 */
195 public void onFileClick(OCFile file);
196
197 /**
198 * Getter for the current DataStorageManager in the container activity
199 */
200 public DataStorageManager getStorageManager();
201
202 }
203
204 }