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
.ExtendedListView
;
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
.R
;
41 * A Fragment that lists all files and folders in a given LOCAL path.
43 * @author David A. Velasco
46 public class LocalFileListFragment
extends FragmentListView
{
47 private static final String TAG
= "LocalFileListFragment";
48 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
50 /** Reference to the Activity which this fragment is attached to. For callbacks */
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
;
64 public void onAttach(Activity activity
) {
65 super.onAttach(activity
);
67 mContainerActivity
= (ContainerActivity
) activity
;
68 } catch (ClassCastException e
) {
69 throw new ClassCastException(activity
.toString() + " must implement " + LocalFileListFragment
.ContainerActivity
.class.getCanonicalName());
78 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
79 Log
.i(TAG
, "onCreateView() start");
80 super.onCreateView(inflater
, container
, savedInstanceState
);
81 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
82 getListView().setDividerHeight(1);
83 getListView().setChoiceMode(ListView
.CHOICE_MODE_MULTIPLE
);
85 Log
.i(TAG
, "onCreateView() end");
94 public void onActivityCreated(Bundle savedInstanceState
) {
95 Log
.i(TAG
, "onActivityCreated() start");
97 super.onCreate(savedInstanceState
);
98 mAdapter
= new LocalFileListAdapter(mContainerActivity
.getInitialDirectory(), getActivity());
99 setListAdapter(mAdapter
);
101 if (savedInstanceState
!= null
) {
102 Log
.i(TAG
, "savedInstanceState is not null");
103 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
104 setReferencePosition(position
);
107 Log
.i(TAG
, "onActivityCreated() stop");
112 public void onSaveInstanceState(Bundle savedInstanceState
) {
113 Log
.i(TAG
, "onSaveInstanceState() start");
115 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
118 Log
.i(TAG
, "onSaveInstanceState() stop");
123 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
124 * the device is turned to other position.
126 * THe current policy is take as a reference the visible item in the center of the screen.
128 * @return The position in the list of the visible item in the center of the screen.
130 private int getReferencePosition() {
131 return (getListView().getFirstVisiblePosition() + getListView().getLastVisiblePosition()) / 2;
136 * Sets the visible part of the list from the reference position.
138 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
140 private void setReferencePosition(int position
) {
141 ((ExtendedListView
)getListView()).setAndCenterSelection(position
);
146 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
149 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
150 File file
= (File
) mAdapter
.getItem(position
);
152 /// Click on a directory
153 if (file
.isDirectory()) {
154 // just local updates
156 // notify the click to container Activity
157 mContainerActivity
.onDirectoryClick(file
);
159 } else { /// Click on a file
160 ImageView checkBoxV
= (ImageView
) v
.findViewById(R
.id
.custom_checkbox
);
161 if (checkBoxV
!= null
) {
162 if (getListView().isItemChecked(position
)) {
163 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
165 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
168 // notify the change to the container Activity
169 mContainerActivity
.onFileClick(file
);
173 Log
.w(TAG
, "Null object in ListAdapter!!");
179 * Call this, when the user presses the up button
181 public void onNavigateUp() {
182 File parentDir
= null
;
183 if(mDirectory
!= null
) {
184 parentDir
= mDirectory
.getParentFile(); // can be null
186 listDirectory(parentDir
);
191 * Use this to query the {@link File} object for the directory
192 * that is currently being displayed by this fragment
194 * @return File The currently displayed directory
196 public File
getCurrentDirectory(){
202 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
203 * to refresh the current directory.
205 public void listDirectory(){
211 * Lists the given directory on the view. When the input parameter is null,
212 * it will either refresh the last known directory, or list the root
213 * if there never was a directory.
215 * @param directory Directory to be listed
217 public void listDirectory(File directory
) {
219 // Check input parameters for null
220 if(directory
== null
) {
221 if(mDirectory
!= null
){
222 directory
= mDirectory
;
224 directory
= Environment
.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
225 if (directory
== null
) return; // no files to show
230 // if that's not a directory -> List its parent
231 if(!directory
.isDirectory()){
232 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
233 directory
= directory
.getParentFile();
236 mList
.clearChoices(); // by now, only files in the same directory will be kept as selected
237 mAdapter
.swapDirectory(directory
);
238 if (mDirectory
== null
|| !mDirectory
.equals(directory
)) {
239 mList
.setSelectionFromTop(0, 0);
241 mDirectory
= directory
;
246 * Returns the fule paths to the files checked by the user
248 * @return File paths to the files checked by the user.
250 public String
[] getCheckedFilePaths() {
251 String
[] result
= null
;
252 SparseBooleanArray positions
= mList
.getCheckedItemPositions();
253 if (positions
.size() > 0) {
254 Log
.d(TAG
, "Returning " + positions
.size() + " selected files");
255 result
= new String
[positions
.size()];
256 for (int i
=0; i
<positions
.size(); i
++) {
257 result
[i
] = ((File
) mList
.getItemAtPosition(positions
.keyAt(i
))).getAbsolutePath();
265 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
267 * @author David A. Velasco
269 public interface ContainerActivity
{
272 * Callback method invoked when a directory is clicked by the user on the files list
276 public void onDirectoryClick(File directory
);
279 * Callback method invoked when a file (non directory) is clicked by the user on the files list
283 public void onFileClick(File file
);
287 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
289 * @return Directory to list firstly. Can be NULL.
291 public File
getInitialDirectory();