81c43a33b9e283cb0db5decd871ca7445e4b8ca9
[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 import java.util.ArrayList;
22
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.os.Environment;
26 import android.util.SparseBooleanArray;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.AdapterView;
31 import android.widget.ImageView;
32 import android.widget.ListView;
33
34 import com.owncloud.android.Log_OC;
35 import com.owncloud.android.R;
36 import com.owncloud.android.ui.adapter.LocalFileListAdapter;
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 ExtendedListFragment {
45 private static final String TAG = "LocalFileListFragment";
46
47 /**
48 * Reference to the Activity which this fragment is attached to. For
49 * callbacks
50 */
51 private LocalFileListFragment.ContainerActivity mContainerActivity;
52
53 /** Directory to show */
54 private File mDirectory = null;
55
56 /** Adapter to connect the data from the directory with the View object */
57 private LocalFileListAdapter mAdapter = null;
58
59 /**
60 * {@inheritDoc}
61 */
62 @Override
63 public void onAttach(Activity activity) {
64 super.onAttach(activity);
65 try {
66 mContainerActivity = (ContainerActivity) activity;
67 } catch (ClassCastException e) {
68 throw new ClassCastException(activity.toString() + " must implement "
69 + LocalFileListFragment.ContainerActivity.class.getSimpleName());
70 }
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 @Override
77 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
78 Log_OC.i(TAG, "onCreateView() start");
79 View v = super.onCreateView(inflater, container, savedInstanceState);
80 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
81 Log_OC.i(TAG, "onCreateView() end");
82 return v;
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public void onActivityCreated(Bundle savedInstanceState) {
90 Log_OC.i(TAG, "onActivityCreated() start");
91
92 super.onCreate(savedInstanceState);
93 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
94 setListAdapter(mAdapter);
95
96 Log_OC.i(TAG, "onActivityCreated() stop");
97 }
98
99 /**
100 * Checks the file clicked over. Browses inside if it is a directory.
101 * Notifies the container activity in any case.
102 */
103 @Override
104 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
105 File file = (File) mAdapter.getItem(position);
106 if (file != null) {
107 // / Click on a directory
108 if (file.isDirectory()) {
109 // just local updates
110 listDirectory(file);
111 // notify the click to container Activity
112 mContainerActivity.onDirectoryClick(file);
113
114 } else { // / Click on a file
115 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
116 if (checkBoxV != null) {
117 if (getListView().isItemChecked(position)) {
118 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
119 } else {
120 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
121 }
122 }
123 // notify the change to the container Activity
124 mContainerActivity.onFileClick(file);
125 }
126
127 } else {
128 Log_OC.w(TAG, "Null object in ListAdapter!!");
129 }
130 }
131
132 /**
133 * Call this, when the user presses the up button
134 */
135 public void onNavigateUp() {
136 File parentDir = null;
137 if (mDirectory != null) {
138 parentDir = mDirectory.getParentFile(); // can be null
139 }
140 listDirectory(parentDir);
141 }
142
143 /**
144 * Use this to query the {@link File} object for the directory that is
145 * currently being displayed by this fragment
146 *
147 * @return File The currently displayed directory
148 */
149 public File getCurrentDirectory() {
150 return mDirectory;
151 }
152
153 /**
154 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null
155 * parameter to refresh the current directory.
156 */
157 public void listDirectory() {
158 listDirectory(null);
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. list the root if there
164 * 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
176 // be
177 // careful
178 // with
179 // the
180 // state
181 // of the
182 // storage;
183 // could
184 // not be
185 // available
186 if (directory == null)
187 return; // no files to show
188 }
189 }
190
191 // if that's not a directory -> List its parent
192 if (!directory.isDirectory()) {
193 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
194 directory = directory.getParentFile();
195 }
196
197 mList.clearChoices(); // by now, only files in the same directory will
198 // be kept as selected
199 mAdapter.swapDirectory(directory);
200 if (mDirectory == null || !mDirectory.equals(directory)) {
201 mList.setSelectionFromTop(0, 0);
202 }
203 mDirectory = directory;
204 }
205
206 /**
207 * Returns the fule paths to the files checked by the user
208 *
209 * @return File paths to the files checked by the user.
210 */
211 public String[] getCheckedFilePaths() {
212 ArrayList<String> result = new ArrayList<String>();
213 SparseBooleanArray positions = mList.getCheckedItemPositions();
214 if (positions.size() > 0) {
215 for (int i = 0; i < positions.size(); i++) {
216 if (positions.get(positions.keyAt(i)) == true) {
217 result.add(((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath());
218 }
219 }
220
221 Log_OC.d(TAG, "Returning " + result.size() + " selected files");
222 }
223 return result.toArray(new String[result.size()]);
224 }
225
226 /**
227 * Interface to implement by any Activity that includes some instance of
228 * LocalFileListFragment
229 *
230 * @author David A. Velasco
231 */
232 public interface ContainerActivity {
233
234 /**
235 * Callback method invoked when a directory is clicked by the user on
236 * the files list
237 *
238 * @param file
239 */
240 public void onDirectoryClick(File directory);
241
242 /**
243 * Callback method invoked when a file (non directory) is clicked by the
244 * user on the files list
245 *
246 * @param file
247 */
248 public void onFileClick(File file);
249
250 /**
251 * Callback method invoked when the parent activity is fully created to
252 * get the directory to list firstly.
253 *
254 * @return Directory to list firstly. Can be NULL.
255 */
256 public File getInitialDirectory();
257
258 }
259
260 }