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 eu
.alefzero
.owncloud
.ui
.fragment
;
20 import java
.util
.Vector
;
22 import android
.app
.Activity
;
23 import android
.os
.Bundle
;
24 import android
.util
.Log
;
25 import android
.view
.LayoutInflater
;
26 import android
.view
.View
;
27 import android
.view
.ViewGroup
;
28 import android
.widget
.AdapterView
;
29 import eu
.alefzero
.owncloud
.R
;
30 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
31 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
32 import eu
.alefzero
.owncloud
.ui
.FragmentListView
;
33 import eu
.alefzero
.owncloud
.ui
.adapter
.FileListListAdapter
;
36 * A Fragment that lists all files and folders in a given path.
38 * @author Bartek Przybylski
41 public class FileListFragment
extends FragmentListView
{
42 private static final String TAG
= "FileListFragment";
44 private FileListFragment
.ContainerActivity mContainerActivity
;
46 private Vector
<OCFile
> mFiles
;
47 private OCFile mFile
= null
;
54 public void onAttach(Activity activity
) {
55 super.onAttach(activity
);
57 mContainerActivity
= (ContainerActivity
) activity
;
58 } catch (ClassCastException e
) {
59 throw new ClassCastException(activity
.toString() + " must implement FileListFragment.ContainerActivity");
65 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
66 Bundle savedInstanceState
) {
67 Log
.i(getClass().toString(), "onCreateView() start");
68 super.onCreateView(inflater
, container
, savedInstanceState
);
69 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
70 getListView().setDividerHeight(1);
72 Log
.i(getClass().toString(), "onCreateView() end");
78 public void onActivityCreated(Bundle savedInstanceState
) {
79 Log
.i(getClass().toString(), "onActivityCreated() start");
81 super.onCreate(savedInstanceState
);
83 Log
.i(getClass().toString(), "onActivityCreated() stop");
88 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
89 if (mFiles
.size() <= position
) {
90 throw new IndexOutOfBoundsException("Incorrect item selected");
92 OCFile file
= mFiles
.get(position
);
94 /// Click on a directory
95 if (file
.getMimetype().equals("DIR")) {
99 // any other updates are let to the container Activity
100 mContainerActivity
.onDirectoryClick(file
);
102 } else { /// Click on a file
103 mContainerActivity
.onFileClick(file
);
109 * Call this, when the user presses the up button
111 public void onNavigateUp() {
112 OCFile parentDir
= null
;
115 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
116 parentDir
= storageManager
.getFileById(mFile
.getParentId());
119 listDirectory(parentDir
);
123 * Use this to query the {@link OCFile} that is currently
124 * being displayed by this fragment
125 * @return The currently viewed OCFile
127 public OCFile
getCurrentFile(){
132 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
134 public void listDirectory(){
139 * Lists the given directory on the view. When the input parameter is null,
140 * it will either refresh the last known directory, or list the root
141 * if there never was a directory.
143 * @param directory File to be listed
145 public void listDirectory(OCFile directory
) {
147 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
149 // Check input parameters for null
150 if(directory
== null
){
154 directory
= storageManager
.getFileByPath("/");
155 if (directory
== null
) return; // no files, wait for sync
160 // If that's not a directory -> List its parent
161 if(!directory
.isDirectory()){
162 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
163 directory
= storageManager
.getFileById(directory
.getParentId());
167 mFiles
= storageManager
.getDirectoryContent(directory
);
169 /*if (mFiles == null || mFiles.size() == 0) {
170 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
172 setListAdapter(new FileListListAdapter(directory
, storageManager
, getActivity()));
178 * Interface to implement by any Activity that includes some instance of FileListFragment
180 * @author David A. Velasco
182 public interface ContainerActivity
{
185 * Callback method invoked when a directory is clicked by the user on the files list
189 public void onDirectoryClick(OCFile file
);
192 * Callback method invoked when a file (non directory) is clicked by the user on the files list
196 public void onFileClick(OCFile file
);
199 * Getter for the current DataStorageManager in the container activity
201 public DataStorageManager
getStorageManager();