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
;
108 * Displays, what files the user has available in his ownCloud.
111 public class FileDisplayActivity
extends HookActivity
112 implements FileFragment
.ContainerActivity
,
113 OnSslUntrustedCertListener
, OnEnforceableRefreshListener
{
115 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
116 private UploadFinishReceiver mUploadFinishReceiver
;
117 private DownloadFinishReceiver mDownloadFinishReceiver
;
118 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
120 private boolean mDualPane
;
121 private View mLeftFragmentContainer
;
122 private View mRightFragmentContainer
;
124 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
125 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
126 private static final String KEY_WAITING_TO_SEND
= "WAITING_TO_SEND";
128 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
130 public static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
131 public static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
132 public static final int ACTION_MOVE_FILES
= 3;
134 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
136 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
137 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
139 private OCFile mWaitingToPreview
;
141 private boolean mSyncInProgress
= false
;
143 private static String DIALOG_UNTRUSTED_CERT
= "DIALOG_UNTRUSTED_CERT";
144 private static String DIALOG_CREATE_FOLDER
= "DIALOG_CREATE_FOLDER";
145 private static String DIALOG_UPLOAD_SOURCE
= "DIALOG_UPLOAD_SOURCE";
146 private static String DIALOG_CERT_NOT_SAVED
= "DIALOG_CERT_NOT_SAVED";
148 private OCFile mWaitingToSend
;
149 private Menu mOptionsMenu
;
153 protected void onCreate(Bundle savedInstanceState
) {
154 Log_OC
.v(TAG
, "onCreate() start");
155 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
157 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account
160 /// grant that FileObserverService is watching favorite files
161 if (savedInstanceState
== null
) {
162 Intent initObserversIntent
= FileObserverService
.makeInitIntent(this);
163 startService(initObserversIntent
);
166 /// Load of saved instance state
167 if(savedInstanceState
!= null
) {
168 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(
169 FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
170 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
171 mWaitingToSend
= (OCFile
) savedInstanceState
.getParcelable(
172 FileDisplayActivity
.KEY_WAITING_TO_SEND
);
175 mWaitingToPreview
= null
;
176 mSyncInProgress
= false
;
177 mWaitingToSend
= null
;
182 // Inflate and set the layout view
183 setContentView(R
.layout
.files
);
188 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
189 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
190 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
191 if (savedInstanceState
== null
) {
192 createMinFragments();
196 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS,
197 // according to the official
200 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
201 /*|| mRefreshSharesInProgress*/);
202 // always AFTER setContentView(...) ; to work around bug in its implementation
206 Log_OC
.v(TAG
, "onCreate() end");
210 protected void onStart() {
211 Log_OC
.v(TAG
, "onStart() start");
213 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
214 Log_OC
.v(TAG
, "onStart() end");
218 protected void onDestroy() {
219 Log_OC
.v(TAG
, "onDestroy() start");
221 Log_OC
.v(TAG
, "onDestroy() end");
225 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
228 protected void onAccountSet(boolean stateWasRecovered
) {
229 super.onAccountSet(stateWasRecovered
);
230 if (getAccount() != null
) {
231 /// Check whether the 'main' OCFile handled by the Activity is contained in the
233 OCFile file
= getFile();
234 // get parent from path
235 String parentPath
= "";
237 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
238 // upload in progress - right now, files are not inserted in the local
239 // cache until the upload is successful get parent from path
240 parentPath
= file
.getRemotePath().substring(0,
241 file
.getRemotePath().lastIndexOf(file
.getFileName()));
242 if (getStorageManager().getFileByPath(parentPath
) == null
)
243 file
= null
; // not able to know the directory where the file is uploading
245 file
= getStorageManager().getFileByPath(file
.getRemotePath());
246 // currentDir = null if not in the current Account
250 // fall back to root folder
251 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
255 if (!stateWasRecovered
) {
256 Log_OC
.d(TAG
, "Initializing Fragments in onAccountChanged..");
257 initFragmentsWithFile();
258 if (file
.isFolder()) {
259 startSyncFolderOperation(file
, false
);
263 updateFragmentsVisibility(!file
.isFolder());
264 updateActionBarTitleAndHomeButton(file
.isFolder() ? null
: file
);
270 private void createMinFragments() {
271 OCFileListFragment listOfFiles
= new OCFileListFragment();
272 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
273 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
274 transaction
.commit();
277 private void initFragmentsWithFile() {
278 if (getAccount() != null
&& getFile() != null
) {
280 OCFileListFragment listOfFiles
= getListOfFilesFragment();
281 if (listOfFiles
!= null
) {
282 listOfFiles
.listDirectory(getCurrentDir());
283 // TODO Enable when "On Device" is recovered
284 // listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
286 Log_OC
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
290 OCFile file
= getFile();
291 Fragment secondFragment
= chooseInitialSecondFragment(file
);
292 if (secondFragment
!= null
) {
293 setSecondFragment(secondFragment
);
294 updateFragmentsVisibility(true
);
295 updateActionBarTitleAndHomeButton(file
);
298 cleanSecondFragment();
301 if (DisplayUtils
.isGridView(getFile(), getStorageManager())){
308 Log_OC
.wtf(TAG
, "initFragments() called with invalid NULLs!");
309 if (getAccount() == null
) {
310 Log_OC
.wtf(TAG
, "\t account is NULL");
312 if (getFile() == null
) {
313 Log_OC
.wtf(TAG
, "\t file is NULL");
318 private Fragment
chooseInitialSecondFragment(OCFile file
) {
319 Fragment secondFragment
= null
;
320 if (file
!= null
&& !file
.isFolder()) {
321 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
322 && file
.getLastSyncDateForProperties() > 0 // temporal fix
324 int startPlaybackPosition
=
325 getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
327 getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
328 secondFragment
= new PreviewMediaFragment(file
, getAccount(),
329 startPlaybackPosition
, autoplay
);
332 secondFragment
= FileDetailFragment
.newInstance(file
, getAccount());
335 return secondFragment
;
340 * Replaces the second fragment managed by the activity with the received as
343 * Assumes never will be more than two fragments managed at the same time.
345 * @param fragment New second Fragment to set.
347 private void setSecondFragment(Fragment fragment
) {
348 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
349 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
350 transaction
.commit();
354 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
356 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
357 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
359 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
360 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
363 } else if (existsSecondFragment
) {
364 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
365 mLeftFragmentContainer
.setVisibility(View
.GONE
);
367 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
368 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
372 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
373 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
375 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
376 mRightFragmentContainer
.setVisibility(View
.GONE
);
382 private OCFileListFragment
getListOfFilesFragment() {
383 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(
384 FileDisplayActivity
.TAG_LIST_OF_FILES
);
385 if (listOfFiles
!= null
) {
386 return (OCFileListFragment
)listOfFiles
;
388 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
392 public FileFragment
getSecondFragment() {
393 Fragment second
= getSupportFragmentManager().findFragmentByTag(
394 FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
395 if (second
!= null
) {
396 return (FileFragment
)second
;
401 protected void cleanSecondFragment() {
402 Fragment second
= getSecondFragment();
403 if (second
!= null
) {
404 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
408 updateFragmentsVisibility(false
);
409 updateActionBarTitleAndHomeButton(null
);
412 protected void refreshListOfFilesFragment() {
413 OCFileListFragment fileListFragment
= getListOfFilesFragment();
414 if (fileListFragment
!= null
) {
415 fileListFragment
.listDirectory();
416 // TODO Enable when "On Device" is recovered ?
417 // fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
421 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
,
423 FileFragment secondFragment
= getSecondFragment();
424 boolean waitedPreview
= (mWaitingToPreview
!= null
&&
425 mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
426 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
427 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
428 OCFile fileInFragment
= detailsFragment
.getFile();
429 if (fileInFragment
!= null
&&
430 !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
431 // the user browsed to other file ; forget the automatic preview
432 mWaitingToPreview
= null
;
434 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
435 // grant that the right panel updates the progress bar
436 detailsFragment
.listenForTransferProgress();
437 detailsFragment
.updateFileDetails(true
, false
);
439 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
440 // update the right panel
441 boolean detailsFragmentChanged
= false
;
444 mWaitingToPreview
= getStorageManager().getFileById(
445 mWaitingToPreview
.getFileId()); // update the file from database,
446 // for the local storage path
447 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
448 startMediaPreview(mWaitingToPreview
, 0, true
);
449 detailsFragmentChanged
= true
;
451 getFileOperationsHelper().openFile(mWaitingToPreview
);
454 mWaitingToPreview
= null
;
456 if (!detailsFragmentChanged
) {
457 detailsFragment
.updateFileDetails(false
, (success
));
464 public boolean onPrepareOptionsMenu(Menu menu
) {
465 boolean drawerOpen
= mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
466 menu
.findItem(R
.id
.action_upload
).setVisible(!drawerOpen
);
467 menu
.findItem(R
.id
.action_create_dir
).setVisible(!drawerOpen
);
468 menu
.findItem(R
.id
.action_sort
).setVisible(!drawerOpen
);
469 menu
.findItem(R
.id
.action_sync_account
).setVisible(!drawerOpen
);
471 return super.onPrepareOptionsMenu(menu
);
475 public boolean onCreateOptionsMenu(Menu menu
) {
476 MenuInflater inflater
= getMenuInflater();
477 inflater
.inflate(R
.menu
.main_menu
, menu
);
480 MenuItem menuItem
= mOptionsMenu
.findItem(R
.id
.action_switch_view
);
482 if (DisplayUtils
.isGridView(getFile(), getStorageManager())){
483 menuItem
.setTitle(getApplicationContext().getString(R
.string
.action_switch_list_view
));
485 menuItem
.setTitle(getApplicationContext().getString(R
.string
.action_switch_grid_view
));
493 public boolean onOptionsItemSelected(MenuItem item
) {
494 boolean retval
= true
;
495 switch (item
.getItemId()) {
496 case R
.id
.action_create_dir
: {
497 CreateFolderDialogFragment dialog
=
498 CreateFolderDialogFragment
.newInstance(getCurrentDir());
499 dialog
.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER
);
502 case R
.id
.action_sync_account
: {
503 startSynchronization();
506 case R
.id
.action_upload
: {
507 UploadSourceDialogFragment dialog
=
508 UploadSourceDialogFragment
.newInstance(getAccount());
509 dialog
.show(getSupportFragmentManager(), DIALOG_UPLOAD_SOURCE
);
513 case android
.R
.id
.home
: {
514 FileFragment second
= getSecondFragment();
515 OCFile currentDir
= getCurrentDir();
516 if (mDrawerLayout
.isDrawerOpen(GravityCompat
.START
)) {
517 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
518 } else if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
519 (second
!= null
&& second
.getFile() != null
)) {
523 mDrawerLayout
.openDrawer(GravityCompat
.START
);
527 case R
.id
.action_sort
: {
528 SharedPreferences appPreferences
= PreferenceManager
529 .getDefaultSharedPreferences(this);
531 // Read sorting order, default to sort by name ascending
532 Integer sortOrder
= appPreferences
533 .getInt("sortOrder", FileStorageUtils
.SORT_NAME
);
535 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
536 builder
.setTitle(R
.string
.actionbar_sort_title
)
537 .setSingleChoiceItems(R
.array
.actionbar_sortby
, sortOrder
,
538 new DialogInterface
.OnClickListener() {
539 public void onClick(DialogInterface dialog
, int which
) {
552 builder
.create().show();
555 case R
.id
.action_switch_view
:{
557 item
.setTitle(getApplicationContext().getString(R
.string
.action_switch_list_view
));
558 DisplayUtils
.setViewMode(getFile(), false
);
561 item
.setTitle(getApplicationContext().getString(R
.string
.action_switch_grid_view
));
562 DisplayUtils
.setViewMode(getFile(), true
);
568 retval
= super.onOptionsItemSelected(item
);
573 private void startSynchronization() {
574 Log_OC
.d(TAG
, "Got to start sync");
575 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
576 Log_OC
.d(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
577 ContentResolver
.cancelSync(null
, MainApp
.getAuthority());
578 // cancel the current synchronizations of any ownCloud account
579 Bundle bundle
= new Bundle();
580 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
581 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
582 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
583 MainApp
.getAuthority());
584 ContentResolver
.requestSync(
586 MainApp
.getAuthority(), bundle
);
588 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
589 MainApp
.getAuthority() + " with new API");
590 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
591 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
592 builder
.setExpedited(true
);
593 builder
.setManual(true
);
596 // Fix bug in Android Lollipop when you click on refresh the whole account
597 Bundle extras
= new Bundle();
598 builder
.setExtras(extras
);
600 SyncRequest request
= builder
.build();
601 ContentResolver
.requestSync(request
);
606 * Called, when the user selected something for uploading
609 @TargetApi(Build
.VERSION_CODES
.JELLY_BEAN
)
611 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
613 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
||
614 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
615 //getClipData is only supported on api level 16+, Jelly Bean
616 if (data
.getData() == null
&& Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
){
617 for( int i
= 0; i
< data
.getClipData().getItemCount(); i
++){
618 Intent intent
= new Intent();
619 intent
.setData(data
.getClipData().getItemAt(i
).getUri());
620 requestSimpleUpload(intent
, resultCode
);
623 requestSimpleUpload(data
, resultCode
);
625 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
||
626 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
627 requestMultipleUpload(data
, resultCode
);
629 } else if (requestCode
== ACTION_MOVE_FILES
&& resultCode
== RESULT_OK
){
631 final Intent fData
= data
;
632 final int fResultCode
= resultCode
;
633 getHandler().postDelayed(
637 requestMoveOperation(fData
, fResultCode
);
640 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
644 super.onActivityResult(requestCode
, resultCode
, data
);
649 private void requestMultipleUpload(Intent data
, int resultCode
) {
650 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
651 if (filePaths
!= null
) {
652 String
[] remotePaths
= new String
[filePaths
.length
];
653 String remotePathBase
= getCurrentDir().getRemotePath();
654 for (int j
= 0; j
< remotePaths
.length
; j
++) {
655 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
658 Intent i
= new Intent(this, FileUploader
.class);
659 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
660 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
661 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
662 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
663 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
664 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
668 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
669 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
),
677 private void requestSimpleUpload(Intent data
, int resultCode
) {
678 String filePath
= null
;
679 String mimeType
= null
;
681 Uri selectedImageUri
= data
.getData();
684 mimeType
= getContentResolver().getType(selectedImageUri
);
686 String fileManagerString
= selectedImageUri
.getPath();
687 String selectedImagePath
= UriUtils
.getLocalPath(selectedImageUri
, this);
689 if (selectedImagePath
!= null
)
690 filePath
= selectedImagePath
;
692 filePath
= fileManagerString
;
694 } catch (Exception e
) {
695 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of " +
696 "Intent.ACTION_GET_CONTENT", e
);
699 if (filePath
== null
) {
700 Log_OC
.e(TAG
, "Couldn't resolve path to file");
701 Toast t
= Toast
.makeText(
702 this, getString(R
.string
.filedisplay_unexpected_bad_get_content
),
710 Intent i
= new Intent(this, FileUploader
.class);
711 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
712 OCFile currentDir
= getCurrentDir();
713 String remotePath
= (currentDir
!= null
) ? currentDir
.getRemotePath() : OCFile
.ROOT_PATH
;
715 if (filePath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
)) {
716 Cursor cursor
= getContentResolver().query(Uri
.parse(filePath
), null
, null
, null
, null
);
718 if (cursor
!= null
&& cursor
.moveToFirst()) {
719 String displayName
= cursor
.getString(cursor
.getColumnIndex(
720 OpenableColumns
.DISPLAY_NAME
));
721 Log_OC
.v(TAG
, "Display Name: " + displayName
);
723 displayName
.replace(File
.separatorChar
, '_');
724 displayName
.replace(File
.pathSeparatorChar
, '_');
725 remotePath
+= displayName
+ DisplayUtils
.getComposedFileExtension(filePath
);
728 // and what happens in case of error?; wrong target name for the upload
734 remotePath
+= new File(filePath
).getName();
737 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePath
);
738 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePath
);
739 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mimeType
);
740 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
741 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
742 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
747 * Request the operation for moving the file/folder from one path to another
749 * @param data Intent received
750 * @param resultCode Result code received
752 private void requestMoveOperation(Intent data
, int resultCode
) {
753 OCFile folderToMoveAt
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FOLDER
);
754 OCFile targetFile
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
755 getFileOperationsHelper().moveFile(folderToMoveAt
, targetFile
);
759 public void onBackPressed() {
760 OCFileListFragment listOfFiles
= getListOfFilesFragment();
761 if (mDualPane
|| getSecondFragment() == null
) {
762 OCFile currentDir
= getCurrentDir();
763 if (currentDir
== null
|| currentDir
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
) {
767 if (listOfFiles
!= null
) { // should never be null, indeed
768 listOfFiles
.onBrowseUp();
771 if (listOfFiles
!= null
) { // should never be null, indeed
772 setFile(listOfFiles
.getCurrentFile());
774 cleanSecondFragment();
776 MenuItem menuItem
= mOptionsMenu
.findItem(R
.id
.action_switch_view
);
777 if (DisplayUtils
.isGridView(getFile(), getStorageManager())){
778 menuItem
.setTitle(getApplicationContext().getString(R
.string
.action_switch_list_view
));
780 menuItem
.setTitle(getApplicationContext().getString(R
.string
.action_switch_grid_view
));
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");
802 protected void onResume() {
803 Log_OC
.v(TAG
, "onResume() start");
806 // refresh Navigation Drawer account list
807 mNavigationDrawerAdapter
.updateAccountList();
810 // refresh list of files
811 refreshListOfFilesFragment();
813 // Listen for sync messages
814 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
815 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
816 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
817 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
818 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
819 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
820 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
821 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
822 // syncIntentFilter);
824 // Listen for upload messages
825 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
826 mUploadFinishReceiver
= new UploadFinishReceiver();
827 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
829 // Listen for download messages
830 IntentFilter downloadIntentFilter
= new IntentFilter(
831 FileDownloader
.getDownloadAddedMessage());
832 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
833 mDownloadFinishReceiver
= new DownloadFinishReceiver();
834 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
836 Log_OC
.v(TAG
, "onResume() end");
841 protected void onPause() {
842 Log_OC
.v(TAG
, "onPause() start");
843 if (mSyncBroadcastReceiver
!= null
) {
844 unregisterReceiver(mSyncBroadcastReceiver
);
845 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
846 mSyncBroadcastReceiver
= null
;
848 if (mUploadFinishReceiver
!= null
) {
849 unregisterReceiver(mUploadFinishReceiver
);
850 mUploadFinishReceiver
= null
;
852 if (mDownloadFinishReceiver
!= null
) {
853 unregisterReceiver(mDownloadFinishReceiver
);
854 mDownloadFinishReceiver
= null
;
858 Log_OC
.v(TAG
, "onPause() end");
862 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
865 * {@link BroadcastReceiver} to enable syncing feedback in UI
868 public void onReceive(Context context
, Intent intent
) {
870 String event
= intent
.getAction();
871 Log_OC
.d(TAG
, "Received broadcast " + event
);
872 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
873 String synchFolderRemotePath
=
874 intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
875 RemoteOperationResult synchResult
=
876 (RemoteOperationResult
)intent
.getSerializableExtra(
877 FileSyncAdapter
.EXTRA_RESULT
);
878 boolean sameAccount
= (getAccount() != null
&&
879 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
883 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
884 mSyncInProgress
= true
;
887 OCFile currentFile
= (getFile() == null
) ? null
:
888 getStorageManager().getFileByPath(getFile().getRemotePath());
889 OCFile currentDir
= (getCurrentDir() == null
) ? null
:
890 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
892 if (currentDir
== null
) {
893 // current folder was removed from the server
894 Toast
.makeText( FileDisplayActivity
.this,
897 sync_current_folder_was_removed
),
898 synchFolderRemotePath
),
904 if (currentFile
== null
&& !getFile().isFolder()) {
905 // currently selected file was removed in the server, and now we
907 cleanSecondFragment();
908 currentFile
= currentDir
;
911 if (synchFolderRemotePath
!= null
&&
912 currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
913 OCFileListFragment fileListFragment
= getListOfFilesFragment();
914 if (fileListFragment
!= null
) {
915 fileListFragment
.listDirectory();
916 // TODO Enable when "On Device" is recovered ?
917 // fileListFragment.listDirectory(currentDir,
918 // MainApp.getOnlyOnDevice());
921 setFile(currentFile
);
924 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
925 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
928 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
930 /// TODO refactor and make common
931 synchResult
!= null
&& !synchResult
.isSuccess() &&
932 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
933 synchResult
.isIdPRedirection() ||
934 (synchResult
.isException() && synchResult
.getException()
935 instanceof AuthenticatorException
))) {
939 OwnCloudClient client
;
940 OwnCloudAccount ocAccount
=
941 new OwnCloudAccount(getAccount(), context
);
942 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
943 removeClientFor(ocAccount
));
945 if (client
!= null
) {
946 OwnCloudCredentials cred
= client
.getCredentials();
948 AccountManager am
= AccountManager
.get(context
);
949 if (cred
.authTokenExpires()) {
950 am
.invalidateAuthToken(
955 am
.clearPassword(getAccount());
959 requestCredentialsUpdate();
961 } catch (AccountNotFoundException e
) {
962 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
967 removeStickyBroadcast(intent
);
968 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
969 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
970 /*|| mRefreshSharesInProgress*/);
976 if (synchResult
!= null
) {
977 if (synchResult
.getCode().equals(
978 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
979 mLastSslUntrustedServerResult
= synchResult
;
982 } catch (RuntimeException e
) {
983 // avoid app crashes after changing the serial id of RemoteOperationResult
984 // in owncloud library with broadcast notifications pending to process
985 removeStickyBroadcast(intent
);
991 * Show a text message on screen view for notifying user if content is
992 * loading or folder is empty
994 private void setBackgroundText() {
995 OCFileListFragment ocFileListFragment
= getListOfFilesFragment();
996 if (ocFileListFragment
!= null
) {
997 int message
= R
.string
.file_list_loading
;
998 if (!mSyncInProgress
) {
999 // In case file list is empty
1000 message
= R
.string
.file_list_empty
;
1002 ocFileListFragment
.setMessageForEmptyList(getString(message
));
1004 Log_OC
.e(TAG
, "OCFileListFragment is null");
1009 * Once the file upload has finished -> update view
1011 private class UploadFinishReceiver
extends BroadcastReceiver
{
1013 * 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
);
1176 MenuItem menuItem
= mOptionsMenu
.findItem(R
.id
.action_switch_view
);
1178 if (DisplayUtils
.isGridView(directory
, getStorageManager())){
1179 menuItem
.setTitle(getApplicationContext().getString(R
.string
.action_switch_list_view
));
1182 menuItem
.setTitle(getApplicationContext().getString(R
.string
.action_switch_grid_view
));
1188 * Shows the information of the {@link OCFile} received as a
1189 * parameter in the second fragment.
1191 * @param file {@link OCFile} whose details will be shown
1194 public void showDetails(OCFile file
) {
1195 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1196 setSecondFragment(detailFragment
);
1197 updateFragmentsVisibility(true
);
1198 updateActionBarTitleAndHomeButton(file
);
1203 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
1205 // in dual pane mode, keep the focus of title an action bar in the current folder
1206 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1209 super.updateActionBarTitleAndHomeButton(chosenFile
);
1216 protected ServiceConnection
newTransferenceServiceConnection() {
1217 return new ListServiceConnection();
1220 /** Defines callbacks for service binding, passed to bindService() */
1221 private class ListServiceConnection
implements ServiceConnection
{
1224 public void onServiceConnected(ComponentName component
, IBinder service
) {
1225 if (component
.equals(new ComponentName(
1226 FileDisplayActivity
.this, FileDownloader
.class))) {
1227 Log_OC
.d(TAG
, "Download service connected");
1228 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1229 if (mWaitingToPreview
!= null
)
1230 if (getStorageManager() != null
) {
1233 getStorageManager().getFileById(mWaitingToPreview
.getFileId());
1234 if (!mWaitingToPreview
.isDown()) {
1235 requestForDownload();
1239 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1240 FileUploader
.class))) {
1241 Log_OC
.d(TAG
, "Upload service connected");
1242 mUploaderBinder
= (FileUploaderBinder
) service
;
1246 // a new chance to get the mDownloadBinder through
1247 // getFileDownloadBinder() - THIS IS A MESS
1248 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1249 if (listOfFiles
!= null
) {
1250 listOfFiles
.listDirectory();
1251 // TODO Enable when "On Device" is recovered ?
1252 // listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1254 FileFragment secondFragment
= getSecondFragment();
1255 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1256 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1257 detailFragment
.listenForTransferProgress();
1258 detailFragment
.updateFileDetails(false
, false
);
1263 public void onServiceDisconnected(ComponentName component
) {
1264 if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1265 FileDownloader
.class))) {
1266 Log_OC
.d(TAG
, "Download service disconnected");
1267 mDownloaderBinder
= null
;
1268 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1269 FileUploader
.class))) {
1270 Log_OC
.d(TAG
, "Upload service disconnected");
1271 mUploaderBinder
= null
;
1277 public void onSavedCertificate() {
1278 startSyncFolderOperation(getCurrentDir(), false
);
1283 public void onFailedSavingCertificate() {
1284 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
1285 R
.string
.ssl_validator_not_saved
, new String
[]{}, R
.string
.common_ok
, -1, -1
1287 dialog
.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED
);
1291 public void onCancelCertificate() {
1296 * Updates the view associated to the activity after the finish of some operation over files
1297 * in the current account.
1299 * @param operation Removal operation performed.
1300 * @param result Result of the removal.
1303 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1304 super.onRemoteOperationFinish(operation
, result
);
1306 if (operation
instanceof RemoveFileOperation
) {
1307 onRemoveFileOperationFinish((RemoveFileOperation
) operation
, result
);
1309 } else if (operation
instanceof RenameFileOperation
) {
1310 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1312 } else if (operation
instanceof SynchronizeFileOperation
) {
1313 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1315 } else if (operation
instanceof CreateFolderOperation
) {
1316 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1318 } else if (operation
instanceof CreateShareOperation
) {
1319 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1321 } else if (operation
instanceof UnshareLinkOperation
) {
1322 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
1324 } else if (operation
instanceof MoveFileOperation
) {
1325 onMoveFileOperationFinish((MoveFileOperation
)operation
, result
);
1331 private void onCreateShareOperationFinish(CreateShareOperation operation
,
1332 RemoteOperationResult result
) {
1333 if (result
.isSuccess()) {
1334 refreshShowDetails();
1335 refreshListOfFilesFragment();
1340 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
1341 RemoteOperationResult result
) {
1342 if (result
.isSuccess()) {
1343 refreshShowDetails();
1344 refreshListOfFilesFragment();
1346 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
1347 cleanSecondFragment();
1348 refreshListOfFilesFragment();
1352 private void refreshShowDetails() {
1353 FileFragment details
= getSecondFragment();
1354 if (details
!= null
) {
1355 OCFile file
= details
.getFile();
1357 file
= getStorageManager().getFileByPath(file
.getRemotePath());
1358 if (details
instanceof PreviewMediaFragment
) {
1359 // Refresh OCFile of the fragment
1360 ((PreviewMediaFragment
) details
).updateFile(file
);
1365 invalidateOptionsMenu();
1370 * Updates the view associated to the activity after the finish of an operation trying to
1373 * @param operation Removal operation performed.
1374 * @param result Result of the removal.
1376 private void onRemoveFileOperationFinish(RemoveFileOperation operation
,
1377 RemoteOperationResult result
) {
1378 dismissLoadingDialog();
1380 Toast msg
= Toast
.makeText(this,
1381 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1385 if (result
.isSuccess()) {
1386 OCFile removedFile
= operation
.getFile();
1387 FileFragment second
= getSecondFragment();
1388 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1389 if (second
instanceof PreviewMediaFragment
) {
1390 ((PreviewMediaFragment
)second
).stopPreview(true
);
1392 setFile(getStorageManager().getFileById(removedFile
.getParentId()));
1393 cleanSecondFragment();
1395 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1396 refreshListOfFilesFragment();
1398 invalidateOptionsMenu();
1400 if (result
.isSslRecoverableException()) {
1401 mLastSslUntrustedServerResult
= result
;
1402 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1409 * Updates the view associated to the activity after the finish of an operation trying to move a
1412 * @param operation Move operation performed.
1413 * @param result Result of the move operation.
1415 private void onMoveFileOperationFinish(MoveFileOperation operation
,
1416 RemoteOperationResult result
) {
1417 if (result
.isSuccess()) {
1418 dismissLoadingDialog();
1419 refreshListOfFilesFragment();
1421 dismissLoadingDialog();
1423 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1424 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1428 } catch (NotFoundException e
) {
1429 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1436 * Updates the view associated to the activity after the finish of an operation trying to rename
1439 * @param operation Renaming operation performed.
1440 * @param result Result of the renaming.
1442 private void onRenameFileOperationFinish(RenameFileOperation operation
,
1443 RemoteOperationResult result
) {
1444 dismissLoadingDialog();
1445 OCFile renamedFile
= operation
.getFile();
1446 if (result
.isSuccess()) {
1447 FileFragment details
= getSecondFragment();
1448 if (details
!= null
) {
1449 if (details
instanceof FileDetailFragment
&&
1450 renamedFile
.equals(details
.getFile()) ) {
1451 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1452 showDetails(renamedFile
);
1454 } else if (details
instanceof PreviewMediaFragment
&&
1455 renamedFile
.equals(details
.getFile())) {
1456 ((PreviewMediaFragment
) details
).updateFile(renamedFile
);
1457 if (PreviewMediaFragment
.canBePreviewed(renamedFile
)) {
1458 int position
= ((PreviewMediaFragment
)details
).getPosition();
1459 startMediaPreview(renamedFile
, position
, true
);
1461 getFileOperationsHelper().openFile(renamedFile
);
1466 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())){
1467 refreshListOfFilesFragment();
1471 Toast msg
= Toast
.makeText(this,
1472 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1476 if (result
.isSslRecoverableException()) {
1477 mLastSslUntrustedServerResult
= result
;
1478 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1483 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
1484 RemoteOperationResult result
) {
1485 if (result
.isSuccess()) {
1486 if (operation
.transferWasRequested()) {
1487 OCFile syncedFile
= operation
.getLocalFile();
1488 onTransferStateChanged(syncedFile
, true
, true
);
1489 invalidateOptionsMenu();
1495 * Updates the view associated to the activity after the finish of an operation trying create a
1498 * @param operation Creation operation performed.
1499 * @param result Result of the creation.
1501 private void onCreateFolderOperationFinish(CreateFolderOperation operation
,
1502 RemoteOperationResult result
) {
1503 if (result
.isSuccess()) {
1504 dismissLoadingDialog();
1505 refreshListOfFilesFragment();
1507 dismissLoadingDialog();
1509 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1510 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1514 } catch (NotFoundException e
) {
1515 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1525 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1526 refreshListOfFilesFragment();
1527 FileFragment details
= getSecondFragment();
1528 if (details
!= null
&& details
instanceof FileDetailFragment
&&
1529 file
.equals(details
.getFile()) ) {
1530 if (downloading
|| uploading
) {
1531 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1533 if (!file
.fileExists()) {
1534 cleanSecondFragment();
1536 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1544 private void requestForDownload() {
1545 Account account
= getAccount();
1546 //if (!mWaitingToPreview.isDownloading()) {
1547 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1548 Intent i
= new Intent(this, FileDownloader
.class);
1549 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1550 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1556 private OCFile
getCurrentDir() {
1557 OCFile file
= getFile();
1559 if (file
.isFolder()) {
1561 } else if (getStorageManager() != null
) {
1562 String parentPath
= file
.getRemotePath().substring(0,
1563 file
.getRemotePath().lastIndexOf(file
.getFileName()));
1564 return getStorageManager().getFileByPath(parentPath
);
1570 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
1571 long currentSyncTime
= System
.currentTimeMillis();
1573 mSyncInProgress
= true
;
1575 // perform folder synchronization
1576 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
1579 getFileOperationsHelper().isSharedSupported(),
1581 getStorageManager(),
1583 getApplicationContext()
1585 synchFolderOp
.execute(getAccount(), MainApp
.getAppContext(), this, null
, null
);
1587 setSupportProgressBarIndeterminateVisibility(true
);
1589 setBackgroundText();
1593 * Show untrusted cert dialog
1595 public void showUntrustedCertDialog(RemoteOperationResult result
) {
1596 // Show a dialog with the certificate info
1597 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.newInstanceForFullSslError(
1598 (CertificateCombinedException
) result
.getException());
1599 FragmentManager fm
= getSupportFragmentManager();
1600 FragmentTransaction ft
= fm
.beginTransaction();
1601 dialog
.show(ft
, DIALOG_UNTRUSTED_CERT
);
1604 private void requestForDownload(OCFile file
) {
1605 Account account
= getAccount();
1606 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1607 Intent i
= new Intent(this, FileDownloader
.class);
1608 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1609 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
1614 private void sendDownloadedFile(){
1615 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend
);
1616 mWaitingToSend
= null
;
1621 * Requests the download of the received {@link OCFile} , updates the UI
1622 * to monitor the download progress and prepares the activity to send the file
1623 * when the download finishes.
1625 * @param file {@link OCFile} to download and preview.
1627 public void startDownloadForSending(OCFile file
) {
1628 mWaitingToSend
= file
;
1629 requestForDownload(mWaitingToSend
);
1630 boolean hasSecondFragment
= (getSecondFragment()!= null
);
1631 updateFragmentsVisibility(hasSecondFragment
);
1635 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1637 * @param file Image {@link OCFile} to show.
1639 public void startImagePreview(OCFile file
) {
1640 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1641 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1642 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1643 startActivity(showDetailsIntent
);
1648 * Stars the preview of an already down media {@link OCFile}.
1650 * @param file Media {@link OCFile} to preview.
1651 * @param startPlaybackPosition Media position where the playback will be started,
1653 * @param autoplay When 'true', the playback will start without user
1656 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1657 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
,
1659 setSecondFragment(mediaFragment
);
1660 updateFragmentsVisibility(true
);
1661 updateActionBarTitleAndHomeButton(file
);
1666 * Requests the download of the received {@link OCFile} , updates the UI
1667 * to monitor the download progress and prepares the activity to preview
1668 * or open the file when the download finishes.
1670 * @param file {@link OCFile} to download and preview.
1672 public void startDownloadForPreview(OCFile file
) {
1673 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1674 setSecondFragment(detailFragment
);
1675 mWaitingToPreview
= file
;
1676 requestForDownload();
1677 updateFragmentsVisibility(true
);
1678 updateActionBarTitleAndHomeButton(file
);
1683 public void cancelTransference(OCFile file
) {
1684 getFileOperationsHelper().cancelTransference(file
);
1685 if (mWaitingToPreview
!= null
&&
1686 mWaitingToPreview
.getRemotePath().equals(file
.getRemotePath())) {
1687 mWaitingToPreview
= null
;
1689 if (mWaitingToSend
!= null
&&
1690 mWaitingToSend
.getRemotePath().equals(file
.getRemotePath())) {
1691 mWaitingToSend
= null
;
1693 onTransferStateChanged(file
, false
, false
);
1697 public void onRefresh(boolean ignoreETag
) {
1698 refreshList(ignoreETag
);
1702 public void onRefresh() {
1706 private void refreshList(boolean ignoreETag
) {
1707 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1708 if (listOfFiles
!= null
) {
1709 OCFile folder
= listOfFiles
.getCurrentFile();
1710 if (folder
!= null
) {
1711 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1712 listDirectory(mFile);*/
1713 startSyncFolderOperation(folder
, ignoreETag
);
1718 private void sortByDate(boolean ascending
){
1719 getListOfFilesFragment().sortByDate(ascending
);
1722 private void sortBySize(boolean ascending
){
1723 getListOfFilesFragment().sortBySize(ascending
);
1726 private void sortByName(boolean ascending
){
1727 getListOfFilesFragment().sortByName(ascending
);
1729 private boolean isGridView(){ return getListOfFilesFragment().isGridView(); }
1730 private void switchToGridView() {
1731 getListOfFilesFragment().switchToGridView();
1733 private void switchToListView() {
1734 getListOfFilesFragment().switchToListView();
1737 public void allFilesOption() {