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 eu
.alefzero
.owncloud
.ui
.fragment
;
20 import java
.util
.Vector
;
22 import com
.actionbarsherlock
.app
.ActionBar
;
24 import android
.accounts
.Account
;
25 import android
.content
.Intent
;
26 import android
.os
.Bundle
;
27 import android
.support
.v4
.app
.FragmentTransaction
;
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 android
.widget
.Toast
;
34 import eu
.alefzero
.owncloud
.AccountUtils
;
35 import eu
.alefzero
.owncloud
.R
;
36 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
37 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
38 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
39 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
40 import eu
.alefzero
.owncloud
.ui
.FragmentListView
;
41 import eu
.alefzero
.owncloud
.ui
.activity
.FileDetailActivity
;
42 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
43 import eu
.alefzero
.owncloud
.ui
.adapter
.FileListListAdapter
;
46 * A Fragment that lists all files and folders in a given path.
48 * @author Bartek Przybylski
51 public class FileListFragment
extends FragmentListView
{
52 private static final String TAG
= "FileListFragment";
53 //private Account mAccount; // dvelasco : the fragment is not recreated when other account is selected; keep as an attribute is dangerous
54 private Vector
<OCFile
> mFiles
;
55 //private DataStorageManager mStorageManager; // dvelasco : just the same; it depends upon the current account ; it's updated in FileDisplayActivity!!
57 private boolean mIsLargeDevice
= false
;
60 public void onCreate(Bundle savedInstanceState
) {
61 super.onCreate(savedInstanceState
);
63 Intent intent
= getActivity().getIntent();
64 OCFile directory
= intent
.getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
70 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
71 Bundle savedInstanceState
) {
72 super.onCreateView(inflater
, container
, savedInstanceState
);
73 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
74 getListView().setDividerHeight(1);
76 //listDirectory(mFile);
82 public void onStart() {
83 // Create a placeholder upon launch
84 View fragmentContainer
= getActivity().findViewById(R
.id
.file_details_container
);
85 if (fragmentContainer
!= null
) {
86 mIsLargeDevice
= true
;
87 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
88 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(true
));
95 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
96 if (mFiles
.size() <= position
) {
97 throw new IndexOutOfBoundsException("Incorrect item selected");
99 OCFile file
= mFiles
.get(position
);
101 // Update ActionBarPath
102 if (file
.getMimetype().equals("DIR")) {
104 ((FileDisplayActivity
) getActivity()).pushDirname(file
);
105 ActionBar actionBar
= ((FileDisplayActivity
) getActivity()).getSupportActionBar();
106 actionBar
.setDisplayHomeAsUpEnabled(true
);
112 Intent showDetailsIntent
= new Intent(getActivity(), FileDetailActivity
.class);
113 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
114 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(getActivity()));
116 // If we are on a large device -> update fragment
117 if (mIsLargeDevice
) {
118 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
119 if (fileDetails
== null
) {
120 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
121 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(showDetailsIntent
), "FileDetails");
122 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
123 transaction
.commit();
125 fileDetails
.updateFileDetails(showDetailsIntent
);
128 startActivity(showDetailsIntent
);
133 * Resets the FileDetailsFragment on Tablets so that it always displays
134 * "Tab on a file to display it's details"
136 private void resetFileFragment() {
137 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
138 if (fileDetails
!= null
) {
139 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
140 transaction
.remove(fileDetails
);
141 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(true
));
142 transaction
.commit();
147 * Call this, when the user presses the up button
149 public void onNavigateUp() {
150 OCFile parentDir
= null
;
153 DataStorageManager storageManager
= ((FileDisplayActivity
)getActivity()).getStorageManager();
154 parentDir
= storageManager
.getFileById(mFile
.getParentId());
158 listDirectory(parentDir
);
163 * Use this to query the {@link OCFile} that is currently
164 * being displayed by this fragment
165 * @return The currently viewed OCFile
167 public OCFile
getCurrentFile(){
172 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
174 public void listDirectory(){
179 * Lists the given directory on the view. When the input parameter is null,
180 * it will either refresh the last known directory, or list the root
181 * if there never was a directory.
183 * @param directory File to be listed
185 public void listDirectory(OCFile directory
) {
187 DataStorageManager storageManager
= ((FileDisplayActivity
)getActivity()).getStorageManager();
189 // Check input parameters for null
190 if(directory
== null
){
194 directory
= storageManager
.getFileByPath("/");
195 if (directory
== null
) return; // no files, wait for sync
200 // If that's not a directory -> List its parent
201 if(!directory
.isDirectory()){
202 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
203 directory
= storageManager
.getFileById(directory
.getParentId());
208 mFiles
= storageManager
.getDirectoryContent(directory
);
209 if (mFiles
== null
|| mFiles
.size() == 0) {
210 Toast
.makeText(getActivity(), "There are no files here", Toast
.LENGTH_LONG
).show();
212 setListAdapter(new FileListListAdapter(directory
, storageManager
, getActivity()));
216 public void onSaveInstanceState(Bundle outState
) {
217 super.onSaveInstanceState(outState
);
218 outState
.putParcelable("ACCOUNT", AccountUtils
.getCurrentOwnCloudAccount(getActivity()));