update
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 package com.owncloud.android.ui.activity;
24
25 import android.accounts.Account;
26 import android.accounts.AccountManager;
27 import android.accounts.AuthenticatorException;
28 import android.annotation.TargetApi;
29 import android.os.Parcelable;
30 import android.support.v7.app.AlertDialog;
31 import android.content.BroadcastReceiver;
32 import android.content.ComponentName;
33 import android.content.ContentResolver;
34 import android.content.Context;
35 import android.content.DialogInterface;
36 import android.content.Intent;
37 import android.content.IntentFilter;
38 import android.content.ServiceConnection;
39 import android.content.SharedPreferences;
40 import android.content.SyncRequest;
41 import android.content.res.Resources.NotFoundException;
42 import android.database.Cursor;
43 import android.net.Uri;
44 import android.os.Build;
45 import android.os.Bundle;
46 import android.os.IBinder;
47 import android.preference.PreferenceManager;
48 import android.provider.OpenableColumns;
49 import android.support.v4.app.Fragment;
50 import android.support.v4.app.FragmentManager;
51 import android.support.v4.app.FragmentTransaction;
52 import android.support.v4.content.ContextCompat;
53 import android.support.v4.view.GravityCompat;
54 import android.view.Menu;
55 import android.view.MenuInflater;
56 import android.view.MenuItem;
57 import android.view.View;
58 import android.widget.ProgressBar;
59 import android.widget.RelativeLayout;
60 import android.widget.TextView;
61 import android.widget.Toast;
62
63 import com.owncloud.android.MainApp;
64 import com.owncloud.android.R;
65 import com.owncloud.android.datamodel.FileDataStorageManager;
66 import com.owncloud.android.datamodel.OCFile;
67 import com.owncloud.android.files.services.FileDownloader;
68 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
69 import com.owncloud.android.files.services.FileUploader;
70 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
71 import com.owncloud.android.lib.common.OwnCloudAccount;
72 import com.owncloud.android.lib.common.OwnCloudClient;
73 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
74 import com.owncloud.android.lib.common.OwnCloudCredentials;
75 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
76 import com.owncloud.android.lib.common.network.CertificateCombinedException;
77 import com.owncloud.android.lib.common.operations.RemoteOperation;
78 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
79 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
80 import com.owncloud.android.lib.common.utils.Log_OC;
81 import com.owncloud.android.operations.CopyFileOperation;
82 import com.owncloud.android.operations.CreateFolderOperation;
83 import com.owncloud.android.operations.CreateShareViaLinkOperation;
84 import com.owncloud.android.operations.CreateShareWithShareeOperation;
85 import com.owncloud.android.operations.MoveFileOperation;
86 import com.owncloud.android.operations.RefreshFolderOperation;
87 import com.owncloud.android.operations.RemoveFileOperation;
88 import com.owncloud.android.operations.RenameFileOperation;
89 import com.owncloud.android.operations.SynchronizeFileOperation;
90 import com.owncloud.android.operations.UnshareOperation;
91 import com.owncloud.android.services.observer.FileObserverService;
92 import com.owncloud.android.syncadapter.FileSyncAdapter;
93 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
94 import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
95 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
96 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
97 import com.owncloud.android.ui.dialog.UploadSourceDialogFragment;
98 import com.owncloud.android.ui.fragment.FileDetailFragment;
99 import com.owncloud.android.ui.fragment.FileFragment;
100 import com.owncloud.android.ui.fragment.OCFileListFragment;
101 import com.owncloud.android.ui.preview.PreviewImageActivity;
102 import com.owncloud.android.ui.preview.PreviewImageFragment;
103 import com.owncloud.android.ui.preview.PreviewMediaFragment;
104 import com.owncloud.android.ui.preview.PreviewTextFragment;
105 import com.owncloud.android.ui.preview.PreviewVideoActivity;
106 import com.owncloud.android.utils.DisplayUtils;
107 import com.owncloud.android.utils.ErrorMessageAdapter;
108 import com.owncloud.android.utils.FileStorageUtils;
109 import com.owncloud.android.utils.UriUtils;
110
111 import java.io.File;
112 import java.util.ArrayList;
113 import java.util.Iterator;
114
115 /**
116 * Displays, what files the user has available in his ownCloud.
117 */
118
119 public class FileDisplayActivity extends HookActivity
120 implements FileFragment.ContainerActivity,
121 OnSslUntrustedCertListener, OnEnforceableRefreshListener {
122
123 private SyncBroadcastReceiver mSyncBroadcastReceiver;
124 private UploadFinishReceiver mUploadFinishReceiver;
125 private DownloadFinishReceiver mDownloadFinishReceiver;
126 private RemoteOperationResult mLastSslUntrustedServerResult = null;
127
128 private boolean mDualPane;
129 private View mLeftFragmentContainer;
130 private View mRightFragmentContainer;
131 private ProgressBar mProgressBar;
132
133 private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
134 private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
135 private static final String KEY_WAITING_TO_SEND = "WAITING_TO_SEND";
136
137 public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS";
138
139 public static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;
140 public static final int ACTION_SELECT_MULTIPLE_FILES = 2;
141 public static final int ACTION_MOVE_FILES = 3;
142 public static final int ACTION_COPY_FILES = 4;
143
144 private static final String TAG = FileDisplayActivity.class.getSimpleName();
145
146 private static final String TAG_LIST_OF_FILES = "LIST_OF_FILES";
147 private static final String TAG_SECOND_FRAGMENT = "SECOND_FRAGMENT";
148
149 private OCFile mWaitingToPreview;
150
151 private boolean mSyncInProgress = false;
152
153 private static String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT";
154 public static String DIALOG_CREATE_FOLDER = "DIALOG_CREATE_FOLDER";
155 private static String DIALOG_UPLOAD_SOURCE = "DIALOG_UPLOAD_SOURCE";
156 private static String DIALOG_CERT_NOT_SAVED = "DIALOG_CERT_NOT_SAVED";
157
158 private OCFile mWaitingToSend;
159 private Menu mOptionsMenu;
160
161
162 @Override
163 protected void onCreate(Bundle savedInstanceState) {
164 Log_OC.v(TAG, "onCreate() start");
165
166 super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account
167 // is valid
168
169 /// grant that FileObserverService is watching favorite files
170 if (savedInstanceState == null) {
171 Intent initObserversIntent = FileObserverService.makeInitIntent(this);
172 startService(initObserversIntent);
173 }
174
175 /// Load of saved instance state
176 if(savedInstanceState != null) {
177 mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(
178 FileDisplayActivity.KEY_WAITING_TO_PREVIEW);
179 mSyncInProgress = savedInstanceState.getBoolean(KEY_SYNC_IN_PROGRESS);
180 mWaitingToSend = (OCFile) savedInstanceState.getParcelable(
181 FileDisplayActivity.KEY_WAITING_TO_SEND);
182 } else {
183 mWaitingToPreview = null;
184 mSyncInProgress = false;
185 mWaitingToSend = null;
186 }
187
188 /// USER INTERFACE
189
190 // Inflate and set the layout view
191 setContentView(R.layout.files);
192
193 // Navigation Drawer
194 initDrawer();
195
196 mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
197 mProgressBar.setIndeterminateDrawable(
198 ContextCompat.getDrawable(this,
199 R.drawable.actionbar_progress_indeterminate_horizontal));
200
201 mDualPane = getResources().getBoolean(R.bool.large_land_layout);
202 mLeftFragmentContainer = findViewById(R.id.left_fragment_container);
203 mRightFragmentContainer = findViewById(R.id.right_fragment_container);
204 if (savedInstanceState == null) {
205 createMinFragments();
206 }
207
208 // Action bar setup
209 getSupportActionBar().setHomeButtonEnabled(true); // mandatory since Android ICS,
210 // according to the official
211 // documentation
212
213 // enable ActionBar app icon to behave as action to toggle nav drawer
214 //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
215 getSupportActionBar().setHomeButtonEnabled(true);
216
217 mProgressBar.setIndeterminate(mSyncInProgress);
218 // always AFTER setContentView(...) ; to work around bug in its implementation
219
220 setBackgroundText();
221
222 Log_OC.v(TAG, "onCreate() end");
223 }
224
225 @Override
226 protected void onStart() {
227 Log_OC.v(TAG, "onStart() start");
228 super.onStart();
229 Log_OC.v(TAG, "onStart() end");
230 }
231
232 @Override
233 protected void onStop() {
234 Log_OC.v(TAG, "onStop() start");
235 super.onStop();
236 Log_OC.v(TAG, "onStop() end");
237 }
238
239 @Override
240 protected void onDestroy() {
241 Log_OC.v(TAG, "onDestroy() start");
242 super.onDestroy();
243 Log_OC.v(TAG, "onDestroy() end");
244 }
245
246 /**
247 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
248 */
249 @Override
250 protected void onAccountSet(boolean stateWasRecovered) {
251 super.onAccountSet(stateWasRecovered);
252 if (getAccount() != null) {
253 /// Check whether the 'main' OCFile handled by the Activity is contained in the
254 // current Account
255 OCFile file = getFile();
256 // get parent from path
257 String parentPath = "";
258 if (file != null) {
259 if (file.isDown() && file.getLastSyncDateForProperties() == 0) {
260 // upload in progress - right now, files are not inserted in the local
261 // cache until the upload is successful get parent from path
262 parentPath = file.getRemotePath().substring(0,
263 file.getRemotePath().lastIndexOf(file.getFileName()));
264 if (getStorageManager().getFileByPath(parentPath) == null)
265 file = null; // not able to know the directory where the file is uploading
266 } else {
267 file = getStorageManager().getFileByPath(file.getRemotePath());
268 // currentDir = null if not in the current Account
269 }
270 }
271 if (file == null) {
272 // fall back to root folder
273 file = getStorageManager().getFileByPath(OCFile.ROOT_PATH); // never returns null
274 }
275 setFile(file);
276
277 if (mAccountWasSet) {
278 setUsernameInDrawer((RelativeLayout) findViewById(R.id.left_drawer), getAccount());
279 }
280
281 if (!stateWasRecovered) {
282 Log_OC.d(TAG, "Initializing Fragments in onAccountChanged..");
283 initFragmentsWithFile();
284 if (file.isFolder()) {
285 startSyncFolderOperation(file, false);
286 }
287
288 } else {
289 updateFragmentsVisibility(!file.isFolder());
290 updateActionBarTitleAndHomeButton(file.isFolder() ? null : file);
291 }
292 }
293 }
294
295 private void createMinFragments() {
296 OCFileListFragment listOfFiles = new OCFileListFragment();
297 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
298 transaction.add(R.id.left_fragment_container, listOfFiles, TAG_LIST_OF_FILES);
299 transaction.commit();
300 }
301
302 private void initFragmentsWithFile() {
303 if (getAccount() != null && getFile() != null) {
304 /// First fragment
305 OCFileListFragment listOfFiles = getListOfFilesFragment();
306 if (listOfFiles != null) {
307 listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
308 } else {
309 Log_OC.e(TAG, "Still have a chance to lose the initializacion of list fragment >(");
310 }
311
312 /// Second fragment
313 OCFile file = getFile();
314 Fragment secondFragment = chooseInitialSecondFragment(file);
315 if (secondFragment != null) {
316 setSecondFragment(secondFragment);
317 updateFragmentsVisibility(true);
318 updateActionBarTitleAndHomeButton(file);
319
320 } else {
321 cleanSecondFragment();
322 if (file.isDown() && PreviewTextFragment.canBePreviewed(file))
323 startTextPreview(file);
324 }
325
326 switchLayout(getFile());
327
328 } else {
329 Log_OC.wtf(TAG, "initFragments() called with invalid NULLs!");
330 if (getAccount() == null) {
331 Log_OC.wtf(TAG, "\t account is NULL");
332 }
333 if (getFile() == null) {
334 Log_OC.wtf(TAG, "\t file is NULL");
335 }
336 }
337 }
338
339 private void switchLayout(OCFile file){
340 if (DisplayUtils.isGridView(file, getStorageManager())){
341 switchToGridView();
342 } else {
343 switchToListView();
344 }
345 }
346
347 private Fragment chooseInitialSecondFragment(OCFile file) {
348 Fragment secondFragment = null;
349 if (file != null && !file.isFolder()) {
350 if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)
351 && file.getLastSyncDateForProperties() > 0 // temporal fix
352 ) {
353 int startPlaybackPosition =
354 getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
355 boolean autoplay =
356 getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
357 secondFragment = new PreviewMediaFragment(file, getAccount(),
358 startPlaybackPosition, autoplay);
359
360 } else if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) {
361 secondFragment = null;
362 } else {
363 secondFragment = FileDetailFragment.newInstance(file, getAccount());
364 }
365 }
366 return secondFragment;
367 }
368
369
370 /**
371 * Replaces the second fragment managed by the activity with the received as
372 * a parameter.
373 * <p/>
374 * Assumes never will be more than two fragments managed at the same time.
375 *
376 * @param fragment New second Fragment to set.
377 */
378 private void setSecondFragment(Fragment fragment) {
379 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
380 transaction.replace(R.id.right_fragment_container, fragment, TAG_SECOND_FRAGMENT);
381 transaction.commit();
382 }
383
384
385 private void updateFragmentsVisibility(boolean existsSecondFragment) {
386 if (mDualPane) {
387 if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
388 mLeftFragmentContainer.setVisibility(View.VISIBLE);
389 }
390 if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
391 mRightFragmentContainer.setVisibility(View.VISIBLE);
392 }
393
394 } else if (existsSecondFragment) {
395 if (mLeftFragmentContainer.getVisibility() != View.GONE) {
396 mLeftFragmentContainer.setVisibility(View.GONE);
397 }
398 if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
399 mRightFragmentContainer.setVisibility(View.VISIBLE);
400 }
401
402 } else {
403 if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
404 mLeftFragmentContainer.setVisibility(View.VISIBLE);
405 }
406 if (mRightFragmentContainer.getVisibility() != View.GONE) {
407 mRightFragmentContainer.setVisibility(View.GONE);
408 }
409 }
410 }
411
412
413 private OCFileListFragment getListOfFilesFragment() {
414 Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(
415 FileDisplayActivity.TAG_LIST_OF_FILES);
416 if (listOfFiles != null) {
417 return (OCFileListFragment) listOfFiles;
418 }
419 Log_OC.wtf(TAG, "Access to unexisting list of files fragment!!");
420 return null;
421 }
422
423 public FileFragment getSecondFragment() {
424 Fragment second = getSupportFragmentManager().findFragmentByTag(
425 FileDisplayActivity.TAG_SECOND_FRAGMENT);
426 if (second != null) {
427 return (FileFragment) second;
428 }
429 return null;
430 }
431
432 protected void cleanSecondFragment() {
433 Fragment second = getSecondFragment();
434 if (second != null) {
435 FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
436 tr.remove(second);
437 tr.commit();
438 }
439 updateFragmentsVisibility(false);
440 updateActionBarTitleAndHomeButton(null);
441 }
442
443 protected void refreshListOfFilesFragment() {
444 OCFileListFragment fileListFragment = getListOfFilesFragment();
445 if (fileListFragment != null) {
446 fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
447 }
448 }
449
450 protected void refreshSecondFragment(String downloadEvent, String downloadedRemotePath,
451 boolean success) {
452 FileFragment secondFragment = getSecondFragment();
453 boolean waitedPreview = (mWaitingToPreview != null &&
454 mWaitingToPreview.getRemotePath().equals(downloadedRemotePath));
455 if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
456 FileDetailFragment detailsFragment = (FileDetailFragment) secondFragment;
457 OCFile fileInFragment = detailsFragment.getFile();
458 if (fileInFragment != null &&
459 !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
460 // the user browsed to other file ; forget the automatic preview
461 mWaitingToPreview = null;
462
463 } else if (downloadEvent.equals(FileDownloader.getDownloadAddedMessage())) {
464 // grant that the right panel updates the progress bar
465 detailsFragment.listenForTransferProgress();
466 detailsFragment.updateFileDetails(true, false);
467
468 } else if (downloadEvent.equals(FileDownloader.getDownloadFinishMessage())) {
469 // update the right panel
470 boolean detailsFragmentChanged = false;
471 if (waitedPreview) {
472 if (success) {
473 mWaitingToPreview = getStorageManager().getFileById(
474 mWaitingToPreview.getFileId()); // update the file from database,
475 // for the local storage path
476 if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) {
477 startMediaPreview(mWaitingToPreview, 0, true);
478 detailsFragmentChanged = true;
479 } else if (PreviewTextFragment.canBePreviewed(mWaitingToPreview)) {
480 startTextPreview(mWaitingToPreview);
481 detailsFragmentChanged = true;
482 } else {
483 getFileOperationsHelper().openFile(mWaitingToPreview);
484 }
485 }
486 mWaitingToPreview = null;
487 }
488 if (!detailsFragmentChanged) {
489 detailsFragment.updateFileDetails(false, (success));
490 }
491 }
492 }
493 }
494
495 @Override
496 public boolean onPrepareOptionsMenu(Menu menu) {
497 boolean drawerOpen = mDrawerLayout.isDrawerOpen(GravityCompat.START);
498 menu.findItem(R.id.action_sort).setVisible(!drawerOpen);
499 menu.findItem(R.id.action_sync_account).setVisible(!drawerOpen);
500 menu.findItem(R.id.action_switch_view).setVisible(!drawerOpen);
501
502 return super.onPrepareOptionsMenu(menu);
503 }
504
505 @Override
506 public boolean onCreateOptionsMenu(Menu menu) {
507 MenuInflater inflater = getMenuInflater();
508 inflater.inflate(R.menu.main_menu, menu);
509 menu.findItem(R.id.action_create_dir).setVisible(false);
510 mOptionsMenu = menu;
511
512 MenuItem menuItem = mOptionsMenu.findItem(R.id.action_switch_view);
513
514 changeGridIcon();
515
516 return true;
517 }
518
519
520 @Override
521 public boolean onOptionsItemSelected(MenuItem item) {
522 boolean retval = true;
523 switch (item.getItemId()) {
524 case R.id.action_sync_account: {
525 startSynchronization();
526 break;
527 }
528 case android.R.id.home: {
529 FileFragment second = getSecondFragment();
530 OCFile currentDir = getCurrentDir();
531 if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
532 mDrawerLayout.closeDrawer(GravityCompat.START);
533 } else if((currentDir != null && currentDir.getParentId() != 0) ||
534 (second != null && second.getFile() != null)) {
535 onBackPressed();
536
537 } else {
538 mDrawerLayout.openDrawer(GravityCompat.START);
539 }
540 break;
541 }
542 case R.id.action_sort: {
543 SharedPreferences appPreferences = PreferenceManager
544 .getDefaultSharedPreferences(this);
545
546 // Read sorting order, default to sort by name ascending
547 Integer sortOrder = appPreferences
548 .getInt("sortOrder", FileStorageUtils.SORT_NAME);
549
550 AlertDialog.Builder builder = new AlertDialog.Builder(this);
551 builder.setTitle(R.string.actionbar_sort_title)
552 .setSingleChoiceItems(R.array.actionbar_sortby, sortOrder ,
553 new DialogInterface.OnClickListener() {
554 public void onClick(DialogInterface dialog, int which) {
555 switch (which){
556 case 0:
557 sortByName(true);
558 break;
559 case 1:
560 sortByDate(false);
561 break;
562 }
563
564 dialog.dismiss();
565 }
566 });
567 builder.create().show();
568 break;
569 }
570 case R.id.action_switch_view:{
571 if (isGridView()){
572 item.setTitle(getApplicationContext().getString(R.string.action_switch_grid_view));
573 item.setIcon(ContextCompat.getDrawable(getApplicationContext(),
574 R.drawable.ic_view_module));
575 DisplayUtils.setViewMode(getFile(), false);
576 switchToListView();
577 } else {
578 item.setTitle(getApplicationContext().getString(R.string.action_switch_list_view));
579 item.setIcon(ContextCompat.getDrawable(getApplicationContext(),
580 R.drawable.ic_view_list));
581 DisplayUtils.setViewMode(getFile(), true);
582 switchToGridView();
583 }
584
585 return true;
586 }
587 default:
588 retval = super.onOptionsItemSelected(item);
589 }
590 return retval;
591 }
592
593 public void createFolder() {
594 CreateFolderDialogFragment dialog =
595 CreateFolderDialogFragment.newInstance(getCurrentDir());
596 dialog.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
597 }
598
599 public void uploadLocalFilesSelected() {
600 Intent action = new Intent(this, UploadFilesActivity.class);
601 action.putExtra(
602 UploadFilesActivity.EXTRA_ACCOUNT,
603 getAccount()
604 );
605 startActivityForResult(action, ACTION_SELECT_MULTIPLE_FILES);
606 }
607
608 public void uploadFromOtherAppsSelected() {
609 Intent action = new Intent(Intent.ACTION_GET_CONTENT);
610 action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
611 //Intent.EXTRA_ALLOW_MULTIPLE is only supported on api level 18+, Jelly Bean
612 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
613 action.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
614 }
615 startActivityForResult(
616 Intent.createChooser(action, getString(R.string.upload_chooser_title)),
617 ACTION_SELECT_CONTENT_FROM_APPS
618 );
619 }
620
621 private void startSynchronization() {
622 Log_OC.d(TAG, "Got to start sync");
623 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
624 Log_OC.d(TAG, "Canceling all syncs for " + MainApp.getAuthority());
625 ContentResolver.cancelSync(null, MainApp.getAuthority());
626 // cancel the current synchronizations of any ownCloud account
627 Bundle bundle = new Bundle();
628 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
629 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
630 Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
631 MainApp.getAuthority());
632 ContentResolver.requestSync(
633 getAccount(),
634 MainApp.getAuthority(), bundle);
635 } else {
636 Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
637 MainApp.getAuthority() + " with new API");
638 SyncRequest.Builder builder = new SyncRequest.Builder();
639 builder.setSyncAdapter(getAccount(), MainApp.getAuthority());
640 builder.setExpedited(true);
641 builder.setManual(true);
642 builder.syncOnce();
643
644 // Fix bug in Android Lollipop when you click on refresh the whole account
645 Bundle extras = new Bundle();
646 builder.setExtras(extras);
647
648 SyncRequest request = builder.build();
649 ContentResolver.requestSync(request);
650 }
651 }
652
653 /**
654 * Called, when the user selected something for uploading
655 *
656 */
657 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
658 @Override
659 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
660
661 if (requestCode == ACTION_SELECT_CONTENT_FROM_APPS && (resultCode == RESULT_OK ||
662 resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
663 //getClipData is only supported on api level 16+, Jelly Bean
664 if (data.getData() == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
665 for( int i = 0; i < data.getClipData().getItemCount(); i++){
666 Intent intent = new Intent();
667 intent.setData(data.getClipData().getItemAt(i).getUri());
668 requestSimpleUpload(intent, resultCode);
669 }
670 }else {
671 requestSimpleUpload(data, resultCode);
672 }
673 } else if (requestCode == ACTION_SELECT_MULTIPLE_FILES && (resultCode == RESULT_OK ||
674 resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
675 requestMultipleUpload(data, resultCode);
676
677 } else if (requestCode == ACTION_MOVE_FILES && resultCode == RESULT_OK){
678 final Intent fData = data;
679 final int fResultCode = resultCode;
680 getHandler().postDelayed(
681 new Runnable() {
682 @Override
683 public void run() {
684 requestMoveOperation(fData, fResultCode);
685 }
686 },
687 DELAY_TO_REQUEST_OPERATIONS_LATER
688 );
689
690 } else if (requestCode == ACTION_COPY_FILES && resultCode == RESULT_OK) {
691
692 final Intent fData = data;
693 final int fResultCode = resultCode;
694 getHandler().postDelayed(
695 new Runnable() {
696 @Override
697 public void run() {
698 requestCopyOperation(fData, fResultCode);
699 }
700 },
701 DELAY_TO_REQUEST_OPERATIONS_LATER
702 );
703
704 } else {
705 super.onActivityResult(requestCode, resultCode, data);
706 }
707
708 }
709
710 private void requestMultipleUpload(Intent data, int resultCode) {
711 String[] filePaths = data.getStringArrayExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES);
712 if (filePaths != null) {
713 String[] remotePaths = new String[filePaths.length];
714 String remotePathBase = getCurrentDir().getRemotePath();
715 for (int j = 0; j< remotePaths.length; j++) {
716 remotePaths[j] = remotePathBase + (new File(filePaths[j])).getName();
717 }
718
719 Intent i = new Intent(this, FileUploader.class);
720 i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
721 i.putExtra(FileUploader.KEY_LOCAL_FILE, filePaths);
722 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
723 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
724 if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
725 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
726 startService(i);
727
728 } else {
729 Log_OC.d(TAG, "User clicked on 'Update' with no selection");
730 Toast t = Toast.makeText(this, getString(R.string.filedisplay_no_file_selected),
731 Toast.LENGTH_LONG);
732 t.show();
733 return;
734 }
735 }
736
737
738 private void requestSimpleUpload(Intent data, int resultCode) {
739 String filePath = null;
740 String mimeType = null;
741
742 Uri selectedImageUri = data.getData();
743
744 try {
745 mimeType = getContentResolver().getType(selectedImageUri);
746
747 String fileManagerString = selectedImageUri.getPath();
748 String selectedImagePath = UriUtils.getLocalPath(selectedImageUri, this);
749
750 if (selectedImagePath != null)
751 filePath = selectedImagePath;
752 else
753 filePath = fileManagerString;
754
755 } catch (Exception e) {
756 Log_OC.e(TAG, "Unexpected exception when trying to read the result of " +
757 "Intent.ACTION_GET_CONTENT", e);
758
759 } finally {
760 if (filePath == null) {
761 Log_OC.e(TAG, "Couldn't resolve path to file");
762 Toast t = Toast.makeText(
763 this, getString(R.string.filedisplay_unexpected_bad_get_content),
764 Toast.LENGTH_LONG
765 );
766 t.show();
767 return;
768 }
769 }
770
771 Intent i = new Intent(this, FileUploader.class);
772 i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
773 OCFile currentDir = getCurrentDir();
774 String remotePath = (currentDir != null) ? currentDir.getRemotePath() : OCFile.ROOT_PATH;
775
776 if (filePath.startsWith(UriUtils.URI_CONTENT_SCHEME)) {
777 Cursor cursor = getContentResolver().query(Uri.parse(filePath), null, null, null, null);
778 try {
779 if (cursor != null && cursor.moveToFirst()) {
780 String displayName = cursor.getString(cursor.getColumnIndex(
781 OpenableColumns.DISPLAY_NAME));
782 Log_OC.v(TAG, "Display Name: " + displayName );
783
784 displayName.replace(File.separatorChar, '_');
785 displayName.replace(File.pathSeparatorChar, '_');
786 remotePath += displayName + DisplayUtils.getComposedFileExtension(filePath);
787
788 }
789 // and what happens in case of error?; wrong target name for the upload
790 } finally {
791 cursor.close();
792 }
793
794 } else {
795 remotePath += new File(filePath).getName();
796 }
797
798 i.putExtra(FileUploader.KEY_LOCAL_FILE, filePath);
799 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePath);
800 i.putExtra(FileUploader.KEY_MIME_TYPE, mimeType);
801 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
802 if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
803 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
804 startService(i);
805 }
806
807 /**
808 * Request the operation for moving the file/folder from one path to another
809 *
810 * @param data Intent received
811 * @param resultCode Result code received
812 */
813 private void requestMoveOperation(Intent data, int resultCode) {
814 OCFile folderToMoveAt = (OCFile) data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
815
816 ArrayList<OCFile> files = data.getParcelableArrayListExtra(FolderPickerActivity.EXTRA_FILES);
817
818 for (Parcelable file : files) {
819 getFileOperationsHelper().moveFile(folderToMoveAt, (OCFile) file);
820 }
821 }
822
823 /**
824 * Request the operation for copying the file/folder from one path to another
825 *
826 * @param data Intent received
827 * @param resultCode Result code received
828 */
829 private void requestCopyOperation(Intent data, int resultCode) {
830 OCFile folderToMoveAt = data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
831
832 ArrayList<OCFile> files = data.getParcelableArrayListExtra(FolderPickerActivity.EXTRA_FILES);
833
834 for (Parcelable file : files) {
835 getFileOperationsHelper().copyFile(folderToMoveAt, (OCFile) file);
836 }
837 }
838
839 @Override
840 public void onBackPressed() {
841 boolean isFabOpen = isFabOpen();
842 boolean isDrawerOpen = isDrawerOpen();
843
844 /*
845 * BackPressed priority/hierarchy:
846 * 1. close drawer if opened
847 * 2. close FAB if open (only if drawer isn't open)
848 * 3. navigate up (only if drawer and FAB aren't open)
849 */
850 if(isDrawerOpen && isFabOpen) {
851 // close drawer first
852 super.onBackPressed();
853 } else if(isDrawerOpen && !isFabOpen) {
854 // close drawer
855 super.onBackPressed();
856 } else if (!isDrawerOpen && isFabOpen) {
857 // close fab
858 getListOfFilesFragment().getFabMain().collapse();
859 } else {
860 // all closed
861 OCFileListFragment listOfFiles = getListOfFilesFragment();
862 if (mDualPane || getSecondFragment() == null) {
863 OCFile currentDir = getCurrentDir();
864 if (currentDir == null || currentDir.getParentId() == FileDataStorageManager.ROOT_PARENT_ID) {
865 finish();
866 return;
867 }
868 if (listOfFiles != null) { // should never be null, indeed
869 listOfFiles.onBrowseUp();
870 }
871 }
872 if (listOfFiles != null) { // should never be null, indeed
873 setFile(listOfFiles.getCurrentFile());
874 }
875 cleanSecondFragment();
876 changeGridIcon();
877 }
878 }
879
880 private void changeGridIcon(){
881 MenuItem menuItem = mOptionsMenu.findItem(R.id.action_switch_view);
882 if (DisplayUtils.isGridView(getFile(), getStorageManager())){
883 menuItem.setTitle(getApplicationContext().getString(R.string.action_switch_list_view));
884 menuItem.setIcon(ContextCompat.getDrawable(getApplicationContext(),
885 R.drawable.ic_view_list));
886 } else {
887 menuItem.setTitle(getApplicationContext().getString(R.string.action_switch_grid_view));
888 menuItem.setIcon(ContextCompat.getDrawable(getApplicationContext(),
889 R.drawable.ic_view_module));
890 }
891 }
892
893 @Override
894 protected void onSaveInstanceState(Bundle outState) {
895 // responsibility of restore is preferred in onCreate() before than in
896 // onRestoreInstanceState when there are Fragments involved
897 Log_OC.v(TAG, "onSaveInstanceState() start");
898 super.onSaveInstanceState(outState);
899 outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
900 outState.putBoolean(FileDisplayActivity.KEY_SYNC_IN_PROGRESS, mSyncInProgress);
901 //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
902 // mRefreshSharesInProgress);
903 outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_SEND, mWaitingToSend);
904
905 Log_OC.v(TAG, "onSaveInstanceState() end");
906 }
907
908
909 @Override
910 protected void onResume() {
911 Log_OC.v(TAG, "onResume() start");
912 super.onResume();
913 // refresh Navigation Drawer account list
914 mNavigationDrawerAdapter.updateAccountList();
915
916 // refresh list of files
917 refreshListOfFilesFragment();
918
919 // Listen for sync messages
920 IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START);
921 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END);
922 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED);
923 syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED);
924 syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED);
925 mSyncBroadcastReceiver = new SyncBroadcastReceiver();
926 registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
927 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
928 // syncIntentFilter);
929
930 // Listen for upload messages
931 IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.getUploadFinishMessage());
932 mUploadFinishReceiver = new UploadFinishReceiver();
933 registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
934
935 // Listen for download messages
936 IntentFilter downloadIntentFilter = new IntentFilter(
937 FileDownloader.getDownloadAddedMessage());
938 downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
939 mDownloadFinishReceiver = new DownloadFinishReceiver();
940 registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
941
942 Log_OC.v(TAG, "onResume() end");
943
944 }
945
946
947 @Override
948 protected void onPause() {
949 Log_OC.v(TAG, "onPause() start");
950 if (mSyncBroadcastReceiver != null) {
951 unregisterReceiver(mSyncBroadcastReceiver);
952 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
953 mSyncBroadcastReceiver = null;
954 }
955 if (mUploadFinishReceiver != null) {
956 unregisterReceiver(mUploadFinishReceiver);
957 mUploadFinishReceiver = null;
958 }
959 if (mDownloadFinishReceiver != null) {
960 unregisterReceiver(mDownloadFinishReceiver);
961 mDownloadFinishReceiver = null;
962 }
963
964 super.onPause();
965 Log_OC.v(TAG, "onPause() end");
966 }
967
968 public boolean isFabOpen() {
969 if(getListOfFilesFragment() != null && getListOfFilesFragment().getFabMain() != null && getListOfFilesFragment().getFabMain().isExpanded()) {
970 return true;
971 } else {
972 return false;
973 }
974 }
975
976
977 private class SyncBroadcastReceiver extends BroadcastReceiver {
978
979 /**
980 * {@link BroadcastReceiver} to enable syncing feedback in UI
981 */
982 @Override
983 public void onReceive(Context context, Intent intent) {
984 try {
985 String event = intent.getAction();
986 Log_OC.d(TAG, "Received broadcast " + event);
987 String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
988 String synchFolderRemotePath =
989 intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
990 RemoteOperationResult synchResult =
991 (RemoteOperationResult)intent.getSerializableExtra(
992 FileSyncAdapter.EXTRA_RESULT);
993 boolean sameAccount = (getAccount() != null &&
994 accountName.equals(getAccount().name) && getStorageManager() != null);
995
996 if (sameAccount) {
997
998 if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
999 mSyncInProgress = true;
1000
1001 } else {
1002 OCFile currentFile = (getFile() == null) ? null :
1003 getStorageManager().getFileByPath(getFile().getRemotePath());
1004 OCFile currentDir = (getCurrentDir() == null) ? null :
1005 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
1006
1007 if (currentDir == null) {
1008 // current folder was removed from the server
1009 Toast.makeText( FileDisplayActivity.this,
1010 String.format(
1011 getString(R.string.
1012 sync_current_folder_was_removed),
1013 synchFolderRemotePath),
1014 Toast.LENGTH_LONG)
1015 .show();
1016
1017 browseToRoot();
1018
1019 } else {
1020 if (currentFile == null && !getFile().isFolder()) {
1021 // currently selected file was removed in the server, and now we
1022 // know it
1023 cleanSecondFragment();
1024 currentFile = currentDir;
1025 }
1026
1027 if (synchFolderRemotePath != null &&
1028 currentDir.getRemotePath().equals(synchFolderRemotePath)) {
1029 OCFileListFragment fileListFragment = getListOfFilesFragment();
1030 if (fileListFragment != null) {
1031 fileListFragment.listDirectory(currentDir,
1032 MainApp.getOnlyOnDevice());
1033 }
1034 }
1035 setFile(currentFile);
1036 }
1037
1038 mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) &&
1039 !RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED
1040 .equals(event));
1041
1042 if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.
1043 equals(event) &&/// TODO refactor and make common
1044
1045 synchResult != null && !synchResult.isSuccess() &&
1046 (synchResult.getCode() == ResultCode.UNAUTHORIZED ||
1047 synchResult.isIdPRedirection() ||
1048 (synchResult.isException() && synchResult.getException()
1049 instanceof AuthenticatorException))) {
1050
1051
1052 try {
1053 OwnCloudClient client;
1054 OwnCloudAccount ocAccount =
1055 new OwnCloudAccount(getAccount(), context);
1056 client = (OwnCloudClientManagerFactory.getDefaultSingleton().
1057 removeClientFor(ocAccount));
1058 if (client != null) {
1059 OwnCloudCredentials cred = client.getCredentials();
1060 if (cred != null) {
1061 AccountManager am = AccountManager.get(context);
1062 if (cred.authTokenExpires()) {
1063 am.invalidateAuthToken(
1064 getAccount().type,
1065 cred.getAuthToken()
1066 );
1067 } else {
1068 am.clearPassword(getAccount());
1069 }
1070 }
1071 }
1072 requestCredentialsUpdate();
1073
1074 } catch (AccountNotFoundException e) {
1075 Log_OC.e(TAG, "Account " + getAccount() + " was removed!", e);
1076 }
1077
1078 }
1079
1080 }
1081 removeStickyBroadcast(intent);
1082 Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
1083 mProgressBar.setIndeterminate(mSyncInProgress);
1084 //mProgressBar.setVisibility((mSyncInProgress) ? View.VISIBLE : View.INVISIBLE);
1085 //setSupportProgressBarIndeterminateVisibility(mSyncInProgress
1086 /*|| mRefreshSharesInProgress*/ //);
1087
1088 setBackgroundText();
1089
1090 }
1091
1092 if (synchResult != null) {
1093 if (synchResult.getCode().equals(
1094 RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
1095 mLastSslUntrustedServerResult = synchResult;
1096 }
1097 }
1098 } catch (RuntimeException e) {
1099 // avoid app crashes after changing the serial id of RemoteOperationResult
1100 // in owncloud library with broadcast notifications pending to process
1101 removeStickyBroadcast(intent);
1102 }
1103 }
1104 }
1105
1106 /**
1107 * Show a text message on screen view for notifying user if content is
1108 * loading or folder is empty
1109 */
1110 private void setBackgroundText() {
1111 OCFileListFragment ocFileListFragment = getListOfFilesFragment();
1112 if (ocFileListFragment != null) {
1113 int message = R.string.file_list_loading;
1114 if (!mSyncInProgress) {
1115 // In case file list is empty
1116 message = R.string.file_list_empty;
1117 }
1118 ocFileListFragment.setMessageForEmptyList(getString(message));
1119 } else {
1120 Log_OC.e(TAG, "OCFileListFragment is null");
1121 }
1122 }
1123
1124 /**
1125 * Once the file upload has finished -> update view
1126 */
1127 private class UploadFinishReceiver extends BroadcastReceiver {
1128 /**
1129 * Once the file upload has finished -> update view
1130 *
1131 * @author David A. Velasco
1132 * {@link BroadcastReceiver} to enable upload feedback in UI
1133 */
1134 @Override
1135 public void onReceive(Context context, Intent intent) {
1136 try {
1137 String uploadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
1138 String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
1139 boolean sameAccount = getAccount() != null && accountName.equals(getAccount().name);
1140 OCFile currentDir = getCurrentDir();
1141 boolean isDescendant = (currentDir != null) && (uploadedRemotePath != null) &&
1142 (uploadedRemotePath.startsWith(currentDir.getRemotePath()));
1143
1144 if (sameAccount && isDescendant) {
1145 String linkedToRemotePath =
1146 intent.getStringExtra(FileDownloader.EXTRA_LINKED_TO_PATH);
1147 if (linkedToRemotePath == null || isAscendant(linkedToRemotePath)) {
1148 refreshListOfFilesFragment();
1149 }
1150 }
1151
1152 boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT,
1153 false);
1154 boolean renamedInUpload = getFile().getRemotePath().
1155 equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH));
1156 boolean sameFile = getFile().getRemotePath().equals(uploadedRemotePath) ||
1157 renamedInUpload;
1158 FileFragment details = getSecondFragment();
1159 boolean detailFragmentIsShown = (details != null &&
1160 details instanceof FileDetailFragment);
1161
1162 if (sameAccount && sameFile && detailFragmentIsShown) {
1163 if (uploadWasFine) {
1164 setFile(getStorageManager().getFileByPath(uploadedRemotePath));
1165 }
1166 if (renamedInUpload) {
1167 String newName = (new File(uploadedRemotePath)).getName();
1168 Toast msg = Toast.makeText(
1169 context,
1170 String.format(
1171 getString(R.string.filedetails_renamed_in_upload_msg),
1172 newName),
1173 Toast.LENGTH_LONG);
1174 msg.show();
1175 }
1176 if (uploadWasFine || getFile().fileExists()) {
1177 ((FileDetailFragment) details).updateFileDetails(false, true);
1178 } else {
1179 cleanSecondFragment();
1180 }
1181
1182 // Force the preview if the file is an image or text file
1183 if (uploadWasFine) {
1184 OCFile ocFile = getFile();
1185 if (PreviewImageFragment.canBePreviewed(ocFile))
1186 startImagePreview(getFile());
1187 else if (PreviewTextFragment.canBePreviewed(ocFile))
1188 startTextPreview(ocFile);
1189 // TODO what about other kind of previews?
1190 }
1191 }
1192
1193 mProgressBar.setIndeterminate(false);
1194 } finally {
1195 if (intent != null) {
1196 removeStickyBroadcast(intent);
1197 }
1198 }
1199
1200 }
1201
1202 // TODO refactor this receiver, and maybe DownloadFinishReceiver; this method is duplicated :S
1203 private boolean isAscendant(String linkedToRemotePath) {
1204 OCFile currentDir = getCurrentDir();
1205 return (
1206 currentDir != null &&
1207 currentDir.getRemotePath().startsWith(linkedToRemotePath)
1208 );
1209 }
1210
1211
1212 }
1213
1214
1215 /**
1216 * Class waiting for broadcast events from the {@link FileDownloader} service.
1217 *
1218 * Updates the UI when a download is started or finished, provided that it is relevant for the
1219 * current folder.
1220 */
1221 private class DownloadFinishReceiver extends BroadcastReceiver {
1222
1223 @Override
1224 public void onReceive(Context context, Intent intent) {
1225 try {
1226 boolean sameAccount = isSameAccount(intent);
1227 String downloadedRemotePath =
1228 intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
1229 boolean isDescendant = isDescendant(downloadedRemotePath);
1230
1231 if (sameAccount && isDescendant) {
1232 String linkedToRemotePath =
1233 intent.getStringExtra(FileDownloader.EXTRA_LINKED_TO_PATH);
1234 if (linkedToRemotePath == null || isAscendant(linkedToRemotePath)) {
1235 refreshListOfFilesFragment();
1236 }
1237 refreshSecondFragment(
1238 intent.getAction(),
1239 downloadedRemotePath,
1240 intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)
1241 );
1242 }
1243
1244 if (mWaitingToSend != null) {
1245 mWaitingToSend =
1246 getStorageManager().getFileByPath(mWaitingToSend.getRemotePath());
1247 if (mWaitingToSend.isDown()) {
1248 sendDownloadedFile();
1249 }
1250 }
1251
1252 } finally {
1253 if (intent != null) {
1254 removeStickyBroadcast(intent);
1255 }
1256 }
1257 }
1258
1259 private boolean isDescendant(String downloadedRemotePath) {
1260 OCFile currentDir = getCurrentDir();
1261 return (
1262 currentDir != null &&
1263 downloadedRemotePath != null &&
1264 downloadedRemotePath.startsWith(currentDir.getRemotePath())
1265 );
1266 }
1267
1268 private boolean isAscendant(String linkedToRemotePath) {
1269 OCFile currentDir = getCurrentDir();
1270 return (
1271 currentDir != null &&
1272 currentDir.getRemotePath().startsWith(linkedToRemotePath)
1273 );
1274 }
1275
1276 private boolean isSameAccount(Intent intent) {
1277 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
1278 return (accountName != null && getAccount() != null &&
1279 accountName.equals(getAccount().name));
1280 }
1281 }
1282
1283
1284 public void browseToRoot() {
1285 OCFileListFragment listOfFiles = getListOfFilesFragment();
1286 if (listOfFiles != null) { // should never be null, indeed
1287 OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
1288 listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
1289 setFile(listOfFiles.getCurrentFile());
1290 startSyncFolderOperation(root, false);
1291 }
1292 cleanSecondFragment();
1293 }
1294
1295
1296 /**
1297 * {@inheritDoc}
1298 * <p/>
1299 * Updates action bar and second fragment, if in dual pane mode.
1300 */
1301 @Override
1302 public void onBrowsedDownTo(OCFile directory) {
1303 setFile(directory);
1304 cleanSecondFragment();
1305 // Sync Folder
1306 startSyncFolderOperation(directory, false);
1307
1308 MenuItem menuItem = mOptionsMenu.findItem(R.id.action_switch_view);
1309
1310 changeGridIcon();
1311 switchLayout(directory);
1312 }
1313
1314 /**
1315 * Shows the information of the {@link OCFile} received as a
1316 * parameter in the second fragment.
1317 *
1318 * @param file {@link OCFile} whose details will be shown
1319 */
1320 @Override
1321 public void showDetails(OCFile file) {
1322 Fragment detailFragment = FileDetailFragment.newInstance(file, getAccount());
1323 setSecondFragment(detailFragment);
1324 updateFragmentsVisibility(true);
1325 updateActionBarTitleAndHomeButton(file);
1326 setFile(file);
1327 }
1328
1329 @Override
1330 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
1331 if (mDualPane) {
1332 // in dual pane mode, keep the focus of title an action bar in the current folder
1333 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1334
1335 } else {
1336 super.updateActionBarTitleAndHomeButton(chosenFile);
1337 }
1338
1339 }
1340
1341 @Override
1342 protected ServiceConnection newTransferenceServiceConnection() {
1343 return new ListServiceConnection();
1344 }
1345
1346 /**
1347 * Defines callbacks for service binding, passed to bindService()
1348 */
1349 private class ListServiceConnection implements ServiceConnection {
1350
1351 @Override
1352 public void onServiceConnected(ComponentName component, IBinder service) {
1353 if (component.equals(new ComponentName(
1354 FileDisplayActivity.this, FileDownloader.class))) {
1355 Log_OC.d(TAG, "Download service connected");
1356 mDownloaderBinder = (FileDownloaderBinder) service;
1357 if (mWaitingToPreview != null)
1358 if (getStorageManager() != null) {
1359 // update the file
1360 mWaitingToPreview =
1361 getStorageManager().getFileById(mWaitingToPreview.getFileId());
1362 if (!mWaitingToPreview.isDown()) {
1363 requestForDownload();
1364 }
1365 }
1366
1367 } else if (component.equals(new ComponentName(FileDisplayActivity.this,
1368 FileUploader.class))) {
1369 Log_OC.d(TAG, "Upload service connected");
1370 mUploaderBinder = (FileUploaderBinder) service;
1371 } else {
1372 return;
1373 }
1374 // a new chance to get the mDownloadBinder through
1375 // getFileDownloadBinder() - THIS IS A MESS
1376 OCFileListFragment listOfFiles = getListOfFilesFragment();
1377 if (listOfFiles != null) {
1378 listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1379 }
1380 FileFragment secondFragment = getSecondFragment();
1381 if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
1382 FileDetailFragment detailFragment = (FileDetailFragment) secondFragment;
1383 detailFragment.listenForTransferProgress();
1384 detailFragment.updateFileDetails(false, false);
1385 }
1386 }
1387
1388 @Override
1389 public void onServiceDisconnected(ComponentName component) {
1390 if (component.equals(new ComponentName(FileDisplayActivity.this,
1391 FileDownloader.class))) {
1392 Log_OC.d(TAG, "Download service disconnected");
1393 mDownloaderBinder = null;
1394 } else if (component.equals(new ComponentName(FileDisplayActivity.this,
1395 FileUploader.class))) {
1396 Log_OC.d(TAG, "Upload service disconnected");
1397 mUploaderBinder = null;
1398 }
1399 }
1400 }
1401
1402 @Override
1403 public void onSavedCertificate() {
1404 startSyncFolderOperation(getCurrentDir(), false);
1405 }
1406
1407
1408 @Override
1409 public void onFailedSavingCertificate() {
1410 ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
1411 R.string.ssl_validator_not_saved, new String[]{}, R.string.common_ok, -1, -1
1412 );
1413 dialog.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED);
1414 }
1415
1416 @Override
1417 public void onCancelCertificate() {
1418 // nothing to do
1419 }
1420
1421 /**
1422 * Updates the view associated to the activity after the finish of some operation over files
1423 * in the current account.
1424 *
1425 * @param operation Removal operation performed.
1426 * @param result Result of the removal.
1427 */
1428 @Override
1429 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
1430 super.onRemoteOperationFinish(operation, result);
1431
1432 if (operation instanceof RemoveFileOperation) {
1433 onRemoveFileOperationFinish((RemoveFileOperation) operation, result);
1434
1435 } else if (operation instanceof RenameFileOperation) {
1436 onRenameFileOperationFinish((RenameFileOperation) operation, result);
1437
1438 } else if (operation instanceof SynchronizeFileOperation) {
1439 onSynchronizeFileOperationFinish((SynchronizeFileOperation) operation, result);
1440
1441 } else if (operation instanceof CreateFolderOperation) {
1442 onCreateFolderOperationFinish((CreateFolderOperation) operation, result);
1443
1444 } else if (operation instanceof CreateShareViaLinkOperation ||
1445 operation instanceof CreateShareWithShareeOperation ) {
1446
1447 refreshShowDetails();
1448 refreshListOfFilesFragment();
1449
1450 } else if (operation instanceof UnshareOperation) {
1451 onUnshareLinkOperationFinish((UnshareOperation) operation, result);
1452
1453 } else if (operation instanceof MoveFileOperation) {
1454 onMoveFileOperationFinish((MoveFileOperation) operation, result);
1455
1456 } else if (operation instanceof CopyFileOperation) {
1457 onCopyFileOperationFinish((CopyFileOperation) operation, result);
1458 }
1459
1460 }
1461
1462 private void onUnshareLinkOperationFinish(UnshareOperation operation,
1463 RemoteOperationResult result) {
1464 if (result.isSuccess()) {
1465 refreshShowDetails();
1466 refreshListOfFilesFragment();
1467
1468 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
1469 cleanSecondFragment();
1470 refreshListOfFilesFragment();
1471 }
1472 }
1473
1474 private void refreshShowDetails() {
1475 FileFragment details = getSecondFragment();
1476 if (details != null) {
1477 OCFile file = details.getFile();
1478 if (file != null) {
1479 file = getStorageManager().getFileByPath(file.getRemotePath());
1480 if (details instanceof PreviewMediaFragment) {
1481 // Refresh OCFile of the fragment
1482 ((PreviewMediaFragment) details).updateFile(file);
1483 } else if (details instanceof PreviewTextFragment) {
1484 // Refresh OCFile of the fragment
1485 ((PreviewTextFragment) details).updateFile(file);
1486 } else {
1487 showDetails(file);
1488 }
1489 }
1490 invalidateOptionsMenu();
1491 }
1492 }
1493
1494 /**
1495 * Updates the view associated to the activity after the finish of an operation trying to
1496 * remove a file.
1497 *
1498 * @param operation Removal operation performed.
1499 * @param result Result of the removal.
1500 */
1501 private void onRemoveFileOperationFinish(RemoveFileOperation operation,
1502 RemoteOperationResult result) {
1503 Toast msg = Toast.makeText(this,
1504 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1505 Toast.LENGTH_LONG);
1506 msg.show();
1507
1508 if (result.isSuccess()) {
1509 OCFile removedFile = operation.getFile();
1510 FileFragment second = getSecondFragment();
1511 if (second != null && removedFile.equals(second.getFile())) {
1512 if (second instanceof PreviewMediaFragment) {
1513 ((PreviewMediaFragment) second).stopPreview(true);
1514 }
1515 setFile(getStorageManager().getFileById(removedFile.getParentId()));
1516 cleanSecondFragment();
1517 }
1518 if (getStorageManager().getFileById(removedFile.getParentId()).equals(getCurrentDir())){
1519 refreshListOfFilesFragment();
1520 }
1521 invalidateOptionsMenu();
1522 } else {
1523 if (result.isSslRecoverableException()) {
1524 mLastSslUntrustedServerResult = result;
1525 showUntrustedCertDialog(mLastSslUntrustedServerResult);
1526 }
1527 }
1528 }
1529
1530
1531 /**
1532 * Updates the view associated to the activity after the finish of an operation trying to move a
1533 * file.
1534 *
1535 * @param operation Move operation performed.
1536 * @param result Result of the move operation.
1537 */
1538 private void onMoveFileOperationFinish(MoveFileOperation operation,
1539 RemoteOperationResult result) {
1540 if (result.isSuccess()) {
1541 refreshListOfFilesFragment();
1542 } else {
1543 try {
1544 Toast msg = Toast.makeText(FileDisplayActivity.this,
1545 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1546 Toast.LENGTH_LONG);
1547 msg.show();
1548
1549 } catch (NotFoundException e) {
1550 Log_OC.e(TAG, "Error while trying to show fail message ", e);
1551 }
1552 }
1553 }
1554
1555 /**
1556 * Updates the view associated to the activity after the finish of an operation trying to copy a
1557 * file.
1558 *
1559 * @param operation Copy operation performed.
1560 * @param result Result of the copy operation.
1561 */
1562 private void onCopyFileOperationFinish(CopyFileOperation operation, RemoteOperationResult result) {
1563 if (result.isSuccess()) {
1564 refreshListOfFilesFragment();
1565 } else {
1566 try {
1567 Toast msg = Toast.makeText(FileDisplayActivity.this,
1568 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1569 Toast.LENGTH_LONG);
1570 msg.show();
1571
1572 } catch (NotFoundException e) {
1573 Log_OC.e(TAG, "Error while trying to show fail message ", e);
1574 }
1575 }
1576 }
1577
1578 /**
1579 * Updates the view associated to the activity after the finish of an operation trying to rename
1580 * a file.
1581 *
1582 * @param operation Renaming operation performed.
1583 * @param result Result of the renaming.
1584 */
1585 private void onRenameFileOperationFinish(RenameFileOperation operation,
1586 RemoteOperationResult result) {
1587 OCFile renamedFile = operation.getFile();
1588 if (result.isSuccess()) {
1589 FileFragment details = getSecondFragment();
1590 if (details != null) {
1591 if (details instanceof FileDetailFragment &&
1592 renamedFile.equals(details.getFile()) ) {
1593 ((FileDetailFragment) details).updateFileDetails(renamedFile, getAccount());
1594 showDetails(renamedFile);
1595
1596 } else if (details instanceof PreviewMediaFragment &&
1597 renamedFile.equals(details.getFile())) {
1598 ((PreviewMediaFragment) details).updateFile(renamedFile);
1599 if (PreviewMediaFragment.canBePreviewed(renamedFile)) {
1600 int position = ((PreviewMediaFragment) details).getPosition();
1601 startMediaPreview(renamedFile, position, true);
1602 } else {
1603 getFileOperationsHelper().openFile(renamedFile);
1604 }
1605 } else if (details instanceof PreviewTextFragment &&
1606 renamedFile.equals(details.getFile())) {
1607 ((PreviewTextFragment) details).updateFile(renamedFile);
1608 if (PreviewTextFragment.canBePreviewed(renamedFile)) {
1609 startTextPreview(renamedFile);
1610 } else {
1611 getFileOperationsHelper().openFile(renamedFile);
1612 }
1613 }
1614 }
1615
1616 if (getStorageManager().getFileById(renamedFile.getParentId()).equals(getCurrentDir())){
1617 refreshListOfFilesFragment();
1618 }
1619
1620 } else {
1621 Toast msg = Toast.makeText(this,
1622 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1623 Toast.LENGTH_LONG);
1624 msg.show();
1625
1626 if (result.isSslRecoverableException()) {
1627 mLastSslUntrustedServerResult = result;
1628 showUntrustedCertDialog(mLastSslUntrustedServerResult);
1629 }
1630 }
1631 }
1632
1633 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation,
1634 RemoteOperationResult result) {
1635 if (result.isSuccess()) {
1636 if (operation.transferWasRequested()) {
1637 OCFile syncedFile = operation.getLocalFile();
1638 onTransferStateChanged(syncedFile, true, true);
1639 invalidateOptionsMenu();
1640 refreshShowDetails();
1641 }
1642 }
1643 }
1644
1645 /**
1646 * Updates the view associated to the activity after the finish of an operation trying create a
1647 * new folder
1648 *
1649 * @param operation Creation operation performed.
1650 * @param result Result of the creation.
1651 */
1652 private void onCreateFolderOperationFinish(CreateFolderOperation operation,
1653 RemoteOperationResult result) {
1654 if (result.isSuccess()) {
1655 refreshListOfFilesFragment();
1656 } else {
1657 try {
1658 Toast msg = Toast.makeText(FileDisplayActivity.this,
1659 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1660 Toast.LENGTH_LONG);
1661 msg.show();
1662
1663 } catch (NotFoundException e) {
1664 Log_OC.e(TAG, "Error while trying to show fail message ", e);
1665 }
1666 }
1667 }
1668
1669
1670 /**
1671 * {@inheritDoc}
1672 */
1673 @Override
1674 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
1675 refreshListOfFilesFragment();
1676 FileFragment details = getSecondFragment();
1677 if (details != null && details instanceof FileDetailFragment &&
1678 file.equals(details.getFile()) ) {
1679 if (downloading || uploading) {
1680 ((FileDetailFragment) details).updateFileDetails(file, getAccount());
1681 } else {
1682 if (!file.fileExists()) {
1683 cleanSecondFragment();
1684 } else {
1685 ((FileDetailFragment) details).updateFileDetails(false, true);
1686 }
1687 }
1688 }
1689
1690 }
1691
1692
1693 private void requestForDownload() {
1694 Account account = getAccount();
1695 //if (!mWaitingToPreview.isDownloading()) {
1696 if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
1697 Intent i = new Intent(this, FileDownloader.class);
1698 i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
1699 i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
1700 startService(i);
1701 }
1702 }
1703
1704
1705 private OCFile getCurrentDir() {
1706 OCFile file = getFile();
1707 if (file != null) {
1708 if (file.isFolder()) {
1709 return file;
1710 } else if (getStorageManager() != null) {
1711 String parentPath = file.getRemotePath().substring(0,
1712 file.getRemotePath().lastIndexOf(file.getFileName()));
1713 return getStorageManager().getFileByPath(parentPath);
1714 }
1715 }
1716 return null;
1717 }
1718
1719 /**
1720 * Starts an operation to refresh the requested folder.
1721 *
1722 * The operation is run in a new background thread created on the fly.
1723 *
1724 * The refresh updates is a "light sync": properties of regular files in folder are updated (including
1725 * associated shares), but not their contents. Only the contents of files marked to be kept-in-sync are
1726 * synchronized too.
1727 *
1728 * @param folder Folder to refresh.
1729 * @param ignoreETag If 'true', the data from the server will be fetched and sync'ed even if the eTag
1730 * didn't change.
1731 */
1732 public void startSyncFolderOperation(final OCFile folder, final boolean ignoreETag) {
1733
1734 // the execution is slightly delayed to allow the activity get the window focus if it's being started
1735 // or if the method is called from a dialog that is being dismissed
1736 getHandler().postDelayed(
1737 new Runnable() {
1738 @Override
1739 public void run() {
1740 if (hasWindowFocus()) {
1741 long currentSyncTime = System.currentTimeMillis();
1742 mSyncInProgress = true;
1743
1744 // perform folder synchronization
1745 RemoteOperation synchFolderOp = new RefreshFolderOperation(folder,
1746 currentSyncTime,
1747 false,
1748 getFileOperationsHelper().isSharedSupported(),
1749 ignoreETag,
1750 getStorageManager(),
1751 getAccount(),
1752 getApplicationContext()
1753 );
1754 synchFolderOp.execute(
1755 getAccount(),
1756 MainApp.getAppContext(),
1757 FileDisplayActivity.this,
1758 null,
1759 null
1760 );
1761
1762 mProgressBar.setIndeterminate(true);
1763
1764 setBackgroundText();
1765
1766 } // else: NOTHING ; lets' not refresh when the user rotates the device but there is
1767 // another window floating over
1768 }
1769 },
1770 DELAY_TO_REQUEST_OPERATIONS_LATER
1771 );
1772
1773 }
1774
1775 /**
1776 * Show untrusted cert dialog
1777 */
1778 public void showUntrustedCertDialog(RemoteOperationResult result) {
1779 // Show a dialog with the certificate info
1780 SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstanceForFullSslError(
1781 (CertificateCombinedException) result.getException());
1782 FragmentManager fm = getSupportFragmentManager();
1783 FragmentTransaction ft = fm.beginTransaction();
1784 dialog.show(ft, DIALOG_UNTRUSTED_CERT);
1785 }
1786
1787 private void requestForDownload(OCFile file) {
1788 Account account = getAccount();
1789 if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
1790 Intent i = new Intent(this, FileDownloader.class);
1791 i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
1792 i.putExtra(FileDownloader.EXTRA_FILE, file);
1793 startService(i);
1794 }
1795 }
1796
1797 private void sendDownloadedFile() {
1798 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend);
1799 mWaitingToSend = null;
1800 }
1801
1802
1803 /**
1804 * Requests the download of the received {@link OCFile} , updates the UI
1805 * to monitor the download progress and prepares the activity to send the file
1806 * when the download finishes.
1807 *
1808 * @param file {@link OCFile} to download and preview.
1809 */
1810 public void startDownloadForSending(OCFile file) {
1811 mWaitingToSend = file;
1812 requestForDownload(mWaitingToSend);
1813 boolean hasSecondFragment = (getSecondFragment() != null);
1814 updateFragmentsVisibility(hasSecondFragment);
1815 }
1816
1817 /**
1818 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1819 *
1820 * @param file Image {@link OCFile} to show.
1821 */
1822 public void startImagePreview(OCFile file) {
1823 Intent showDetailsIntent = new Intent(this, PreviewImageActivity.class);
1824 showDetailsIntent.putExtra(EXTRA_FILE, file);
1825 showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount());
1826 startActivity(showDetailsIntent);
1827 }
1828
1829 /**
1830 * Stars the preview of an already down media {@link OCFile}.
1831 *
1832 * @param file Media {@link OCFile} to preview.
1833 * @param startPlaybackPosition Media position where the playback will be started,
1834 * in milliseconds.
1835 * @param autoplay When 'true', the playback will start without user
1836 * interactions.
1837 */
1838 public void startMediaPreview(OCFile file, int startPlaybackPosition, boolean autoplay) {
1839 Fragment mediaFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition,
1840 autoplay);
1841 setSecondFragment(mediaFragment);
1842 updateFragmentsVisibility(true);
1843 updateActionBarTitleAndHomeButton(file);
1844 setFile(file);
1845 }
1846
1847 /**
1848 * Stars the preview of a text file {@link OCFile}.
1849 *
1850 * @param file Text {@link OCFile} to preview.
1851 */
1852 public void startTextPreview(OCFile file) {
1853 Bundle args = new Bundle();
1854 args.putParcelable(EXTRA_FILE, file);
1855 args.putParcelable(EXTRA_ACCOUNT, getAccount());
1856 Fragment textPreviewFragment = Fragment.instantiate(getApplicationContext(),
1857 PreviewTextFragment.class.getName(), args);
1858 setSecondFragment(textPreviewFragment);
1859 updateFragmentsVisibility(true);
1860 //updateNavigationElementsInActionBar(file);
1861 setFile(file);
1862 }
1863
1864 /**
1865 * Requests the download of the received {@link OCFile} , updates the UI
1866 * to monitor the download progress and prepares the activity to preview
1867 * or open the file when the download finishes.
1868 *
1869 * @param file {@link OCFile} to download and preview.
1870 */
1871 public void startDownloadForPreview(OCFile file) {
1872 Fragment detailFragment = FileDetailFragment.newInstance(file, getAccount());
1873 setSecondFragment(detailFragment);
1874 mWaitingToPreview = file;
1875 requestForDownload();
1876 updateFragmentsVisibility(true);
1877 updateActionBarTitleAndHomeButton(file);
1878 setFile(file);
1879 }
1880
1881
1882 public void cancelTransference(OCFile file) {
1883 getFileOperationsHelper().cancelTransference(file);
1884 if (mWaitingToPreview != null &&
1885 mWaitingToPreview.getRemotePath().equals(file.getRemotePath())) {
1886 mWaitingToPreview = null;
1887 }
1888 if (mWaitingToSend != null &&
1889 mWaitingToSend.getRemotePath().equals(file.getRemotePath())) {
1890 mWaitingToSend = null;
1891 }
1892 onTransferStateChanged(file, false, false);
1893 }
1894
1895 @Override
1896 public void onRefresh(boolean ignoreETag) {
1897 refreshList(ignoreETag);
1898 }
1899
1900 @Override
1901 public void onRefresh() {
1902 refreshList(true);
1903 }
1904
1905 private void refreshList(boolean ignoreETag) {
1906 OCFileListFragment listOfFiles = getListOfFilesFragment();
1907 if (listOfFiles != null) {
1908 OCFile folder = listOfFiles.getCurrentFile();
1909 if (folder != null) {
1910 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1911 listDirectory(mFile);*/
1912 startSyncFolderOperation(folder, ignoreETag);
1913 }
1914 }
1915 }
1916
1917 private void sortByDate(boolean ascending) {
1918 getListOfFilesFragment().sortByDate(ascending);
1919 }
1920
1921 private void sortBySize(boolean ascending) {
1922 getListOfFilesFragment().sortBySize(ascending);
1923 }
1924
1925 private void sortByName(boolean ascending) {
1926 getListOfFilesFragment().sortByName(ascending);
1927 }
1928 private boolean isGridView(){ return getListOfFilesFragment().isGridView(); }
1929 private void switchToGridView() {
1930 getListOfFilesFragment().switchToGridView();
1931 }
1932 private void switchToListView() {
1933 getListOfFilesFragment().switchToListView();
1934 }
1935
1936 public void allFilesOption() {
1937 browseToRoot();
1938 }
1939
1940 public void refreshDirectory(){
1941 getListOfFilesFragment().refreshDirectory();
1942 }
1943 }