1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.fragment
;
21 import java
.util
.ArrayList
;
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
;
34 import com
.owncloud
.android
.R
;
35 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
36 import com
.owncloud
.android
.ui
.adapter
.LocalFileListAdapter
;
40 * A Fragment that lists all files and folders in a given LOCAL path.
42 * @author David A. Velasco
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 " + LocalFileListFragment
.ContainerActivity
.class.getSimpleName());
76 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
77 Log_OC
.i(TAG
, "onCreateView() start");
78 View v
= super.onCreateView(inflater
, container
, savedInstanceState
);
79 getListView().setChoiceMode(ListView
.CHOICE_MODE_MULTIPLE
);
80 disableSwipe(); // Disable pull refresh
81 setMessageForEmptyList(getString(R
.string
.local_file_list_empty
));
82 Log_OC
.i(TAG
, "onCreateView() end");
91 public void onActivityCreated(Bundle savedInstanceState
) {
92 Log_OC
.i(TAG
, "onActivityCreated() start");
94 super.onActivityCreated(savedInstanceState
);
95 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
96 setListAdapter(mAdapter
);
98 Log_OC
.i(TAG
, "onActivityCreated() stop");
101 public void selectAll(){
102 int numberOfFiles
= mAdapter
.getCount();
103 for(int i
= 0; i
< numberOfFiles
; i
++){
104 File file
= (File
) mAdapter
.getItem(i
);
106 if (!file
.isDirectory()) {
108 getListView().setItemChecked(i
, true
);
109 // notify the change to the container Activity
110 mContainerActivity
.onFileClick(file
);
116 public void deselectAll(){
117 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
118 setListAdapter(mAdapter
);
122 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
125 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
126 File file
= (File
) mAdapter
.getItem(position
);
129 /// Click on a directory
130 if (file
.isDirectory()) {
131 // just local updates
133 // notify the click to container Activity
134 mContainerActivity
.onDirectoryClick(file
);
135 // save index and top position
136 saveIndexAndTopPosition(position
);
138 } else { /// Click on a file
139 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
140 if (checkBoxV
!= null
) {
141 if (getListView().isItemChecked(position
)) {
142 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
144 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
147 // notify the change to the container Activity
148 mContainerActivity
.onFileClick(file
);
152 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
158 * Call this, when the user presses the up button
160 public void onNavigateUp() {
161 File parentDir
= null
;
162 if(mDirectory
!= null
) {
163 parentDir
= mDirectory
.getParentFile(); // can be null
165 listDirectory(parentDir
);
167 // restore index and top position
168 restoreIndexAndTopPosition();
173 * Use this to query the {@link File} object for the directory
174 * that is currently being displayed by this fragment
176 * @return File The currently displayed directory
178 public File
getCurrentDirectory(){
184 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
185 * to refresh the current directory.
187 public void listDirectory(){
193 * Lists the given directory on the view. When the input parameter is null,
194 * it will either refresh the last known directory. list the root
195 * if there never was a directory.
197 * @param directory Directory to be listed
199 public void listDirectory(File directory
) {
201 // Check input parameters for null
202 if(directory
== null
) {
203 if(mDirectory
!= null
){
204 directory
= mDirectory
;
206 directory
= Environment
.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
207 if (directory
== null
) return; // no files to show
212 // if that's not a directory -> List its parent
213 if(!directory
.isDirectory()){
214 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
215 directory
= directory
.getParentFile();
218 mList
.clearChoices(); // by now, only files in the same directory will be kept as selected
219 mAdapter
.swapDirectory(directory
);
220 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
221 mList
.setSelectionFromTop(0, 0);
223 mDirectory
= directory
;
228 * Returns the fule paths to the files checked by the user
230 * @return File paths to the files checked by the user.
232 public String
[] getCheckedFilePaths() {
233 ArrayList
<String
> result
= new ArrayList
<String
>();
234 SparseBooleanArray positions
= mList
.getCheckedItemPositions();
235 if (positions
.size() > 0) {
236 for (int i
= 0; i
< positions
.size(); i
++) {
237 if (positions
.get(positions
.keyAt(i
)) == true
) {
238 result
.add(((File
) mList
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath());
242 Log_OC
.d(TAG
, "Returning " + result
.size() + " selected files");
244 return result
.toArray(new String
[result
.size()]);
249 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
251 * @author David A. Velasco
253 public interface ContainerActivity
{
256 * Callback method invoked when a directory is clicked by the user on the files list
260 public void onDirectoryClick(File directory
);
263 * Callback method invoked when a file (non directory) is clicked by the user on the files list
267 public void onFileClick(File file
);
271 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
273 * @return Directory to list firstly. Can be NULL.
275 public File
getInitialDirectory();