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
.CopyFileOperation
;
78 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
79 import com
.owncloud
.android
.operations
.CreateShareOperation
;
80 import com
.owncloud
.android
.operations
.MoveFileOperation
;
81 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
82 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
83 import com
.owncloud
.android
.operations
.RenameFileOperation
;
84 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
85 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
86 import com
.owncloud
.android
.services
.observer
.FileObserverService
;
87 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
88 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
89 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
90 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
;
91 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
.OnSslUntrustedCertListener
;
92 import com
.owncloud
.android
.ui
.dialog
.UploadSourceDialogFragment
;
93 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
94 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
95 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
96 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
97 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
98 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
99 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
100 import com
.owncloud
.android
.utils
.DisplayUtils
;
101 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
102 import com
.owncloud
.android
.utils
.FileStorageUtils
;
103 import com
.owncloud
.android
.utils
.UriUtils
;
109 * Displays, what files the user has available in his ownCloud.
112 public class FileDisplayActivity
extends HookActivity
113 implements FileFragment
.ContainerActivity
,
114 OnSslUntrustedCertListener
, OnEnforceableRefreshListener
{
116 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
117 private UploadFinishReceiver mUploadFinishReceiver
;
118 private DownloadFinishReceiver mDownloadFinishReceiver
;
119 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
121 private boolean mDualPane
;
122 private View mLeftFragmentContainer
;
123 private View mRightFragmentContainer
;
125 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
126 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
127 private static final String KEY_WAITING_TO_SEND
= "WAITING_TO_SEND";
129 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
131 public static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
132 public static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
133 public static final int ACTION_MOVE_FILES
= 3;
134 public static final int ACTION_COPY_FILES
= 4;
136 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
138 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
139 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
141 private OCFile mWaitingToPreview
;
143 private boolean mSyncInProgress
= false
;
145 private static String DIALOG_UNTRUSTED_CERT
= "DIALOG_UNTRUSTED_CERT";
146 private static String DIALOG_CREATE_FOLDER
= "DIALOG_CREATE_FOLDER";
147 private static String DIALOG_UPLOAD_SOURCE
= "DIALOG_UPLOAD_SOURCE";
148 private static String DIALOG_CERT_NOT_SAVED
= "DIALOG_CERT_NOT_SAVED";
150 private OCFile mWaitingToSend
;
154 protected void onCreate(Bundle savedInstanceState
) {
155 Log_OC
.v(TAG
, "onCreate() start");
156 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
158 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account
161 /// grant that FileObserverService is watching favorite files
162 if (savedInstanceState
== null
) {
163 Intent initObserversIntent
= FileObserverService
.makeInitIntent(this);
164 startService(initObserversIntent
);
167 /// Load of saved instance state
168 if(savedInstanceState
!= null
) {
169 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(
170 FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
171 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
172 mWaitingToSend
= (OCFile
) savedInstanceState
.getParcelable(
173 FileDisplayActivity
.KEY_WAITING_TO_SEND
);
176 mWaitingToPreview
= null
;
177 mSyncInProgress
= false
;
178 mWaitingToSend
= null
;
183 // Inflate and set the layout view
184 setContentView(R
.layout
.files
);
189 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
190 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
191 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
192 if (savedInstanceState
== null
) {
193 createMinFragments();
197 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS,
198 // according to the official
201 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
202 /*|| mRefreshSharesInProgress*/);
203 // always AFTER setContentView(...) ; to work around bug in its implementation
207 Log_OC
.v(TAG
, "onCreate() end");
211 protected void onStart() {
212 Log_OC
.v(TAG
, "onStart() start");
214 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
215 Log_OC
.v(TAG
, "onStart() end");
219 protected void onDestroy() {
220 Log_OC
.v(TAG
, "onDestroy() start");
222 Log_OC
.v(TAG
, "onDestroy() end");
226 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
229 protected void onAccountSet(boolean stateWasRecovered
) {
230 super.onAccountSet(stateWasRecovered
);
231 if (getAccount() != null
) {
232 /// Check whether the 'main' OCFile handled by the Activity is contained in the
234 OCFile file
= getFile();
235 // get parent from path
236 String parentPath
= "";
238 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
239 // upload in progress - right now, files are not inserted in the local
240 // cache until the upload is successful get parent from path
241 parentPath
= file
.getRemotePath().substring(0,
242 file
.getRemotePath().lastIndexOf(file
.getFileName()));
243 if (getStorageManager().getFileByPath(parentPath
) == null
)
244 file
= null
; // not able to know the directory where the file is uploading
246 file
= getStorageManager().getFileByPath(file
.getRemotePath());
247 // currentDir = null if not in the current Account
251 // fall back to root folder
252 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
256 if (!stateWasRecovered
) {
257 Log_OC
.d(TAG
, "Initializing Fragments in onAccountChanged..");
258 initFragmentsWithFile();
259 if (file
.isFolder()) {
260 startSyncFolderOperation(file
, false
);
264 updateFragmentsVisibility(!file
.isFolder());
265 updateActionBarTitleAndHomeButton(file
.isFolder() ? null
: file
);
271 private void createMinFragments() {
272 OCFileListFragment listOfFiles
= new OCFileListFragment();
273 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
274 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
275 transaction
.commit();
278 private void initFragmentsWithFile() {
279 if (getAccount() != null
&& getFile() != null
) {
281 OCFileListFragment listOfFiles
= getListOfFilesFragment();
282 if (listOfFiles
!= null
) {
283 listOfFiles
.listDirectory(getCurrentDir());
284 // TODO Enable when "On Device" is recovered
285 // listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
287 Log_OC
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
291 OCFile file
= getFile();
292 Fragment secondFragment
= chooseInitialSecondFragment(file
);
293 if (secondFragment
!= null
) {
294 setSecondFragment(secondFragment
);
295 updateFragmentsVisibility(true
);
296 updateActionBarTitleAndHomeButton(file
);
299 cleanSecondFragment();
303 Log_OC
.wtf(TAG
, "initFragments() called with invalid NULLs!");
304 if (getAccount() == null
) {
305 Log_OC
.wtf(TAG
, "\t account is NULL");
307 if (getFile() == null
) {
308 Log_OC
.wtf(TAG
, "\t file is NULL");
313 private Fragment
chooseInitialSecondFragment(OCFile file
) {
314 Fragment secondFragment
= null
;
315 if (file
!= null
&& !file
.isFolder()) {
316 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
317 && file
.getLastSyncDateForProperties() > 0 // temporal fix
319 int startPlaybackPosition
=
320 getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
322 getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
323 secondFragment
= new PreviewMediaFragment(file
, getAccount(),
324 startPlaybackPosition
, autoplay
);
327 secondFragment
= FileDetailFragment
.newInstance(file
, getAccount());
330 return secondFragment
;
335 * Replaces the second fragment managed by the activity with the received as
338 * Assumes never will be more than two fragments managed at the same time.
340 * @param fragment New second Fragment to set.
342 private void setSecondFragment(Fragment fragment
) {
343 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
344 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
345 transaction
.commit();
349 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
351 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
352 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
354 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
355 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
358 } else if (existsSecondFragment
) {
359 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
360 mLeftFragmentContainer
.setVisibility(View
.GONE
);
362 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
363 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
367 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
368 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
370 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
371 mRightFragmentContainer
.setVisibility(View
.GONE
);
377 private OCFileListFragment
getListOfFilesFragment() {
378 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(
379 FileDisplayActivity
.TAG_LIST_OF_FILES
);
380 if (listOfFiles
!= null
) {
381 return (OCFileListFragment
) listOfFiles
;
383 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
387 public FileFragment
getSecondFragment() {
388 Fragment second
= getSupportFragmentManager().findFragmentByTag(
389 FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
390 if (second
!= null
) {
391 return (FileFragment
) second
;
396 protected void cleanSecondFragment() {
397 Fragment second
= getSecondFragment();
398 if (second
!= null
) {
399 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
403 updateFragmentsVisibility(false
);
404 updateActionBarTitleAndHomeButton(null
);
407 protected void refreshListOfFilesFragment() {
408 OCFileListFragment fileListFragment
= getListOfFilesFragment();
409 if (fileListFragment
!= null
) {
410 fileListFragment
.listDirectory();
411 // TODO Enable when "On Device" is recovered ?
412 // fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
416 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
,
418 FileFragment secondFragment
= getSecondFragment();
419 boolean waitedPreview
= (mWaitingToPreview
!= null
&&
420 mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
421 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
422 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
423 OCFile fileInFragment
= detailsFragment
.getFile();
424 if (fileInFragment
!= null
&&
425 !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
426 // the user browsed to other file ; forget the automatic preview
427 mWaitingToPreview
= null
;
429 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
430 // grant that the right panel updates the progress bar
431 detailsFragment
.listenForTransferProgress();
432 detailsFragment
.updateFileDetails(true
, false
);
434 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
435 // update the right panel
436 boolean detailsFragmentChanged
= false
;
439 mWaitingToPreview
= getStorageManager().getFileById(
440 mWaitingToPreview
.getFileId()); // update the file from database,
441 // for the local storage path
442 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
443 startMediaPreview(mWaitingToPreview
, 0, true
);
444 detailsFragmentChanged
= true
;
446 getFileOperationsHelper().openFile(mWaitingToPreview
);
449 mWaitingToPreview
= null
;
451 if (!detailsFragmentChanged
) {
452 detailsFragment
.updateFileDetails(false
, (success
));
459 public boolean onPrepareOptionsMenu(Menu menu
) {
460 boolean drawerOpen
= mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
461 menu
.findItem(R
.id
.action_upload
).setVisible(!drawerOpen
);
462 menu
.findItem(R
.id
.action_create_dir
).setVisible(!drawerOpen
);
463 menu
.findItem(R
.id
.action_sort
).setVisible(!drawerOpen
);
464 menu
.findItem(R
.id
.action_sync_account
).setVisible(!drawerOpen
);
466 return super.onPrepareOptionsMenu(menu
);
470 public boolean onCreateOptionsMenu(Menu menu
) {
471 MenuInflater inflater
= getMenuInflater();
472 inflater
.inflate(R
.menu
.main_menu
, menu
);
478 public boolean onOptionsItemSelected(MenuItem item
) {
479 boolean retval
= true
;
480 switch (item
.getItemId()) {
481 case R
.id
.action_create_dir
: {
482 CreateFolderDialogFragment dialog
=
483 CreateFolderDialogFragment
.newInstance(getCurrentDir());
484 dialog
.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER
);
487 case R
.id
.action_sync_account
: {
488 startSynchronization();
491 case R
.id
.action_upload
: {
492 UploadSourceDialogFragment dialog
=
493 UploadSourceDialogFragment
.newInstance(getAccount());
494 dialog
.show(getSupportFragmentManager(), DIALOG_UPLOAD_SOURCE
);
498 case android
.R
.id
.home
: {
499 FileFragment second
= getSecondFragment();
500 OCFile currentDir
= getCurrentDir();
501 if (mDrawerLayout
.isDrawerOpen(GravityCompat
.START
)) {
502 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
503 } else if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
504 (second
!= null
&& second
.getFile() != null
)) {
508 mDrawerLayout
.openDrawer(GravityCompat
.START
);
512 case R
.id
.action_sort
: {
513 SharedPreferences appPreferences
= PreferenceManager
514 .getDefaultSharedPreferences(this);
516 // Read sorting order, default to sort by name ascending
517 Integer sortOrder
= appPreferences
518 .getInt("sortOrder", FileStorageUtils
.SORT_NAME
);
520 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
521 builder
.setTitle(R
.string
.actionbar_sort_title
)
522 .setSingleChoiceItems(R
.array
.actionbar_sortby
, sortOrder
,
523 new DialogInterface
.OnClickListener() {
524 public void onClick(DialogInterface dialog
, int which
) {
537 builder
.create().show();
541 retval
= super.onOptionsItemSelected(item
);
546 private void startSynchronization() {
547 Log_OC
.d(TAG
, "Got to start sync");
548 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
549 Log_OC
.d(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
550 ContentResolver
.cancelSync(null
, MainApp
.getAuthority());
551 // cancel the current synchronizations of any ownCloud account
552 Bundle bundle
= new Bundle();
553 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
554 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
555 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
556 MainApp
.getAuthority());
557 ContentResolver
.requestSync(
559 MainApp
.getAuthority(), bundle
);
561 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
562 MainApp
.getAuthority() + " with new API");
563 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
564 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
565 builder
.setExpedited(true
);
566 builder
.setManual(true
);
569 // Fix bug in Android Lollipop when you click on refresh the whole account
570 Bundle extras
= new Bundle();
571 builder
.setExtras(extras
);
573 SyncRequest request
= builder
.build();
574 ContentResolver
.requestSync(request
);
579 * Called, when the user selected something for uploading
582 @TargetApi(Build
.VERSION_CODES
.JELLY_BEAN
)
584 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
586 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
||
587 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
588 //getClipData is only supported on api level 16+, Jelly Bean
589 if (data
.getData() == null
&& Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
){
590 for( int i
= 0; i
< data
.getClipData().getItemCount(); i
++){
591 Intent intent
= new Intent();
592 intent
.setData(data
.getClipData().getItemAt(i
).getUri());
593 requestSimpleUpload(intent
, resultCode
);
596 requestSimpleUpload(data
, resultCode
);
598 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
||
599 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
600 requestMultipleUpload(data
, resultCode
);
603 } else if (requestCode
== ACTION_MOVE_FILES
&& resultCode
== RESULT_OK
){
605 } else if (requestCode == ACTION_MOVE_FILES && (resultCode == RESULT_OK ||
606 resultCode == MoveActivity.RESULT_OK_AND_MOVE)) {
607 >>>>>>> Added 'ACTION_COPY_FILES'.
610 final Intent fData
= data
;
611 final int fResultCode
= resultCode
;
612 getHandler().postDelayed(
616 requestMoveOperation(fData
, fResultCode
);
619 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
622 } else if (requestCode
== ACTION_COPY_FILES
&& (resultCode
== RESULT_OK
||
623 resultCode
== CopyActivity
.RESULT_OK_AND_COPY
)) {
625 final Intent fData
= data
;
626 final int fResultCode
= resultCode
;
627 getHandler().postDelayed(
631 requestCopyOperation(fData
, fResultCode
);
634 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
638 super.onActivityResult(requestCode
, resultCode
, data
);
643 private void requestMultipleUpload(Intent data
, int resultCode
) {
644 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
645 if (filePaths
!= null
) {
646 String
[] remotePaths
= new String
[filePaths
.length
];
647 String remotePathBase
= getCurrentDir().getRemotePath();
648 for (int j
= 0; j
< remotePaths
.length
; j
++) {
649 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
652 Intent i
= new Intent(this, FileUploader
.class);
653 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
654 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
655 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
656 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
657 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
658 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
662 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
663 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
),
671 private void requestSimpleUpload(Intent data
, int resultCode
) {
672 String filePath
= null
;
673 String mimeType
= null
;
675 Uri selectedImageUri
= data
.getData();
678 mimeType
= getContentResolver().getType(selectedImageUri
);
680 String fileManagerString
= selectedImageUri
.getPath();
681 String selectedImagePath
= UriUtils
.getLocalPath(selectedImageUri
, this);
683 if (selectedImagePath
!= null
)
684 filePath
= selectedImagePath
;
686 filePath
= fileManagerString
;
688 } catch (Exception e
) {
689 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of " +
690 "Intent.ACTION_GET_CONTENT", e
);
693 if (filePath
== null
) {
694 Log_OC
.e(TAG
, "Couldn't resolve path to file");
695 Toast t
= Toast
.makeText(
696 this, getString(R
.string
.filedisplay_unexpected_bad_get_content
),
704 Intent i
= new Intent(this, FileUploader
.class);
705 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
706 OCFile currentDir
= getCurrentDir();
707 String remotePath
= (currentDir
!= null
) ? currentDir
.getRemotePath() : OCFile
.ROOT_PATH
;
709 if (filePath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
)) {
710 Cursor cursor
= getContentResolver().query(Uri
.parse(filePath
), null
, null
, null
, null
);
712 if (cursor
!= null
&& cursor
.moveToFirst()) {
713 String displayName
= cursor
.getString(cursor
.getColumnIndex(
714 OpenableColumns
.DISPLAY_NAME
));
715 Log_OC
.v(TAG
, "Display Name: " + displayName
);
717 displayName
.replace(File
.separatorChar
, '_');
718 displayName
.replace(File
.pathSeparatorChar
, '_');
719 remotePath
+= displayName
+ DisplayUtils
.getComposedFileExtension(filePath
);
722 // and what happens in case of error?; wrong target name for the upload
728 remotePath
+= new File(filePath
).getName();
731 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePath
);
732 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePath
);
733 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mimeType
);
734 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
735 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
736 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
741 * Request the operation for moving the file/folder from one path to another
743 * @param data Intent received
744 * @param resultCode Result code received
746 private void requestMoveOperation(Intent data
, int resultCode
) {
747 OCFile folderToMoveAt
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FOLDER
);
748 OCFile targetFile
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
749 getFileOperationsHelper().moveFile(folderToMoveAt
, targetFile
);
753 * Request the operation for copying the file/folder from one path to another
755 * @param data Intent received
756 * @param resultCode Result code received
758 private void requestCopyOperation(Intent data
, int resultCode
) {
759 OCFile folderToMoveAt
= data
.getParcelableExtra(CopyActivity
.EXTRA_CURRENT_FOLDER
);
760 OCFile targetFile
= data
.getParcelableExtra(CopyActivity
.EXTRA_TARGET_FILE
);
761 getFileOperationsHelper().copyFile(folderToMoveAt
, targetFile
);
765 public void onBackPressed() {
766 OCFileListFragment listOfFiles
= getListOfFilesFragment();
767 if (mDualPane
|| getSecondFragment() == null
) {
768 OCFile currentDir
= getCurrentDir();
769 if (currentDir
== null
|| currentDir
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
) {
773 if (listOfFiles
!= null
) { // should never be null, indeed
774 listOfFiles
.onBrowseUp();
777 if (listOfFiles
!= null
) { // should never be null, indeed
778 setFile(listOfFiles
.getCurrentFile());
780 cleanSecondFragment();
785 protected void onSaveInstanceState(Bundle outState
) {
786 // responsibility of restore is preferred in onCreate() before than in
787 // onRestoreInstanceState when there are Fragments involved
788 Log_OC
.v(TAG
, "onSaveInstanceState() start");
789 super.onSaveInstanceState(outState
);
790 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
791 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
792 //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
793 // mRefreshSharesInProgress);
794 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_SEND
, mWaitingToSend
);
796 Log_OC
.v(TAG
, "onSaveInstanceState() end");
801 protected void onResume() {
802 Log_OC
.v(TAG
, "onResume() start");
805 // refresh Navigation Drawer account list
806 mNavigationDrawerAdapter
.updateAccountList();
809 // refresh list of files
810 refreshListOfFilesFragment();
812 // Listen for sync messages
813 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
814 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
815 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
816 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
817 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
818 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
819 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
820 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
821 // syncIntentFilter);
823 // Listen for upload messages
824 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
825 mUploadFinishReceiver
= new UploadFinishReceiver();
826 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
828 // Listen for download messages
829 IntentFilter downloadIntentFilter
= new IntentFilter(
830 FileDownloader
.getDownloadAddedMessage());
831 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
832 mDownloadFinishReceiver
= new DownloadFinishReceiver();
833 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
835 Log_OC
.v(TAG
, "onResume() end");
840 protected void onPause() {
841 Log_OC
.v(TAG
, "onPause() start");
842 if (mSyncBroadcastReceiver
!= null
) {
843 unregisterReceiver(mSyncBroadcastReceiver
);
844 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
845 mSyncBroadcastReceiver
= null
;
847 if (mUploadFinishReceiver
!= null
) {
848 unregisterReceiver(mUploadFinishReceiver
);
849 mUploadFinishReceiver
= null
;
851 if (mDownloadFinishReceiver
!= null
) {
852 unregisterReceiver(mDownloadFinishReceiver
);
853 mDownloadFinishReceiver
= null
;
857 Log_OC
.v(TAG
, "onPause() end");
861 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
864 * {@link BroadcastReceiver} to enable syncing feedback in UI
867 public void onReceive(Context context
, Intent intent
) {
869 String event
= intent
.getAction();
870 Log_OC
.d(TAG
, "Received broadcast " + event
);
871 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
872 String synchFolderRemotePath
=
873 intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
874 RemoteOperationResult synchResult
=
875 (RemoteOperationResult
)intent
.getSerializableExtra(
876 FileSyncAdapter
.EXTRA_RESULT
);
877 boolean sameAccount
= (getAccount() != null
&&
878 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
882 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
883 mSyncInProgress
= true
;
886 OCFile currentFile
= (getFile() == null
) ? null
:
887 getStorageManager().getFileByPath(getFile().getRemotePath());
888 OCFile currentDir
= (getCurrentDir() == null
) ? null
:
889 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
891 if (currentDir
== null
) {
892 // current folder was removed from the server
893 Toast
.makeText( FileDisplayActivity
.this,
896 sync_current_folder_was_removed
),
897 synchFolderRemotePath
),
903 if (currentFile
== null
&& !getFile().isFolder()) {
904 // currently selected file was removed in the server, and now we
906 cleanSecondFragment();
907 currentFile
= currentDir
;
910 if (synchFolderRemotePath
!= null
&&
911 currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
912 OCFileListFragment fileListFragment
= getListOfFilesFragment();
913 if (fileListFragment
!= null
) {
914 fileListFragment
.listDirectory();
915 // TODO Enable when "On Device" is recovered ?
916 // fileListFragment.listDirectory(currentDir,
917 // MainApp.getOnlyOnDevice());
920 setFile(currentFile
);
923 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
924 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
927 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
929 /// TODO refactor and make common
930 synchResult
!= null
&& !synchResult
.isSuccess() &&
931 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
932 synchResult
.isIdPRedirection() ||
933 (synchResult
.isException() && synchResult
.getException()
934 instanceof AuthenticatorException
))) {
938 OwnCloudClient client
;
939 OwnCloudAccount ocAccount
=
940 new OwnCloudAccount(getAccount(), context
);
941 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
942 removeClientFor(ocAccount
));
944 if (client
!= null
) {
945 OwnCloudCredentials cred
= client
.getCredentials();
947 AccountManager am
= AccountManager
.get(context
);
948 if (cred
.authTokenExpires()) {
949 am
.invalidateAuthToken(
954 am
.clearPassword(getAccount());
958 requestCredentialsUpdate();
960 } catch (AccountNotFoundException e
) {
961 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
966 removeStickyBroadcast(intent
);
967 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
968 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
969 /*|| mRefreshSharesInProgress*/);
975 if (synchResult
!= null
) {
976 if (synchResult
.getCode().equals(
977 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
978 mLastSslUntrustedServerResult
= synchResult
;
981 } catch (RuntimeException e
) {
982 // avoid app crashes after changing the serial id of RemoteOperationResult
983 // in owncloud library with broadcast notifications pending to process
984 removeStickyBroadcast(intent
);
990 * Show a text message on screen view for notifying user if content is
991 * loading or folder is empty
993 private void setBackgroundText() {
994 OCFileListFragment ocFileListFragment
= getListOfFilesFragment();
995 if (ocFileListFragment
!= null
) {
996 int message
= R
.string
.file_list_loading
;
997 if (!mSyncInProgress
) {
998 // In case file list is empty
999 message
= R
.string
.file_list_empty
;
1001 ocFileListFragment
.setMessageForEmptyList(getString(message
));
1003 Log_OC
.e(TAG
, "OCFileListFragment is null");
1008 * Once the file upload has finished -> update view
1010 private class UploadFinishReceiver
extends BroadcastReceiver
{
1012 * Once the file upload has finished -> update view
1014 * @author David A. Velasco
1015 * {@link BroadcastReceiver} to enable upload feedback in UI
1018 public void onReceive(Context context
, Intent intent
) {
1020 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1021 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
1022 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
1023 OCFile currentDir
= getCurrentDir();
1024 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) &&
1025 (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1027 if (sameAccount
&& isDescendant
) {
1028 refreshListOfFilesFragment();
1031 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
,
1033 boolean renamedInUpload
= getFile().getRemotePath().
1034 equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
1035 boolean sameFile
= getFile().getRemotePath().equals(uploadedRemotePath
) ||
1037 FileFragment details
= getSecondFragment();
1038 boolean detailFragmentIsShown
= (details
!= null
&&
1039 details
instanceof FileDetailFragment
);
1041 if (sameAccount
&& sameFile
&& detailFragmentIsShown
) {
1042 if (uploadWasFine
) {
1043 setFile(getStorageManager().getFileByPath(uploadedRemotePath
));
1045 if (renamedInUpload
) {
1046 String newName
= (new File(uploadedRemotePath
)).getName();
1047 Toast msg
= Toast
.makeText(
1050 getString(R
.string
.filedetails_renamed_in_upload_msg
),
1055 if (uploadWasFine
|| getFile().fileExists()) {
1056 ((FileDetailFragment
) details
).updateFileDetails(false
, true
);
1058 cleanSecondFragment();
1061 // Force the preview if the file is an image
1062 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
1063 startImagePreview(getFile());
1064 } // TODO what about other kind of previews?
1068 if (intent
!= null
) {
1069 removeStickyBroadcast(intent
);
1079 * Class waiting for broadcast events from the {@link FileDownloader} service.
1081 * Updates the UI when a download is started or finished, provided that it is relevant for the
1084 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1086 //int refreshCounter = 0;
1088 public void onReceive(Context context
, Intent intent
) {
1090 boolean sameAccount
= isSameAccount(context
, intent
);
1091 String downloadedRemotePath
=
1092 intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1093 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1095 if (sameAccount
&& isDescendant
) {
1096 String linkedToRemotePath
=
1097 intent
.getStringExtra(FileDownloader
.EXTRA_LINKED_TO_PATH
);
1098 if (linkedToRemotePath
== null
|| isAscendant(linkedToRemotePath
)) {
1099 //Log_OC.v(TAG, "refresh #" + ++refreshCounter);
1100 refreshListOfFilesFragment();
1102 refreshSecondFragment(
1104 downloadedRemotePath
,
1105 intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)
1109 if (mWaitingToSend
!= null
) {
1111 getStorageManager().getFileByPath(mWaitingToSend
.getRemotePath());
1112 if (mWaitingToSend
.isDown()) {
1113 sendDownloadedFile();
1118 if (intent
!= null
) {
1119 removeStickyBroadcast(intent
);
1124 private boolean isDescendant(String downloadedRemotePath
) {
1125 OCFile currentDir
= getCurrentDir();
1127 currentDir
!= null
&&
1128 downloadedRemotePath
!= null
&&
1129 downloadedRemotePath
.startsWith(currentDir
.getRemotePath())
1133 private boolean isAscendant(String linkedToRemotePath
) {
1134 OCFile currentDir
= getCurrentDir();
1136 currentDir
!= null
&&
1137 currentDir
.getRemotePath().startsWith(linkedToRemotePath
)
1141 private boolean isSameAccount(Context context
, Intent intent
) {
1142 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1143 return (accountName
!= null
&& getAccount() != null
&&
1144 accountName
.equals(getAccount().name
));
1149 public void browseToRoot() {
1150 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1151 if (listOfFiles
!= null
) { // should never be null, indeed
1152 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
1153 listOfFiles
.listDirectory(root
);
1154 // TODO Enable when "On Device" is recovered ?
1155 // listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
1156 setFile(listOfFiles
.getCurrentFile());
1157 startSyncFolderOperation(root
, false
);
1159 cleanSecondFragment();
1167 * Updates action bar and second fragment, if in dual pane mode.
1170 public void onBrowsedDownTo(OCFile directory
) {
1172 cleanSecondFragment();
1174 startSyncFolderOperation(directory
, false
);
1178 * Shows the information of the {@link OCFile} received as a
1179 * parameter in the second fragment.
1181 * @param file {@link OCFile} whose details will be shown
1184 public void showDetails(OCFile file
) {
1185 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1186 setSecondFragment(detailFragment
);
1187 updateFragmentsVisibility(true
);
1188 updateActionBarTitleAndHomeButton(file
);
1193 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
1195 // in dual pane mode, keep the focus of title an action bar in the current folder
1196 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1199 super.updateActionBarTitleAndHomeButton(chosenFile
);
1206 protected ServiceConnection
newTransferenceServiceConnection() {
1207 return new ListServiceConnection();
1211 * Defines callbacks for service binding, passed to bindService()
1213 private class ListServiceConnection
implements ServiceConnection
{
1216 public void onServiceConnected(ComponentName component
, IBinder service
) {
1217 if (component
.equals(new ComponentName(
1218 FileDisplayActivity
.this, FileDownloader
.class))) {
1219 Log_OC
.d(TAG
, "Download service connected");
1220 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1221 if (mWaitingToPreview
!= null
)
1222 if (getStorageManager() != null
) {
1225 getStorageManager().getFileById(mWaitingToPreview
.getFileId());
1226 if (!mWaitingToPreview
.isDown()) {
1227 requestForDownload();
1231 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1232 FileUploader
.class))) {
1233 Log_OC
.d(TAG
, "Upload service connected");
1234 mUploaderBinder
= (FileUploaderBinder
) service
;
1238 // a new chance to get the mDownloadBinder through
1239 // getFileDownloadBinder() - THIS IS A MESS
1240 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1241 if (listOfFiles
!= null
) {
1242 listOfFiles
.listDirectory();
1243 // TODO Enable when "On Device" is recovered ?
1244 // listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1246 FileFragment secondFragment
= getSecondFragment();
1247 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1248 FileDetailFragment detailFragment
= (FileDetailFragment
) secondFragment
;
1249 detailFragment
.listenForTransferProgress();
1250 detailFragment
.updateFileDetails(false
, false
);
1255 public void onServiceDisconnected(ComponentName component
) {
1256 if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1257 FileDownloader
.class))) {
1258 Log_OC
.d(TAG
, "Download service disconnected");
1259 mDownloaderBinder
= null
;
1260 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1261 FileUploader
.class))) {
1262 Log_OC
.d(TAG
, "Upload service disconnected");
1263 mUploaderBinder
= null
;
1269 public void onSavedCertificate() {
1270 startSyncFolderOperation(getCurrentDir(), false
);
1275 public void onFailedSavingCertificate() {
1276 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
1277 R
.string
.ssl_validator_not_saved
, new String
[]{}, R
.string
.common_ok
, -1, -1
1279 dialog
.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED
);
1283 public void onCancelCertificate() {
1288 * Updates the view associated to the activity after the finish of some operation over files
1289 * in the current account.
1291 * @param operation Removal operation performed.
1292 * @param result Result of the removal.
1295 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1296 super.onRemoteOperationFinish(operation
, result
);
1298 if (operation
instanceof RemoveFileOperation
) {
1299 onRemoveFileOperationFinish((RemoveFileOperation
) operation
, result
);
1301 } else if (operation
instanceof RenameFileOperation
) {
1302 onRenameFileOperationFinish((RenameFileOperation
) operation
, result
);
1304 } else if (operation
instanceof SynchronizeFileOperation
) {
1305 onSynchronizeFileOperationFinish((SynchronizeFileOperation
) operation
, result
);
1307 } else if (operation
instanceof CreateFolderOperation
) {
1308 onCreateFolderOperationFinish((CreateFolderOperation
) operation
, result
);
1310 } else if (operation
instanceof CreateShareOperation
) {
1311 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1313 } else if (operation
instanceof UnshareLinkOperation
) {
1314 onUnshareLinkOperationFinish((UnshareLinkOperation
) operation
, result
);
1316 } else if (operation
instanceof MoveFileOperation
) {
1317 onMoveFileOperationFinish((MoveFileOperation
) operation
, result
);
1319 } else if (operation
instanceof CopyFileOperation
) {
1320 onCopyFileOperationFinish((CopyFileOperation
) operation
, result
);
1326 private void onCreateShareOperationFinish(CreateShareOperation operation
,
1327 RemoteOperationResult result
) {
1328 if (result
.isSuccess()) {
1329 refreshShowDetails();
1330 refreshListOfFilesFragment();
1335 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
1336 RemoteOperationResult result
) {
1337 if (result
.isSuccess()) {
1338 refreshShowDetails();
1339 refreshListOfFilesFragment();
1341 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
1342 cleanSecondFragment();
1343 refreshListOfFilesFragment();
1347 private void refreshShowDetails() {
1348 FileFragment details
= getSecondFragment();
1349 if (details
!= null
) {
1350 OCFile file
= details
.getFile();
1352 file
= getStorageManager().getFileByPath(file
.getRemotePath());
1353 if (details
instanceof PreviewMediaFragment
) {
1354 // Refresh OCFile of the fragment
1355 ((PreviewMediaFragment
) details
).updateFile(file
);
1360 invalidateOptionsMenu();
1365 * Updates the view associated to the activity after the finish of an operation trying to
1368 * @param operation Removal operation performed.
1369 * @param result Result of the removal.
1371 private void onRemoveFileOperationFinish(RemoveFileOperation operation
,
1372 RemoteOperationResult result
) {
1373 dismissLoadingDialog();
1375 Toast msg
= Toast
.makeText(this,
1376 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1380 if (result
.isSuccess()) {
1381 OCFile removedFile
= operation
.getFile();
1382 FileFragment second
= getSecondFragment();
1383 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1384 if (second
instanceof PreviewMediaFragment
) {
1385 ((PreviewMediaFragment
) second
).stopPreview(true
);
1387 setFile(getStorageManager().getFileById(removedFile
.getParentId()));
1388 cleanSecondFragment();
1390 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1391 refreshListOfFilesFragment();
1393 invalidateOptionsMenu();
1395 if (result
.isSslRecoverableException()) {
1396 mLastSslUntrustedServerResult
= result
;
1397 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1404 * Updates the view associated to the activity after the finish of an operation trying to move a
1407 * @param operation Move operation performed.
1408 * @param result Result of the move operation.
1410 private void onMoveFileOperationFinish(MoveFileOperation operation
,
1411 RemoteOperationResult result
) {
1412 if (result
.isSuccess()) {
1413 dismissLoadingDialog();
1414 refreshListOfFilesFragment();
1416 dismissLoadingDialog();
1418 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1419 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1423 } catch (NotFoundException e
) {
1424 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1430 * Updates the view associated to the activity after the finish of an operation trying to copy a
1433 * @param operation Copy operation performed.
1434 * @param result Result of the copy operation.
1436 private void onCopyFileOperationFinish(CopyFileOperation operation
, RemoteOperationResult result
) {
1437 if (result
.isSuccess()) {
1438 dismissLoadingDialog();
1439 refreshListOfFilesFragment();
1441 dismissLoadingDialog();
1443 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1444 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1448 } catch (NotFoundException e
) {
1449 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1455 * Updates the view associated to the activity after the finish of an operation trying to rename
1458 * @param operation Renaming operation performed.
1459 * @param result Result of the renaming.
1461 private void onRenameFileOperationFinish(RenameFileOperation operation
,
1462 RemoteOperationResult result
) {
1463 dismissLoadingDialog();
1464 OCFile renamedFile
= operation
.getFile();
1465 if (result
.isSuccess()) {
1466 FileFragment details
= getSecondFragment();
1467 if (details
!= null
) {
1468 if (details
instanceof FileDetailFragment
&&
1469 renamedFile
.equals(details
.getFile()) ) {
1470 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1471 showDetails(renamedFile
);
1473 } else if (details
instanceof PreviewMediaFragment
&&
1474 renamedFile
.equals(details
.getFile())) {
1475 ((PreviewMediaFragment
) details
).updateFile(renamedFile
);
1476 if (PreviewMediaFragment
.canBePreviewed(renamedFile
)) {
1477 int position
= ((PreviewMediaFragment
) details
).getPosition();
1478 startMediaPreview(renamedFile
, position
, true
);
1480 getFileOperationsHelper().openFile(renamedFile
);
1485 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())){
1486 refreshListOfFilesFragment();
1490 Toast msg
= Toast
.makeText(this,
1491 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1495 if (result
.isSslRecoverableException()) {
1496 mLastSslUntrustedServerResult
= result
;
1497 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1502 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
1503 RemoteOperationResult result
) {
1504 if (result
.isSuccess()) {
1505 if (operation
.transferWasRequested()) {
1506 OCFile syncedFile
= operation
.getLocalFile();
1507 onTransferStateChanged(syncedFile
, true
, true
);
1508 invalidateOptionsMenu();
1514 * Updates the view associated to the activity after the finish of an operation trying create a
1517 * @param operation Creation operation performed.
1518 * @param result Result of the creation.
1520 private void onCreateFolderOperationFinish(CreateFolderOperation operation
,
1521 RemoteOperationResult result
) {
1522 if (result
.isSuccess()) {
1523 dismissLoadingDialog();
1524 refreshListOfFilesFragment();
1526 dismissLoadingDialog();
1528 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1529 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1533 } catch (NotFoundException e
) {
1534 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1544 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1545 refreshListOfFilesFragment();
1546 FileFragment details
= getSecondFragment();
1547 if (details
!= null
&& details
instanceof FileDetailFragment
&&
1548 file
.equals(details
.getFile()) ) {
1549 if (downloading
|| uploading
) {
1550 ((FileDetailFragment
) details
).updateFileDetails(file
, getAccount());
1552 if (!file
.fileExists()) {
1553 cleanSecondFragment();
1555 ((FileDetailFragment
) details
).updateFileDetails(false
, true
);
1563 private void requestForDownload() {
1564 Account account
= getAccount();
1565 //if (!mWaitingToPreview.isDownloading()) {
1566 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1567 Intent i
= new Intent(this, FileDownloader
.class);
1568 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1569 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1575 private OCFile
getCurrentDir() {
1576 OCFile file
= getFile();
1578 if (file
.isFolder()) {
1580 } else if (getStorageManager() != null
) {
1581 String parentPath
= file
.getRemotePath().substring(0,
1582 file
.getRemotePath().lastIndexOf(file
.getFileName()));
1583 return getStorageManager().getFileByPath(parentPath
);
1589 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
1590 long currentSyncTime
= System
.currentTimeMillis();
1592 mSyncInProgress
= true
;
1594 // perform folder synchronization
1595 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
1598 getFileOperationsHelper().isSharedSupported(),
1600 getStorageManager(),
1602 getApplicationContext()
1604 synchFolderOp
.execute(getAccount(), MainApp
.getAppContext(), this, null
, null
);
1606 setSupportProgressBarIndeterminateVisibility(true
);
1608 setBackgroundText();
1612 * Show untrusted cert dialog
1614 public void showUntrustedCertDialog(RemoteOperationResult result
) {
1615 // Show a dialog with the certificate info
1616 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.newInstanceForFullSslError(
1617 (CertificateCombinedException
) result
.getException());
1618 FragmentManager fm
= getSupportFragmentManager();
1619 FragmentTransaction ft
= fm
.beginTransaction();
1620 dialog
.show(ft
, DIALOG_UNTRUSTED_CERT
);
1623 private void requestForDownload(OCFile file
) {
1624 Account account
= getAccount();
1625 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1626 Intent i
= new Intent(this, FileDownloader
.class);
1627 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1628 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
1633 private void sendDownloadedFile() {
1634 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend
);
1635 mWaitingToSend
= null
;
1640 * Requests the download of the received {@link OCFile} , updates the UI
1641 * to monitor the download progress and prepares the activity to send the file
1642 * when the download finishes.
1644 * @param file {@link OCFile} to download and preview.
1646 public void startDownloadForSending(OCFile file
) {
1647 mWaitingToSend
= file
;
1648 requestForDownload(mWaitingToSend
);
1649 boolean hasSecondFragment
= (getSecondFragment() != null
);
1650 updateFragmentsVisibility(hasSecondFragment
);
1654 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1656 * @param file Image {@link OCFile} to show.
1658 public void startImagePreview(OCFile file
) {
1659 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1660 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1661 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1662 startActivity(showDetailsIntent
);
1667 * Stars the preview of an already down media {@link OCFile}.
1669 * @param file Media {@link OCFile} to preview.
1670 * @param startPlaybackPosition Media position where the playback will be started,
1672 * @param autoplay When 'true', the playback will start without user
1675 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1676 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
,
1678 setSecondFragment(mediaFragment
);
1679 updateFragmentsVisibility(true
);
1680 updateActionBarTitleAndHomeButton(file
);
1685 * Requests the download of the received {@link OCFile} , updates the UI
1686 * to monitor the download progress and prepares the activity to preview
1687 * or open the file when the download finishes.
1689 * @param file {@link OCFile} to download and preview.
1691 public void startDownloadForPreview(OCFile file
) {
1692 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1693 setSecondFragment(detailFragment
);
1694 mWaitingToPreview
= file
;
1695 requestForDownload();
1696 updateFragmentsVisibility(true
);
1697 updateActionBarTitleAndHomeButton(file
);
1702 public void cancelTransference(OCFile file
) {
1703 getFileOperationsHelper().cancelTransference(file
);
1704 if (mWaitingToPreview
!= null
&&
1705 mWaitingToPreview
.getRemotePath().equals(file
.getRemotePath())) {
1706 mWaitingToPreview
= null
;
1708 if (mWaitingToSend
!= null
&&
1709 mWaitingToSend
.getRemotePath().equals(file
.getRemotePath())) {
1710 mWaitingToSend
= null
;
1712 onTransferStateChanged(file
, false
, false
);
1716 public void onRefresh(boolean ignoreETag
) {
1717 refreshList(ignoreETag
);
1721 public void onRefresh() {
1725 private void refreshList(boolean ignoreETag
) {
1726 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1727 if (listOfFiles
!= null
) {
1728 OCFile folder
= listOfFiles
.getCurrentFile();
1729 if (folder
!= null
) {
1730 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1731 listDirectory(mFile);*/
1732 startSyncFolderOperation(folder
, ignoreETag
);
1737 private void sortByDate(boolean ascending
){
1738 getListOfFilesFragment().sortByDate(ascending
);
1741 private void sortBySize(boolean ascending
){
1742 getListOfFilesFragment().sortBySize(ascending
);
1745 private void sortByName(boolean ascending
){
1746 getListOfFilesFragment().sortByName(ascending
);
1749 public void allFilesOption() {