Multiple upload available from the app
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.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.io.File;
21
22 import com.owncloud.android.ui.FragmentListView;
23 import com.owncloud.android.ui.adapter.LocalFileListAdapter;
24
25 import android.app.Activity;
26 import android.os.Bundle;
27 import android.os.Environment;
28 import android.util.Log;
29 import android.util.SparseBooleanArray;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.AdapterView;
34 import android.widget.ListView;
35
36 import com.owncloud.android.R;
37
38 /**
39 * A Fragment that lists all files and folders in a given LOCAL path.
40 *
41 * @author David A. Velasco
42 *
43 */
44 public class LocalFileListFragment extends FragmentListView {
45 private static final String TAG = "LocalFileListFragment";
46
47 /** Reference to the Activity which this fragment is attached to. For callbacks */
48 private LocalFileListFragment.ContainerActivity mContainerActivity;
49
50 /** Directory to show */
51 private File mDirectory = null;
52
53 /** Adapter to connect the data from the directory with the View object */
54 private LocalFileListAdapter mAdapter = null;
55
56
57 /**
58 * {@inheritDoc}
59 */
60 @Override
61 public void onAttach(Activity activity) {
62 super.onAttach(activity);
63 try {
64 mContainerActivity = (ContainerActivity) activity;
65 } catch (ClassCastException e) {
66 throw new ClassCastException(activity.toString() + " must implement " + LocalFileListFragment.ContainerActivity.class.getCanonicalName());
67 }
68 }
69
70
71 /**
72 * {@inheritDoc}
73 */
74 @Override
75 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
76 Log.i(getClass().toString(), "onCreateView() start");
77 super.onCreateView(inflater, container, savedInstanceState);
78 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
79 getListView().setDividerHeight(1);
80 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
81
82 if (savedInstanceState != null) {
83 // TODO recover previous state; or maybe in onActivityCreated
84 }
85
86 Log.i(getClass().toString(), "onCreateView() end");
87 return getListView();
88 }
89
90
91 /**
92 * {@inheritDoc}
93 */
94 @Override
95 public void onActivityCreated(Bundle savedInstanceState) {
96 Log.i(getClass().toString(), "onActivityCreated() start");
97
98 super.onCreate(savedInstanceState);
99
100 Log.i(getClass().toString(), "onActivityCreated() stop");
101 }
102
103
104 /**
105 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
106 */
107 @Override
108 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
109 File file = (File) mAdapter.getItem(position);
110 if (file != null) {
111 /// Click on a directory
112 if (file.isDirectory()) {
113 // just local updates
114 listDirectory(file);
115 // notify the click to container Activity
116 mContainerActivity.onDirectoryClick(file);
117
118 } else { /// Click on a file
119 // notify the change to the container Activity
120 mContainerActivity.onFileClick(file);
121 }
122
123 } else {
124 Log.w(TAG, "Null object in ListAdapter!!");
125 }
126 }
127
128
129 /**
130 * Call this, when the user presses the up button
131 */
132 public void onNavigateUp() {
133 File parentDir = null;
134 if(mDirectory != null) {
135 parentDir = mDirectory.getParentFile(); // can be null
136 }
137 listDirectory(parentDir);
138 }
139
140
141 /**
142 * Use this to query the {@link File} object for the directory
143 * that is currently being displayed by this fragment
144 *
145 * @return File The currently displayed directory
146 */
147 public File getCurrentDirectory(){
148 return mDirectory;
149 }
150
151
152 /**
153 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
154 * to refresh the current directory.
155 */
156 public void listDirectory(){
157 listDirectory(null);
158 }
159
160
161 /**
162 * Lists the given directory on the view. When the input parameter is null,
163 * it will either refresh the last known directory, or list the root
164 * if there never was a directory.
165 *
166 * @param directory Directory to be listed
167 */
168 public void listDirectory(File directory) {
169
170 // Check input parameters for null
171 if(directory == null) {
172 if(mDirectory != null){
173 directory = mDirectory;
174 } else {
175 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
176 if (directory == null) return; // no files, wait for sync
177 }
178 }
179
180
181 // if that's not a directory -> List its parent
182 if(!directory.isDirectory()){
183 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
184 directory = directory.getParentFile();
185 }
186
187 mDirectory = directory;
188 if (mAdapter == null) {
189 mAdapter = new LocalFileListAdapter(mDirectory, getActivity());
190 setListAdapter(mAdapter);
191 } else {
192 mList.clearChoices(); // by now, only files in the same directory will be kept as selected
193 mAdapter.swapDirectory(mDirectory);
194 }
195
196 }
197
198
199 /**
200 * Returns the fule paths to the files checked by the user
201 *
202 * @return File paths to the files checked by the user.
203 */
204 public String[] getCheckedFilePaths() {
205 String [] result = null;
206 SparseBooleanArray positions = mList.getCheckedItemPositions();
207 if (positions.size() > 0) {
208 Log.d(TAG, "Returning " + positions.size() + " selected files");
209 result = new String[positions.size()];
210 for (int i=0; i<positions.size(); i++) {
211 result[i] = ((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath();
212 }
213 }
214 return result;
215 }
216
217
218 /**
219 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
220 *
221 * @author David A. Velasco
222 */
223 public interface ContainerActivity {
224
225 /**
226 * Callback method invoked when a directory is clicked by the user on the files list
227 *
228 * @param file
229 */
230 public void onDirectoryClick(File directory);
231
232 /**
233 * Callback method invoked when a file (non directory) is clicked by the user on the files list
234 *
235 * @param file
236 */
237 public void onFileClick(File file);
238
239 }
240
241
242 }