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
.LayoutInflater
;
30 import android
.view
.View
;
31 import android
.view
.ViewGroup
;
32 import android
.widget
.AdapterView
;
33 import com
.owncloud
.android
.R
;
36 * A Fragment that lists all files and folders in a given path.
38 * @author Bartek Przybylski
41 public class OCFileListFragment
extends FragmentListView
{
42 private static final String TAG
= "FileListFragment";
44 private OCFileListFragment
.ContainerActivity mContainerActivity
;
46 private OCFile mFile
= null
;
47 private FileListListAdapter mAdapter
;
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 " + OCFileListFragment
.ContainerActivity
.class.getCanonicalName());
68 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
69 Bundle savedInstanceState
) {
70 Log
.i(TAG
, "onCreateView() start");
71 super.onCreateView(inflater
, container
, savedInstanceState
);
72 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
73 getListView().setDividerHeight(1);
75 Log
.i(TAG
, "onCreateView() end");
84 public void onActivityCreated(Bundle savedInstanceState
) {
85 Log
.i(TAG
, "onActivityCreated() start");
87 super.onActivityCreated(savedInstanceState
);
88 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
89 setListAdapter(mAdapter
);
91 if (savedInstanceState
!= null
) {
92 Log
.i(TAG
, "savedInstanceState is not null");
93 int position
= savedInstanceState
.getInt("LIST_POSITION");
94 getListView().setSelectionFromTop(position
, 0);
96 //mAdapter = new FileListListAdapter();
98 Log
.i(TAG
, "onActivityCreated() stop");
103 public void onSaveInstanceState(Bundle savedInstanceState
) {
104 Log
.i(TAG
, "onSaveInstanceState() start");
106 savedInstanceState
.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
108 Log
.i(TAG
, "onSaveInstanceState() stop");
113 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
114 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
116 /// Click on a directory
117 if (file
.getMimetype().equals("DIR")) {
118 // just local updates
121 // any other updates are let to the container Activity
122 mContainerActivity
.onDirectoryClick(file
);
124 } else { /// Click on a file
125 mContainerActivity
.onFileClick(file
);
129 Log
.d(TAG
, "Null object in ListAdapter!!");
135 * Call this, when the user presses the up button
137 public void onNavigateUp() {
138 OCFile parentDir
= null
;
141 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
142 parentDir
= storageManager
.getFileById(mFile
.getParentId());
145 listDirectory(parentDir
);
149 * Use this to query the {@link OCFile} that is currently
150 * being displayed by this fragment
151 * @return The currently viewed OCFile
153 public OCFile
getCurrentFile(){
158 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
160 public void listDirectory(){
161 int position
= mList
.getFirstVisiblePosition();
163 mList
.setSelectionFromTop(position
, 0);
167 * Lists the given directory on the view. When the input parameter is null,
168 * it will either refresh the last known directory, or list the root
169 * if there never was a directory.
171 * @param directory File to be listed
173 public void listDirectory(OCFile directory
) {
174 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
176 // Check input parameters for null
177 if(directory
== null
){
181 directory
= storageManager
.getFileByPath("/");
182 if (directory
== null
) return; // no files, wait for sync
187 // If that's not a directory -> List its parent
188 if(!directory
.isDirectory()){
189 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
190 directory
= storageManager
.getFileById(directory
.getParentId());
194 mAdapter
.swapDirectory(mFile
);
195 mList
.setSelectionFromTop(0, 0);
202 * Interface to implement by any Activity that includes some instance of FileListFragment
204 * @author David A. Velasco
206 public interface ContainerActivity
extends TransferServiceGetter
{
209 * Callback method invoked when a directory is clicked by the user on the files list
213 public void onDirectoryClick(OCFile file
);
216 * Callback method invoked when a file (non directory) is clicked by the user on the files list
220 public void onFileClick(OCFile file
);
223 * Getter for the current DataStorageManager in the container activity
225 public DataStorageManager
getStorageManager();
229 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
231 * @return Directory to list firstly. Can be NULL.
233 public OCFile
getInitialDirectory();