1 /* ownCloud Android client application
3 * @author David A. Velasco
4 * Copyright (C) 2011 Bartek Przybylski
5 * Copyright (C) 2012-2013 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.fragment
;
23 import java
.util
.ArrayList
;
25 import android
.app
.Activity
;
26 import android
.os
.Bundle
;
27 import android
.os
.Environment
;
28 import android
.util
.SparseBooleanArray
;
29 import android
.view
.LayoutInflater
;
30 import android
.view
.View
;
31 import android
.view
.ViewGroup
;
32 import android
.widget
.AdapterView
;
33 import android
.widget
.ImageView
;
34 import android
.widget
.ListView
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
38 import com
.owncloud
.android
.ui
.adapter
.LocalFileListAdapter
;
42 * A Fragment that lists all files and folders in a given LOCAL path.
44 public class LocalFileListFragment
extends ExtendedListFragment
{
45 private static final String TAG
= "LocalFileListFragment";
47 /** Reference to the Activity which this fragment is attached to. For callbacks */
48 private LocalFileListFragment
.ContainerActivity mContainerActivity
;
50 /** Directory to show */
51 private File mDirectory
= null
;
53 /** Adapter to connect the data from the directory with the View object */
54 private LocalFileListAdapter mAdapter
= null
;
61 public void onAttach(Activity activity
) {
62 super.onAttach(activity
);
64 mContainerActivity
= (ContainerActivity
) activity
;
65 } catch (ClassCastException e
) {
66 throw new ClassCastException(activity
.toString() + " must implement " + LocalFileListFragment
.ContainerActivity
.class.getSimpleName());
75 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
76 Log_OC
.i(TAG
, "onCreateView() start");
77 View v
= super.onCreateView(inflater
, container
, savedInstanceState
);
78 setChoiceMode(ListView
.CHOICE_MODE_MULTIPLE
);
79 setSwipeEnabled(false
); // Disable pull-to-refresh
80 setMessageForEmptyList(getString(R
.string
.local_file_list_empty
));
81 Log_OC
.i(TAG
, "onCreateView() end");
90 public void onActivityCreated(Bundle savedInstanceState
) {
91 Log_OC
.i(TAG
, "onActivityCreated() start");
93 super.onActivityCreated(savedInstanceState
);
94 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
95 setListAdapter(mAdapter
);
97 Log_OC
.i(TAG
, "onActivityCreated() stop");
101 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
104 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
105 File file
= (File
) mAdapter
.getItem(position
);
107 /// Click on a directory
108 if (file
.isDirectory()) {
109 // just local updates
111 // notify the click to container Activity
112 mContainerActivity
.onDirectoryClick(file
);
113 // save index and top position
114 saveIndexAndTopPosition(position
);
116 } else { /// Click on a file
117 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
118 if (checkBoxV
!= null
) {
119 if (getListView().isItemChecked(position
)) {
120 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
122 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
125 // notify the change to the container Activity
126 mContainerActivity
.onFileClick(file
);
130 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
136 * Call this, when the user presses the up button
138 public void onNavigateUp() {
139 File parentDir
= null
;
140 if(mDirectory
!= null
) {
141 parentDir
= mDirectory
.getParentFile(); // can be null
143 listDirectory(parentDir
);
145 // restore index and top position
146 restoreIndexAndTopPosition();
151 * Use this to query the {@link File} object for the directory
152 * that is currently being displayed by this fragment
154 * @return File The currently displayed directory
156 public File
getCurrentDirectory(){
162 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
163 * to refresh the current directory.
165 public void listDirectory(){
171 * Lists the given directory on the view. When the input parameter is null,
172 * it will either refresh the last known directory. list the root
173 * if there never was a directory.
175 * @param directory Directory to be listed
177 public void listDirectory(File directory
) {
179 // Check input parameters for null
180 if(directory
== null
) {
181 if(mDirectory
!= null
){
182 directory
= mDirectory
;
184 directory
= Environment
.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
185 if (directory
== null
) return; // no files to show
190 // if that's not a directory -> List its parent
191 if(!directory
.isDirectory()){
192 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
193 directory
= directory
.getParentFile();
196 mCurrentListView
.clearChoices(); // by now, only files in the same directory will be kept as selected
197 mAdapter
.swapDirectory(directory
);
198 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
199 mCurrentListView
.setSelection(0);
201 mDirectory
= directory
;
206 * Returns the fule paths to the files checked by the user
208 * @return File paths to the files checked by the user.
210 public String
[] getCheckedFilePaths() {
211 ArrayList
<String
> result
= new ArrayList
<String
>();
212 SparseBooleanArray positions
= mCurrentListView
.getCheckedItemPositions();
213 if (positions
.size() > 0) {
214 for (int i
= 0; i
< positions
.size(); i
++) {
215 if (positions
.get(positions
.keyAt(i
)) == true
) {
216 result
.add(((File
) mCurrentListView
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath());
220 Log_OC
.d(TAG
, "Returning " + result
.size() + " selected files");
222 return result
.toArray(new String
[result
.size()]);
227 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
229 public interface ContainerActivity
{
232 * Callback method invoked when a directory is clicked by the user on the files list
236 public void onDirectoryClick(File directory
);
239 * Callback method invoked when a file (non directory) is clicked by the user on the files list
243 public void onFileClick(File file
);
247 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
249 * @return Directory to list firstly. Can be NULL.
251 public File
getInitialDirectory();