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
.support
.v7
.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
.content
.ContextCompat
;
52 import android
.support
.v4
.view
.GravityCompat
;
53 import android
.view
.Menu
;
54 import android
.view
.MenuInflater
;
55 import android
.view
.MenuItem
;
56 import android
.view
.View
;
57 import android
.widget
.ProgressBar
;
58 import android
.widget
.RelativeLayout
;
59 import android
.widget
.TextView
;
60 import android
.widget
.Toast
;
62 import com
.owncloud
.android
.MainApp
;
63 import com
.owncloud
.android
.R
;
64 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
65 import com
.owncloud
.android
.datamodel
.OCFile
;
66 import com
.owncloud
.android
.files
.services
.FileDownloader
;
67 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
68 import com
.owncloud
.android
.files
.services
.FileUploader
;
69 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
70 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
71 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
72 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
73 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
74 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
75 import com
.owncloud
.android
.lib
.common
.network
.CertificateCombinedException
;
76 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
77 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
78 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
79 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
80 import com
.owncloud
.android
.operations
.CopyFileOperation
;
81 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
82 import com
.owncloud
.android
.operations
.CreateShareOperation
;
83 import com
.owncloud
.android
.operations
.MoveFileOperation
;
84 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
85 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
86 import com
.owncloud
.android
.operations
.RenameFileOperation
;
87 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
88 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
89 import com
.owncloud
.android
.services
.observer
.FileObserverService
;
90 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
91 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
92 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
93 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
;
94 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
.OnSslUntrustedCertListener
;
95 import com
.owncloud
.android
.ui
.dialog
.UploadSourceDialogFragment
;
96 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
97 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
98 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
99 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
100 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
101 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
102 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
103 import com
.owncloud
.android
.utils
.DisplayUtils
;
104 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
105 import com
.owncloud
.android
.utils
.FileStorageUtils
;
106 import com
.owncloud
.android
.utils
.UriUtils
;
112 * Displays, what files the user has available in his ownCloud.
115 public class FileDisplayActivity
extends HookActivity
116 implements FileFragment
.ContainerActivity
,
117 OnSslUntrustedCertListener
, OnEnforceableRefreshListener
{
119 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
120 private UploadFinishReceiver mUploadFinishReceiver
;
121 private DownloadFinishReceiver mDownloadFinishReceiver
;
122 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
124 private boolean mDualPane
;
125 private View mLeftFragmentContainer
;
126 private View mRightFragmentContainer
;
127 private ProgressBar mProgressBar
;
129 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
130 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
131 private static final String KEY_WAITING_TO_SEND
= "WAITING_TO_SEND";
133 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
135 public static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
136 public static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
137 public static final int ACTION_MOVE_FILES
= 3;
138 public static final int ACTION_COPY_FILES
= 4;
140 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
142 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
143 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
145 private OCFile mWaitingToPreview
;
147 private boolean mSyncInProgress
= false
;
149 private static String DIALOG_UNTRUSTED_CERT
= "DIALOG_UNTRUSTED_CERT";
150 private static String DIALOG_CREATE_FOLDER
= "DIALOG_CREATE_FOLDER";
151 private static String DIALOG_UPLOAD_SOURCE
= "DIALOG_UPLOAD_SOURCE";
152 private static String DIALOG_CERT_NOT_SAVED
= "DIALOG_CERT_NOT_SAVED";
154 private OCFile mWaitingToSend
;
158 protected void onCreate(Bundle savedInstanceState
) {
159 Log_OC
.v(TAG
, "onCreate() start");
161 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account
164 /// grant that FileObserverService is watching favorite files
165 if (savedInstanceState
== null
) {
166 Intent initObserversIntent
= FileObserverService
.makeInitIntent(this);
167 startService(initObserversIntent
);
170 /// Load of saved instance state
171 if(savedInstanceState
!= null
) {
172 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(
173 FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
174 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
175 mWaitingToSend
= (OCFile
) savedInstanceState
.getParcelable(
176 FileDisplayActivity
.KEY_WAITING_TO_SEND
);
179 mWaitingToPreview
= null
;
180 mSyncInProgress
= false
;
181 mWaitingToSend
= null
;
186 // Inflate and set the layout view
187 setContentView(R
.layout
.files
);
192 mProgressBar
= (ProgressBar
) findViewById(R
.id
.progressBar
);
193 mProgressBar
.setIndeterminateDrawable(
194 ContextCompat
.getDrawable(this,
195 R
.drawable
.actionbar_progress_indeterminate_horizontal
));
197 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
198 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
199 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
200 if (savedInstanceState
== null
) {
201 createMinFragments();
205 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS,
206 // according to the official
209 // enable ActionBar app icon to behave as action to toggle nav drawer
210 //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
211 getSupportActionBar().setHomeButtonEnabled(true
);
213 mProgressBar
.setIndeterminate(mSyncInProgress
);
214 // always AFTER setContentView(...) ; to work around bug in its implementation
218 Log_OC
.v(TAG
, "onCreate() end");
222 protected void onStart() {
223 Log_OC
.v(TAG
, "onStart() start");
225 Log_OC
.v(TAG
, "onStart() end");
229 protected void onDestroy() {
230 Log_OC
.v(TAG
, "onDestroy() start");
232 Log_OC
.v(TAG
, "onDestroy() end");
236 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
239 protected void onAccountSet(boolean stateWasRecovered
) {
240 super.onAccountSet(stateWasRecovered
);
241 if (getAccount() != null
) {
242 /// Check whether the 'main' OCFile handled by the Activity is contained in the
244 OCFile file
= getFile();
245 // get parent from path
246 String parentPath
= "";
248 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
249 // upload in progress - right now, files are not inserted in the local
250 // cache until the upload is successful get parent from path
251 parentPath
= file
.getRemotePath().substring(0,
252 file
.getRemotePath().lastIndexOf(file
.getFileName()));
253 if (getStorageManager().getFileByPath(parentPath
) == null
)
254 file
= null
; // not able to know the directory where the file is uploading
256 file
= getStorageManager().getFileByPath(file
.getRemotePath());
257 // currentDir = null if not in the current Account
261 // fall back to root folder
262 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
266 if (mAccountWasSet
) {
267 setUsernameInDrawer((RelativeLayout
) findViewById(R
.id
.left_drawer
), getAccount());
270 if (!stateWasRecovered
) {
271 Log_OC
.d(TAG
, "Initializing Fragments in onAccountChanged..");
272 initFragmentsWithFile();
273 if (file
.isFolder()) {
274 startSyncFolderOperation(file
, false
);
278 updateFragmentsVisibility(!file
.isFolder());
279 updateActionBarTitleAndHomeButton(file
.isFolder() ? null
: file
);
284 private void createMinFragments() {
285 OCFileListFragment listOfFiles
= new OCFileListFragment();
286 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
287 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
288 transaction
.commit();
291 private void initFragmentsWithFile() {
292 if (getAccount() != null
&& getFile() != null
) {
294 OCFileListFragment listOfFiles
= getListOfFilesFragment();
295 if (listOfFiles
!= null
) {
296 listOfFiles
.listDirectory(getCurrentDir());
297 // TODO Enable when "On Device" is recovered
298 // listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
300 Log_OC
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
304 OCFile file
= getFile();
305 Fragment secondFragment
= chooseInitialSecondFragment(file
);
306 if (secondFragment
!= null
) {
307 setSecondFragment(secondFragment
);
308 updateFragmentsVisibility(true
);
309 updateActionBarTitleAndHomeButton(file
);
312 cleanSecondFragment();
316 Log_OC
.wtf(TAG
, "initFragments() called with invalid NULLs!");
317 if (getAccount() == null
) {
318 Log_OC
.wtf(TAG
, "\t account is NULL");
320 if (getFile() == null
) {
321 Log_OC
.wtf(TAG
, "\t file is NULL");
326 private Fragment
chooseInitialSecondFragment(OCFile file
) {
327 Fragment secondFragment
= null
;
328 if (file
!= null
&& !file
.isFolder()) {
329 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
330 && file
.getLastSyncDateForProperties() > 0 // temporal fix
332 int startPlaybackPosition
=
333 getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
335 getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
336 secondFragment
= new PreviewMediaFragment(file
, getAccount(),
337 startPlaybackPosition
, autoplay
);
340 secondFragment
= FileDetailFragment
.newInstance(file
, getAccount());
343 return secondFragment
;
348 * Replaces the second fragment managed by the activity with the received as
351 * Assumes never will be more than two fragments managed at the same time.
353 * @param fragment New second Fragment to set.
355 private void setSecondFragment(Fragment fragment
) {
356 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
357 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
358 transaction
.commit();
362 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
364 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
365 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
367 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
368 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
371 } else if (existsSecondFragment
) {
372 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
373 mLeftFragmentContainer
.setVisibility(View
.GONE
);
375 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
376 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
380 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
381 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
383 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
384 mRightFragmentContainer
.setVisibility(View
.GONE
);
390 private OCFileListFragment
getListOfFilesFragment() {
391 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(
392 FileDisplayActivity
.TAG_LIST_OF_FILES
);
393 if (listOfFiles
!= null
) {
394 return (OCFileListFragment
) listOfFiles
;
396 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
400 public FileFragment
getSecondFragment() {
401 Fragment second
= getSupportFragmentManager().findFragmentByTag(
402 FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
403 if (second
!= null
) {
404 return (FileFragment
) second
;
409 protected void cleanSecondFragment() {
410 Fragment second
= getSecondFragment();
411 if (second
!= null
) {
412 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
416 updateFragmentsVisibility(false
);
417 updateActionBarTitleAndHomeButton(null
);
420 protected void refreshListOfFilesFragment() {
421 OCFileListFragment fileListFragment
= getListOfFilesFragment();
422 if (fileListFragment
!= null
) {
423 fileListFragment
.listDirectory();
424 // TODO Enable when "On Device" is recovered ?
425 // fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
429 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
,
431 FileFragment secondFragment
= getSecondFragment();
432 boolean waitedPreview
= (mWaitingToPreview
!= null
&&
433 mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
434 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
435 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
436 OCFile fileInFragment
= detailsFragment
.getFile();
437 if (fileInFragment
!= null
&&
438 !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
439 // the user browsed to other file ; forget the automatic preview
440 mWaitingToPreview
= null
;
442 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
443 // grant that the right panel updates the progress bar
444 detailsFragment
.listenForTransferProgress();
445 detailsFragment
.updateFileDetails(true
, false
);
447 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
448 // update the right panel
449 boolean detailsFragmentChanged
= false
;
452 mWaitingToPreview
= getStorageManager().getFileById(
453 mWaitingToPreview
.getFileId()); // update the file from database,
454 // for the local storage path
455 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
456 startMediaPreview(mWaitingToPreview
, 0, true
);
457 detailsFragmentChanged
= true
;
459 getFileOperationsHelper().openFile(mWaitingToPreview
);
462 mWaitingToPreview
= null
;
464 if (!detailsFragmentChanged
) {
465 detailsFragment
.updateFileDetails(false
, (success
));
472 public boolean onPrepareOptionsMenu(Menu menu
) {
473 boolean drawerOpen
= mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
474 menu
.findItem(R
.id
.action_upload
).setVisible(!drawerOpen
);
475 menu
.findItem(R
.id
.action_create_dir
).setVisible(!drawerOpen
);
476 menu
.findItem(R
.id
.action_sort
).setVisible(!drawerOpen
);
477 menu
.findItem(R
.id
.action_sync_account
).setVisible(!drawerOpen
);
479 return super.onPrepareOptionsMenu(menu
);
483 public boolean onCreateOptionsMenu(Menu menu
) {
484 MenuInflater inflater
= getMenuInflater();
485 inflater
.inflate(R
.menu
.main_menu
, menu
);
491 public boolean onOptionsItemSelected(MenuItem item
) {
492 boolean retval
= true
;
493 switch (item
.getItemId()) {
494 case R
.id
.action_create_dir
: {
495 CreateFolderDialogFragment dialog
=
496 CreateFolderDialogFragment
.newInstance(getCurrentDir());
497 dialog
.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER
);
500 case R
.id
.action_sync_account
: {
501 startSynchronization();
504 case R
.id
.action_upload
: {
505 UploadSourceDialogFragment dialog
=
506 UploadSourceDialogFragment
.newInstance(getAccount());
507 dialog
.show(getSupportFragmentManager(), DIALOG_UPLOAD_SOURCE
);
511 case android
.R
.id
.home
: {
512 FileFragment second
= getSecondFragment();
513 OCFile currentDir
= getCurrentDir();
514 if (mDrawerLayout
.isDrawerOpen(GravityCompat
.START
)) {
515 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
516 } else if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
517 (second
!= null
&& second
.getFile() != null
)) {
521 mDrawerLayout
.openDrawer(GravityCompat
.START
);
525 case R
.id
.action_sort
: {
526 SharedPreferences appPreferences
= PreferenceManager
527 .getDefaultSharedPreferences(this);
529 // Read sorting order, default to sort by name ascending
530 Integer sortOrder
= appPreferences
531 .getInt("sortOrder", FileStorageUtils
.SORT_NAME
);
533 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
534 builder
.setTitle(R
.string
.actionbar_sort_title
)
535 .setSingleChoiceItems(R
.array
.actionbar_sortby
, sortOrder
,
536 new DialogInterface
.OnClickListener() {
537 public void onClick(DialogInterface dialog
, int which
) {
550 builder
.create().show();
554 retval
= super.onOptionsItemSelected(item
);
559 private void startSynchronization() {
560 Log_OC
.d(TAG
, "Got to start sync");
561 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
562 Log_OC
.d(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
563 ContentResolver
.cancelSync(null
, MainApp
.getAuthority());
564 // cancel the current synchronizations of any ownCloud account
565 Bundle bundle
= new Bundle();
566 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
567 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
568 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
569 MainApp
.getAuthority());
570 ContentResolver
.requestSync(
572 MainApp
.getAuthority(), bundle
);
574 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
575 MainApp
.getAuthority() + " with new API");
576 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
577 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
578 builder
.setExpedited(true
);
579 builder
.setManual(true
);
582 // Fix bug in Android Lollipop when you click on refresh the whole account
583 Bundle extras
= new Bundle();
584 builder
.setExtras(extras
);
586 SyncRequest request
= builder
.build();
587 ContentResolver
.requestSync(request
);
592 * Called, when the user selected something for uploading
595 @TargetApi(Build
.VERSION_CODES
.JELLY_BEAN
)
597 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
599 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
||
600 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
601 //getClipData is only supported on api level 16+, Jelly Bean
602 if (data
.getData() == null
&& Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
){
603 for( int i
= 0; i
< data
.getClipData().getItemCount(); i
++){
604 Intent intent
= new Intent();
605 intent
.setData(data
.getClipData().getItemAt(i
).getUri());
606 requestSimpleUpload(intent
, resultCode
);
609 requestSimpleUpload(data
, resultCode
);
611 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
||
612 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
613 requestMultipleUpload(data
, resultCode
);
615 } else if (requestCode
== ACTION_MOVE_FILES
&& resultCode
== RESULT_OK
){
616 final Intent fData
= data
;
617 final int fResultCode
= resultCode
;
618 getHandler().postDelayed(
622 requestMoveOperation(fData
, fResultCode
);
625 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
628 } else if (requestCode
== ACTION_COPY_FILES
&& resultCode
== RESULT_OK
) {
630 final Intent fData
= data
;
631 final int fResultCode
= resultCode
;
632 getHandler().postDelayed(
636 requestCopyOperation(fData
, fResultCode
);
639 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
643 super.onActivityResult(requestCode
, resultCode
, data
);
648 private void requestMultipleUpload(Intent data
, int resultCode
) {
649 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
650 if (filePaths
!= null
) {
651 String
[] remotePaths
= new String
[filePaths
.length
];
652 String remotePathBase
= getCurrentDir().getRemotePath();
653 for (int j
= 0; j
< remotePaths
.length
; j
++) {
654 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
657 Intent i
= new Intent(this, FileUploader
.class);
658 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
659 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
660 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
661 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
662 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
663 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
667 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
668 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
),
676 private void requestSimpleUpload(Intent data
, int resultCode
) {
677 String filePath
= null
;
678 String mimeType
= null
;
680 Uri selectedImageUri
= data
.getData();
683 mimeType
= getContentResolver().getType(selectedImageUri
);
685 String fileManagerString
= selectedImageUri
.getPath();
686 String selectedImagePath
= UriUtils
.getLocalPath(selectedImageUri
, this);
688 if (selectedImagePath
!= null
)
689 filePath
= selectedImagePath
;
691 filePath
= fileManagerString
;
693 } catch (Exception e
) {
694 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of " +
695 "Intent.ACTION_GET_CONTENT", e
);
698 if (filePath
== null
) {
699 Log_OC
.e(TAG
, "Couldn't resolve path to file");
700 Toast t
= Toast
.makeText(
701 this, getString(R
.string
.filedisplay_unexpected_bad_get_content
),
709 Intent i
= new Intent(this, FileUploader
.class);
710 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
711 OCFile currentDir
= getCurrentDir();
712 String remotePath
= (currentDir
!= null
) ? currentDir
.getRemotePath() : OCFile
.ROOT_PATH
;
714 if (filePath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
)) {
715 Cursor cursor
= getContentResolver().query(Uri
.parse(filePath
), null
, null
, null
, null
);
717 if (cursor
!= null
&& cursor
.moveToFirst()) {
718 String displayName
= cursor
.getString(cursor
.getColumnIndex(
719 OpenableColumns
.DISPLAY_NAME
));
720 Log_OC
.v(TAG
, "Display Name: " + displayName
);
722 displayName
.replace(File
.separatorChar
, '_');
723 displayName
.replace(File
.pathSeparatorChar
, '_');
724 remotePath
+= displayName
+ DisplayUtils
.getComposedFileExtension(filePath
);
727 // and what happens in case of error?; wrong target name for the upload
733 remotePath
+= new File(filePath
).getName();
736 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePath
);
737 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePath
);
738 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mimeType
);
739 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
740 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
741 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
746 * Request the operation for moving the file/folder from one path to another
748 * @param data Intent received
749 * @param resultCode Result code received
751 private void requestMoveOperation(Intent data
, int resultCode
) {
752 OCFile folderToMoveAt
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FOLDER
);
753 OCFile targetFile
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
754 getFileOperationsHelper().moveFile(folderToMoveAt
, targetFile
);
758 * Request the operation for copying the file/folder from one path to another
760 * @param data Intent received
761 * @param resultCode Result code received
763 private void requestCopyOperation(Intent data
, int resultCode
) {
764 OCFile folderToMoveAt
= data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FOLDER
);
765 OCFile targetFile
= data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
766 getFileOperationsHelper().copyFile(folderToMoveAt
, targetFile
);
770 public void onBackPressed() {
771 if (!isDrawerOpen()){
772 OCFileListFragment listOfFiles
= getListOfFilesFragment();
773 if (mDualPane
|| getSecondFragment() == null
) {
774 OCFile currentDir
= getCurrentDir();
775 if (currentDir
== null
|| currentDir
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
) {
779 if (listOfFiles
!= null
) { // should never be null, indeed
780 listOfFiles
.onBrowseUp();
783 if (listOfFiles
!= null
) { // should never be null, indeed
784 setFile(listOfFiles
.getCurrentFile());
786 cleanSecondFragment();
788 super.onBackPressed();
793 protected void onSaveInstanceState(Bundle outState
) {
794 // responsibility of restore is preferred in onCreate() before than in
795 // onRestoreInstanceState when there are Fragments involved
796 Log_OC
.v(TAG
, "onSaveInstanceState() start");
797 super.onSaveInstanceState(outState
);
798 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
799 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
800 //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
801 // mRefreshSharesInProgress);
802 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_SEND
, mWaitingToSend
);
804 Log_OC
.v(TAG
, "onSaveInstanceState() end");
809 protected void onResume() {
810 Log_OC
.v(TAG
, "onResume() start");
813 // refresh Navigation Drawer account list
814 mNavigationDrawerAdapter
.updateAccountList();
817 // refresh list of files
818 refreshListOfFilesFragment();
820 // Listen for sync messages
821 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
822 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
823 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
824 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
825 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
826 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
827 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
828 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
829 // syncIntentFilter);
831 // Listen for upload messages
832 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
833 mUploadFinishReceiver
= new UploadFinishReceiver();
834 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
836 // Listen for download messages
837 IntentFilter downloadIntentFilter
= new IntentFilter(
838 FileDownloader
.getDownloadAddedMessage());
839 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
840 mDownloadFinishReceiver
= new DownloadFinishReceiver();
841 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
843 Log_OC
.v(TAG
, "onResume() end");
848 protected void onPause() {
849 Log_OC
.v(TAG
, "onPause() start");
850 if (mSyncBroadcastReceiver
!= null
) {
851 unregisterReceiver(mSyncBroadcastReceiver
);
852 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
853 mSyncBroadcastReceiver
= null
;
855 if (mUploadFinishReceiver
!= null
) {
856 unregisterReceiver(mUploadFinishReceiver
);
857 mUploadFinishReceiver
= null
;
859 if (mDownloadFinishReceiver
!= null
) {
860 unregisterReceiver(mDownloadFinishReceiver
);
861 mDownloadFinishReceiver
= null
;
865 Log_OC
.v(TAG
, "onPause() end");
869 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
872 * {@link BroadcastReceiver} to enable syncing feedback in UI
875 public void onReceive(Context context
, Intent intent
) {
877 String event
= intent
.getAction();
878 Log_OC
.d(TAG
, "Received broadcast " + event
);
879 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
880 String synchFolderRemotePath
=
881 intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
882 RemoteOperationResult synchResult
=
883 (RemoteOperationResult
)intent
.getSerializableExtra(
884 FileSyncAdapter
.EXTRA_RESULT
);
885 boolean sameAccount
= (getAccount() != null
&&
886 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
890 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
891 mSyncInProgress
= true
;
894 OCFile currentFile
= (getFile() == null
) ? null
:
895 getStorageManager().getFileByPath(getFile().getRemotePath());
896 OCFile currentDir
= (getCurrentDir() == null
) ? null
:
897 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
899 if (currentDir
== null
) {
900 // current folder was removed from the server
901 Toast
.makeText( FileDisplayActivity
.this,
904 sync_current_folder_was_removed
),
905 synchFolderRemotePath
),
911 if (currentFile
== null
&& !getFile().isFolder()) {
912 // currently selected file was removed in the server, and now we
914 cleanSecondFragment();
915 currentFile
= currentDir
;
918 if (synchFolderRemotePath
!= null
&&
919 currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
920 OCFileListFragment fileListFragment
= getListOfFilesFragment();
921 if (fileListFragment
!= null
) {
922 fileListFragment
.listDirectory();
923 // TODO Enable when "On Device" is recovered ?
924 // fileListFragment.listDirectory(currentDir,
925 // MainApp.getOnlyOnDevice());
928 setFile(currentFile
);
931 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
932 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
935 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
937 /// TODO refactor and make common
938 synchResult
!= null
&& !synchResult
.isSuccess() &&
939 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
940 synchResult
.isIdPRedirection() ||
941 (synchResult
.isException() && synchResult
.getException()
942 instanceof AuthenticatorException
))) {
946 OwnCloudClient client
;
947 OwnCloudAccount ocAccount
=
948 new OwnCloudAccount(getAccount(), context
);
949 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
950 removeClientFor(ocAccount
));
952 if (client
!= null
) {
953 OwnCloudCredentials cred
= client
.getCredentials();
955 AccountManager am
= AccountManager
.get(context
);
956 if (cred
.authTokenExpires()) {
957 am
.invalidateAuthToken(
962 am
.clearPassword(getAccount());
966 requestCredentialsUpdate();
968 } catch (AccountNotFoundException e
) {
969 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
974 removeStickyBroadcast(intent
);
975 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
976 mProgressBar
.setIndeterminate(mSyncInProgress
);
977 //mProgressBar.setVisibility((mSyncInProgress) ? View.VISIBLE : View.INVISIBLE);
978 //setSupportProgressBarIndeterminateVisibility(mSyncInProgress
979 /*|| mRefreshSharesInProgress*/ //);
985 if (synchResult
!= null
) {
986 if (synchResult
.getCode().equals(
987 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
988 mLastSslUntrustedServerResult
= synchResult
;
991 } catch (RuntimeException e
) {
992 // avoid app crashes after changing the serial id of RemoteOperationResult
993 // in owncloud library with broadcast notifications pending to process
994 removeStickyBroadcast(intent
);
1000 * Show a text message on screen view for notifying user if content is
1001 * loading or folder is empty
1003 private void setBackgroundText() {
1004 OCFileListFragment ocFileListFragment
= getListOfFilesFragment();
1005 if (ocFileListFragment
!= null
) {
1006 int message
= R
.string
.file_list_loading
;
1007 if (!mSyncInProgress
) {
1008 // In case file list is empty
1009 message
= R
.string
.file_list_empty
;
1011 ocFileListFragment
.setMessageForEmptyList(getString(message
));
1013 Log_OC
.e(TAG
, "OCFileListFragment is null");
1018 * Once the file upload has finished -> update view
1020 private class UploadFinishReceiver
extends BroadcastReceiver
{
1022 * Once the file upload has finished -> update view
1024 * @author David A. Velasco
1025 * {@link BroadcastReceiver} to enable upload feedback in UI
1028 public void onReceive(Context context
, Intent intent
) {
1030 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1031 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
1032 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
1033 OCFile currentDir
= getCurrentDir();
1034 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) &&
1035 (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1037 if (sameAccount
&& isDescendant
) {
1038 refreshListOfFilesFragment();
1041 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
,
1043 boolean renamedInUpload
= getFile().getRemotePath().
1044 equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
1045 boolean sameFile
= getFile().getRemotePath().equals(uploadedRemotePath
) ||
1047 FileFragment details
= getSecondFragment();
1048 boolean detailFragmentIsShown
= (details
!= null
&&
1049 details
instanceof FileDetailFragment
);
1051 if (sameAccount
&& sameFile
&& detailFragmentIsShown
) {
1052 if (uploadWasFine
) {
1053 setFile(getStorageManager().getFileByPath(uploadedRemotePath
));
1055 if (renamedInUpload
) {
1056 String newName
= (new File(uploadedRemotePath
)).getName();
1057 Toast msg
= Toast
.makeText(
1060 getString(R
.string
.filedetails_renamed_in_upload_msg
),
1065 if (uploadWasFine
|| getFile().fileExists()) {
1066 ((FileDetailFragment
) details
).updateFileDetails(false
, true
);
1068 cleanSecondFragment();
1071 // Force the preview if the file is an image
1072 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
1073 startImagePreview(getFile());
1074 } // TODO what about other kind of previews?
1077 mProgressBar
.setIndeterminate(false
);
1079 if (intent
!= null
) {
1080 removeStickyBroadcast(intent
);
1090 * Class waiting for broadcast events from the {@link FileDownloader} service.
1092 * Updates the UI when a download is started or finished, provided that it is relevant for the
1095 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1097 //int refreshCounter = 0;
1099 public void onReceive(Context context
, Intent intent
) {
1101 boolean sameAccount
= isSameAccount(context
, intent
);
1102 String downloadedRemotePath
=
1103 intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1104 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1106 if (sameAccount
&& isDescendant
) {
1107 String linkedToRemotePath
=
1108 intent
.getStringExtra(FileDownloader
.EXTRA_LINKED_TO_PATH
);
1109 if (linkedToRemotePath
== null
|| isAscendant(linkedToRemotePath
)) {
1110 //Log_OC.v(TAG, "refresh #" + ++refreshCounter);
1111 refreshListOfFilesFragment();
1113 refreshSecondFragment(
1115 downloadedRemotePath
,
1116 intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)
1120 if (mWaitingToSend
!= null
) {
1122 getStorageManager().getFileByPath(mWaitingToSend
.getRemotePath());
1123 if (mWaitingToSend
.isDown()) {
1124 sendDownloadedFile();
1129 if (intent
!= null
) {
1130 removeStickyBroadcast(intent
);
1135 private boolean isDescendant(String downloadedRemotePath
) {
1136 OCFile currentDir
= getCurrentDir();
1138 currentDir
!= null
&&
1139 downloadedRemotePath
!= null
&&
1140 downloadedRemotePath
.startsWith(currentDir
.getRemotePath())
1144 private boolean isAscendant(String linkedToRemotePath
) {
1145 OCFile currentDir
= getCurrentDir();
1147 currentDir
!= null
&&
1148 currentDir
.getRemotePath().startsWith(linkedToRemotePath
)
1152 private boolean isSameAccount(Context context
, Intent intent
) {
1153 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1154 return (accountName
!= null
&& getAccount() != null
&&
1155 accountName
.equals(getAccount().name
));
1160 public void browseToRoot() {
1161 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1162 if (listOfFiles
!= null
) { // should never be null, indeed
1163 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
1164 listOfFiles
.listDirectory(root
);
1165 // TODO Enable when "On Device" is recovered ?
1166 // listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
1167 setFile(listOfFiles
.getCurrentFile());
1168 startSyncFolderOperation(root
, false
);
1170 cleanSecondFragment();
1178 * Updates action bar and second fragment, if in dual pane mode.
1181 public void onBrowsedDownTo(OCFile directory
) {
1183 cleanSecondFragment();
1185 startSyncFolderOperation(directory
, false
);
1189 * Shows the information of the {@link OCFile} received as a
1190 * parameter in the second fragment.
1192 * @param file {@link OCFile} whose details will be shown
1195 public void showDetails(OCFile file
) {
1196 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1197 setSecondFragment(detailFragment
);
1198 updateFragmentsVisibility(true
);
1199 updateActionBarTitleAndHomeButton(file
);
1204 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
1206 // in dual pane mode, keep the focus of title an action bar in the current folder
1207 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1210 super.updateActionBarTitleAndHomeButton(chosenFile
);
1216 protected ServiceConnection
newTransferenceServiceConnection() {
1217 return new ListServiceConnection();
1221 * Defines callbacks for service binding, passed to bindService()
1223 private class ListServiceConnection
implements ServiceConnection
{
1226 public void onServiceConnected(ComponentName component
, IBinder service
) {
1227 if (component
.equals(new ComponentName(
1228 FileDisplayActivity
.this, FileDownloader
.class))) {
1229 Log_OC
.d(TAG
, "Download service connected");
1230 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1231 if (mWaitingToPreview
!= null
)
1232 if (getStorageManager() != null
) {
1235 getStorageManager().getFileById(mWaitingToPreview
.getFileId());
1236 if (!mWaitingToPreview
.isDown()) {
1237 requestForDownload();
1241 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1242 FileUploader
.class))) {
1243 Log_OC
.d(TAG
, "Upload service connected");
1244 mUploaderBinder
= (FileUploaderBinder
) service
;
1248 // a new chance to get the mDownloadBinder through
1249 // getFileDownloadBinder() - THIS IS A MESS
1250 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1251 if (listOfFiles
!= null
) {
1252 listOfFiles
.listDirectory();
1253 // TODO Enable when "On Device" is recovered ?
1254 // listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1256 FileFragment secondFragment
= getSecondFragment();
1257 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1258 FileDetailFragment detailFragment
= (FileDetailFragment
) secondFragment
;
1259 detailFragment
.listenForTransferProgress();
1260 detailFragment
.updateFileDetails(false
, false
);
1265 public void onServiceDisconnected(ComponentName component
) {
1266 if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1267 FileDownloader
.class))) {
1268 Log_OC
.d(TAG
, "Download service disconnected");
1269 mDownloaderBinder
= null
;
1270 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1271 FileUploader
.class))) {
1272 Log_OC
.d(TAG
, "Upload service disconnected");
1273 mUploaderBinder
= null
;
1279 public void onSavedCertificate() {
1280 startSyncFolderOperation(getCurrentDir(), false
);
1285 public void onFailedSavingCertificate() {
1286 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
1287 R
.string
.ssl_validator_not_saved
, new String
[]{}, R
.string
.common_ok
, -1, -1
1289 dialog
.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED
);
1293 public void onCancelCertificate() {
1298 * Updates the view associated to the activity after the finish of some operation over files
1299 * in the current account.
1301 * @param operation Removal operation performed.
1302 * @param result Result of the removal.
1305 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1306 super.onRemoteOperationFinish(operation
, result
);
1308 if (operation
instanceof RemoveFileOperation
) {
1309 onRemoveFileOperationFinish((RemoveFileOperation
) operation
, result
);
1311 } else if (operation
instanceof RenameFileOperation
) {
1312 onRenameFileOperationFinish((RenameFileOperation
) operation
, result
);
1314 } else if (operation
instanceof SynchronizeFileOperation
) {
1315 onSynchronizeFileOperationFinish((SynchronizeFileOperation
) operation
, result
);
1317 } else if (operation
instanceof CreateFolderOperation
) {
1318 onCreateFolderOperationFinish((CreateFolderOperation
) operation
, result
);
1320 } else if (operation
instanceof CreateShareOperation
) {
1321 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1323 } else if (operation
instanceof UnshareLinkOperation
) {
1324 onUnshareLinkOperationFinish((UnshareLinkOperation
) operation
, result
);
1326 } else if (operation
instanceof MoveFileOperation
) {
1327 onMoveFileOperationFinish((MoveFileOperation
) operation
, result
);
1329 } else if (operation
instanceof CopyFileOperation
) {
1330 onCopyFileOperationFinish((CopyFileOperation
) operation
, result
);
1336 private void onCreateShareOperationFinish(CreateShareOperation operation
,
1337 RemoteOperationResult result
) {
1338 if (result
.isSuccess()) {
1339 refreshShowDetails();
1340 refreshListOfFilesFragment();
1345 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
1346 RemoteOperationResult result
) {
1347 if (result
.isSuccess()) {
1348 refreshShowDetails();
1349 refreshListOfFilesFragment();
1351 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
1352 cleanSecondFragment();
1353 refreshListOfFilesFragment();
1357 private void refreshShowDetails() {
1358 FileFragment details
= getSecondFragment();
1359 if (details
!= null
) {
1360 OCFile file
= details
.getFile();
1362 file
= getStorageManager().getFileByPath(file
.getRemotePath());
1363 if (details
instanceof PreviewMediaFragment
) {
1364 // Refresh OCFile of the fragment
1365 ((PreviewMediaFragment
) details
).updateFile(file
);
1370 invalidateOptionsMenu();
1375 * Updates the view associated to the activity after the finish of an operation trying to
1378 * @param operation Removal operation performed.
1379 * @param result Result of the removal.
1381 private void onRemoveFileOperationFinish(RemoveFileOperation operation
,
1382 RemoteOperationResult result
) {
1383 dismissLoadingDialog();
1385 Toast msg
= Toast
.makeText(this,
1386 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1390 if (result
.isSuccess()) {
1391 OCFile removedFile
= operation
.getFile();
1392 FileFragment second
= getSecondFragment();
1393 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1394 if (second
instanceof PreviewMediaFragment
) {
1395 ((PreviewMediaFragment
) second
).stopPreview(true
);
1397 setFile(getStorageManager().getFileById(removedFile
.getParentId()));
1398 cleanSecondFragment();
1400 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1401 refreshListOfFilesFragment();
1403 invalidateOptionsMenu();
1405 if (result
.isSslRecoverableException()) {
1406 mLastSslUntrustedServerResult
= result
;
1407 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1414 * Updates the view associated to the activity after the finish of an operation trying to move a
1417 * @param operation Move operation performed.
1418 * @param result Result of the move operation.
1420 private void onMoveFileOperationFinish(MoveFileOperation operation
,
1421 RemoteOperationResult result
) {
1422 if (result
.isSuccess()) {
1423 dismissLoadingDialog();
1424 refreshListOfFilesFragment();
1426 dismissLoadingDialog();
1428 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1429 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1433 } catch (NotFoundException e
) {
1434 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1440 * Updates the view associated to the activity after the finish of an operation trying to copy a
1443 * @param operation Copy operation performed.
1444 * @param result Result of the copy operation.
1446 private void onCopyFileOperationFinish(CopyFileOperation operation
, RemoteOperationResult result
) {
1447 if (result
.isSuccess()) {
1448 dismissLoadingDialog();
1449 refreshListOfFilesFragment();
1451 dismissLoadingDialog();
1453 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1454 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1458 } catch (NotFoundException e
) {
1459 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1465 * Updates the view associated to the activity after the finish of an operation trying to rename
1468 * @param operation Renaming operation performed.
1469 * @param result Result of the renaming.
1471 private void onRenameFileOperationFinish(RenameFileOperation operation
,
1472 RemoteOperationResult result
) {
1473 dismissLoadingDialog();
1474 OCFile renamedFile
= operation
.getFile();
1475 if (result
.isSuccess()) {
1476 FileFragment details
= getSecondFragment();
1477 if (details
!= null
) {
1478 if (details
instanceof FileDetailFragment
&&
1479 renamedFile
.equals(details
.getFile()) ) {
1480 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1481 showDetails(renamedFile
);
1483 } else if (details
instanceof PreviewMediaFragment
&&
1484 renamedFile
.equals(details
.getFile())) {
1485 ((PreviewMediaFragment
) details
).updateFile(renamedFile
);
1486 if (PreviewMediaFragment
.canBePreviewed(renamedFile
)) {
1487 int position
= ((PreviewMediaFragment
) details
).getPosition();
1488 startMediaPreview(renamedFile
, position
, true
);
1490 getFileOperationsHelper().openFile(renamedFile
);
1495 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())){
1496 refreshListOfFilesFragment();
1500 Toast msg
= Toast
.makeText(this,
1501 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1505 if (result
.isSslRecoverableException()) {
1506 mLastSslUntrustedServerResult
= result
;
1507 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1512 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
1513 RemoteOperationResult result
) {
1514 if (result
.isSuccess()) {
1515 if (operation
.transferWasRequested()) {
1516 OCFile syncedFile
= operation
.getLocalFile();
1517 onTransferStateChanged(syncedFile
, true
, true
);
1518 invalidateOptionsMenu();
1524 * Updates the view associated to the activity after the finish of an operation trying create a
1527 * @param operation Creation operation performed.
1528 * @param result Result of the creation.
1530 private void onCreateFolderOperationFinish(CreateFolderOperation operation
,
1531 RemoteOperationResult result
) {
1532 if (result
.isSuccess()) {
1533 dismissLoadingDialog();
1534 refreshListOfFilesFragment();
1536 dismissLoadingDialog();
1538 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1539 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1543 } catch (NotFoundException e
) {
1544 Log_OC
.e(TAG
, "Error while trying to show fail message ", e
);
1554 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1555 refreshListOfFilesFragment();
1556 FileFragment details
= getSecondFragment();
1557 if (details
!= null
&& details
instanceof FileDetailFragment
&&
1558 file
.equals(details
.getFile()) ) {
1559 if (downloading
|| uploading
) {
1560 ((FileDetailFragment
) details
).updateFileDetails(file
, getAccount());
1562 if (!file
.fileExists()) {
1563 cleanSecondFragment();
1565 ((FileDetailFragment
) details
).updateFileDetails(false
, true
);
1573 private void requestForDownload() {
1574 Account account
= getAccount();
1575 //if (!mWaitingToPreview.isDownloading()) {
1576 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1577 Intent i
= new Intent(this, FileDownloader
.class);
1578 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1579 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1585 private OCFile
getCurrentDir() {
1586 OCFile file
= getFile();
1588 if (file
.isFolder()) {
1590 } else if (getStorageManager() != null
) {
1591 String parentPath
= file
.getRemotePath().substring(0,
1592 file
.getRemotePath().lastIndexOf(file
.getFileName()));
1593 return getStorageManager().getFileByPath(parentPath
);
1599 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
1600 long currentSyncTime
= System
.currentTimeMillis();
1602 mSyncInProgress
= true
;
1604 // perform folder synchronization
1605 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
1608 getFileOperationsHelper().isSharedSupported(),
1610 getStorageManager(),
1612 getApplicationContext()
1614 synchFolderOp
.execute(getAccount(), MainApp
.getAppContext(), this, null
, null
);
1615 mProgressBar
.setIndeterminate(true
);
1617 setBackgroundText();
1621 * Show untrusted cert dialog
1623 public void showUntrustedCertDialog(RemoteOperationResult result
) {
1624 // Show a dialog with the certificate info
1625 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.newInstanceForFullSslError(
1626 (CertificateCombinedException
) result
.getException());
1627 FragmentManager fm
= getSupportFragmentManager();
1628 FragmentTransaction ft
= fm
.beginTransaction();
1629 dialog
.show(ft
, DIALOG_UNTRUSTED_CERT
);
1632 private void requestForDownload(OCFile file
) {
1633 Account account
= getAccount();
1634 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1635 Intent i
= new Intent(this, FileDownloader
.class);
1636 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1637 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
1642 private void sendDownloadedFile() {
1643 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend
);
1644 mWaitingToSend
= null
;
1649 * Requests the download of the received {@link OCFile} , updates the UI
1650 * to monitor the download progress and prepares the activity to send the file
1651 * when the download finishes.
1653 * @param file {@link OCFile} to download and preview.
1655 public void startDownloadForSending(OCFile file
) {
1656 mWaitingToSend
= file
;
1657 requestForDownload(mWaitingToSend
);
1658 boolean hasSecondFragment
= (getSecondFragment() != null
);
1659 updateFragmentsVisibility(hasSecondFragment
);
1663 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1665 * @param file Image {@link OCFile} to show.
1667 public void startImagePreview(OCFile file
) {
1668 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1669 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1670 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1671 startActivity(showDetailsIntent
);
1676 * Stars the preview of an already down media {@link OCFile}.
1678 * @param file Media {@link OCFile} to preview.
1679 * @param startPlaybackPosition Media position where the playback will be started,
1681 * @param autoplay When 'true', the playback will start without user
1684 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1685 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
,
1687 setSecondFragment(mediaFragment
);
1688 updateFragmentsVisibility(true
);
1689 updateActionBarTitleAndHomeButton(file
);
1694 * Requests the download of the received {@link OCFile} , updates the UI
1695 * to monitor the download progress and prepares the activity to preview
1696 * or open the file when the download finishes.
1698 * @param file {@link OCFile} to download and preview.
1700 public void startDownloadForPreview(OCFile file
) {
1701 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1702 setSecondFragment(detailFragment
);
1703 mWaitingToPreview
= file
;
1704 requestForDownload();
1705 updateFragmentsVisibility(true
);
1706 updateActionBarTitleAndHomeButton(file
);
1711 public void cancelTransference(OCFile file
) {
1712 getFileOperationsHelper().cancelTransference(file
);
1713 if (mWaitingToPreview
!= null
&&
1714 mWaitingToPreview
.getRemotePath().equals(file
.getRemotePath())) {
1715 mWaitingToPreview
= null
;
1717 if (mWaitingToSend
!= null
&&
1718 mWaitingToSend
.getRemotePath().equals(file
.getRemotePath())) {
1719 mWaitingToSend
= null
;
1721 onTransferStateChanged(file
, false
, false
);
1725 public void onRefresh(boolean ignoreETag
) {
1726 refreshList(ignoreETag
);
1730 public void onRefresh() {
1734 private void refreshList(boolean ignoreETag
) {
1735 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1736 if (listOfFiles
!= null
) {
1737 OCFile folder
= listOfFiles
.getCurrentFile();
1738 if (folder
!= null
) {
1739 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1740 listDirectory(mFile);*/
1741 startSyncFolderOperation(folder
, ignoreETag
);
1746 private void sortByDate(boolean ascending
) {
1747 getListOfFilesFragment().sortByDate(ascending
);
1750 private void sortBySize(boolean ascending
) {
1751 getListOfFilesFragment().sortBySize(ascending
);
1754 private void sortByName(boolean ascending
) {
1755 getListOfFilesFragment().sortByName(ascending
);
1758 public void allFilesOption() {