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 android
.accounts
.Account
;
23 import android
.content
.Intent
;
24 import android
.os
.Bundle
;
25 import android
.support
.v4
.app
.FragmentTransaction
;
26 import android
.util
.Log
;
27 import android
.view
.View
;
28 import android
.widget
.AdapterView
;
29 import android
.widget
.Toast
;
30 import eu
.alefzero
.owncloud
.AccountUtils
;
31 import eu
.alefzero
.owncloud
.FileDownloader
;
32 import eu
.alefzero
.owncloud
.R
;
33 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
34 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
35 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
36 import eu
.alefzero
.owncloud
.ui
.FragmentListView
;
37 import eu
.alefzero
.owncloud
.ui
.activity
.FileDetailActivity
;
38 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
39 import eu
.alefzero
.owncloud
.ui
.adapter
.FileListListAdapter
;
42 * A Fragment that lists all files and folders in a given path.
44 * @author Bartek Przybylski
47 public class FileListFragment
extends FragmentListView
{
48 private static final String TAG
= "FileListFragment";
49 private Account mAccount
;
50 private Vector
<OCFile
> mFiles
;
51 private DataStorageManager mStorageManager
;
53 private boolean mIsLargeDevice
= false
;
56 public void onCreate(Bundle savedInstanceState
) {
57 super.onCreate(savedInstanceState
);
59 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
60 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
61 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
62 getListView().setDividerHeight(1);
64 Intent intent
= getActivity().getIntent();
65 OCFile directory
= intent
.getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
68 listDirectory(directory
);
72 public void onStart() {
73 // Create a placeholder upon launch
74 View fragmentContainer
= getActivity().findViewById(R
.id
.file_details_container
);
75 if (fragmentContainer
!= null
) {
76 mIsLargeDevice
= true
;
77 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
78 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(true
));
85 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
86 if (mFiles
.size() <= position
) {
87 throw new IndexOutOfBoundsException("Incorrect item selected");
89 OCFile file
= mFiles
.get(position
);
91 // Update ActionBarPath
92 if (file
.getMimetype().equals("DIR")) {
94 ((FileDisplayActivity
) getActivity()).pushDirname(file
);
100 Intent showDetailsIntent
= new Intent(getActivity(), FileDetailActivity
.class);
101 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
102 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
104 // If we are on a large device -> update fragment
105 if (mIsLargeDevice
) {
106 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
107 if (fileDetails
== null
) {
108 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
109 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(showDetailsIntent
), "FileDetails");
110 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
111 transaction
.commit();
113 fileDetails
.updateFileDetails(showDetailsIntent
);
116 startActivity(showDetailsIntent
);
121 * Resets the FileDetailsFragment on Tablets so that it always displays
122 * "Tab on a file to display it's details"
124 private void resetFileFragment() {
125 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
126 if (fileDetails
!= null
) {
127 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
128 transaction
.remove(fileDetails
);
129 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(true
));
130 transaction
.commit();
135 * Call this, when the user presses the up button
137 public void onNavigateUp() {
138 OCFile parentDir
= null
;
141 parentDir
= mStorageManager
.getFileById(mFile
.getParentId());
145 listDirectory(parentDir
);
150 * Use this to query the {@link OCFile} that is currently
151 * being displayed by this fragment
152 * @return The currently viewed OCFile
154 public OCFile
getCurrentFile(){
159 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
161 public void listDirectory(){
166 * Lists the given directory on the view. When the input parameter is null,
167 * it will either refresh the last known directory, or list the root
168 * if there never was a directory.
170 * @param directory File to be listed
172 public void listDirectory(OCFile directory
) {
174 // Check input parameters for null
175 if(directory
== null
){
179 directory
= mStorageManager
.getFileByPath("/");
184 // If that's not a directory -> List its parent
185 if(!directory
.isDirectory()){
186 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
187 directory
= mStorageManager
.getFileById(directory
.getParentId());
192 mFiles
= mStorageManager
.getDirectoryContent(directory
);
193 if (mFiles
== null
|| mFiles
.size() == 0) {
194 Toast
.makeText(getActivity(), "There are no files here", Toast
.LENGTH_LONG
).show();
196 setListAdapter(new FileListListAdapter(directory
, mStorageManager
, getActivity()));
200 public void onSaveInstanceState(Bundle outState
) {
201 super.onSaveInstanceState(outState
);
202 outState
.putParcelable("ACCOUNT", mAccount
);