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
;
20 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
21 import com
.owncloud
.android
.datamodel
.OCFile
;
22 import com
.owncloud
.android
.ui
.FragmentListView
;
23 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
24 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
26 import android
.app
.Activity
;
27 import android
.os
.Bundle
;
28 import android
.util
.Log
;
29 import android
.view
.View
;
30 import android
.widget
.AdapterView
;
33 * A Fragment that lists all files and folders in a given path.
35 * @author Bartek Przybylski
38 public class OCFileListFragment
extends FragmentListView
{
39 private static final String TAG
= "FileListFragment";
40 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
42 private OCFileListFragment
.ContainerActivity mContainerActivity
;
44 private OCFile mFile
= null
;
45 private FileListListAdapter mAdapter
;
52 public void onAttach(Activity activity
) {
53 super.onAttach(activity
);
55 mContainerActivity
= (ContainerActivity
) activity
;
56 } catch (ClassCastException e
) {
57 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
66 public void onActivityCreated(Bundle savedInstanceState
) {
67 Log
.i(TAG
, "onActivityCreated() start");
69 super.onActivityCreated(savedInstanceState
);
70 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
71 setListAdapter(mAdapter
);
73 if (savedInstanceState
!= null
) {
74 Log
.i(TAG
, "savedInstanceState is not null");
75 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
76 setReferencePosition(position
);
79 Log
.i(TAG
, "onActivityCreated() stop");
85 public void onSaveInstanceState(Bundle savedInstanceState
) {
86 Log
.i(TAG
, "onSaveInstanceState() start");
88 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
90 Log
.i(TAG
, "onSaveInstanceState() stop");
95 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
96 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
98 /// Click on a directory
99 if (file
.getMimetype().equals("DIR")) {
100 // just local updates
103 // any other updates are let to the container Activity
104 mContainerActivity
.onDirectoryClick(file
);
106 } else { /// Click on a file
107 mContainerActivity
.onFileClick(file
);
111 Log
.d(TAG
, "Null object in ListAdapter!!");
117 * Call this, when the user presses the up button
119 public void onNavigateUp() {
120 OCFile parentDir
= null
;
123 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
124 parentDir
= storageManager
.getFileById(mFile
.getParentId());
127 listDirectory(parentDir
);
131 * Use this to query the {@link OCFile} that is currently
132 * being displayed by this fragment
133 * @return The currently viewed OCFile
135 public OCFile
getCurrentFile(){
140 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
142 public void listDirectory(){
147 * Lists the given directory on the view. When the input parameter is null,
148 * it will either refresh the last known directory, or list the root
149 * if there never was a directory.
151 * @param directory File to be listed
153 public void listDirectory(OCFile directory
) {
154 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
155 if (storageManager
!= null
) {
157 // Check input parameters for null
158 if(directory
== null
){
162 directory
= storageManager
.getFileByPath("/");
163 if (directory
== null
) return; // no files, wait for sync
168 // If that's not a directory -> List its parent
169 if(!directory
.isDirectory()){
170 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
171 directory
= storageManager
.getFileById(directory
.getParentId());
174 mAdapter
.swapDirectory(directory
, storageManager
);
175 if (mFile
== null
|| !mFile
.equals(directory
)) {
176 mList
.setSelectionFromTop(0, 0);
185 * Interface to implement by any Activity that includes some instance of FileListFragment
187 * @author David A. Velasco
189 public interface ContainerActivity
extends TransferServiceGetter
{
192 * Callback method invoked when a directory is clicked by the user on the files list
196 public void onDirectoryClick(OCFile file
);
199 * Callback method invoked when a file (non directory) is clicked by the user on the files list
203 public void onFileClick(OCFile file
);
206 * Getter for the current DataStorageManager in the container activity
208 public DataStorageManager
getStorageManager();
212 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
214 * @return Directory to list firstly. Can be NULL.
216 public OCFile
getInitialDirectory();