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
;
22 import com
.owncloud
.android
.R
;
23 import com
.owncloud
.android
.ui
.adapter
.LocalFileListAdapter
;
24 import com
.owncloud
.android
.utils
.Log_OC
;
27 import android
.app
.Activity
;
28 import android
.os
.Bundle
;
29 import android
.os
.Environment
;
30 import android
.util
.SparseBooleanArray
;
31 import android
.view
.LayoutInflater
;
32 import android
.view
.View
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.AdapterView
;
35 import android
.widget
.ImageView
;
36 import android
.widget
.ListView
;
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 Log_OC
.i(TAG
, "onCreateView() end");
90 public void onActivityCreated(Bundle savedInstanceState
) {
91 Log_OC
.i(TAG
, "onActivityCreated() start");
93 super.onCreate(savedInstanceState
);
94 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
95 setListAdapter(mAdapter
);
97 Log_OC
.i(TAG
, "onActivityCreated() stop");
100 public void selectAll(){
101 int numberOfFiles
= mAdapter
.getCount();
102 for(int i
= 0; i
< numberOfFiles
; i
++){
103 File file
= (File
) mAdapter
.getItem(i
);
105 if (!file
.isDirectory()) {
107 getListView().setItemChecked(i
, true
);
108 // notify the change to the container Activity
109 mContainerActivity
.onFileClick(file
);
115 public void deselectAll(){
116 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
117 setListAdapter(mAdapter
);
121 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
124 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
125 File file
= (File
) mAdapter
.getItem(position
);
128 /// Click on a directory
129 if (file
.isDirectory()) {
130 // just local updates
132 // notify the click to container Activity
133 mContainerActivity
.onDirectoryClick(file
);
135 } else { /// Click on a file
136 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
137 if (checkBoxV
!= null
) {
138 if (getListView().isItemChecked(position
)) {
139 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
141 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
144 // notify the change to the container Activity
145 mContainerActivity
.onFileClick(file
);
149 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
155 * Call this, when the user presses the up button
157 public void onNavigateUp() {
158 File parentDir
= null
;
159 if(mDirectory
!= null
) {
160 parentDir
= mDirectory
.getParentFile(); // can be null
162 listDirectory(parentDir
);
167 * Use this to query the {@link File} object for the directory
168 * that is currently being displayed by this fragment
170 * @return File The currently displayed directory
172 public File
getCurrentDirectory(){
178 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
179 * to refresh the current directory.
181 public void listDirectory(){
187 * Lists the given directory on the view. When the input parameter is null,
188 * it will either refresh the last known directory. list the root
189 * if there never was a directory.
191 * @param directory Directory to be listed
193 public void listDirectory(File directory
) {
195 // Check input parameters for null
196 if(directory
== null
) {
197 if(mDirectory
!= null
){
198 directory
= mDirectory
;
200 directory
= Environment
.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
201 if (directory
== null
) return; // no files to show
206 // if that's not a directory -> List its parent
207 if(!directory
.isDirectory()){
208 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
209 directory
= directory
.getParentFile();
212 mList
.clearChoices(); // by now, only files in the same directory will be kept as selected
213 mAdapter
.swapDirectory(directory
);
214 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
215 mList
.setSelectionFromTop(0, 0);
217 mDirectory
= directory
;
222 * Returns the fule paths to the files checked by the user
224 * @return File paths to the files checked by the user.
226 public String
[] getCheckedFilePaths() {
227 String
[] result
= null
;
228 SparseBooleanArray positions
= mList
.getCheckedItemPositions();
229 if (positions
.size() > 0) {
230 Log_OC
.d(TAG
, "Returning " + positions
.size() + " selected files");
231 result
= new String
[positions
.size()];
232 for (int i
=0; i
<positions
.size(); i
++) {
233 result
[i
] = ((File
) mList
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath();
241 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
243 * @author David A. Velasco
245 public interface ContainerActivity
{
248 * Callback method invoked when a directory is clicked by the user on the files list
252 public void onDirectoryClick(File directory
);
255 * Callback method invoked when a file (non directory) is clicked by the user on the files list
259 public void onFileClick(File file
);
263 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
265 * @return Directory to list firstly. Can be NULL.
267 public File
getInitialDirectory();