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
.ExtendedListView
;
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";
44 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
46 private OCFileListFragment
.ContainerActivity mContainerActivity
;
48 private OCFile mFile
= null
;
49 private FileListListAdapter mAdapter
;
56 public void onAttach(Activity activity
) {
57 super.onAttach(activity
);
59 mContainerActivity
= (ContainerActivity
) activity
;
60 } catch (ClassCastException e
) {
61 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getCanonicalName());
70 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
71 Bundle savedInstanceState
) {
72 Log
.i(TAG
, "onCreateView() start");
73 super.onCreateView(inflater
, container
, savedInstanceState
);
74 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
75 getListView().setDividerHeight(1);
77 Log
.i(TAG
, "onCreateView() end");
86 public void onActivityCreated(Bundle savedInstanceState
) {
87 Log
.i(TAG
, "onActivityCreated() start");
89 super.onActivityCreated(savedInstanceState
);
90 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
91 setListAdapter(mAdapter
);
93 if (savedInstanceState
!= null
) {
94 Log
.i(TAG
, "savedInstanceState is not null");
95 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
96 setReferencePosition(position
);
99 Log
.i(TAG
, "onActivityCreated() stop");
105 public void onSaveInstanceState(Bundle savedInstanceState
) {
106 Log
.i(TAG
, "onSaveInstanceState() start");
108 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
110 Log
.i(TAG
, "onSaveInstanceState() stop");
115 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
116 * the device is turned to other position.
118 * THe current policy is take as a reference the visible item in the center of the screen.
120 * @return The position in the list of the visible item in the center of the screen.
122 private int getReferencePosition() {
123 return (getListView().getFirstVisiblePosition() + getListView().getLastVisiblePosition()) / 2;
128 * Sets the visible part of the list from the reference position.
130 * @param position Reference position previously returned by {@link OCFileListFragment#getReferencePosition()}
132 private void setReferencePosition(int position
) {
133 ((ExtendedListView
)getListView()).setAndCenterSelection(position
);
138 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
139 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
141 /// Click on a directory
142 if (file
.getMimetype().equals("DIR")) {
143 // just local updates
146 // any other updates are let to the container Activity
147 mContainerActivity
.onDirectoryClick(file
);
149 } else { /// Click on a file
150 mContainerActivity
.onFileClick(file
);
154 Log
.d(TAG
, "Null object in ListAdapter!!");
160 * Call this, when the user presses the up button
162 public void onNavigateUp() {
163 OCFile parentDir
= null
;
166 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
167 parentDir
= storageManager
.getFileById(mFile
.getParentId());
170 listDirectory(parentDir
);
174 * Use this to query the {@link OCFile} that is currently
175 * being displayed by this fragment
176 * @return The currently viewed OCFile
178 public OCFile
getCurrentFile(){
183 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
185 public void listDirectory(){
190 * Lists the given directory on the view. When the input parameter is null,
191 * it will either refresh the last known directory, or list the root
192 * if there never was a directory.
194 * @param directory File to be listed
196 public void listDirectory(OCFile directory
) {
197 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
198 if (storageManager
!= null
) {
200 // Check input parameters for null
201 if(directory
== null
){
205 directory
= storageManager
.getFileByPath("/");
206 if (directory
== null
) return; // no files, wait for sync
211 // If that's not a directory -> List its parent
212 if(!directory
.isDirectory()){
213 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
214 directory
= storageManager
.getFileById(directory
.getParentId());
217 mAdapter
.swapDirectory(mFile
, storageManager
);
218 if (mFile
== null
|| !mFile
.equals(directory
)) {
219 mList
.setSelectionFromTop(0, 0);
228 * Interface to implement by any Activity that includes some instance of FileListFragment
230 * @author David A. Velasco
232 public interface ContainerActivity
extends TransferServiceGetter
{
235 * Callback method invoked when a directory is clicked by the user on the files list
239 public void onDirectoryClick(OCFile file
);
242 * Callback method invoked when a file (non directory) is clicked by the user on the files list
246 public void onFileClick(OCFile file
);
249 * Getter for the current DataStorageManager in the container activity
251 public DataStorageManager
getStorageManager();
255 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
257 * @return Directory to list firstly. Can be NULL.
259 public OCFile
getInitialDirectory();