Merge remote-tracking branch 'remotes/upstream/material_fab' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author masensio
6 * @author David A. Velasco
7 * Copyright (C) 2011 Bartek Przybylski
8 * Copyright (C) 2015 ownCloud Inc.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23 package com.owncloud.android.ui.fragment;
24
25 import android.app.Activity;
26 import android.content.Intent;
27 import android.content.SharedPreferences;
28 import android.os.Build;
29 import android.os.Bundle;
30 import android.preference.PreferenceManager;
31 import android.support.v4.widget.SwipeRefreshLayout;
32 import android.view.ContextMenu;
33 import android.view.Menu;
34 import android.view.MenuInflater;
35 import android.view.MenuItem;
36 import android.view.View;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.AdapterContextMenuInfo;
39 import android.widget.PopupMenu;
40 import android.widget.TextView;
41 import android.widget.Toast;
42
43 import com.owncloud.android.R;
44 import com.owncloud.android.authentication.AccountUtils;
45 import com.owncloud.android.datamodel.FileDataStorageManager;
46 import com.owncloud.android.datamodel.OCFile;
47 import com.owncloud.android.files.FileMenuFilter;
48 import com.owncloud.android.lib.common.utils.Log_OC;
49 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
50 import com.owncloud.android.ui.activity.FileActivity;
51 import com.owncloud.android.ui.activity.FileDisplayActivity;
52 import com.owncloud.android.ui.activity.FolderPickerActivity;
53 import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
54 import com.owncloud.android.ui.activity.UploadFilesActivity;
55 import com.owncloud.android.ui.adapter.FileListListAdapter;
56 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
57 import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
58 import com.owncloud.android.ui.dialog.FileActionsDialogFragment;
59 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
60 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
61 import com.owncloud.android.ui.dialog.UploadSourceDialogFragment;
62 import com.owncloud.android.ui.preview.PreviewImageFragment;
63 import com.owncloud.android.ui.preview.PreviewMediaFragment;
64 import com.owncloud.android.utils.DisplayUtils;
65 import com.owncloud.android.utils.FileStorageUtils;
66 import com.owncloud.android.ui.preview.PreviewTextFragment;
67
68 import java.io.File;
69
70 /**
71 * A Fragment that lists all files and folders in a given path.
72 *
73 * TODO refactor to get rid of direct dependency on FileDisplayActivity
74 */
75 public class OCFileListFragment extends ExtendedListFragment implements FileActionsDialogFragment.FileActionsDialogFragmentListener {
76
77 private static final String TAG = OCFileListFragment.class.getSimpleName();
78
79 private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
80 OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
81
82 public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
83 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
84 public final static String ARG_HIDE_FAB = MY_PACKAGE + ".HIDE_FAB";
85
86 private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
87 private static final String KEY_FAB_EVER_CLICKED = "FAB_EVER_CLICKED";
88
89 private static String DIALOG_CREATE_FOLDER = "DIALOG_CREATE_FOLDER";
90
91 private FileFragment.ContainerActivity mContainerActivity;
92
93 private OCFile mFile = null;
94 private FileListListAdapter mAdapter;
95 private boolean mJustFolders;
96
97 private OCFile mTargetFile;
98
99 private boolean miniFabClicked = false;
100
101 /**
102 * {@inheritDoc}
103 */
104 @Override
105 public void onAttach(Activity activity) {
106 super.onAttach(activity);
107 Log_OC.e(TAG, "onAttach");
108 try {
109 mContainerActivity = (FileFragment.ContainerActivity) activity;
110
111 } catch (ClassCastException e) {
112 throw new ClassCastException(activity.toString() + " must implement " +
113 FileFragment.ContainerActivity.class.getSimpleName());
114 }
115 try {
116 setOnRefreshListener((OnEnforceableRefreshListener) activity);
117
118 } catch (ClassCastException e) {
119 throw new ClassCastException(activity.toString() + " must implement " +
120 SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
121 }
122 }
123
124
125 @Override
126 public void onDetach() {
127 setOnRefreshListener(null);
128 mContainerActivity = null;
129 super.onDetach();
130 }
131
132 /**
133 * {@inheritDoc}
134 */
135 @Override
136 public void onActivityCreated(Bundle savedInstanceState) {
137 super.onActivityCreated(savedInstanceState);
138 Log_OC.e(TAG, "onActivityCreated() start");
139
140 if (savedInstanceState != null) {
141 mFile = savedInstanceState.getParcelable(KEY_FILE);
142 }
143
144 if (mJustFolders) {
145 setFooterEnabled(false);
146 } else {
147 setFooterEnabled(true);
148 }
149
150 Bundle args = getArguments();
151 mJustFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false);
152 mAdapter = new FileListListAdapter(
153 mJustFolders,
154 getActivity(),
155 mContainerActivity
156 );
157 setListAdapter(mAdapter);
158
159 registerLongClickListener();
160
161 boolean hideFab = (args != null) && args.getBoolean(ARG_HIDE_FAB, false);
162 if (hideFab) {
163 setFabEnabled(false);
164 } else {
165 setFabEnabled(true);
166 registerFabListeners();
167
168 // detect if a mini FAB has ever been clicked
169 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
170 if(prefs.getLong(KEY_FAB_EVER_CLICKED, 0) > 0) {
171 miniFabClicked = true;
172 }
173
174 // add labels to the min FABs when none of them has ever been clicked on
175 if(!miniFabClicked) {
176 setFabLabels();
177 } else {
178 removeFabLabels();
179 }
180 }
181 }
182
183 /**
184 * adds labels to all mini FABs.
185 */
186 private void setFabLabels() {
187 getFabUpload().setTitle(getResources().getString(R.string.actionbar_upload));
188 getFabMkdir().setTitle(getResources().getString(R.string.actionbar_mkdir));
189 getFabUploadFromApp().setTitle(getResources().getString(R.string.actionbar_upload_from_apps));
190 }
191
192 /**
193 * registers all listeners on all mini FABs.
194 */
195 private void registerFabListeners() {
196 registerFabUploadListeners();
197 registerFabMkDirListeners();
198 registerFabUploadFromAppListeners();
199 }
200
201 /**
202 * registers {@link android.view.View.OnClickListener} and {@link android.view.View.OnLongClickListener}
203 * on the Upload mini FAB for the linked action and {@link Toast} showing the underlying action.
204 */
205 private void registerFabUploadListeners() {
206 getFabUpload().setOnClickListener(new View.OnClickListener() {
207 @Override
208 public void onClick(View v) {
209 Intent action = new Intent(getActivity(), UploadFilesActivity.class);
210 action.putExtra(
211 UploadFilesActivity.EXTRA_ACCOUNT,
212 ((FileActivity) getActivity()).getAccount()
213 );
214 getActivity().startActivityForResult(action, UploadSourceDialogFragment.ACTION_SELECT_MULTIPLE_FILES);
215 getFabMain().collapse();
216 recordMiniFabClick();
217 }
218 });
219
220 getFabUpload().setOnLongClickListener(new View.OnLongClickListener() {
221 @Override
222 public boolean onLongClick(View v) {
223 Toast.makeText(getActivity(), R.string.actionbar_upload, Toast.LENGTH_SHORT).show();
224 return true;
225 }
226 });
227 }
228
229 /**
230 * registers {@link android.view.View.OnClickListener} and {@link android.view.View.OnLongClickListener}
231 * on the 'Create Dir' mini FAB for the linked action and {@link Toast} showing the underlying action.
232 */
233 private void registerFabMkDirListeners() {
234 getFabMkdir().setOnClickListener(new View.OnClickListener() {
235 @Override
236 public void onClick(View v) {
237 CreateFolderDialogFragment dialog =
238 CreateFolderDialogFragment.newInstance(mFile);
239 dialog.show(getActivity().getSupportFragmentManager(), FileDisplayActivity.DIALOG_CREATE_FOLDER);
240 getFabMain().collapse();
241 recordMiniFabClick();
242 }
243 });
244
245 getFabMkdir().setOnLongClickListener(new View.OnLongClickListener() {
246 @Override
247 public boolean onLongClick(View v) {
248 Toast.makeText(getActivity(), R.string.actionbar_mkdir, Toast.LENGTH_SHORT).show();
249 return true;
250 }
251 });
252 }
253
254 /**
255 * registers {@link android.view.View.OnClickListener} and {@link android.view.View.OnLongClickListener}
256 * on the Upload from App mini FAB for the linked action and {@link Toast} showing the underlying action.
257 */
258 private void registerFabUploadFromAppListeners() {
259 getFabUploadFromApp().setOnClickListener(new View.OnClickListener() {
260 @Override
261 public void onClick(View v) {
262 Intent action = new Intent(Intent.ACTION_GET_CONTENT);
263 action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
264
265 //Intent.EXTRA_ALLOW_MULTIPLE is only supported on api level 18+, Jelly Bean
266 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
267 action.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
268 }
269
270 getActivity().startActivityForResult(
271 Intent.createChooser(action, getString(R.string.upload_chooser_title)),
272 UploadSourceDialogFragment.ACTION_SELECT_CONTENT_FROM_APPS
273 );
274 getFabMain().collapse();
275 recordMiniFabClick();
276 }
277 });
278
279 getFabUploadFromApp().setOnLongClickListener(new View.OnLongClickListener() {
280 @Override
281 public boolean onLongClick(View v) {
282 Toast.makeText(getActivity(),
283 R.string.actionbar_upload_from_apps,
284 Toast.LENGTH_SHORT).show();
285 return true;
286 }
287 });
288 }
289
290 /**
291 * records a click on a mini FAB and thus:
292 * <ol>
293 * <li>persists the click fact</li>
294 * <li>removes the mini FAB labels</li>
295 * </ol>
296 */
297 private void recordMiniFabClick() {
298 // only record if it hasn't been done already at some other time
299 if(!miniFabClicked) {
300 final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
301 sp.edit().putLong(KEY_FAB_EVER_CLICKED, 1).commit();
302 miniFabClicked = true;
303 }
304 }
305
306 /**
307 * removes the labels on all known min FABs.
308 */
309 private void removeFabLabels() {
310 getFabUpload().setTitle(null);
311 getFabMkdir().setTitle(null);
312 getFabUploadFromApp().setTitle(null);
313 ((TextView) getFabUpload().getTag(com.getbase.floatingactionbutton.R.id.fab_label)).setVisibility(View.GONE);
314 ((TextView) getFabMkdir().getTag(com.getbase.floatingactionbutton.R.id.fab_label)).setVisibility(View.GONE);
315 ((TextView) getFabUploadFromApp().getTag(com.getbase.floatingactionbutton.R.id.fab_label)).setVisibility(View.GONE);
316 }
317
318 private void registerLongClickListener() {
319 getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
320 public boolean onItemLongClick(AdapterView<?> arg0, View v,
321 int index, long arg3) {
322 showFileAction(index);
323 return true;
324 }
325 });
326 }
327
328 private void showFileAction(int fileIndex) {
329 Bundle args = getArguments();
330 PopupMenu pm = new PopupMenu(getActivity(),null);
331 Menu menu = pm.getMenu();
332
333 boolean allowContextualActions =
334 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
335
336 if (allowContextualActions) {
337 MenuInflater inflater = getActivity().getMenuInflater();
338
339 inflater.inflate(R.menu.file_actions_menu, menu);
340 OCFile targetFile = (OCFile) mAdapter.getItem(fileIndex);
341
342 if (mContainerActivity.getStorageManager() != null) {
343 FileMenuFilter mf = new FileMenuFilter(
344 targetFile,
345 mContainerActivity.getStorageManager().getAccount(),
346 mContainerActivity,
347 getActivity()
348 );
349 mf.filter(menu);
350 }
351
352 /// TODO break this direct dependency on FileDisplayActivity... if possible
353 MenuItem item = menu.findItem(R.id.action_open_file_with);
354 FileFragment frag = ((FileDisplayActivity)getActivity()).getSecondFragment();
355 if (frag != null && frag instanceof FileDetailFragment &&
356 frag.getFile().getFileId() == targetFile.getFileId()) {
357 item = menu.findItem(R.id.action_see_details);
358 if (item != null) {
359 item.setVisible(false);
360 item.setEnabled(false);
361 }
362 }
363
364 FileActionsDialogFragment dialog = FileActionsDialogFragment.newInstance(menu, fileIndex, targetFile.getFileName());
365 dialog.setTargetFragment(this, 0);
366 dialog.show(getFragmentManager(), FileActionsDialogFragment.FTAG_FILE_ACTIONS);
367 }
368 }
369
370 /**
371 * Saves the current listed folder.
372 */
373 @Override
374 public void onSaveInstanceState(Bundle outState) {
375 super.onSaveInstanceState(outState);
376 outState.putParcelable(KEY_FILE, mFile);
377 }
378
379 /**
380 * Call this, when the user presses the up button.
381 * <p>
382 * Tries to move up the current folder one level. If the parent folder was removed from the
383 * database, it continues browsing up until finding an existing folders.
384 * </p>
385 * @return Count of folder levels browsed up.
386 */
387 public int onBrowseUp() {
388 OCFile parentDir = null;
389 int moveCount = 0;
390
391 if (mFile != null) {
392 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
393
394 String parentPath = null;
395 if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
396 parentPath = new File(mFile.getRemotePath()).getParent();
397 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
398 parentPath + OCFile.PATH_SEPARATOR;
399 parentDir = storageManager.getFileByPath(parentPath);
400 moveCount++;
401 } else {
402 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
403 }
404 while (parentDir == null) {
405 parentPath = new File(parentPath).getParent();
406 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
407 parentPath + OCFile.PATH_SEPARATOR;
408 parentDir = storageManager.getFileByPath(parentPath);
409 moveCount++;
410 } // exit is granted because storageManager.getFileByPath("/") never returns null
411 mFile = parentDir;
412
413 // TODO Enable when "On Device" is recovered ?
414 listDirectory(mFile /*, MainApp.getOnlyOnDevice()*/);
415
416 onRefresh(false);
417
418 // restore index and top position
419 restoreIndexAndTopPosition();
420
421 } // else - should never happen now
422
423 return moveCount;
424 }
425
426 @Override
427 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
428 OCFile file = (OCFile) mAdapter.getItem(position);
429 if (file != null) {
430 if (file.isFolder()) {
431 // update state and view of this fragment
432 // TODO Enable when "On Device" is recovered ?
433 listDirectory(file/*, MainApp.getOnlyOnDevice()*/);
434 // then, notify parent activity to let it update its state and view
435 mContainerActivity.onBrowsedDownTo(file);
436 // save index and top position
437 saveIndexAndTopPosition(position);
438
439 } else { /// Click on a file
440 if (PreviewImageFragment.canBePreviewed(file)) {
441 // preview image - it handles the download, if needed
442 ((FileDisplayActivity)mContainerActivity).startImagePreview(file);
443 } else if (PreviewTextFragment.canBePreviewed(file)){
444 ((FileDisplayActivity)mContainerActivity).startTextPreview(file);
445 } else if (file.isDown()) {
446 if (PreviewMediaFragment.canBePreviewed(file)) {
447 // media preview
448 ((FileDisplayActivity) mContainerActivity).startMediaPreview(file, 0, true);
449 } else {
450 mContainerActivity.getFileOperationsHelper().openFile(file);
451 }
452
453 }
454 }
455
456 } else {
457 Log_OC.d(TAG, "Null object in ListAdapter!!");
458 }
459
460 }
461
462 /**
463 * {@inheritDoc}
464 */
465 @Override
466 public void onCreateContextMenu(
467 ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
468 Bundle args = getArguments();
469 boolean allowContextualActions =
470 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
471 if (allowContextualActions) {
472 MenuInflater inflater = getActivity().getMenuInflater();
473 inflater.inflate(R.menu.file_actions_menu, menu);
474 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
475 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
476
477 if (mContainerActivity.getStorageManager() != null) {
478 FileMenuFilter mf = new FileMenuFilter(
479 targetFile,
480 mContainerActivity.getStorageManager().getAccount(),
481 mContainerActivity,
482 getActivity()
483 );
484 mf.filter(menu);
485 }
486
487 /// TODO break this direct dependency on FileDisplayActivity... if possible
488 MenuItem item = menu.findItem(R.id.action_open_file_with);
489 FileFragment frag = ((FileDisplayActivity)getActivity()).getSecondFragment();
490 if (frag != null && frag instanceof FileDetailFragment &&
491 frag.getFile().getFileId() == targetFile.getFileId()) {
492 item = menu.findItem(R.id.action_see_details);
493 if (item != null) {
494 item.setVisible(false);
495 item.setEnabled(false);
496 }
497 }
498
499 // String.format(mContext.getString(R.string.subject_token),
500 // getClient().getCredentials().getUsername(), file.getFileName()));
501 }
502 }
503
504 /**
505 * {@inheritDoc}
506 */
507 @Override
508 public boolean onFileActionChosen(int menuId, int filePosition) {
509 mTargetFile = (OCFile) mAdapter.getItem(filePosition);
510 switch (menuId) {
511 case R.id.action_share_file: {
512 mContainerActivity.getFileOperationsHelper().shareFileWithLink(mTargetFile);
513 return true;
514 }
515 case R.id.action_open_file_with: {
516 mContainerActivity.getFileOperationsHelper().openFile(mTargetFile);
517 return true;
518 }
519 case R.id.action_unshare_file: {
520 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(mTargetFile);
521 return true;
522 }
523 case R.id.action_rename_file: {
524 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(mTargetFile);
525 dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
526 return true;
527 }
528 case R.id.action_remove_file: {
529 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(mTargetFile);
530 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
531 return true;
532 }
533 case R.id.action_download_file:
534 case R.id.action_sync_file: {
535 mContainerActivity.getFileOperationsHelper().syncFile(mTargetFile);
536 return true;
537 }
538 case R.id.action_cancel_download:
539 case R.id.action_cancel_upload: {
540 ((FileDisplayActivity) mContainerActivity).cancelTransference(mTargetFile);
541 return true;
542 }
543 case R.id.action_see_details: {
544 mContainerActivity.showDetails(mTargetFile);
545 return true;
546 }
547 case R.id.action_send_file: {
548 // Obtain the file
549 if (!mTargetFile.isDown()) { // Download the file
550 Log_OC.d(TAG, mTargetFile.getRemotePath() + " : File must be downloaded");
551 ((FileDisplayActivity) mContainerActivity).startDownloadForSending(mTargetFile);
552
553 } else {
554 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(mTargetFile);
555 }
556 return true;
557 }
558 case R.id.action_move: {
559 Intent action = new Intent(getActivity(), FolderPickerActivity.class);
560
561 // Pass mTargetFile that contains info of selected file/folder
562 action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile);
563 getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
564 return true;
565 }
566 case R.id.action_favorite_file: {
567 mContainerActivity.getFileOperationsHelper().toggleFavorite(mTargetFile, true);
568 return true;
569 }
570 case R.id.action_unfavorite_file: {
571 mContainerActivity.getFileOperationsHelper().toggleFavorite(mTargetFile, false);
572 return true;
573 }
574 case R.id.action_copy:
575 Intent action = new Intent(getActivity(), FolderPickerActivity.class);
576
577 // Pass mTargetFile that contains info of selected file/folder
578 action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile);
579 getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_COPY_FILES);
580 return true;
581 default:
582 return false;
583 }
584 }
585
586 /**
587 * {@inhericDoc}
588 */
589 @Override
590 public boolean onContextItemSelected (MenuItem item) {
591 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
592 boolean matched = onFileActionChosen(item.getItemId(), ((AdapterContextMenuInfo) item.getMenuInfo()).position);
593 if(!matched) {
594 return super.onContextItemSelected(item);
595 } else {
596 return matched;
597 }
598 }
599
600
601 /**
602 * Use this to query the {@link OCFile} that is currently
603 * being displayed by this fragment
604 *
605 * @return The currently viewed OCFile
606 */
607 public OCFile getCurrentFile() {
608 return mFile;
609 }
610
611 /**
612 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
613 */
614 public void listDirectory(/*boolean onlyOnDevice*/){
615 listDirectory(null);
616 // TODO Enable when "On Device" is recovered ?
617 // listDirectory(null, onlyOnDevice);
618 }
619
620 public void refreshDirectory(){
621 // TODO Enable when "On Device" is recovered ?
622 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
623 }
624
625 /**
626 * Lists the given directory on the view. When the input parameter is null,
627 * it will either refresh the last known directory. list the root
628 * if there never was a directory.
629 *
630 * @param directory File to be listed
631 */
632 public void listDirectory(OCFile directory/*, boolean onlyOnDevice*/) {
633 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
634 if (storageManager != null) {
635
636 // Check input parameters for null
637 if (directory == null) {
638 if (mFile != null) {
639 directory = mFile;
640 } else {
641 directory = storageManager.getFileByPath("/");
642 if (directory == null) return; // no files, wait for sync
643 }
644 }
645
646
647 // If that's not a directory -> List its parent
648 if (!directory.isFolder()) {
649 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
650 directory = storageManager.getFileById(directory.getParentId());
651 }
652
653 // TODO Enable when "On Device" is recovered ?
654 mAdapter.swapDirectory(directory, storageManager/*, onlyOnDevice*/);
655 if (mFile == null || !mFile.equals(directory)) {
656 mCurrentListView.setSelection(0);
657 }
658 mFile = directory;
659
660 updateLayout();
661
662 }
663 }
664
665 private void updateLayout() {
666 if (!mJustFolders) {
667 int filesCount = 0, foldersCount = 0, imagesCount = 0;
668 int count = mAdapter.getCount();
669 OCFile file;
670 for (int i=0; i < count ; i++) {
671 file = (OCFile) mAdapter.getItem(i);
672 if (file.isFolder()) {
673 foldersCount++;
674 } else {
675 if (!file.isHidden()) {
676 filesCount++;
677
678 if (file.isImage()) {
679 imagesCount++;
680 }
681 }
682 }
683 }
684 // set footer text
685 setFooterText(generateFooterText(filesCount, foldersCount));
686
687 // decide grid vs list view
688 OwnCloudVersion version = AccountUtils.getServerVersion(
689 ((FileActivity)mContainerActivity).getAccount());
690 if (version != null && version.supportsRemoteThumbnails() &&
691 DisplayUtils.isGridView(mFile, mContainerActivity.getStorageManager())) {
692 switchToGridView();
693 registerLongClickListener();
694 } else {
695 switchToListView();
696 }
697 }
698 }
699
700 private String generateFooterText(int filesCount, int foldersCount) {
701 String output;
702 if (filesCount <= 0) {
703 if (foldersCount <= 0) {
704 output = "";
705
706 } else if (foldersCount == 1) {
707 output = getResources().getString(R.string.file_list__footer__folder);
708
709 } else { // foldersCount > 1
710 output = getResources().getString(R.string.file_list__footer__folders, foldersCount);
711 }
712
713 } else if (filesCount == 1) {
714 if (foldersCount <= 0) {
715 output = getResources().getString(R.string.file_list__footer__file);
716
717 } else if (foldersCount == 1) {
718 output = getResources().getString(R.string.file_list__footer__file_and_folder);
719
720 } else { // foldersCount > 1
721 output = getResources().getString(R.string.file_list__footer__file_and_folders, foldersCount);
722 }
723 } else { // filesCount > 1
724 if (foldersCount <= 0) {
725 output = getResources().getString(R.string.file_list__footer__files, filesCount);
726
727 } else if (foldersCount == 1) {
728 output = getResources().getString(R.string.file_list__footer__files_and_folder, filesCount);
729
730 } else { // foldersCount > 1
731 output = getResources().getString(
732 R.string.file_list__footer__files_and_folders, filesCount, foldersCount
733 );
734
735 }
736 }
737 return output;
738 }
739
740 public void sortByName(boolean descending) {
741 mAdapter.setSortOrder(FileStorageUtils.SORT_NAME, descending);
742 }
743
744 public void sortByDate(boolean descending) {
745 mAdapter.setSortOrder(FileStorageUtils.SORT_DATE, descending);
746 }
747
748 public void sortBySize(boolean descending) {
749 mAdapter.setSortOrder(FileStorageUtils.SORT_SIZE, descending);
750 }
751 }