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
.View
;
30 import android
.widget
.AdapterView
;
31 import android
.widget
.Toast
;
32 import eu
.alefzero
.owncloud
.AccountUtils
;
33 import eu
.alefzero
.owncloud
.R
;
34 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
35 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
36 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
37 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
38 import eu
.alefzero
.owncloud
.ui
.FragmentListView
;
39 import eu
.alefzero
.owncloud
.ui
.activity
.FileDetailActivity
;
40 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
41 import eu
.alefzero
.owncloud
.ui
.adapter
.FileListListAdapter
;
44 * A Fragment that lists all files and folders in a given path.
46 * @author Bartek Przybylski
49 public class FileListFragment
extends FragmentListView
{
50 private static final String TAG
= "FileListFragment";
51 private Account mAccount
;
52 private Vector
<OCFile
> mFiles
;
53 private DataStorageManager mStorageManager
;
55 private boolean mIsLargeDevice
= false
;
58 public void onCreate(Bundle savedInstanceState
) {
59 super.onCreate(savedInstanceState
);
61 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
62 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
63 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
64 getListView().setDividerHeight(1);
66 Intent intent
= getActivity().getIntent();
67 OCFile directory
= intent
.getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
70 listDirectory(directory
);
74 public void onStart() {
75 // Create a placeholder upon launch
76 View fragmentContainer
= getActivity().findViewById(R
.id
.file_details_container
);
77 if (fragmentContainer
!= null
) {
78 mIsLargeDevice
= true
;
79 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
80 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(true
));
87 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
88 if (mFiles
.size() <= position
) {
89 throw new IndexOutOfBoundsException("Incorrect item selected");
91 OCFile file
= mFiles
.get(position
);
93 // Update ActionBarPath
94 if (file
.getMimetype().equals("DIR")) {
96 ((FileDisplayActivity
) getActivity()).pushDirname(file
);
97 ActionBar actionBar
= ((FileDisplayActivity
) getActivity()).getSupportActionBar();
98 actionBar
.setDisplayHomeAsUpEnabled(true
);
104 Intent showDetailsIntent
= new Intent(getActivity(), FileDetailActivity
.class);
105 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
106 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
108 // If we are on a large device -> update fragment
109 if (mIsLargeDevice
) {
110 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
111 if (fileDetails
== null
) {
112 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
113 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(showDetailsIntent
), "FileDetails");
114 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
115 transaction
.commit();
117 fileDetails
.updateFileDetails(showDetailsIntent
);
120 startActivity(showDetailsIntent
);
125 * Resets the FileDetailsFragment on Tablets so that it always displays
126 * "Tab on a file to display it's details"
128 private void resetFileFragment() {
129 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
130 if (fileDetails
!= null
) {
131 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
132 transaction
.remove(fileDetails
);
133 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(true
));
134 transaction
.commit();
139 * Call this, when the user presses the up button
141 public void onNavigateUp() {
142 OCFile parentDir
= null
;
145 parentDir
= mStorageManager
.getFileById(mFile
.getParentId());
149 listDirectory(parentDir
);
154 * Use this to query the {@link OCFile} that is currently
155 * being displayed by this fragment
156 * @return The currently viewed OCFile
158 public OCFile
getCurrentFile(){
163 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
165 public void listDirectory(){
170 * Lists the given directory on the view. When the input parameter is null,
171 * it will either refresh the last known directory, or list the root
172 * if there never was a directory.
174 * @param directory File to be listed
176 public void listDirectory(OCFile directory
) {
178 // Check input parameters for null
179 if(directory
== null
){
183 directory
= mStorageManager
.getFileByPath("/");
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
= mStorageManager
.getFileById(directory
.getParentId());
196 mFiles
= mStorageManager
.getDirectoryContent(directory
);
197 if (mFiles
== null
|| mFiles
.size() == 0) {
198 Toast
.makeText(getActivity(), "There are no files here", Toast
.LENGTH_LONG
).show();
200 setListAdapter(new FileListListAdapter(directory
, mStorageManager
, getActivity()));
204 public void onSaveInstanceState(Bundle outState
) {
205 super.onSaveInstanceState(outState
);
206 outState
.putParcelable("ACCOUNT", mAccount
);