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
.files
.services
.FileDownloader
.FileDownloaderBinder
;
23 import com
.owncloud
.android
.ui
.FragmentListView
;
24 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
25 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
27 import android
.app
.Activity
;
28 import android
.os
.Bundle
;
29 import android
.util
.Log
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.ViewGroup
;
33 import android
.widget
.AdapterView
;
34 import com
.owncloud
.android
.R
;
37 * A Fragment that lists all files and folders in a given path.
39 * @author Bartek Przybylski
42 public class OCFileListFragment
extends FragmentListView
{
43 private static final String TAG
= "FileListFragment";
45 private OCFileListFragment
.ContainerActivity mContainerActivity
;
47 private OCFile mFile
= null
;
48 private FileListListAdapter mAdapter
;
55 public void onAttach(Activity activity
) {
56 super.onAttach(activity
);
58 mContainerActivity
= (ContainerActivity
) activity
;
59 } catch (ClassCastException e
) {
60 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getCanonicalName());
69 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
70 Bundle savedInstanceState
) {
71 Log
.i(TAG
, "onCreateView() start");
72 super.onCreateView(inflater
, container
, savedInstanceState
);
73 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
74 getListView().setDividerHeight(1);
76 Log
.i(TAG
, "onCreateView() end");
85 public void onActivityCreated(Bundle savedInstanceState
) {
86 Log
.i(TAG
, "onActivityCreated() start");
88 super.onActivityCreated(savedInstanceState
);
89 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
90 setListAdapter(mAdapter
);
92 if (savedInstanceState
!= null
) {
93 Log
.i(TAG
, "savedInstanceState is not null");
94 int position
= savedInstanceState
.getInt("LIST_POSITION");
95 getListView().setSelectionFromTop(position
, 0);
97 //mAdapter = new FileListListAdapter();
99 Log
.i(TAG
, "onActivityCreated() stop");
104 public void onSaveInstanceState(Bundle savedInstanceState
) {
105 Log
.i(TAG
, "onSaveInstanceState() start");
107 savedInstanceState
.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
109 Log
.i(TAG
, "onSaveInstanceState() stop");
114 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
115 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
117 /// Click on a directory
118 if (file
.getMimetype().equals("DIR")) {
119 // just local updates
122 // any other updates are let to the container Activity
123 mContainerActivity
.onDirectoryClick(file
);
125 } else { /// Click on a file
126 mContainerActivity
.onFileClick(file
);
130 Log
.d(TAG
, "Null object in ListAdapter!!");
136 * Call this, when the user presses the up button
138 public void onNavigateUp() {
139 OCFile parentDir
= null
;
142 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
143 parentDir
= storageManager
.getFileById(mFile
.getParentId());
146 listDirectory(parentDir
);
150 * Use this to query the {@link OCFile} that is currently
151 * being displayed by this fragment
152 * @return The currently viewed OCFile
154 public OCFile
getCurrentFile(){
159 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
161 public void listDirectory(){
162 int position
= mList
.getFirstVisiblePosition();
164 mList
.setSelectionFromTop(position
, 0);
168 * Lists the given directory on the view. When the input parameter is null,
169 * it will either refresh the last known directory, or list the root
170 * if there never was a directory.
172 * @param directory File to be listed
174 public void listDirectory(OCFile directory
) {
175 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
177 // Check input parameters for null
178 if(directory
== null
){
182 directory
= storageManager
.getFileByPath("/");
183 if (directory
== null
) return; // no files, wait for sync
188 // If that's not a directory -> List its parent
189 if(!directory
.isDirectory()){
190 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
191 directory
= storageManager
.getFileById(directory
.getParentId());
195 mAdapter
.swapDirectory(mFile
);
196 mList
.setSelectionFromTop(0, 0);
203 * Interface to implement by any Activity that includes some instance of FileListFragment
205 * @author David A. Velasco
207 public interface ContainerActivity
extends TransferServiceGetter
{
210 * Callback method invoked when a directory is clicked by the user on the files list
214 public void onDirectoryClick(OCFile file
);
217 * Callback method invoked when a file (non directory) is clicked by the user on the files list
221 public void onFileClick(OCFile file
);
224 * Getter for the current DataStorageManager in the container activity
226 public DataStorageManager
getStorageManager();
230 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
232 * @return Directory to list firstly. Can be NULL.
234 public OCFile
getInitialDirectory();