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