1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.ui
.FragmentListView
;
23 import com
.owncloud
.android
.ui
.adapter
.LocalFileListAdapter
;
25 import android
.app
.Activity
;
26 import android
.os
.Bundle
;
27 import android
.os
.Environment
;
28 import android
.util
.Log
;
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
;
40 * A Fragment that lists all files and folders in a given LOCAL path.
42 * @author David A. Velasco
45 public class LocalFileListFragment
extends FragmentListView
{
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.getCanonicalName());
76 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
77 Log
.i(TAG
, "onCreateView() start");
78 super.onCreateView(inflater
, container
, savedInstanceState
);
79 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
80 getListView().setDividerHeight(1);
81 getListView().setChoiceMode(ListView
.CHOICE_MODE_MULTIPLE
);
83 Log
.i(TAG
, "onCreateView() end");
92 public void onActivityCreated(Bundle savedInstanceState
) {
93 Log
.i(TAG
, "onActivityCreated() start");
95 super.onCreate(savedInstanceState
);
96 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
97 setListAdapter(mAdapter
);
99 if (savedInstanceState
!= null
) {
100 Log
.i(TAG
, "savedInstanceState is not null");
101 int position
= savedInstanceState
.getInt("LIST_POSITION");
102 getListView().setSelectionFromTop(position
, 0);
105 Log
.i(TAG
, "onActivityCreated() stop");
110 public void onSaveInstanceState(Bundle savedInstanceState
) {
111 Log
.i(TAG
, "onSaveInstanceState() start");
113 savedInstanceState
.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
115 Log
.i(TAG
, "onSaveInstanceState() stop");
120 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
123 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
124 File file
= (File
) mAdapter
.getItem(position
);
126 /// Click on a directory
127 if (file
.isDirectory()) {
128 // just local updates
130 // notify the click to container Activity
131 mContainerActivity
.onDirectoryClick(file
);
133 } else { /// Click on a file
134 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
135 if (checkBoxV
!= null
) {
136 if (getListView().isItemChecked(position
)) {
137 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
139 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
142 // notify the change to the container Activity
143 mContainerActivity
.onFileClick(file
);
147 Log
.w(TAG
, "Null object in ListAdapter!!");
153 * Call this, when the user presses the up button
155 public void onNavigateUp() {
156 File parentDir
= null
;
157 if(mDirectory
!= null
) {
158 parentDir
= mDirectory
.getParentFile(); // can be null
160 listDirectory(parentDir
);
165 * Use this to query the {@link File} object for the directory
166 * that is currently being displayed by this fragment
168 * @return File The currently displayed directory
170 public File
getCurrentDirectory(){
176 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
177 * to refresh the current directory.
179 public void listDirectory(){
185 * Lists the given directory on the view. When the input parameter is null,
186 * it will either refresh the last known directory, or list the root
187 * if there never was a directory.
189 * @param directory Directory to be listed
191 public void listDirectory(File directory
) {
193 // Check input parameters for null
194 if(directory
== null
) {
195 if(mDirectory
!= null
){
196 directory
= mDirectory
;
198 directory
= Environment
.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
199 if (directory
== null
) return; // no files to show
204 // if that's not a directory -> List its parent
205 if(!directory
.isDirectory()){
206 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
207 directory
= directory
.getParentFile();
210 mDirectory
= directory
;
211 mList
.clearChoices(); // by now, only files in the same directory will be kept as selected
212 mAdapter
.swapDirectory(mDirectory
);
213 mList
.setSelectionFromTop(0, 0);
218 * Returns the fule paths to the files checked by the user
220 * @return File paths to the files checked by the user.
222 public String
[] getCheckedFilePaths() {
223 String
[] result
= null
;
224 SparseBooleanArray positions
= mList
.getCheckedItemPositions();
225 if (positions
.size() > 0) {
226 Log
.d(TAG
, "Returning " + positions
.size() + " selected files");
227 result
= new String
[positions
.size()];
228 for (int i
=0; i
<positions
.size(); i
++) {
229 result
[i
] = ((File
) mList
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath();
237 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
239 * @author David A. Velasco
241 public interface ContainerActivity
{
244 * Callback method invoked when a directory is clicked by the user on the files list
248 public void onDirectoryClick(File directory
);
251 * Callback method invoked when a file (non directory) is clicked by the user on the files list
255 public void onFileClick(File file
);
259 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
261 * @return Directory to list firstly. Can be NULL.
263 public File
getInitialDirectory();