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
;
22 import android
.accounts
.Account
;
23 import android
.app
.Activity
;
24 import android
.content
.Intent
;
25 import android
.media
.MediaScannerConnection
;
26 import android
.os
.Bundle
;
27 import android
.os
.Handler
;
28 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
29 import android
.view
.ContextMenu
;
30 import android
.view
.MenuInflater
;
31 import android
.view
.MenuItem
;
32 import android
.view
.View
;
33 import android
.widget
.AdapterView
;
34 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.authentication
.AccountUtils
;
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
.files
.services
.FileDownloader
.FileDownloaderBinder
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
43 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
44 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
45 import com
.owncloud
.android
.operations
.RenameFileOperation
;
46 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
47 import com
.owncloud
.android
.ui
.activity
.MoveActivity
;
48 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
49 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
50 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
51 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
52 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
53 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
54 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
57 * A Fragment that lists all files and folders in a given path.
59 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
61 * @author Bartek Przybylski
63 * @author David A. Velasco
65 public class OCFileListFragment
extends ExtendedListFragment
{
67 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
69 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
70 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
72 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
73 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
75 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
77 private FileFragment
.ContainerActivity mContainerActivity
;
79 private OCFile mFile
= null
;
80 private FileListListAdapter mAdapter
;
82 private Handler mHandler
;
83 private OCFile mTargetFile
;
90 public void onAttach(Activity activity
) {
91 super.onAttach(activity
);
92 Log_OC
.e(TAG
, "onAttach");
94 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
96 } catch (ClassCastException e
) {
97 throw new ClassCastException(activity
.toString() + " must implement " +
98 FileFragment
.ContainerActivity
.class.getSimpleName());
101 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
103 } catch (ClassCastException e
) {
104 throw new ClassCastException(activity
.toString() + " must implement " +
105 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
111 public void onDetach() {
112 setOnRefreshListener(null
);
113 mContainerActivity
= null
;
121 public void onActivityCreated(Bundle savedInstanceState
) {
122 super.onActivityCreated(savedInstanceState
);
123 Log_OC
.e(TAG
, "onActivityCreated() start");
125 if (savedInstanceState
!= null
) {
126 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
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 /// additional restrictions for this fragment
262 // TODO allow in the future 'open with' for previewable files
263 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
265 item
.setVisible(false
);
266 item
.setEnabled(false
);
268 /// TODO break this direct dependency on FileDisplayActivity... if possible
269 FileFragment frag
= ((FileDisplayActivity
)getSherlockActivity()).getSecondFragment();
270 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
271 frag
.getFile().getFileId() == targetFile
.getFileId()) {
272 item
= menu
.findItem(R
.id
.action_see_details
);
274 item
.setVisible(false
);
275 item
.setEnabled(false
);
286 public boolean onContextItemSelected (MenuItem item
) {
287 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
288 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
289 switch (item
.getItemId()) {
290 case R
.id
.action_share_file
: {
291 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
294 case R
.id
.action_unshare_file
: {
295 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
298 case R
.id
.action_rename_file
: {
299 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
300 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
303 case R
.id
.action_remove_file
: {
304 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
305 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
308 case R
.id
.action_download_file
:
309 case R
.id
.action_sync_file
: {
310 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
311 triggerMediaScan(mTargetFile
.getStoragePath());
314 case R
.id
.action_cancel_download
:
315 case R
.id
.action_cancel_upload
: {
316 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
319 case R
.id
.action_see_details
: {
320 mContainerActivity
.showDetails(mTargetFile
);
323 case R
.id
.action_send_file
: {
325 if (!mTargetFile
.isDown()) { // Download the file
326 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
327 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
330 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
334 case R
.id
.action_move
: {
335 Intent action
= new Intent(getActivity(), MoveActivity
.class);
337 // Pass mTargetFile that contains info of selected file/folder
338 action
.putExtra(MoveActivity
.EXTRA_TARGET_FILE
, mTargetFile
);
339 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
343 return super.onContextItemSelected(item
);
350 * Use this to query the {@link OCFile} that is currently
351 * being displayed by this fragment
352 * @return The currently viewed OCFile
354 public OCFile
getCurrentFile(){
359 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
361 public void listDirectory(){
366 * Lists the given directory on the view. When the input parameter is null,
367 * it will either refresh the last known directory. list the root
368 * if there never was a directory.
370 * @param directory File to be listed
372 public void listDirectory(OCFile directory
) {
373 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
374 if (storageManager
!= null
) {
376 // Check input parameters for null
377 if(directory
== null
){
381 directory
= storageManager
.getFileByPath("/");
382 if (directory
== null
) return; // no files, wait for sync
387 // If that's not a directory -> List its parent
388 if(!directory
.isFolder()){
389 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
390 directory
= storageManager
.getFileById(directory
.getParentId());
393 mAdapter
.swapDirectory(directory
, storageManager
);
394 if (mFile
== null
|| !mFile
.equals(directory
)) {
395 mList
.setSelectionFromTop(0, 0);
401 private void triggerMediaScan(String path
){
402 MediaScannerConnection
.scanFile(
403 getActivity().getApplicationContext(),