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
;
43 * A Fragment that lists all files and folders in a given LOCAL path.
45 public class LocalFileListFragment
extends ExtendedListFragment
{
46 private static final String TAG
= "LocalFileListFragment";
48 /** Reference to the Activity which this fragment is attached to. For callbacks */
49 private LocalFileListFragment
.ContainerActivity mContainerActivity
;
51 /** Directory to show */
52 private File mDirectory
= null
;
54 /** Adapter to connect the data from the directory with the View object */
55 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 setMessageForEmptyList(getString(R
.string
.local_file_list_empty
));
83 Log_OC
.i(TAG
, "onCreateView() end");
92 public void onActivityCreated(Bundle savedInstanceState
) {
93 Log_OC
.i(TAG
, "onActivityCreated() start");
95 super.onActivityCreated(savedInstanceState
);
96 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
97 setListAdapter(mAdapter
);
99 Log_OC
.i(TAG
, "onActivityCreated() stop");
103 * Checks the file clicked over. Browses inside if it is a directory.
104 * Notifies the container activity in any case.
107 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
108 File file
= (File
) mAdapter
.getItem(position
);
110 /// Click on a directory
111 if (file
.isDirectory()) {
112 // just local updates
114 // notify the click to container Activity
115 mContainerActivity
.onDirectoryClick(file
);
116 // save index and top position
117 saveIndexAndTopPosition(position
);
119 } else { /// Click on a file
120 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
121 if (checkBoxV
!= null
) {
122 if (((ListView
)getListView()).isItemChecked(position
)) {
123 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
125 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
128 // notify the change to the container Activity
129 mContainerActivity
.onFileClick(file
);
133 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
139 * Call this, when the user presses the up button
141 public void onNavigateUp() {
142 File parentDir
= null
;
143 if(mDirectory
!= null
) {
144 parentDir
= mDirectory
.getParentFile(); // can be null
146 listDirectory(parentDir
);
148 // restore index and top position
149 restoreIndexAndTopPosition();
154 * Use this to query the {@link File} object for the directory
155 * that is currently being displayed by this fragment
157 * @return File The currently displayed directory
159 public File
getCurrentDirectory(){
165 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
166 * to refresh the current directory.
168 public void listDirectory(){
174 * Lists the given directory on the view. When the input parameter is null,
175 * it will either refresh the last known directory. list the root
176 * if there never was a directory.
178 * @param directory Directory to be listed
180 public void listDirectory(File directory
) {
182 // Check input parameters for null
183 if(directory
== null
) {
184 if(mDirectory
!= null
){
185 directory
= mDirectory
;
187 directory
= Environment
.getExternalStorageDirectory();
188 // TODO be careful with the state of the storage; could not be available
189 if (directory
== null
) return; // no files to show
194 // if that's not a directory -> List its parent
195 if(!directory
.isDirectory()){
196 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
197 directory
= directory
.getParentFile();
200 // by now, only files in the same directory will be kept as selected
201 ((ListView
)mCurrentListView
).clearChoices();
202 mAdapter
.swapDirectory(directory
);
203 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
204 mCurrentListView
.setSelection(0);
206 mDirectory
= directory
;
211 * Returns the fule paths to the files checked by the user
213 * @return File paths to the files checked by the user.
215 public String
[] getCheckedFilePaths() {
216 ArrayList
<String
> result
= new ArrayList
<String
>();
217 SparseBooleanArray positions
= ((ListView
)mCurrentListView
).getCheckedItemPositions();
218 if (positions
.size() > 0) {
219 for (int i
= 0; i
< positions
.size(); i
++) {
220 if (positions
.get(positions
.keyAt(i
)) == true
) {
221 result
.add(((File
) mCurrentListView
.getItemAtPosition(
222 positions
.keyAt(i
))).getAbsolutePath());
226 Log_OC
.d(TAG
, "Returning " + result
.size() + " selected files");
228 return result
.toArray(new String
[result
.size()]);
233 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
235 public interface ContainerActivity
{
238 * Callback method invoked when a directory is clicked by the user on the files list
242 public void onDirectoryClick(File directory
);
245 * Callback method invoked when a file (non directory)
246 * is clicked by the user on the files list
250 public void onFileClick(File file
);
254 * Callback method invoked when the parent activity
255 * is fully created to get the directory to list firstly.
257 * @return Directory to list firstly. Can be NULL.
259 public File
getInitialDirectory();