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
.Stack
;
21 import java
.util
.Vector
;
23 import android
.accounts
.Account
;
24 import android
.content
.ClipData
;
25 import android
.content
.ClipDescription
;
26 import android
.content
.Intent
;
27 import android
.os
.Bundle
;
28 import android
.support
.v4
.app
.FragmentTransaction
;
29 import android
.util
.Log
;
30 import android
.view
.View
;
31 import android
.widget
.AdapterView
;
32 import android
.widget
.Toast
;
33 import eu
.alefzero
.owncloud
.AccountUtils
;
34 import eu
.alefzero
.owncloud
.FileDownloader
;
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
.ui
.FragmentListView
;
40 import eu
.alefzero
.owncloud
.ui
.activity
.FileDetailActivity
;
41 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
42 import eu
.alefzero
.owncloud
.ui
.adapter
.FileListListAdapter
;
45 * A Fragment that lists all files and folders in a given path.
47 * @author Bartek Przybylski
50 public class FileListFragment
extends FragmentListView
{
51 private static final String TAG
= "FileListFragment";
52 private Account mAccount
;
53 private Stack
<String
> mDirNames
;
54 private Vector
<OCFile
> mFiles
;
55 private DataStorageManager mStorageManager
;
57 private boolean mIsLargeDevice
= false
;
59 public FileListFragment() {
60 mDirNames
= new Stack
<String
>();
64 public void onCreate(Bundle savedInstanceState
) {
65 super.onCreate(savedInstanceState
);
67 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
68 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
69 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
70 getListView().setDividerHeight(1);
72 Intent intent
= getActivity().getIntent();
73 OCFile directory
= intent
.getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
76 listDirectory(directory
);
80 public void onStart() {
81 // Create a placeholder upon launch
82 View fragmentContainer
= getActivity().findViewById(R
.id
.file_details_container
);
83 if (fragmentContainer
!= null
) {
84 mIsLargeDevice
= true
;
85 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
86 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(true
));
93 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
94 if (mFiles
.size() <= position
) {
95 throw new IndexOutOfBoundsException("Incorrect item selected");
97 OCFile file
= mFiles
.get(position
);
100 // Update ActionBarPath
101 if (file
.getMimetype().equals("DIR")) {
102 String dirname
= file
.getFileName();
104 mDirNames
.push(dirname
);
105 ((FileDisplayActivity
) getActivity()).pushPath(dirname
);
113 Intent showDetailsIntent
= new Intent(getActivity(), FileDetailActivity
.class);
114 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
115 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
117 // If we are on a large device -> update fragment
118 if (mIsLargeDevice
) {
119 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
120 if (fileDetails
== null
) {
121 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
122 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(showDetailsIntent
), "FileDetails");
123 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
124 transaction
.commit();
126 fileDetails
.updateFileDetails(showDetailsIntent
);
129 startActivity(showDetailsIntent
);
134 * Resets the FileDetailsFragment on Tablets so that it always displays
135 * "Tab on a file to display it's details"
137 private void resetFileFragment() {
138 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
139 if (fileDetails
!= null
) {
140 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
141 transaction
.remove(fileDetails
);
142 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(true
));
143 transaction
.commit();
148 * Call this, when the user presses the up button
150 public void onNavigateUp() {
152 OCFile parentDir
= null
;
155 parentDir
= mStorageManager
.getFileById(mFile
.getParentId());
159 listDirectory(parentDir
);
164 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
166 public void listDirectory(){
171 * Lists the given directory on the view. When the input parameter is null,
172 * it will either refresh the last known directory, or list the root
173 * if there never was a directory.
175 * @param directory File to be listed
177 public void listDirectory(OCFile directory
) {
179 // Check input parameters for null
180 if(directory
== null
){
184 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());
194 mFiles
= mStorageManager
.getDirectoryContent(directory
);
195 if (mFiles
== null
|| mFiles
.size() == 0) {
196 Toast
.makeText(getActivity(), "There are no files here", Toast
.LENGTH_LONG
).show();
198 setListAdapter(new FileListListAdapter(directory
, mStorageManager
, getActivity()));
202 public void onSaveInstanceState(Bundle outState
) {
203 super.onSaveInstanceState(outState
);
204 outState
.putParcelable("ACCOUNT", mAccount
);