4cdf80fae006c31c3554e52de60eb4a4d1dbcd56
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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.R;
23 import com.owncloud.android.ui.adapter.LocalFileListAdapter;
24 import com.owncloud.android.utils.Log_OC;
25
26
27 import android.app.Activity;
28 import android.os.Bundle;
29 import android.os.Environment;
30 import android.util.SparseBooleanArray;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.AdapterView;
35 import android.widget.ImageView;
36 import android.widget.ListView;
37
38
39 /**
40 * A Fragment that lists all files and folders in a given LOCAL path.
41 *
42 * @author David A. Velasco
43 *
44 */
45 public class LocalFileListFragment extends ExtendedListFragment {
46 private static final String TAG = "LocalFileListFragment";
47
48 /** Reference to the Activity which this fragment is attached to. For callbacks */
49 private LocalFileListFragment.ContainerActivity mContainerActivity;
50
51 /** Directory to show */
52 private File mDirectory = null;
53
54 /** Adapter to connect the data from the directory with the View object */
55 private LocalFileListAdapter mAdapter = null;
56
57
58 /**
59 * {@inheritDoc}
60 */
61 @Override
62 public void onAttach(Activity activity) {
63 super.onAttach(activity);
64 try {
65 mContainerActivity = (ContainerActivity) activity;
66 } catch (ClassCastException e) {
67 throw new ClassCastException(activity.toString() + " must implement " + LocalFileListFragment.ContainerActivity.class.getSimpleName());
68 }
69 }
70
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
77 Log_OC.i(TAG, "onCreateView() start");
78 View v = super.onCreateView(inflater, container, savedInstanceState);
79 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
80 disableSwipe(); // Disable pull refresh
81 Log_OC.i(TAG, "onCreateView() end");
82 return v;
83 }
84
85
86 /**
87 * {@inheritDoc}
88 */
89 @Override
90 public void onActivityCreated(Bundle savedInstanceState) {
91 Log_OC.i(TAG, "onActivityCreated() start");
92
93 super.onCreate(savedInstanceState);
94 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
95 setListAdapter(mAdapter);
96
97 Log_OC.i(TAG, "onActivityCreated() stop");
98 }
99
100 public void selectAll(){
101 int numberOfFiles = mAdapter.getCount();
102 for(int i = 0; i < numberOfFiles; i++){
103 File file = (File) mAdapter.getItem(i);
104 if (file != null) {
105 /// Click on a directory
106 if (file.isDirectory()) {
107
108 } else { /// Click on a file
109 getListView().setItemChecked(i, true);
110
111 // notify the change to the container Activity
112 mContainerActivity.onFileClick(file);
113 }
114 }
115 }
116 }
117
118 public void deselectAll(){
119 /*
120 int numberOfFiles = mAdapter.getCount();
121 for(int i = 0; i < numberOfFiles; i++){
122 File file = (File) mAdapter.getItem(i);
123 if (file != null) {
124 /// Click on a directory
125 if (file.isDirectory()) {
126
127 } else { /// Click on a file
128 if(getListView().isItemChecked(i)){
129 getListView().setItemChecked(i, false);
130 }
131 // notify the change to the container Activity
132 mContainerActivity.onFileClick(file);
133 }
134 }
135 }*/
136 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
137 setListAdapter(mAdapter);
138 }
139
140 /**
141 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
142 */
143 @Override
144 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
145 File file = (File) mAdapter.getItem(position);
146
147 if (file != null) {
148 /// Click on a directory
149 if (file.isDirectory()) {
150 // just local updates
151 listDirectory(file);
152 // notify the click to container Activity
153 mContainerActivity.onDirectoryClick(file);
154
155 } else { /// Click on a file
156 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
157 if (checkBoxV != null) {
158 if (getListView().isItemChecked(position)) {
159 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
160 } else {
161 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
162 }
163 }
164 // notify the change to the container Activity
165 mContainerActivity.onFileClick(file);
166 }
167
168 } else {
169 Log_OC.w(TAG, "Null object in ListAdapter!!");
170 }
171 }
172
173
174 /**
175 * Call this, when the user presses the up button
176 */
177 public void onNavigateUp() {
178 File parentDir = null;
179 if(mDirectory != null) {
180 parentDir = mDirectory.getParentFile(); // can be null
181 }
182 listDirectory(parentDir);
183 }
184
185
186 /**
187 * Use this to query the {@link File} object for the directory
188 * that is currently being displayed by this fragment
189 *
190 * @return File The currently displayed directory
191 */
192 public File getCurrentDirectory(){
193 return mDirectory;
194 }
195
196
197 /**
198 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
199 * to refresh the current directory.
200 */
201 public void listDirectory(){
202 listDirectory(null);
203 }
204
205
206 /**
207 * Lists the given directory on the view. When the input parameter is null,
208 * it will either refresh the last known directory. list the root
209 * if there never was a directory.
210 *
211 * @param directory Directory to be listed
212 */
213 public void listDirectory(File directory) {
214
215 // Check input parameters for null
216 if(directory == null) {
217 if(mDirectory != null){
218 directory = mDirectory;
219 } else {
220 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
221 if (directory == null) return; // no files to show
222 }
223 }
224
225
226 // if that's not a directory -> List its parent
227 if(!directory.isDirectory()){
228 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
229 directory = directory.getParentFile();
230 }
231
232 mList.clearChoices(); // by now, only files in the same directory will be kept as selected
233 mAdapter.swapDirectory(directory);
234 if (mDirectory == null || !mDirectory.equals(directory)) {
235 mList.setSelectionFromTop(0, 0);
236 }
237 mDirectory = directory;
238 }
239
240
241 /**
242 * Returns the fule paths to the files checked by the user
243 *
244 * @return File paths to the files checked by the user.
245 */
246 public String[] getCheckedFilePaths() {
247 String [] result = null;
248 SparseBooleanArray positions = mList.getCheckedItemPositions();
249 if (positions.size() > 0) {
250 Log_OC.d(TAG, "Returning " + positions.size() + " selected files");
251 result = new String[positions.size()];
252 for (int i=0; i<positions.size(); i++) {
253 result[i] = ((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath();
254 }
255 }
256 return result;
257 }
258
259
260 /**
261 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
262 *
263 * @author David A. Velasco
264 */
265 public interface ContainerActivity {
266
267 /**
268 * Callback method invoked when a directory is clicked by the user on the files list
269 *
270 * @param file
271 */
272 public void onDirectoryClick(File directory);
273
274 /**
275 * Callback method invoked when a file (non directory) is clicked by the user on the files list
276 *
277 * @param file
278 */
279 public void onFileClick(File file);
280
281
282 /**
283 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
284 *
285 * @return Directory to list firstly. Can be NULL.
286 */
287 public File getInitialDirectory();
288
289 }
290
291
292 }