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
;
21 import java
.util
.ArrayList
;
23 import android
.app
.Activity
;
24 import android
.os
.Bundle
;
25 import android
.os
.Environment
;
26 import android
.util
.SparseBooleanArray
;
27 import android
.view
.LayoutInflater
;
28 import android
.view
.View
;
29 import android
.view
.ViewGroup
;
30 import android
.widget
.AdapterView
;
31 import android
.widget
.ImageView
;
32 import android
.widget
.ListView
;
34 import com
.owncloud
.android
.Log_OC
;
35 import com
.owncloud
.android
.R
;
36 import com
.owncloud
.android
.ui
.adapter
.LocalFileListAdapter
;
39 * A Fragment that lists all files and folders in a given LOCAL path.
41 * @author David A. Velasco
44 public class LocalFileListFragment
extends ExtendedListFragment
{
45 private static final String TAG
= "LocalFileListFragment";
48 * Reference to the Activity which this fragment is attached to. For
51 private LocalFileListFragment
.ContainerActivity mContainerActivity
;
53 /** Directory to show */
54 private File mDirectory
= null
;
56 /** Adapter to connect the data from the directory with the View object */
57 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 "
69 + 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");
89 public void onActivityCreated(Bundle savedInstanceState
) {
90 Log_OC
.i(TAG
, "onActivityCreated() start");
92 super.onCreate(savedInstanceState
);
93 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
94 setListAdapter(mAdapter
);
96 Log_OC
.i(TAG
, "onActivityCreated() stop");
100 * Checks the file clicked over. Browses inside if it is a directory.
101 * 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
);
114 } else { // / Click on a file
115 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
116 if (checkBoxV
!= null
) {
117 if (getListView().isItemChecked(position
)) {
118 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
120 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
123 // notify the change to the container Activity
124 mContainerActivity
.onFileClick(file
);
128 Log_OC
.w(TAG
, "Null object in ListAdapter!!");
133 * Call this, when the user presses the up button
135 public void onNavigateUp() {
136 File parentDir
= null
;
137 if (mDirectory
!= null
) {
138 parentDir
= mDirectory
.getParentFile(); // can be null
140 listDirectory(parentDir
);
144 * Use this to query the {@link File} object for the directory that is
145 * currently being displayed by this fragment
147 * @return File The currently displayed directory
149 public File
getCurrentDirectory() {
154 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null
155 * parameter to refresh the current directory.
157 public void listDirectory() {
162 * Lists the given directory on the view. When the input parameter is null,
163 * it will either refresh the last known directory. list the root if there
164 * never was a directory.
166 * @param directory Directory to be listed
168 public void listDirectory(File directory
) {
170 // Check input parameters for null
171 if (directory
== null
) {
172 if (mDirectory
!= null
) {
173 directory
= mDirectory
;
175 directory
= Environment
.getExternalStorageDirectory(); // TODO
186 if (directory
== null
)
187 return; // no files to show
191 // if that's not a directory -> List its parent
192 if (!directory
.isDirectory()) {
193 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
194 directory
= directory
.getParentFile();
197 mList
.clearChoices(); // by now, only files in the same directory will
198 // be kept as selected
199 mAdapter
.swapDirectory(directory
);
200 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
201 mList
.setSelectionFromTop(0, 0);
203 mDirectory
= directory
;
207 * Returns the fule paths to the files checked by the user
209 * @return File paths to the files checked by the user.
211 public String
[] getCheckedFilePaths() {
212 ArrayList
<String
> result
= new ArrayList
<String
>();
213 SparseBooleanArray positions
= mList
.getCheckedItemPositions();
214 if (positions
.size() > 0) {
215 for (int i
= 0; i
< positions
.size(); i
++) {
216 if (positions
.get(positions
.keyAt(i
)) == true
) {
217 result
.add(((File
) mList
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath());
221 Log_OC
.d(TAG
, "Returning " + result
.size() + " selected files");
223 return result
.toArray(new String
[result
.size()]);
227 * Interface to implement by any Activity that includes some instance of
228 * LocalFileListFragment
230 * @author David A. Velasco
232 public interface ContainerActivity
{
235 * Callback method invoked when a directory is clicked by the user on
240 public void onDirectoryClick(File directory
);
243 * Callback method invoked when a file (non directory) is clicked by the
244 * user on the files list
248 public void onFileClick(File file
);
251 * Callback method invoked when the parent activity is fully created to
252 * get the directory to list firstly.
254 * @return Directory to list firstly. Can be NULL.
256 public File
getInitialDirectory();