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
.adapter
.FileListListAdapter
;
25 import android
.app
.Activity
;
26 import android
.os
.Bundle
;
27 import android
.util
.Log
;
28 import android
.view
.LayoutInflater
;
29 import android
.view
.View
;
30 import android
.view
.ViewGroup
;
31 import android
.widget
.AdapterView
;
32 import com
.owncloud
.android
.R
;
35 * A Fragment that lists all files and folders in a given path.
37 * @author Bartek Przybylski
40 public class OCFileListFragment
extends FragmentListView
{
41 private static final String TAG
= "FileListFragment";
43 private OCFileListFragment
.ContainerActivity mContainerActivity
;
45 private OCFile mFile
= null
;
46 private FileListListAdapter mAdapter
;
53 public void onAttach(Activity activity
) {
54 super.onAttach(activity
);
56 mContainerActivity
= (ContainerActivity
) activity
;
57 } catch (ClassCastException e
) {
58 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getCanonicalName());
67 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
68 Bundle savedInstanceState
) {
69 Log
.i(TAG
, "onCreateView() start");
70 super.onCreateView(inflater
, container
, savedInstanceState
);
71 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
72 getListView().setDividerHeight(1);
74 Log
.i(TAG
, "onCreateView() end");
80 public void onActivityCreated(Bundle savedInstanceState
) {
81 Log
.i(TAG
, "onActivityCreated() start");
83 super.onCreate(savedInstanceState
);
84 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity());
85 setListAdapter(mAdapter
);
87 if (savedInstanceState
!= null
) {
88 Log
.i(TAG
, "savedInstanceState is not null");
89 int position
= savedInstanceState
.getInt("LIST_POSITION");
90 getListView().setSelectionFromTop(position
, 0);
92 //mAdapter = new FileListListAdapter();
94 Log
.i(TAG
, "onActivityCreated() stop");
99 public void onSaveInstanceState(Bundle savedInstanceState
) {
100 Log
.i(TAG
, "onSaveInstanceState() start");
102 savedInstanceState
.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
104 Log
.i(TAG
, "onSaveInstanceState() stop");
109 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
110 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
112 /// Click on a directory
113 if (file
.getMimetype().equals("DIR")) {
114 // just local updates
117 // any other updates are let to the container Activity
118 mContainerActivity
.onDirectoryClick(file
);
120 } else { /// Click on a file
121 mContainerActivity
.onFileClick(file
);
125 Log
.d(TAG
, "Null object in ListAdapter!!");
131 * Call this, when the user presses the up button
133 public void onNavigateUp() {
134 OCFile parentDir
= null
;
137 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
138 parentDir
= storageManager
.getFileById(mFile
.getParentId());
141 listDirectory(parentDir
);
145 * Use this to query the {@link OCFile} that is currently
146 * being displayed by this fragment
147 * @return The currently viewed OCFile
149 public OCFile
getCurrentFile(){
154 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
156 public void listDirectory(){
161 * Lists the given directory on the view. When the input parameter is null,
162 * it will either refresh the last known directory, or list the root
163 * if there never was a directory.
165 * @param directory File to be listed
167 public void listDirectory(OCFile directory
) {
168 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
170 // Check input parameters for null
171 if(directory
== null
){
175 directory
= storageManager
.getFileByPath("/");
176 if (directory
== null
) return; // no files, wait for sync
181 // If that's not a directory -> List its parent
182 if(!directory
.isDirectory()){
183 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
184 directory
= storageManager
.getFileById(directory
.getParentId());
188 mAdapter
.swapDirectory(mFile
);
189 mList
.setSelectionFromTop(0, 0);
195 * Interface to implement by any Activity that includes some instance of FileListFragment
197 * @author David A. Velasco
199 public interface ContainerActivity
{
202 * Callback method invoked when a directory is clicked by the user on the files list
206 public void onDirectoryClick(OCFile file
);
209 * Callback method invoked when a file (non directory) is clicked by the user on the files list
213 public void onFileClick(OCFile file
);
216 * Getter for the current DataStorageManager in the container activity
218 public DataStorageManager
getStorageManager();
222 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
224 * @return Directory to list firstly. Can be NULL.
226 public OCFile
getInitialDirectory();