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";
54 private Vector
<OCFile
> mFiles
;
56 private boolean mIsLargeDevice
;
59 public void onCreate(Bundle savedInstanceState
) {
60 Log
.i(getClass().toString(), "onCreate() start");
61 super.onCreate(savedInstanceState
);
63 Intent intent
= getActivity().getIntent();
64 OCFile directory
= intent
.getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
66 mIsLargeDevice
= false
;
68 Log
.i(getClass().toString(), "onCreate() stop");
72 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
73 Bundle savedInstanceState
) {
74 Log
.i(getClass().toString(), "onCreateView() start");
75 super.onCreateView(inflater
, container
, savedInstanceState
);
76 getListView().setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
77 getListView().setDividerHeight(1);
79 Log
.i(getClass().toString(), "onCreateView() end");
84 public void onStart() {
85 Log
.i(getClass().toString(), "onStart() start");
87 // Create a placeholder upon launch
88 View fragmentContainer
= getActivity().findViewById(R
.id
.file_details_container
);
89 if (fragmentContainer
!= null
) {
90 mIsLargeDevice
= true
;
91 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
92 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(true
));
95 Log
.i(getClass().toString(), "onStart() end");
99 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
100 if (mFiles
.size() <= position
) {
101 throw new IndexOutOfBoundsException("Incorrect item selected");
103 OCFile file
= mFiles
.get(position
);
105 // Update ActionBarPath
106 if (file
.getMimetype().equals("DIR")) {
108 ((FileDisplayActivity
) getActivity()).pushDirname(file
);
109 ActionBar actionBar
= ((FileDisplayActivity
) getActivity()).getSupportActionBar();
110 actionBar
.setDisplayHomeAsUpEnabled(true
);
116 Intent showDetailsIntent
= new Intent(getActivity(), FileDetailActivity
.class);
117 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
118 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(getActivity()));
120 // If we are on a large device -> update fragment
121 if (mIsLargeDevice
) {
122 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
123 if (fileDetails
== null
) {
124 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
125 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(showDetailsIntent
), "FileDetails");
126 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
127 transaction
.commit();
129 fileDetails
.updateFileDetails(showDetailsIntent
);
132 startActivity(showDetailsIntent
);
137 * Resets the FileDetailsFragment on Tablets so that it always displays
138 * "Tab on a file to display it's details"
140 private void resetFileFragment() {
141 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
142 if (fileDetails
!= null
) {
143 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
144 transaction
.remove(fileDetails
);
145 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(true
));
146 transaction
.commit();
151 * Call this, when the user presses the up button
153 public void onNavigateUp() {
154 OCFile parentDir
= null
;
157 DataStorageManager storageManager
= ((FileDisplayActivity
)getActivity()).getStorageManager();
158 parentDir
= storageManager
.getFileById(mFile
.getParentId());
162 listDirectory(parentDir
);
167 * Use this to query the {@link OCFile} that is currently
168 * being displayed by this fragment
169 * @return The currently viewed OCFile
171 public OCFile
getCurrentFile(){
176 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
178 public void listDirectory(){
183 * Lists the given directory on the view. When the input parameter is null,
184 * it will either refresh the last known directory, or list the root
185 * if there never was a directory.
187 * @param directory File to be listed
189 public void listDirectory(OCFile directory
) {
191 DataStorageManager storageManager
= ((FileDisplayActivity
)getActivity()).getStorageManager();
193 // Check input parameters for null
194 if(directory
== null
){
198 directory
= storageManager
.getFileByPath("/");
199 if (directory
== null
) return; // no files, wait for sync
204 // If that's not a directory -> List its parent
205 if(!directory
.isDirectory()){
206 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
207 directory
= storageManager
.getFileById(directory
.getParentId());
212 mFiles
= storageManager
.getDirectoryContent(directory
);
213 /*if (mFiles == null || mFiles.size() == 0) {
214 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
216 setListAdapter(new FileListListAdapter(directory
, storageManager
, getActivity()));