2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
23 package com
.owncloud
.android
.ui
.activity
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountManager
;
27 import android
.accounts
.AuthenticatorException
;
28 import android
.annotation
.TargetApi
;
29 import android
.app
.AlertDialog
;
30 import android
.content
.BroadcastReceiver
;
31 import android
.content
.ComponentName
;
32 import android
.content
.ContentResolver
;
33 import android
.content
.Context
;
34 import android
.content
.DialogInterface
;
35 import android
.content
.Intent
;
36 import android
.content
.IntentFilter
;
37 import android
.content
.ServiceConnection
;
38 import android
.content
.SharedPreferences
;
39 import android
.content
.SyncRequest
;
40 import android
.content
.res
.Resources
.NotFoundException
;
41 import android
.database
.Cursor
;
42 import android
.net
.Uri
;
43 import android
.os
.Build
;
44 import android
.os
.Bundle
;
45 import android
.os
.IBinder
;
46 import android
.preference
.PreferenceManager
;
47 import android
.provider
.OpenableColumns
;
48 import android
.support
.v4
.app
.Fragment
;
49 import android
.support
.v4
.app
.FragmentManager
;
50 import android
.support
.v4
.app
.FragmentTransaction
;
51 import android
.support
.v4
.view
.GravityCompat
;
52 import android
.view
.Menu
;
53 import android
.view
.MenuInflater
;
54 import android
.view
.MenuItem
;
55 import android
.view
.View
;
56 import android
.view
.Window
;
57 import android
.widget
.Toast
;
59 import com
.owncloud
.android
.MainApp
;
60 import com
.owncloud
.android
.R
;
61 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
62 import com
.owncloud
.android
.datamodel
.OCFile
;
63 import com
.owncloud
.android
.files
.services
.FileDownloader
;
64 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
65 import com
.owncloud
.android
.files
.services
.FileUploader
;
66 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
67 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
68 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
69 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
70 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
71 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
72 import com
.owncloud
.android
.lib
.common
.network
.CertificateCombinedException
;
73 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
74 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
75 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
76 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
77 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
78 import com
.owncloud
.android
.operations
.CreateShareOperation
;
79 import com
.owncloud
.android
.operations
.MoveFileOperation
;
80 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
81 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
82 import com
.owncloud
.android
.operations
.RenameFileOperation
;
83 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
84 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
85 import com
.owncloud
.android
.services
.observer
.FileObserverService
;
86 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
87 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
88 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
89 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
;
90 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
.OnSslUntrustedCertListener
;
91 import com
.owncloud
.android
.ui
.dialog
.UploadSourceDialogFragment
;
92 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
93 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
94 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
95 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
96 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
97 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
98 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
99 import com
.owncloud
.android
.utils
.DisplayUtils
;
100 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
101 import com
.owncloud
.android
.utils
.FileStorageUtils
;
102 import com
.owncloud
.android
.utils
.UriUtils
;
107 import java
.io
.IOException
;
111 * Displays, what files the user has available in his ownCloud.
114 public class FileDisplayActivity
extends HookActivity
115 implements FileFragment
.ContainerActivity
,
116 OnSslUntrustedCertListener
, OnEnforceableRefreshListener
{
118 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
119 private UploadFinishReceiver mUploadFinishReceiver
;
120 private DownloadFinishReceiver mDownloadFinishReceiver
;
121 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
123 private boolean mDualPane
;
124 private View mLeftFragmentContainer
;
125 private View mRightFragmentContainer
;
127 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
128 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
129 private static final String KEY_WAITING_TO_SEND
= "WAITING_TO_SEND";
131 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
133 public static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
134 public static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
135 public static final int ACTION_MOVE_FILES
= 3;
136 public static final int ACTION_COPY_FILES
= 4;
138 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
140 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
141 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
143 private OCFile mWaitingToPreview
;
145 private boolean mSyncInProgress
= false
;
147 private static String DIALOG_UNTRUSTED_CERT
= "DIALOG_UNTRUSTED_CERT";
148 private static String DIALOG_CREATE_FOLDER
= "DIALOG_CREATE_FOLDER";
149 private static String DIALOG_UPLOAD_SOURCE
= "DIALOG_UPLOAD_SOURCE";
150 private static String DIALOG_CERT_NOT_SAVED
= "DIALOG_CERT_NOT_SAVED";
152 private OCFile mWaitingToSend
;
156 protected void onCreate(Bundle savedInstanceState
) {
157 Log_OC
.v(TAG
, "onCreate() start");
158 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
160 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account
163 /// grant that FileObserverService is watching favorite files
164 if (savedInstanceState
== null
) {
165 Intent initObserversIntent
= FileObserverService
.makeInitIntent(this);
166 startService(initObserversIntent
);
169 /// Load of saved instance state
170 if(savedInstanceState
!= null
) {
171 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(
172 FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
173 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
174 mWaitingToSend
= (OCFile
) savedInstanceState
.getParcelable(
175 FileDisplayActivity
.KEY_WAITING_TO_SEND
);
178 mWaitingToPreview
= null
;
179 mSyncInProgress
= false
;
180 mWaitingToSend
= null
;
185 // Inflate and set the layout view
186 setContentView(R
.layout
.files
);
191 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
192 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
193 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
194 if (savedInstanceState
== null
) {
195 createMinFragments();
199 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS,
200 // according to the official
203 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
204 /*|| mRefreshSharesInProgress*/);
205 // always AFTER setContentView(...) ; to work around bug in its implementation
209 Log_OC
.v(TAG
, "onCreate() end");
213 protected void onStart() {
214 Log_OC
.v(TAG
, "onStart() start");
216 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
217 Log_OC
.v(TAG
, "onStart() end");
221 protected void onDestroy() {
222 Log_OC
.v(TAG
, "onDestroy() start");
224 Log_OC
.v(TAG
, "onDestroy() end");
228 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
231 protected void onAccountSet(boolean stateWasRecovered
) {
232 super.onAccountSet(stateWasRecovered
);
233 if (getAccount() != null
) {
234 /// Check whether the 'main' OCFile handled by the Activity is contained in the
236 OCFile file
= getFile();
237 // get parent from path
238 String parentPath
= "";
240 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
241 // upload in progress - right now, files are not inserted in the local
242 // cache until the upload is successful get parent from path
243 parentPath
= file
.getRemotePath().substring(0,
244 file
.getRemotePath().lastIndexOf(file
.getFileName()));
245 if (getStorageManager().getFileByPath(parentPath
) == null
)
246 file
= null
; // not able to know the directory where the file is uploading
248 file
= getStorageManager().getFileByPath(file
.getRemotePath());
249 // currentDir = null if not in the current Account
253 // fall back to root folder
254 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
258 if (!stateWasRecovered
) {
259 Log_OC
.d(TAG
, "Initializing Fragments in onAccountChanged..");
260 initFragmentsWithFile();
261 if (file
.isFolder()) {
262 startSyncFolderOperation(file
, false
);
266 updateFragmentsVisibility(!file
.isFolder());
267 updateActionBarTitleAndHomeButton(file
.isFolder() ? null
: file
);
273 private void createMinFragments() {
274 OCFileListFragment listOfFiles
= new OCFileListFragment();
275 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
276 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
277 transaction
.commit();
280 private void initFragmentsWithFile() {
281 if (getAccount() != null
&& getFile() != null
) {
283 OCFileListFragment listOfFiles
= getListOfFilesFragment();
284 if (listOfFiles
!= null
) {
285 listOfFiles
.listDirectory(getCurrentDir());
286 // TODO Enable when "On Device" is recovered
287 // listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
289 Log_OC
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
293 OCFile file
= getFile();
294 Fragment secondFragment
= chooseInitialSecondFragment(file
);
295 if (secondFragment
!= null
) {
296 setSecondFragment(secondFragment
);
297 updateFragmentsVisibility(true
);
298 updateActionBarTitleAndHomeButton(file
);
301 cleanSecondFragment();
305 Log_OC
.wtf(TAG
, "initFragments() called with invalid NULLs!");
306 if (getAccount() == null
) {
307 Log_OC
.wtf(TAG
, "\t account is NULL");
309 if (getFile() == null
) {
310 Log_OC
.wtf(TAG
, "\t file is NULL");
315 private Fragment
chooseInitialSecondFragment(OCFile file
) {
316 Fragment secondFragment
= null
;
317 if (file
!= null
&& !file
.isFolder()) {
318 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
319 && file
.getLastSyncDateForProperties() > 0 // temporal fix
321 int startPlaybackPosition
=
322 getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
324 getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
325 secondFragment
= new PreviewMediaFragment(file
, getAccount(),
326 startPlaybackPosition
, autoplay
);
329 secondFragment
= FileDetailFragment
.newInstance(file
, getAccount());
332 return secondFragment
;
337 * Replaces the second fragment managed by the activity with the received as
340 * Assumes never will be more than two fragments managed at the same time.
342 * @param fragment New second Fragment to set.
344 private void setSecondFragment(Fragment fragment
) {
345 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
346 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
347 transaction
.commit();
351 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
353 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
354 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
356 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
357 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
360 } else if (existsSecondFragment
) {
361 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
362 mLeftFragmentContainer
.setVisibility(View
.GONE
);
364 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
365 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
369 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
370 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
372 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
373 mRightFragmentContainer
.setVisibility(View
.GONE
);
379 private OCFileListFragment
getListOfFilesFragment() {
380 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(
381 FileDisplayActivity
.TAG_LIST_OF_FILES
);
382 if (listOfFiles
!= null
) {
383 return (OCFileListFragment
) listOfFiles
;
385 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
389 public FileFragment
getSecondFragment() {
390 Fragment second
= getSupportFragmentManager().findFragmentByTag(
391 FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
392 if (second
!= null
) {
393 return (FileFragment
) second
;
398 protected void cleanSecondFragment() {
399 Fragment second
= getSecondFragment();
400 if (second
!= null
) {
401 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
405 updateFragmentsVisibility(false
);
406 updateActionBarTitleAndHomeButton(null
);
409 protected void refreshListOfFilesFragment() {
410 OCFileListFragment fileListFragment
= getListOfFilesFragment();
411 if (fileListFragment
!= null
) {
412 fileListFragment
.listDirectory();
413 // TODO Enable when "On Device" is recovered ?
414 // fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
418 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
,
420 FileFragment secondFragment
= getSecondFragment();
421 boolean waitedPreview
= (mWaitingToPreview
!= null
&&
422 mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
423 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
424 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
425 OCFile fileInFragment
= detailsFragment
.getFile();
426 if (fileInFragment
!= null
&&
427 !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
428 // the user browsed to other file ; forget the automatic preview
429 mWaitingToPreview
= null
;
431 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
432 // grant that the right panel updates the progress bar
433 detailsFragment
.listenForTransferProgress();
434 detailsFragment
.updateFileDetails(true
, false
);
436 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
437 // update the right panel
438 boolean detailsFragmentChanged
= false
;
441 mWaitingToPreview
= getStorageManager().getFileById(
442 mWaitingToPreview
.getFileId()); // update the file from database,
443 // for the local storage path
444 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
445 startMediaPreview(mWaitingToPreview
, 0, true
);
446 detailsFragmentChanged
= true
;
448 getFileOperationsHelper().openFile(mWaitingToPreview
);
451 mWaitingToPreview
= null
;
453 if (!detailsFragmentChanged
) {
454 detailsFragment
.updateFileDetails(false
, (success
));
461 public boolean onPrepareOptionsMenu(Menu menu
) {
462 boolean drawerOpen
= mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
463 menu
.findItem(R
.id
.action_upload
).setVisible(!drawerOpen
);
464 menu
.findItem(R
.id
.action_create_dir
).setVisible(!drawerOpen
);
465 menu
.findItem(R
.id
.action_sort
).setVisible(!drawerOpen
);
466 menu
.findItem(R
.id
.action_sync_account
).setVisible(!drawerOpen
);
468 return super.onPrepareOptionsMenu(menu
);
472 public boolean onCreateOptionsMenu(Menu menu
) {
473 MenuInflater inflater
= getMenuInflater();
474 inflater
.inflate(R
.menu
.main_menu
, menu
);
480 public boolean onOptionsItemSelected(MenuItem item
) {
481 boolean retval
= true
;
482 switch (item
.getItemId()) {
483 case R
.id
.action_create_dir
: {
484 CreateFolderDialogFragment dialog
=
485 CreateFolderDialogFragment
.newInstance(getCurrentDir());
486 dialog
.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER
);
489 case R
.id
.action_sync_account
: {
490 startSynchronization();
493 case R
.id
.action_upload
: {
494 UploadSourceDialogFragment dialog
=
495 UploadSourceDialogFragment
.newInstance(getAccount());
496 dialog
.show(getSupportFragmentManager(), DIALOG_UPLOAD_SOURCE
);
500 case android
.R
.id
.home
: {
501 FileFragment second
= getSecondFragment();
502 OCFile currentDir
= getCurrentDir();
503 if (mDrawerLayout
.isDrawerOpen(GravityCompat
.START
)) {
504 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
505 } else if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
506 (second
!= null
&& second
.getFile() != null
)) {
510 mDrawerLayout
.openDrawer(GravityCompat
.START
);
514 case R
.id
.action_sort
: {
515 SharedPreferences appPreferences
= PreferenceManager
516 .getDefaultSharedPreferences(this);
518 // Read sorting order, default to sort by name ascending
519 Integer sortOrder
= appPreferences
520 .getInt("sortOrder", FileStorageUtils
.SORT_NAME
);
522 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
523 builder
.setTitle(R
.string
.actionbar_sort_title
)
524 .setSingleChoiceItems(R
.array
.actionbar_sortby
, sortOrder
,
525 new DialogInterface
.OnClickListener() {
526 public void onClick(DialogInterface dialog
, int which
) {
539 builder
.create().show();
543 retval
= super.onOptionsItemSelected(item
);
548 private void startSynchronization() {
549 Log_OC
.d(TAG
, "Got to start sync");
550 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
551 Log_OC
.d(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
552 ContentResolver
.cancelSync(null
, MainApp
.getAuthority());
553 // cancel the current synchronizations of any ownCloud account
554 Bundle bundle
= new Bundle();
555 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
556 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
557 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
558 MainApp
.getAuthority());
559 ContentResolver
.requestSync(
561 MainApp
.getAuthority(), bundle
);
563 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
564 MainApp
.getAuthority() + " with new API");
565 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
566 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
567 builder
.setExpedited(true
);
568 builder
.setManual(true
);
571 // Fix bug in Android Lollipop when you click on refresh the whole account
572 Bundle extras
= new Bundle();
573 builder
.setExtras(extras
);
575 SyncRequest request
= builder
.build();
576 ContentResolver
.requestSync(request
);
581 * Called, when the user selected something for uploading
584 @TargetApi(Build
.VERSION_CODES
.JELLY_BEAN
)
586 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
588 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
||
589 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
590 //getClipData is only supported on api level 16+, Jelly Bean
591 if (data
.getData() == null
&& Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
){
592 for( int i
= 0; i
< data
.getClipData().getItemCount(); i
++){
593 Intent intent
= new Intent();
594 intent
.setData(data
.getClipData().getItemAt(i
).getUri());
595 requestSimpleUpload(intent
, resultCode
);
598 requestSimpleUpload(data
, resultCode
);
600 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
||
601 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
602 requestMultipleUpload(data
, resultCode
);
605 } else if (requestCode
== ACTION_MOVE_FILES
&& resultCode
== RESULT_OK
){
607 } else if (requestCode == ACTION_MOVE_FILES && (resultCode == RESULT_OK ||
608 resultCode == MoveActivity.RESULT_OK_AND_MOVE)) {
609 >>>>>>> Added 'ACTION_COPY_FILES'.
612 final Intent fData
= data
;
613 final int fResultCode
= resultCode
;
614 getHandler().postDelayed(
618 requestMoveOperation(fData
, fResultCode
);
621 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
625 super.onActivityResult(requestCode
, resultCode
, data
);
630 private void requestMultipleUpload(Intent data
, int resultCode
) {
631 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
632 if (filePaths
!= null
) {
633 String
[] remotePaths
= new String
[filePaths
.length
];
634 String remotePathBase
= getCurrentDir().getRemotePath();
635 for (int j
= 0; j
< remotePaths
.length
; j
++) {
636 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
639 Intent i
= new Intent(this, FileUploader
.class);
640 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
641 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
642 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
643 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
644 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
645 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
649 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
650 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
),
658 private void requestSimpleUpload(Intent data
, int resultCode
) {
659 String filePath
= null
;
660 String mimeType
= null
;
662 Uri selectedImageUri
= data
.getData();
665 mimeType
= getContentResolver().getType(selectedImageUri
);
667 String fileManagerString
= selectedImageUri
.getPath();
668 String selectedImagePath
= UriUtils
.getLocalPath(selectedImageUri
, this);
670 if (selectedImagePath
!= null
)
671 filePath
= selectedImagePath
;
673 filePath
= fileManagerString
;
675 } catch (Exception e
) {
676 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of " +
677 "Intent.ACTION_GET_CONTENT", e
);
680 if (filePath
== null
) {
681 Log_OC
.e(TAG
, "Couldn't resolve path to file");
682 Toast t
= Toast
.makeText(
683 this, getString(R
.string
.filedisplay_unexpected_bad_get_content
),
691 Intent i
= new Intent(this, FileUploader
.class);
692 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
693 OCFile currentDir
= getCurrentDir();
694 String remotePath
= (currentDir
!= null
) ? currentDir
.getRemotePath() : OCFile
.ROOT_PATH
;
696 if (filePath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
)) {
697 Cursor cursor
= getContentResolver().query(Uri
.parse(filePath
), null
, null
, null
, null
);
699 if (cursor
!= null
&& cursor
.moveToFirst()) {
700 String displayName
= cursor
.getString(cursor
.getColumnIndex(
701 OpenableColumns
.DISPLAY_NAME
));
702 Log_OC
.v(TAG
, "Display Name: " + displayName
);
704 displayName
.replace(File
.separatorChar
, '_');
705 displayName
.replace(File
.pathSeparatorChar
, '_');
706 remotePath
+= displayName
+ DisplayUtils
.getComposedFileExtension(filePath
);
709 // and what happens in case of error?; wrong target name for the upload
715 remotePath
+= new File(filePath
).getName();
718 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePath
);
719 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePath
);
720 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mimeType
);
721 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
722 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
723 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
728 * Request the operation for moving the file/folder from one path to another
730 * @param data Intent received
731 * @param resultCode Result code received
733 private void requestMoveOperation(Intent data
, int resultCode
) {
734 OCFile folderToMoveAt
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FOLDER
);
735 OCFile targetFile
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
736 getFileOperationsHelper().moveFile(folderToMoveAt
, targetFile
);
740 public void onBackPressed() {
741 OCFileListFragment listOfFiles
= getListOfFilesFragment();
742 if (mDualPane
|| getSecondFragment() == null
) {
743 OCFile currentDir
= getCurrentDir();
744 if (currentDir
== null
|| currentDir
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
) {
748 if (listOfFiles
!= null
) { // should never be null, indeed
749 listOfFiles
.onBrowseUp();
752 if (listOfFiles
!= null
) { // should never be null, indeed
753 setFile(listOfFiles
.getCurrentFile());
755 cleanSecondFragment();
760 protected void onSaveInstanceState(Bundle outState
) {
761 // responsibility of restore is preferred in onCreate() before than in
762 // onRestoreInstanceState when there are Fragments involved
763 Log_OC
.v(TAG
, "onSaveInstanceState() start");
764 super.onSaveInstanceState(outState
);
765 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
766 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
767 //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
768 // mRefreshSharesInProgress);
769 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_SEND
, mWaitingToSend
);
771 Log_OC
.v(TAG
, "onSaveInstanceState() end");
776 protected void onResume() {
777 Log_OC
.v(TAG
, "onResume() start");
780 // refresh Navigation Drawer account list
781 mNavigationDrawerAdapter
.updateAccountList();
784 // refresh list of files
785 refreshListOfFilesFragment();
787 // Listen for sync messages
788 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
789 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
790 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
791 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
792 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
793 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
794 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
795 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
796 // syncIntentFilter);
798 // Listen for upload messages
799 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
800 mUploadFinishReceiver
= new UploadFinishReceiver();
801 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
803 // Listen for download messages
804 IntentFilter downloadIntentFilter
= new IntentFilter(
805 FileDownloader
.getDownloadAddedMessage());
806 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
807 mDownloadFinishReceiver
= new DownloadFinishReceiver();
808 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
810 Log_OC
.v(TAG
, "onResume() end");
815 protected void onPause() {
816 Log_OC
.v(TAG
, "onPause() start");
817 if (mSyncBroadcastReceiver
!= null
) {
818 unregisterReceiver(mSyncBroadcastReceiver
);
819 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
820 mSyncBroadcastReceiver
= null
;
822 if (mUploadFinishReceiver
!= null
) {
823 unregisterReceiver(mUploadFinishReceiver
);
824 mUploadFinishReceiver
= null
;
826 if (mDownloadFinishReceiver
!= null
) {
827 unregisterReceiver(mDownloadFinishReceiver
);
828 mDownloadFinishReceiver
= null
;
832 Log_OC
.v(TAG
, "onPause() end");
836 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
839 * {@link BroadcastReceiver} to enable syncing feedback in UI
842 public void onReceive(Context context
, Intent intent
) {
844 String event
= intent
.getAction();
845 Log_OC
.d(TAG
, "Received broadcast " + event
);
846 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
847 String synchFolderRemotePath
=
848 intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
849 RemoteOperationResult synchResult
=
850 (RemoteOperationResult
)intent
.getSerializableExtra(
851 FileSyncAdapter
.EXTRA_RESULT
);
852 boolean sameAccount
= (getAccount() != null
&&
853 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
857 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
858 mSyncInProgress
= true
;
861 OCFile currentFile
= (getFile() == null
) ? null
:
862 getStorageManager().getFileByPath(getFile().getRemotePath());
863 OCFile currentDir
= (getCurrentDir() == null
) ? null
:
864 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
866 if (currentDir
== null
) {
867 // current folder was removed from the server
868 Toast
.makeText( FileDisplayActivity
.this,
871 sync_current_folder_was_removed
),
872 synchFolderRemotePath
),
878 if (currentFile
== null
&& !getFile().isFolder()) {
879 // currently selected file was removed in the server, and now we
881 cleanSecondFragment();
882 currentFile
= currentDir
;
885 if (synchFolderRemotePath
!= null
&&
886 currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
887 OCFileListFragment fileListFragment
= getListOfFilesFragment();
888 if (fileListFragment
!= null
) {
889 fileListFragment
.listDirectory();
890 // TODO Enable when "On Device" is recovered ?
891 // fileListFragment.listDirectory(currentDir,
892 // MainApp.getOnlyOnDevice());
895 setFile(currentFile
);
898 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
899 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
902 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
904 /// TODO refactor and make common
905 synchResult
!= null
&& !synchResult
.isSuccess() &&
906 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
907 synchResult
.isIdPRedirection() ||
908 (synchResult
.isException() && synchResult
.getException()
909 instanceof AuthenticatorException
))) {
913 OwnCloudClient client
;
914 OwnCloudAccount ocAccount
=
915 new OwnCloudAccount(getAccount(), context
);
916 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
917 removeClientFor(ocAccount
));
919 if (client
!= null
) {
920 OwnCloudCredentials cred
= client
.getCredentials();
922 AccountManager am
= AccountManager
.get(context
);
923 if (cred
.authTokenExpires()) {
924 am
.invalidateAuthToken(
929 am
.clearPassword(getAccount());
933 requestCredentialsUpdate();
935 } catch (AccountNotFoundException e
) {
936 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
941 removeStickyBroadcast(intent
);
942 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
943 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
944 /*|| mRefreshSharesInProgress*/);
950 if (synchResult
!= null
) {
951 if (synchResult
.getCode().equals(
952 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
953 mLastSslUntrustedServerResult
= synchResult
;
956 } catch (RuntimeException e
) {
957 // avoid app crashes after changing the serial id of RemoteOperationResult
958 // in owncloud library with broadcast notifications pending to process
959 removeStickyBroadcast(intent
);
965 * Show a text message on screen view for notifying user if content is
966 * loading or folder is empty
968 private void setBackgroundText() {
969 OCFileListFragment ocFileListFragment
= getListOfFilesFragment();
970 if (ocFileListFragment
!= null
) {
971 int message
= R
.string
.file_list_loading
;
972 if (!mSyncInProgress
) {
973 // In case file list is empty
974 message
= R
.string
.file_list_empty
;
976 ocFileListFragment
.setMessageForEmptyList(getString(message
));
978 Log_OC
.e(TAG
, "OCFileListFragment is null");
983 * Once the file upload has finished -> update view
985 private class UploadFinishReceiver
extends BroadcastReceiver
{
987 * Once the file upload has finished -> update view
989 * @author David A. Velasco
990 * {@link BroadcastReceiver} to enable upload feedback in UI
993 public void onReceive(Context context
, Intent intent
) {
995 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
996 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
997 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
998 OCFile currentDir
= getCurrentDir();
999 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) &&
1000 (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1002 if (sameAccount
&& isDescendant
) {
1003 refreshListOfFilesFragment();
1006 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
,
1008 boolean renamedInUpload
= getFile().getRemotePath().
1009 equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
1010 boolean sameFile
= getFile().getRemotePath().equals(uploadedRemotePath
) ||
1012 FileFragment details
= getSecondFragment();
1013 boolean detailFragmentIsShown
= (details
!= null
&&
1014 details
instanceof FileDetailFragment
);
1016 if (sameAccount
&& sameFile
&& detailFragmentIsShown
) {
1017 if (uploadWasFine
) {
1018 setFile(getStorageManager().getFileByPath(uploadedRemotePath
));
1020 if (renamedInUpload
) {
1021 String newName
= (new File(uploadedRemotePath
)).getName();
1022 Toast msg
= Toast
.makeText(
1025 getString(R
.string
.filedetails_renamed_in_upload_msg
),
1030 if (uploadWasFine
|| getFile().fileExists()) {
1031 ((FileDetailFragment
) details
).updateFileDetails(false
, true
);
1033 cleanSecondFragment();
1036 // Force the preview if the file is an image
1037 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
1038 startImagePreview(getFile());
1039 } // TODO what about other kind of previews?
1043 if (intent
!= null
) {
1044 removeStickyBroadcast(intent
);
1054 * Class waiting for broadcast events from the {@link FileDownloader} service.
1056 * Updates the UI when a download is started or finished, provided that it is relevant for the
1059 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1061 //int refreshCounter = 0;
1063 public void onReceive(Context context
, Intent intent
) {
1065 boolean sameAccount
= isSameAccount(context
, intent
);
1066 String downloadedRemotePath
=
1067 intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1068 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1070 if (sameAccount
&& isDescendant
) {
1071 String linkedToRemotePath
=
1072 intent
.getStringExtra(FileDownloader
.EXTRA_LINKED_TO_PATH
);
1073 if (linkedToRemotePath
== null
|| isAscendant(linkedToRemotePath
)) {
1074 //Log_OC.v(TAG, "refresh #" + ++refreshCounter);
1075 refreshListOfFilesFragment();
1077 refreshSecondFragment(
1079 downloadedRemotePath
,
1080 intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)
1084 if (mWaitingToSend
!= null
) {
1086 getStorageManager().getFileByPath(mWaitingToSend
.getRemotePath());
1087 if (mWaitingToSend
.isDown()) {
1088 sendDownloadedFile();
1093 if (intent
!= null
) {
1094 removeStickyBroadcast(intent
);
1099 private boolean isDescendant(String downloadedRemotePath
) {
1100 OCFile currentDir
= getCurrentDir();
1102 currentDir
!= null
&&
1103 downloadedRemotePath
!= null
&&
1104 downloadedRemotePath
.startsWith(currentDir
.getRemotePath())
1108 private boolean isAscendant(String linkedToRemotePath
) {
1109 OCFile currentDir
= getCurrentDir();
1111 currentDir
!= null
&&
1112 currentDir
.getRemotePath().startsWith(linkedToRemotePath
)
1116 private boolean isSameAccount(Context context
, Intent intent
) {
1117 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1118 return (accountName
!= null
&& getAccount() != null
&&
1119 accountName
.equals(getAccount().name
));
1124 public void browseToRoot() {
1125 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1126 if (listOfFiles
!= null
) { // should never be null, indeed
1127 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
1128 listOfFiles
.listDirectory(root
);
1129 // TODO Enable when "On Device" is recovered ?
1130 // listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
1131 setFile(listOfFiles
.getCurrentFile());
1132 startSyncFolderOperation(root
, false
);
1134 cleanSecondFragment();
1142 * Updates action bar and second fragment, if in dual pane mode.
1145 public void onBrowsedDownTo(OCFile directory
) {
1147 cleanSecondFragment();
1149 startSyncFolderOperation(directory
, false
);
1153 * Shows the information of the {@link OCFile} received as a
1154 * parameter in the second fragment.
1156 * @param file {@link OCFile} whose details will be shown
1159 public void showDetails(OCFile file
) {
1160 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1161 setSecondFragment(detailFragment
);
1162 updateFragmentsVisibility(true
);
1163 updateActionBarTitleAndHomeButton(file
);
1168 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
1170 // in dual pane mode, keep the focus of title an action bar in the current folder
1171 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1174 super.updateActionBarTitleAndHomeButton(chosenFile
);
1181 protected ServiceConnection
newTransferenceServiceConnection() {
1182 return new ListServiceConnection();
1186 * Defines callbacks for service binding, passed to bindService()
1188 private class ListServiceConnection
implements ServiceConnection
{
1191 public void onServiceConnected(ComponentName component
, IBinder service
) {
1192 if (component
.equals(new ComponentName(
1193 FileDisplayActivity
.this, FileDownloader
.class))) {
1194 Log_OC
.d(TAG
, "Download service connected");
1195 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1196 if (mWaitingToPreview
!= null
)
1197 if (getStorageManager() != null
) {
1200 getStorageManager().getFileById(mWaitingToPreview
.getFileId());
1201 if (!mWaitingToPreview
.isDown()) {
1202 requestForDownload();
1206 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1207 FileUploader
.class))) {
1208 Log_OC
.d(TAG
, "Upload service connected");
1209 mUploaderBinder
= (FileUploaderBinder
) service
;
1213 // a new chance to get the mDownloadBinder through
1214 // getFileDownloadBinder() - THIS IS A MESS
1215 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1216 if (listOfFiles
!= null
) {
1217 listOfFiles
.listDirectory();
1218 // TODO Enable when "On Device" is recovered ?
1219 // listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1221 FileFragment secondFragment
= getSecondFragment();
1222 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1223 FileDetailFragment detailFragment
= (FileDetailFragment
) secondFragment
;
1224 detailFragment
.listenForTransferProgress();
1225 detailFragment
.updateFileDetails(false
, false
);
1230 public void onServiceDisconnected(ComponentName component
) {
1231 if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1232 FileDownloader
.class))) {
1233 Log_OC
.d(TAG
, "Download service disconnected");
1234 mDownloaderBinder
= null
;
1235 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1236 FileUploader
.class))) {
1237 Log_OC
.d(TAG
, "Upload service disconnected");
1238 mUploaderBinder
= null
;
1244 public void onSavedCertificate() {
1245 startSyncFolderOperation(getCurrentDir(), false
);
1250 public void onFailedSavingCertificate() {
1251 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
1252 R
.string
.ssl_validator_not_saved
, new String
[]{}, R
.string
.common_ok
, -1, -1
1254 dialog
.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED
);
1258 public void onCancelCertificate() {
1263 * Updates the view associated to the activity after the finish of some operation over files
1264 * in the current account.
1266 * @param operation Removal operation performed.
1267 * @param result Result of the removal.
1270 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1271 super.onRemoteOperationFinish(operation
, result
);
1273 if (operation
instanceof RemoveFileOperation
) {
1274 onRemoveFileOperationFinish((RemoveFileOperation
) operation
, result
);
1276 } else if (operation
instanceof RenameFileOperation
) {
1277 onRenameFileOperationFinish((RenameFileOperation
) operation
, result
);
1279 } else if (operation
instanceof SynchronizeFileOperation
) {
1280 onSynchronizeFileOperationFinish((SynchronizeFileOperation
) operation
, result
);
1282 } else if (operation
instanceof CreateFolderOperation
) {
1283 onCreateFolderOperationFinish((CreateFolderOperation
) operation
, result
);
1285 } else if (operation
instanceof CreateShareOperation
) {
1286 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1288 } else if (operation
instanceof UnshareLinkOperation
) {
1289 onUnshareLinkOperationFinish((UnshareLinkOperation
) operation
, result
);
1291 } else if (operation
instanceof MoveFileOperation
) {
1292 onMoveFileOperationFinish((MoveFileOperation
) operation
, result
);
1298 private void onCreateShareOperationFinish(CreateShareOperation operation
,
1299 RemoteOperationResult result
) {
1300 if (result
.isSuccess()) {
1301 refreshShowDetails();
1302 refreshListOfFilesFragment();
1307 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
1308 RemoteOperationResult result
) {
1309 if (result
.isSuccess()) {
1310 refreshShowDetails();
1311 refreshListOfFilesFragment();
1313 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
1314 cleanSecondFragment();
1315 refreshListOfFilesFragment();
1319 private void refreshShowDetails() {
1320 FileFragment details
= getSecondFragment();
1321 if (details
!= null
) {
1322 OCFile file
= details
.getFile();
1324 file
= getStorageManager().getFileByPath(file
.getRemotePath());
1325 if (details
instanceof PreviewMediaFragment
) {
1326 // Refresh OCFile of the fragment
1327 ((PreviewMediaFragment
) details
).updateFile(file
);
1332 invalidateOptionsMenu();
1337 * Updates the view associated to the activity after the finish of an operation trying to
1340 * @param operation Removal operation performed.
1341 * @param result Result of the removal.
1343 private void onRemoveFileOperationFinish(RemoveFileOperation operation
,
1344 RemoteOperationResult result
) {
1345 dismissLoadingDialog();
1347 Toast msg
= Toast
.makeText(this,
1348 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1352 if (result
.isSuccess()) {
1353 OCFile removedFile
= operation
.getFile();
1354 FileFragment second
= getSecondFragment();
1355 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1356 if (second
instanceof PreviewMediaFragment
) {
1357 ((PreviewMediaFragment
) second
).stopPreview(true
);
1359 setFile(getStorageManager().getFileById(removedFile
.getParentId()));
1360 cleanSecondFragment();
1362 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1363 refreshListOfFilesFragment();
1365 invalidateOptionsMenu();
1367 if (result
.isSslRecoverableException()) {
1368 mLastSslUntrustedServerResult
= result
;
1369 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1376 * Updates the view associated to the activity after the finish of an operation trying to move a
1379 * @param operation Move operation performed.
1380 * @param result Result of the move operation.
1382 private void onMoveFileOperationFinish(MoveFileOperation operation
,
1383 RemoteOperationResult result
) {
1384 if (result
.isSuccess()) {
1385 dismissLoadingDialog();
1386 refreshListOfFilesFragment();
1388 dismissLoadingDialog();
1390 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1391 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1395 } catch (NotFoundException e
) {
1396 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1403 * Updates the view associated to the activity after the finish of an operation trying to rename
1406 * @param operation Renaming operation performed.
1407 * @param result Result of the renaming.
1409 private void onRenameFileOperationFinish(RenameFileOperation operation
,
1410 RemoteOperationResult result
) {
1411 dismissLoadingDialog();
1412 OCFile renamedFile
= operation
.getFile();
1413 if (result
.isSuccess()) {
1414 FileFragment details
= getSecondFragment();
1415 if (details
!= null
) {
1416 if (details
instanceof FileDetailFragment
&&
1417 renamedFile
.equals(details
.getFile()) ) {
1418 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1419 showDetails(renamedFile
);
1421 } else if (details
instanceof PreviewMediaFragment
&&
1422 renamedFile
.equals(details
.getFile())) {
1423 ((PreviewMediaFragment
) details
).updateFile(renamedFile
);
1424 if (PreviewMediaFragment
.canBePreviewed(renamedFile
)) {
1425 int position
= ((PreviewMediaFragment
) details
).getPosition();
1426 startMediaPreview(renamedFile
, position
, true
);
1428 getFileOperationsHelper().openFile(renamedFile
);
1433 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())){
1434 refreshListOfFilesFragment();
1438 Toast msg
= Toast
.makeText(this,
1439 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1443 if (result
.isSslRecoverableException()) {
1444 mLastSslUntrustedServerResult
= result
;
1445 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1450 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
1451 RemoteOperationResult result
) {
1452 if (result
.isSuccess()) {
1453 if (operation
.transferWasRequested()) {
1454 OCFile syncedFile
= operation
.getLocalFile();
1455 onTransferStateChanged(syncedFile
, true
, true
);
1456 invalidateOptionsMenu();
1462 * Updates the view associated to the activity after the finish of an operation trying create a
1465 * @param operation Creation operation performed.
1466 * @param result Result of the creation.
1468 private void onCreateFolderOperationFinish(CreateFolderOperation operation
,
1469 RemoteOperationResult result
) {
1470 if (result
.isSuccess()) {
1471 dismissLoadingDialog();
1472 refreshListOfFilesFragment();
1474 dismissLoadingDialog();
1476 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1477 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1481 } catch (NotFoundException e
) {
1482 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1492 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1493 refreshListOfFilesFragment();
1494 FileFragment details
= getSecondFragment();
1495 if (details
!= null
&& details
instanceof FileDetailFragment
&&
1496 file
.equals(details
.getFile()) ) {
1497 if (downloading
|| uploading
) {
1498 ((FileDetailFragment
) details
).updateFileDetails(file
, getAccount());
1500 if (!file
.fileExists()) {
1501 cleanSecondFragment();
1503 ((FileDetailFragment
) details
).updateFileDetails(false
, true
);
1511 private void requestForDownload() {
1512 Account account
= getAccount();
1513 //if (!mWaitingToPreview.isDownloading()) {
1514 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1515 Intent i
= new Intent(this, FileDownloader
.class);
1516 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1517 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1523 private OCFile
getCurrentDir() {
1524 OCFile file
= getFile();
1526 if (file
.isFolder()) {
1528 } else if (getStorageManager() != null
) {
1529 String parentPath
= file
.getRemotePath().substring(0,
1530 file
.getRemotePath().lastIndexOf(file
.getFileName()));
1531 return getStorageManager().getFileByPath(parentPath
);
1537 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
1538 long currentSyncTime
= System
.currentTimeMillis();
1540 mSyncInProgress
= true
;
1542 // perform folder synchronization
1543 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
1546 getFileOperationsHelper().isSharedSupported(),
1548 getStorageManager(),
1550 getApplicationContext()
1552 synchFolderOp
.execute(getAccount(), MainApp
.getAppContext(), this, null
, null
);
1554 setSupportProgressBarIndeterminateVisibility(true
);
1556 setBackgroundText();
1560 * Show untrusted cert dialog
1562 public void showUntrustedCertDialog(RemoteOperationResult result
) {
1563 // Show a dialog with the certificate info
1564 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.newInstanceForFullSslError(
1565 (CertificateCombinedException
) result
.getException());
1566 FragmentManager fm
= getSupportFragmentManager();
1567 FragmentTransaction ft
= fm
.beginTransaction();
1568 dialog
.show(ft
, DIALOG_UNTRUSTED_CERT
);
1571 private void requestForDownload(OCFile file
) {
1572 Account account
= getAccount();
1573 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1574 Intent i
= new Intent(this, FileDownloader
.class);
1575 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1576 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
1581 private void sendDownloadedFile() {
1582 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend
);
1583 mWaitingToSend
= null
;
1588 * Requests the download of the received {@link OCFile} , updates the UI
1589 * to monitor the download progress and prepares the activity to send the file
1590 * when the download finishes.
1592 * @param file {@link OCFile} to download and preview.
1594 public void startDownloadForSending(OCFile file
) {
1595 mWaitingToSend
= file
;
1596 requestForDownload(mWaitingToSend
);
1597 boolean hasSecondFragment
= (getSecondFragment() != null
);
1598 updateFragmentsVisibility(hasSecondFragment
);
1602 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1604 * @param file Image {@link OCFile} to show.
1606 public void startImagePreview(OCFile file
) {
1607 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1608 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1609 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1610 startActivity(showDetailsIntent
);
1615 * Stars the preview of an already down media {@link OCFile}.
1617 * @param file Media {@link OCFile} to preview.
1618 * @param startPlaybackPosition Media position where the playback will be started,
1620 * @param autoplay When 'true', the playback will start without user
1623 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1624 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
,
1626 setSecondFragment(mediaFragment
);
1627 updateFragmentsVisibility(true
);
1628 updateActionBarTitleAndHomeButton(file
);
1633 * Requests the download of the received {@link OCFile} , updates the UI
1634 * to monitor the download progress and prepares the activity to preview
1635 * or open the file when the download finishes.
1637 * @param file {@link OCFile} to download and preview.
1639 public void startDownloadForPreview(OCFile file
) {
1640 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1641 setSecondFragment(detailFragment
);
1642 mWaitingToPreview
= file
;
1643 requestForDownload();
1644 updateFragmentsVisibility(true
);
1645 updateActionBarTitleAndHomeButton(file
);
1650 public void cancelTransference(OCFile file
) {
1651 getFileOperationsHelper().cancelTransference(file
);
1652 if (mWaitingToPreview
!= null
&&
1653 mWaitingToPreview
.getRemotePath().equals(file
.getRemotePath())) {
1654 mWaitingToPreview
= null
;
1656 if (mWaitingToSend
!= null
&&
1657 mWaitingToSend
.getRemotePath().equals(file
.getRemotePath())) {
1658 mWaitingToSend
= null
;
1660 onTransferStateChanged(file
, false
, false
);
1664 public void onRefresh(boolean ignoreETag
) {
1665 refreshList(ignoreETag
);
1669 public void onRefresh() {
1673 private void refreshList(boolean ignoreETag
) {
1674 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1675 if (listOfFiles
!= null
) {
1676 OCFile folder
= listOfFiles
.getCurrentFile();
1677 if (folder
!= null
) {
1678 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1679 listDirectory(mFile);*/
1680 startSyncFolderOperation(folder
, ignoreETag
);
1685 private void sortByDate(boolean ascending
){
1686 getListOfFilesFragment().sortByDate(ascending
);
1689 private void sortBySize(boolean ascending
){
1690 getListOfFilesFragment().sortBySize(ascending
);
1693 private void sortByName(boolean ascending
){
1694 getListOfFilesFragment().sortByName(ascending
);
1697 public void allFilesOption() {