4cdfbd3571017dcec6615ecbf820a1a6a45619b0
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.java
1 /* ownCloud Android client application
2 *
3 * @author David A. Velasco
4 * Copyright (C) 2011 Bartek Przybylski
5 * Copyright (C) 2012-2013 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package com.owncloud.android.ui.fragment;
21
22 import java.io.File;
23 import java.util.ArrayList;
24
25 import android.app.Activity;
26 import android.os.Bundle;
27 import android.os.Environment;
28 import android.util.SparseBooleanArray;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.ImageView;
34 import android.widget.ListView;
35
36 import com.owncloud.android.R;
37 import com.owncloud.android.lib.common.utils.Log_OC;
38 import com.owncloud.android.ui.adapter.LocalFileListAdapter;
39
40
41 /**
42 * A Fragment that lists all files and folders in a given LOCAL path.
43 */
44 public class LocalFileListFragment extends ExtendedListFragment {
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.getSimpleName());
67 }
68 }
69
70
71 /**
72 * {@inheritDoc}
73 */
74 @Override
75 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
76 Log_OC.i(TAG, "onCreateView() start");
77 View v = super.onCreateView(inflater, container, savedInstanceState);
78 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
79 setSwipeEnabled(false); // Disable pull-to-refresh
80 setMessageForEmptyList(getString(R.string.local_file_list_empty));
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.onActivityCreated(savedInstanceState);
94 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
95 setListAdapter(mAdapter);
96
97 Log_OC.i(TAG, "onActivityCreated() stop");
98 }
99
100 /**
101 * Checks the file clicked over. Browses inside if it is a directory. 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 // save index and top position
114 saveIndexAndTopPosition(position);
115
116 } else { /// Click on a file
117 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
118 if (checkBoxV != null) {
119 if (getListView().isItemChecked(position)) {
120 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
121 } else {
122 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
123 }
124 }
125 // notify the change to the container Activity
126 mContainerActivity.onFileClick(file);
127 }
128
129 } else {
130 Log_OC.w(TAG, "Null object in ListAdapter!!");
131 }
132 }
133
134
135 /**
136 * Call this, when the user presses the up button
137 */
138 public void onNavigateUp() {
139 File parentDir = null;
140 if(mDirectory != null) {
141 parentDir = mDirectory.getParentFile(); // can be null
142 }
143 listDirectory(parentDir);
144
145 // restore index and top position
146 restoreIndexAndTopPosition();
147 }
148
149
150 /**
151 * Use this to query the {@link File} object for the directory
152 * that is currently being displayed by this fragment
153 *
154 * @return File The currently displayed directory
155 */
156 public File getCurrentDirectory(){
157 return mDirectory;
158 }
159
160
161 /**
162 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
163 * to refresh the current directory.
164 */
165 public void listDirectory(){
166 listDirectory(null);
167 }
168
169
170 /**
171 * Lists the given directory on the view. When the input parameter is null,
172 * it will either refresh the last known directory. list the root
173 * if there never was a directory.
174 *
175 * @param directory Directory to be listed
176 */
177 public void listDirectory(File directory) {
178
179 // Check input parameters for null
180 if(directory == null) {
181 if(mDirectory != null){
182 directory = mDirectory;
183 } else {
184 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
185 if (directory == null) return; // no files to show
186 }
187 }
188
189
190 // if that's not a directory -> List its parent
191 if(!directory.isDirectory()){
192 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
193 directory = directory.getParentFile();
194 }
195
196 mCurrentListView.clearChoices(); // by now, only files in the same directory will be kept as selected
197 mAdapter.swapDirectory(directory);
198 if (mDirectory == null || !mDirectory.equals(directory)) {
199 mCurrentListView.setSelection(0);
200 }
201 mDirectory = directory;
202 }
203
204
205 /**
206 * Returns the fule paths to the files checked by the user
207 *
208 * @return File paths to the files checked by the user.
209 */
210 public String[] getCheckedFilePaths() {
211 ArrayList<String> result = new ArrayList<String>();
212 SparseBooleanArray positions = mCurrentListView.getCheckedItemPositions();
213 if (positions.size() > 0) {
214 for (int i = 0; i < positions.size(); i++) {
215 if (positions.get(positions.keyAt(i)) == true) {
216 result.add(((File) mCurrentListView.getItemAtPosition(positions.keyAt(i))).getAbsolutePath());
217 }
218 }
219
220 Log_OC.d(TAG, "Returning " + result.size() + " selected files");
221 }
222 return result.toArray(new String[result.size()]);
223 }
224
225
226 /**
227 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
228 */
229 public interface ContainerActivity {
230
231 /**
232 * Callback method invoked when a directory is clicked by the user on the files list
233 *
234 * @param directory
235 */
236 public void onDirectoryClick(File directory);
237
238 /**
239 * Callback method invoked when a file (non directory) is clicked by the user on the files list
240 *
241 * @param file
242 */
243 public void onFileClick(File file);
244
245
246 /**
247 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
248 *
249 * @return Directory to list firstly. Can be NULL.
250 */
251 public File getInitialDirectory();
252
253 }
254
255
256 }