2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2011 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.ui
.fragment
;
24 import java
.util
.ArrayList
;
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
;
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
;
44 * A Fragment that lists all files and folders in a given LOCAL path.
46 public class LocalFileListFragment
extends ExtendedListFragment
{
47 private static final String TAG
= "LocalFileListFragment";
49 /** Reference to the Activity which this fragment is attached to. For callbacks */
50 private LocalFileListFragment
.ContainerActivity mContainerActivity
;
52 /** Directory to show */
53 private File mDirectory
= null
;
55 /** Adapter to connect the data from the directory with the View object */
56 private LocalFileListAdapter mAdapter
= null
;
62 public void onAttach(Activity activity
) {
63 super.onAttach(activity
);
65 mContainerActivity
= (ContainerActivity
) activity
;
66 } catch (ClassCastException e
) {
67 throw new ClassCastException(activity
.toString() + " must implement " +
68 LocalFileListFragment
.ContainerActivity
.class.getSimpleName());
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");
93 public void onActivityCreated(Bundle savedInstanceState
) {
94 Log_OC
.i(TAG
, "onActivityCreated() start");
96 super.onActivityCreated(savedInstanceState
);
97 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
98 setListAdapter(mAdapter
);
100 Log_OC
.i(TAG
, "onActivityCreated() stop");
104 * Checks the file clicked over. Browses inside if it is a directory.
105 * Notifies the container activity in any case.
108 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
109 File file
= (File
) mAdapter
.getItem(position
);
111 /// Click on a directory
112 if (file
.isDirectory()) {
113 // just local updates
115 // notify the click to container Activity
116 mContainerActivity
.onDirectoryClick(file
);
117 // save index and top position
118 saveIndexAndTopPosition(position
);
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
);
126 checkBoxV
.setImageResource(R
.drawable
.ic_checkbox_blank_outline
);
129 // notify the change to the container Activity
130 mContainerActivity
.onFileClick(file
);
134 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
140 * Call this, when the user presses the up button
142 public void onNavigateUp() {
143 File parentDir
= null
;
144 if(mDirectory
!= null
) {
145 parentDir
= mDirectory
.getParentFile(); // can be null
147 listDirectory(parentDir
);
149 // restore index and top position
150 restoreIndexAndTopPosition();
155 * Use this to query the {@link File} object for the directory
156 * that is currently being displayed by this fragment
158 * @return File The currently displayed directory
160 public File
getCurrentDirectory(){
166 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
167 * to refresh the current directory.
169 public void listDirectory(){
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.
179 * @param directory Directory to be listed
181 public void listDirectory(File directory
) {
183 // Check input parameters for null
184 if(directory
== null
) {
185 if(mDirectory
!= null
){
186 directory
= mDirectory
;
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
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();
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);
207 mDirectory
= directory
;
212 * Returns the fule paths to the files checked by the user
214 * @return File paths to the files checked by the user.
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());
227 Log_OC
.d(TAG
, "Returning " + result
.size() + " selected files");
229 return result
.toArray(new String
[result
.size()]);
232 public void sortByName(boolean descending
) {
233 mAdapter
.setSortOrder(FileStorageUtils
.SORT_NAME
, descending
);
236 public void sortByDate(boolean descending
) {
237 mAdapter
.setSortOrder(FileStorageUtils
.SORT_DATE
, descending
);
240 public void sortBySize(boolean descending
) {
241 mAdapter
.setSortOrder(FileStorageUtils
.SORT_SIZE
, descending
);
246 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
248 public interface ContainerActivity
{
251 * Callback method invoked when a directory is clicked by the user on the files list
255 public void onDirectoryClick(File directory
);
258 * Callback method invoked when a file (non directory)
259 * is clicked by the user on the files list
263 public void onFileClick(File file
);
267 * Callback method invoked when the parent activity
268 * is fully created to get the directory to list firstly.
270 * @return Directory to list firstly. Can be NULL.
272 public File
getInitialDirectory();