1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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 com
.owncloud
.android
.ui
.fragment
;
21 import java
.util
.Vector
;
23 import android
.app
.Activity
;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.os
.Bundle
;
27 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
28 import android
.view
.ContextMenu
;
29 import android
.view
.MenuInflater
;
30 import android
.view
.MenuItem
;
31 import android
.view
.View
;
32 import android
.widget
.AdapterView
;
33 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
34 import android
.widget
.TextView
;
35 import android
.view
.LayoutInflater
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.files
.FileMenuFilter
;
41 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
42 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
43 import com
.owncloud
.android
.ui
.activity
.FolderPickerActivity
;
44 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
45 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
46 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
47 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
48 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
49 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
50 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
53 * A Fragment that lists all files and folders in a given path.
55 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
57 * @author Bartek Przybylski
59 * @author David A. Velasco
61 public class OCFileListFragment
extends ExtendedListFragment
{
63 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
65 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
66 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
68 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
69 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
71 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
73 private FileFragment
.ContainerActivity mContainerActivity
;
75 private OCFile mFile
= null
;
76 private FileListListAdapter mAdapter
;
77 private View mFooterView
;
79 private OCFile mTargetFile
;
86 public void onAttach(Activity activity
) {
87 super.onAttach(activity
);
88 Log_OC
.e(TAG
, "onAttach");
90 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
92 } catch (ClassCastException e
) {
93 throw new ClassCastException(activity
.toString() + " must implement " +
94 FileFragment
.ContainerActivity
.class.getSimpleName());
97 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
99 } catch (ClassCastException e
) {
100 throw new ClassCastException(activity
.toString() + " must implement " +
101 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
107 public void onDetach() {
108 setOnRefreshListener(null
);
109 mContainerActivity
= null
;
117 public void onActivityCreated(Bundle savedInstanceState
) {
118 super.onActivityCreated(savedInstanceState
);
119 Log_OC
.e(TAG
, "onActivityCreated() start");
121 if (savedInstanceState
!= null
) {
122 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
125 mFooterView
= ((LayoutInflater
) getActivity().getSystemService(Context
.LAYOUT_INFLATER_SERVICE
)).inflate(
126 R
.layout
.list_footer
, null
, false
);
127 setFooterView(mFooterView
);
129 Bundle args
= getArguments();
130 boolean justFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
131 mAdapter
= new FileListListAdapter(
133 getSherlockActivity(),
136 setListAdapter(mAdapter
);
138 registerForContextMenu(getListView());
139 getListView().setOnCreateContextMenuListener(this);
143 * Saves the current listed folder.
146 public void onSaveInstanceState (Bundle outState
) {
147 super.onSaveInstanceState(outState
);
148 outState
.putParcelable(KEY_FILE
, mFile
);
152 * Call this, when the user presses the up button.
154 * Tries to move up the current folder one level. If the parent folder was removed from the
155 * database, it continues browsing up until finding an existing folders.
157 * return Count of folder levels browsed up.
159 public int onBrowseUp() {
160 OCFile parentDir
= null
;
164 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
166 String parentPath
= null
;
167 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
168 parentPath
= new File(mFile
.getRemotePath()).getParent();
169 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
170 parentPath
+ OCFile
.PATH_SEPARATOR
;
171 parentDir
= storageManager
.getFileByPath(parentPath
);
174 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
176 while (parentDir
== null
) {
177 parentPath
= new File(parentPath
).getParent();
178 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
179 parentPath
+ OCFile
.PATH_SEPARATOR
;
180 parentDir
= storageManager
.getFileByPath(parentPath
);
182 } // exit is granted because storageManager.getFileByPath("/") never returns null
185 listDirectory(mFile
);
189 // restore index and top position
190 restoreIndexAndTopPosition();
192 } // else - should never happen now
198 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
199 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
201 if (file
.isFolder()) {
202 // update state and view of this fragment
204 // then, notify parent activity to let it update its state and view
205 mContainerActivity
.onBrowsedDownTo(file
);
206 // save index and top position
207 saveIndexAndTopPosition(position
);
209 } else { /// Click on a file
210 if (PreviewImageFragment
.canBePreviewed(file
)) {
211 // preview image - it handles the download, if needed
212 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
214 } else if (file
.isDown()) {
215 if (PreviewMediaFragment
.canBePreviewed(file
)) {
217 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
219 mContainerActivity
.getFileOperationsHelper().openFile(file
);
223 // automatic download, preview on finish
224 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
230 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
239 public void onCreateContextMenu (
240 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
241 super.onCreateContextMenu(menu
, v
, menuInfo
);
242 Bundle args
= getArguments();
243 boolean allowContextualActions
=
244 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
245 if (allowContextualActions
) {
246 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
247 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
248 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
249 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
251 if (mContainerActivity
.getStorageManager() != null
) {
252 FileMenuFilter mf
= new FileMenuFilter(
254 mContainerActivity
.getStorageManager().getAccount(),
256 getSherlockActivity()
261 /// TODO break this direct dependency on FileDisplayActivity... if possible
262 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
263 FileFragment frag
= ((FileDisplayActivity
)getSherlockActivity()).getSecondFragment();
264 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
265 frag
.getFile().getFileId() == targetFile
.getFileId()) {
266 item
= menu
.findItem(R
.id
.action_see_details
);
268 item
.setVisible(false
);
269 item
.setEnabled(false
);
280 public boolean onContextItemSelected (MenuItem item
) {
281 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
282 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
283 switch (item
.getItemId()) {
284 case R
.id
.action_share_file
: {
285 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
288 case R
.id
.action_open_file_with
: {
289 mContainerActivity
.getFileOperationsHelper().openFile(mTargetFile
);
292 case R
.id
.action_unshare_file
: {
293 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
296 case R
.id
.action_rename_file
: {
297 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
298 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
301 case R
.id
.action_remove_file
: {
302 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
303 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
306 case R
.id
.action_download_file
:
307 case R
.id
.action_sync_file
: {
308 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
311 case R
.id
.action_cancel_download
:
312 case R
.id
.action_cancel_upload
: {
313 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
316 case R
.id
.action_see_details
: {
317 mContainerActivity
.showDetails(mTargetFile
);
320 case R
.id
.action_send_file
: {
322 if (!mTargetFile
.isDown()) { // Download the file
323 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
324 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
327 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
331 case R
.id
.action_move
: {
332 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
334 // Pass mTargetFile that contains info of selected file/folder
335 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
336 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
340 return super.onContextItemSelected(item
);
346 * Use this to query the {@link OCFile} that is currently
347 * being displayed by this fragment
348 * @return The currently viewed OCFile
350 public OCFile
getCurrentFile(){
355 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
357 public void listDirectory(){
362 * Lists the given directory on the view. When the input parameter is null,
363 * it will either refresh the last known directory. list the root
364 * if there never was a directory.
366 * @param directory File to be listed
368 public void listDirectory(OCFile directory
) {
369 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
370 if (storageManager
!= null
) {
372 // Check input parameters for null
373 if(directory
== null
){
377 directory
= storageManager
.getFileByPath("/");
378 if (directory
== null
) return; // no files, wait for sync
383 // If that's not a directory -> List its parent
384 if(!directory
.isFolder()){
385 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
386 directory
= storageManager
.getFileById(directory
.getParentId());
389 mAdapter
.swapDirectory(directory
, storageManager
);
390 if (mFile
== null
|| !mFile
.equals(directory
)) {
391 mList
.setSelectionFromTop(0, 0);
396 TextView footerText
= (TextView
) mFooterView
.findViewById(R
.id
.footerText
);
397 Log_OC
.d("footer", String
.valueOf(System
.currentTimeMillis()));
398 footerText
.setText(generateFooterText(directory
));
399 Log_OC
.d("footer", String
.valueOf(System
.currentTimeMillis()));
403 private String
generateFooterText(OCFile directory
) {
407 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
408 Vector
<OCFile
> mFiles
= storageManager
.getFolderContent(mFile
);
410 for (OCFile ocFile
: mFiles
) {
411 if (ocFile
.isFolder()) {
422 output
= output
+ files
.toString() + " " + getResources().getString(R
.string
.file_list_file
);
424 output
= output
+ files
.toString() + " " + getResources().getString(R
.string
.file_list_files
);
427 if (folders
> 0 && files
> 0){
428 output
= output
+ ", ";
431 output
= output
+ folders
.toString() + " " + getResources().getString(R
.string
.file_list_folder
);
432 } else if (folders
> 1) {
433 output
= output
+ folders
.toString() + " " + getResources().getString(R
.string
.file_list_folders
);
439 public void sortByName(boolean descending
) {
440 mAdapter
.setSortOrder(FileListListAdapter
.SORT_NAME
, descending
);
443 public void sortByDate(boolean descending
) {
444 mAdapter
.setSortOrder(FileListListAdapter
.SORT_DATE
, descending
);
447 public void sortBySize(boolean descending
) {
448 mAdapter
.setSortOrder(FileListListAdapter
.SORT_SIZE
, descending
);