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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.fragment
;
23 import com
.owncloud
.android
.ui
.FragmentListView
;
24 import com
.owncloud
.android
.ui
.adapter
.LocalFileListAdapter
;
26 import android
.app
.Activity
;
27 import android
.os
.Bundle
;
28 import android
.os
.Environment
;
29 import android
.util
.Log
;
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
;
38 import com
.owncloud
.android
.Log_OC
;
39 import com
.owncloud
.android
.R
;
42 * A Fragment that lists all files and folders in a given LOCAL path.
44 * @author David A. Velasco
47 public class LocalFileListFragment
extends FragmentListView
{
48 private static final String TAG
= "LocalFileListFragment";
49 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
51 /** Reference to the Activity which this fragment is attached to. For callbacks */
52 private LocalFileListFragment
.ContainerActivity mContainerActivity
;
54 /** Directory to show */
55 private File mDirectory
= null
;
57 /** Adapter to connect the data from the directory with the View object */
58 private LocalFileListAdapter mAdapter
= null
;
65 public void onAttach(Activity activity
) {
66 super.onAttach(activity
);
68 mContainerActivity
= (ContainerActivity
) activity
;
69 } catch (ClassCastException e
) {
70 throw new ClassCastException(activity
.toString() + " must implement " + LocalFileListFragment
.ContainerActivity
.class.getSimpleName());
79 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
80 Log_OC
.i(TAG
, "onCreateView() start");
81 View v
= super.onCreateView(inflater
, container
, savedInstanceState
);
82 getListView().setChoiceMode(ListView
.CHOICE_MODE_MULTIPLE
);
83 Log_OC
.i(TAG
, "onCreateView() end");
92 public void onActivityCreated(Bundle savedInstanceState
) {
93 Log_OC
.i(TAG
, "onActivityCreated() start");
95 super.onCreate(savedInstanceState
);
96 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
97 setListAdapter(mAdapter
);
99 if (savedInstanceState
!= null
) {
100 Log_OC
.i(TAG
, "savedInstanceState is not null");
101 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
102 setReferencePosition(position
);
105 Log_OC
.i(TAG
, "onActivityCreated() stop");
110 public void onSaveInstanceState(Bundle savedInstanceState
) {
111 Log_OC
.i(TAG
, "onSaveInstanceState() start");
113 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
116 Log_OC
.i(TAG
, "onSaveInstanceState() stop");
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
);
127 /// Click on a directory
128 if (file
.isDirectory()) {
129 // just local updates
131 // notify the click to container Activity
132 mContainerActivity
.onDirectoryClick(file
);
134 } else { /// Click on a file
135 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
136 if (checkBoxV
!= null
) {
137 if (getListView().isItemChecked(position
)) {
138 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
140 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
143 // notify the change to the container Activity
144 mContainerActivity
.onFileClick(file
);
148 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
154 * Call this, when the user presses the up button
156 public void onNavigateUp() {
157 File parentDir
= null
;
158 if(mDirectory
!= null
) {
159 parentDir
= mDirectory
.getParentFile(); // can be null
161 listDirectory(parentDir
);
166 * Use this to query the {@link File} object for the directory
167 * that is currently being displayed by this fragment
169 * @return File The currently displayed directory
171 public File
getCurrentDirectory(){
177 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
178 * to refresh the current directory.
180 public void listDirectory(){
186 * Lists the given directory on the view. When the input parameter is null,
187 * it will either refresh the last known directory, or list the root
188 * if there never was a directory.
190 * @param directory Directory to be listed
192 public void listDirectory(File directory
) {
194 // Check input parameters for null
195 if(directory
== null
) {
196 if(mDirectory
!= null
){
197 directory
= mDirectory
;
199 directory
= Environment
.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
200 if (directory
== null
) return; // no files to show
205 // if that's not a directory -> List its parent
206 if(!directory
.isDirectory()){
207 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
208 directory
= directory
.getParentFile();
211 mList
.clearChoices(); // by now, only files in the same directory will be kept as selected
212 mAdapter
.swapDirectory(directory
);
213 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
214 mList
.setSelectionFromTop(0, 0);
216 mDirectory
= directory
;
221 * Returns the fule paths to the files checked by the user
223 * @return File paths to the files checked by the user.
225 public String
[] getCheckedFilePaths() {
226 String
[] result
= null
;
227 SparseBooleanArray positions
= mList
.getCheckedItemPositions();
228 if (positions
.size() > 0) {
229 Log_OC
.d(TAG
, "Returning " + positions
.size() + " selected files");
230 result
= new String
[positions
.size()];
231 for (int i
=0; i
<positions
.size(); i
++) {
232 result
[i
] = ((File
) mList
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath();
240 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
242 * @author David A. Velasco
244 public interface ContainerActivity
{
247 * Callback method invoked when a directory is clicked by the user on the files list
251 public void onDirectoryClick(File directory
);
254 * Callback method invoked when a file (non directory) is clicked by the user on the files list
258 public void onFileClick(File file
);
262 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
264 * @return Directory to list firstly. Can be NULL.
266 public File
getInitialDirectory();