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
.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
.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
.Log_OC
;
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";
47 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
49 /** Reference to the Activity which this fragment is attached to. For callbacks */
50 private LocalFileListFragment
.ContainerActivity mContainerActivity
;
52 /** Directory to show */
53 private File mDirectory
= null
;
55 /** Adapter to connect the data from the directory with the View object */
56 private LocalFileListAdapter mAdapter
= null
;
63 public void onAttach(Activity activity
) {
64 super.onAttach(activity
);
66 mContainerActivity
= (ContainerActivity
) activity
;
67 } catch (ClassCastException e
) {
68 throw new ClassCastException(activity
.toString() + " must implement " + 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 getListView().setChoiceMode(ListView
.CHOICE_MODE_MULTIPLE
);
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 if (savedInstanceState
!= null
) {
98 Log_OC
.i(TAG
, "savedInstanceState is not null");
99 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
100 setReferencePosition(position
);
103 Log_OC
.i(TAG
, "onActivityCreated() stop");
108 public void onSaveInstanceState(Bundle savedInstanceState
) {
109 Log_OC
.i(TAG
, "onSaveInstanceState() start");
111 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
114 Log_OC
.i(TAG
, "onSaveInstanceState() stop");
119 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
122 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
123 File file
= (File
) mAdapter
.getItem(position
);
125 /// Click on a directory
126 if (file
.isDirectory()) {
127 // just local updates
129 // notify the click to container Activity
130 mContainerActivity
.onDirectoryClick(file
);
132 } else { /// Click on a file
133 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
134 if (checkBoxV
!= null
) {
135 if (getListView().isItemChecked(position
)) {
136 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
138 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
141 // notify the change to the container Activity
142 mContainerActivity
.onFileClick(file
);
146 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
152 * Call this, when the user presses the up button
154 public void onNavigateUp() {
155 File parentDir
= null
;
156 if(mDirectory
!= null
) {
157 parentDir
= mDirectory
.getParentFile(); // can be null
159 listDirectory(parentDir
);
164 * Use this to query the {@link File} object for the directory
165 * that is currently being displayed by this fragment
167 * @return File The currently displayed directory
169 public File
getCurrentDirectory(){
175 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
176 * to refresh the current directory.
178 public void listDirectory(){
184 * Lists the given directory on the view. When the input parameter is null,
185 * it will either refresh the last known directory. list the root
186 * if there never was a directory.
188 * @param directory Directory to be listed
190 public void listDirectory(File directory
) {
192 // Check input parameters for null
193 if(directory
== null
) {
194 if(mDirectory
!= null
){
195 directory
= mDirectory
;
197 directory
= Environment
.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
198 if (directory
== null
) return; // no files to show
203 // if that's not a directory -> List its parent
204 if(!directory
.isDirectory()){
205 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
206 directory
= directory
.getParentFile();
209 mList
.clearChoices(); // by now, only files in the same directory will be kept as selected
210 mAdapter
.swapDirectory(directory
);
211 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
212 mList
.setSelectionFromTop(0, 0);
214 mDirectory
= directory
;
219 * Returns the fule paths to the files checked by the user
221 * @return File paths to the files checked by the user.
223 public String
[] getCheckedFilePaths() {
224 String
[] result
= null
;
225 SparseBooleanArray positions
= mList
.getCheckedItemPositions();
226 if (positions
.size() > 0) {
227 Log_OC
.d(TAG
, "Returning " + positions
.size() + " selected files");
228 result
= new String
[positions
.size()];
229 for (int i
=0; i
<positions
.size(); i
++) {
230 result
[i
] = ((File
) mList
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath();
238 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
240 * @author David A. Velasco
242 public interface ContainerActivity
{
245 * Callback method invoked when a directory is clicked by the user on the files list
249 public void onDirectoryClick(File directory
);
252 * Callback method invoked when a file (non directory) is clicked by the user on the files list
256 public void onFileClick(File file
);
260 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
262 * @return Directory to list firstly. Can be NULL.
264 public File
getInitialDirectory();