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
;
152 protected void onCreate(Bundle savedInstanceState
) {
153 Log_OC
.v(TAG
, "onCreate() start");
154 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
156 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account
159 /// grant that FileObserverService is watching favorite files
160 if (savedInstanceState
== null
) {
161 Intent initObserversIntent
= FileObserverService
.makeInitIntent(this);
162 startService(initObserversIntent
);
165 /// Load of saved instance state
166 if(savedInstanceState
!= null
) {
167 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(
168 FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
169 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
170 mWaitingToSend
= (OCFile
) savedInstanceState
.getParcelable(
171 FileDisplayActivity
.KEY_WAITING_TO_SEND
);
174 mWaitingToPreview
= null
;
175 mSyncInProgress
= false
;
176 mWaitingToSend
= null
;
181 // Inflate and set the layout view
182 setContentView(R
.layout
.files
);
187 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
188 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
189 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
190 if (savedInstanceState
== null
) {
191 createMinFragments();
195 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS,
196 // according to the official
199 // enable ActionBar app icon to behave as action to toggle nav drawer
200 //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
201 getSupportActionBar().setHomeButtonEnabled(true
);
203 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
204 /*|| mRefreshSharesInProgress*/);
205 // always AFTER setContentView(...) ; to work around bug in its implementation
211 Log_OC
.v(TAG
, "onCreate() end");
215 protected void onStart() {
216 Log_OC
.v(TAG
, "onStart() start");
218 Log_OC
.v(TAG
, "onStart() end");
222 protected void onDestroy() {
223 Log_OC
.v(TAG
, "onDestroy() start");
225 Log_OC
.v(TAG
, "onDestroy() end");
229 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
232 protected void onAccountSet(boolean stateWasRecovered
) {
233 super.onAccountSet(stateWasRecovered
);
234 if (getAccount() != null
) {
235 /// Check whether the 'main' OCFile handled by the Activity is contained in the
237 OCFile file
= getFile();
238 // get parent from path
239 String parentPath
= "";
241 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
242 // upload in progress - right now, files are not inserted in the local
243 // cache until the upload is successful get parent from path
244 parentPath
= file
.getRemotePath().substring(0,
245 file
.getRemotePath().lastIndexOf(file
.getFileName()));
246 if (getStorageManager().getFileByPath(parentPath
) == null
)
247 file
= null
; // not able to know the directory where the file is uploading
249 file
= getStorageManager().getFileByPath(file
.getRemotePath());
250 // currentDir = null if not in the current Account
254 // fall back to root folder
255 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
259 if (!stateWasRecovered
) {
260 Log_OC
.d(TAG
, "Initializing Fragments in onAccountChanged..");
261 initFragmentsWithFile();
262 if (file
.isFolder()) {
263 startSyncFolderOperation(file
, false
);
267 updateFragmentsVisibility(!file
.isFolder());
268 updateActionBarTitleAndHomeButton(file
.isFolder() ? null
: file
);
274 private void createMinFragments() {
275 OCFileListFragment listOfFiles
= new OCFileListFragment();
276 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
277 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
278 transaction
.commit();
281 private void initFragmentsWithFile() {
282 if (getAccount() != null
&& getFile() != null
) {
284 OCFileListFragment listOfFiles
= getListOfFilesFragment();
285 if (listOfFiles
!= null
) {
286 listOfFiles
.listDirectory(getCurrentDir());
287 // TODO Enable when "On Device" is recovered
288 // listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
290 Log_OC
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
294 OCFile file
= getFile();
295 Fragment secondFragment
= chooseInitialSecondFragment(file
);
296 if (secondFragment
!= null
) {
297 setSecondFragment(secondFragment
);
298 updateFragmentsVisibility(true
);
299 updateActionBarTitleAndHomeButton(file
);
302 cleanSecondFragment();
306 Log_OC
.wtf(TAG
, "initFragments() called with invalid NULLs!");
307 if (getAccount() == null
) {
308 Log_OC
.wtf(TAG
, "\t account is NULL");
310 if (getFile() == null
) {
311 Log_OC
.wtf(TAG
, "\t file is NULL");
316 private Fragment
chooseInitialSecondFragment(OCFile file
) {
317 Fragment secondFragment
= null
;
318 if (file
!= null
&& !file
.isFolder()) {
319 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
320 && file
.getLastSyncDateForProperties() > 0 // temporal fix
322 int startPlaybackPosition
=
323 getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
325 getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
326 secondFragment
= new PreviewMediaFragment(file
, getAccount(),
327 startPlaybackPosition
, autoplay
);
330 secondFragment
= FileDetailFragment
.newInstance(file
, getAccount());
333 return secondFragment
;
338 * Replaces the second fragment managed by the activity with the received as
341 * Assumes never will be more than two fragments managed at the same time.
343 * @param fragment New second Fragment to set.
345 private void setSecondFragment(Fragment fragment
) {
346 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
347 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
348 transaction
.commit();
352 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
354 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
355 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
357 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
358 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
361 } else if (existsSecondFragment
) {
362 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
363 mLeftFragmentContainer
.setVisibility(View
.GONE
);
365 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
366 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
370 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
371 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
373 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
374 mRightFragmentContainer
.setVisibility(View
.GONE
);
380 private OCFileListFragment
getListOfFilesFragment() {
381 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(
382 FileDisplayActivity
.TAG_LIST_OF_FILES
);
383 if (listOfFiles
!= null
) {
384 return (OCFileListFragment
)listOfFiles
;
386 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
390 public FileFragment
getSecondFragment() {
391 Fragment second
= getSupportFragmentManager().findFragmentByTag(
392 FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
393 if (second
!= null
) {
394 return (FileFragment
)second
;
399 protected void cleanSecondFragment() {
400 Fragment second
= getSecondFragment();
401 if (second
!= null
) {
402 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
406 updateFragmentsVisibility(false
);
407 updateActionBarTitleAndHomeButton(null
);
410 protected void refreshListOfFilesFragment() {
411 OCFileListFragment fileListFragment
= getListOfFilesFragment();
412 if (fileListFragment
!= null
) {
413 fileListFragment
.listDirectory();
414 // TODO Enable when "On Device" is recovered ?
415 // fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
419 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
,
421 FileFragment secondFragment
= getSecondFragment();
422 boolean waitedPreview
= (mWaitingToPreview
!= null
&&
423 mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
424 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
425 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
426 OCFile fileInFragment
= detailsFragment
.getFile();
427 if (fileInFragment
!= null
&&
428 !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
429 // the user browsed to other file ; forget the automatic preview
430 mWaitingToPreview
= null
;
432 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
433 // grant that the right panel updates the progress bar
434 detailsFragment
.listenForTransferProgress();
435 detailsFragment
.updateFileDetails(true
, false
);
437 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
438 // update the right panel
439 boolean detailsFragmentChanged
= false
;
442 mWaitingToPreview
= getStorageManager().getFileById(
443 mWaitingToPreview
.getFileId()); // update the file from database,
444 // for the local storage path
445 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
446 startMediaPreview(mWaitingToPreview
, 0, true
);
447 detailsFragmentChanged
= true
;
449 getFileOperationsHelper().openFile(mWaitingToPreview
);
452 mWaitingToPreview
= null
;
454 if (!detailsFragmentChanged
) {
455 detailsFragment
.updateFileDetails(false
, (success
));
462 public boolean onPrepareOptionsMenu(Menu menu
) {
463 boolean drawerOpen
= mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
464 menu
.findItem(R
.id
.action_upload
).setVisible(!drawerOpen
);
465 menu
.findItem(R
.id
.action_create_dir
).setVisible(!drawerOpen
);
466 menu
.findItem(R
.id
.action_sort
).setVisible(!drawerOpen
);
467 menu
.findItem(R
.id
.action_sync_account
).setVisible(!drawerOpen
);
469 return super.onPrepareOptionsMenu(menu
);
473 public boolean onCreateOptionsMenu(Menu menu
) {
474 MenuInflater inflater
= getMenuInflater();
475 inflater
.inflate(R
.menu
.main_menu
, menu
);
481 public boolean onOptionsItemSelected(MenuItem item
) {
482 boolean retval
= true
;
483 switch (item
.getItemId()) {
484 case R
.id
.action_create_dir
: {
485 CreateFolderDialogFragment dialog
=
486 CreateFolderDialogFragment
.newInstance(getCurrentDir());
487 dialog
.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER
);
490 case R
.id
.action_sync_account
: {
491 startSynchronization();
494 case R
.id
.action_upload
: {
495 UploadSourceDialogFragment dialog
=
496 UploadSourceDialogFragment
.newInstance(getAccount());
497 dialog
.show(getSupportFragmentManager(), DIALOG_UPLOAD_SOURCE
);
501 case android
.R
.id
.home
: {
502 FileFragment second
= getSecondFragment();
503 OCFile currentDir
= getCurrentDir();
504 if (mDrawerLayout
.isDrawerOpen(GravityCompat
.START
)) {
505 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
506 } else if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
507 (second
!= null
&& second
.getFile() != null
)) {
511 mDrawerLayout
.openDrawer(GravityCompat
.START
);
515 case R
.id
.action_sort
: {
516 SharedPreferences appPreferences
= PreferenceManager
517 .getDefaultSharedPreferences(this);
519 // Read sorting order, default to sort by name ascending
520 Integer sortOrder
= appPreferences
521 .getInt("sortOrder", FileStorageUtils
.SORT_NAME
);
523 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
524 builder
.setTitle(R
.string
.actionbar_sort_title
)
525 .setSingleChoiceItems(R
.array
.actionbar_sortby
, sortOrder
,
526 new DialogInterface
.OnClickListener() {
527 public void onClick(DialogInterface dialog
, int which
) {
540 builder
.create().show();
544 retval
= super.onOptionsItemSelected(item
);
549 private void startSynchronization() {
550 Log_OC
.d(TAG
, "Got to start sync");
551 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
552 Log_OC
.d(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
553 ContentResolver
.cancelSync(null
, MainApp
.getAuthority());
554 // cancel the current synchronizations of any ownCloud account
555 Bundle bundle
= new Bundle();
556 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
557 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
558 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
559 MainApp
.getAuthority());
560 ContentResolver
.requestSync(
562 MainApp
.getAuthority(), bundle
);
564 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
565 MainApp
.getAuthority() + " with new API");
566 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
567 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
568 builder
.setExpedited(true
);
569 builder
.setManual(true
);
572 // Fix bug in Android Lollipop when you click on refresh the whole account
573 Bundle extras
= new Bundle();
574 builder
.setExtras(extras
);
576 SyncRequest request
= builder
.build();
577 ContentResolver
.requestSync(request
);
582 * Called, when the user selected something for uploading
585 @TargetApi(Build
.VERSION_CODES
.JELLY_BEAN
)
587 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
589 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
||
590 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
591 //getClipData is only supported on api level 16+, Jelly Bean
592 if (data
.getData() == null
&& Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
){
593 for( int i
= 0; i
< data
.getClipData().getItemCount(); i
++){
594 Intent intent
= new Intent();
595 intent
.setData(data
.getClipData().getItemAt(i
).getUri());
596 requestSimpleUpload(intent
, resultCode
);
599 requestSimpleUpload(data
, resultCode
);
601 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
||
602 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
603 requestMultipleUpload(data
, resultCode
);
605 } else if (requestCode
== ACTION_MOVE_FILES
&& resultCode
== RESULT_OK
){
607 final Intent fData
= data
;
608 final int fResultCode
= resultCode
;
609 getHandler().postDelayed(
613 requestMoveOperation(fData
, fResultCode
);
616 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
620 super.onActivityResult(requestCode
, resultCode
, data
);
625 private void requestMultipleUpload(Intent data
, int resultCode
) {
626 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
627 if (filePaths
!= null
) {
628 String
[] remotePaths
= new String
[filePaths
.length
];
629 String remotePathBase
= getCurrentDir().getRemotePath();
630 for (int j
= 0; j
< remotePaths
.length
; j
++) {
631 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
634 Intent i
= new Intent(this, FileUploader
.class);
635 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
636 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
637 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
638 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
639 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
640 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
644 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
645 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
),
653 private void requestSimpleUpload(Intent data
, int resultCode
) {
654 String filePath
= null
;
655 String mimeType
= null
;
657 Uri selectedImageUri
= data
.getData();
660 mimeType
= getContentResolver().getType(selectedImageUri
);
662 String fileManagerString
= selectedImageUri
.getPath();
663 String selectedImagePath
= UriUtils
.getLocalPath(selectedImageUri
, this);
665 if (selectedImagePath
!= null
)
666 filePath
= selectedImagePath
;
668 filePath
= fileManagerString
;
670 } catch (Exception e
) {
671 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of " +
672 "Intent.ACTION_GET_CONTENT", e
);
675 if (filePath
== null
) {
676 Log_OC
.e(TAG
, "Couldn't resolve path to file");
677 Toast t
= Toast
.makeText(
678 this, getString(R
.string
.filedisplay_unexpected_bad_get_content
),
686 Intent i
= new Intent(this, FileUploader
.class);
687 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
688 OCFile currentDir
= getCurrentDir();
689 String remotePath
= (currentDir
!= null
) ? currentDir
.getRemotePath() : OCFile
.ROOT_PATH
;
691 if (filePath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
)) {
692 Cursor cursor
= getContentResolver().query(Uri
.parse(filePath
), null
, null
, null
, null
);
694 if (cursor
!= null
&& cursor
.moveToFirst()) {
695 String displayName
= cursor
.getString(cursor
.getColumnIndex(
696 OpenableColumns
.DISPLAY_NAME
));
697 Log_OC
.v(TAG
, "Display Name: " + displayName
);
699 displayName
.replace(File
.separatorChar
, '_');
700 displayName
.replace(File
.pathSeparatorChar
, '_');
701 remotePath
+= displayName
+ DisplayUtils
.getComposedFileExtension(filePath
);
704 // and what happens in case of error?; wrong target name for the upload
710 remotePath
+= new File(filePath
).getName();
713 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePath
);
714 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePath
);
715 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mimeType
);
716 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
717 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
718 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
723 * Request the operation for moving the file/folder from one path to another
725 * @param data Intent received
726 * @param resultCode Result code received
728 private void requestMoveOperation(Intent data
, int resultCode
) {
729 OCFile folderToMoveAt
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FOLDER
);
730 OCFile targetFile
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
731 getFileOperationsHelper().moveFile(folderToMoveAt
, targetFile
);
735 public void onBackPressed() {
736 if (!isDrawerOpen()){
737 OCFileListFragment listOfFiles
= getListOfFilesFragment();
738 if (mDualPane
|| getSecondFragment() == null
) {
739 OCFile currentDir
= getCurrentDir();
740 if (currentDir
== null
|| currentDir
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
) {
744 if (listOfFiles
!= null
) { // should never be null, indeed
745 listOfFiles
.onBrowseUp();
748 if (listOfFiles
!= null
) { // should never be null, indeed
749 setFile(listOfFiles
.getCurrentFile());
751 cleanSecondFragment();
753 super.onBackPressed();
758 protected void onSaveInstanceState(Bundle outState
) {
759 // responsibility of restore is preferred in onCreate() before than in
760 // onRestoreInstanceState when there are Fragments involved
761 Log_OC
.v(TAG
, "onSaveInstanceState() start");
762 super.onSaveInstanceState(outState
);
763 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
764 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
765 //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
766 // mRefreshSharesInProgress);
767 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_SEND
, mWaitingToSend
);
769 Log_OC
.v(TAG
, "onSaveInstanceState() end");
775 protected void onResume() {
776 Log_OC
.v(TAG
, "onResume() start");
779 // refresh Navigation Drawer account list
780 mNavigationDrawerAdapter
.updateAccountList();
783 // refresh list of files
784 refreshListOfFilesFragment();
786 // Listen for sync messages
787 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
788 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
789 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
790 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
791 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
792 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
793 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
794 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
795 // syncIntentFilter);
797 // Listen for upload messages
798 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
799 mUploadFinishReceiver
= new UploadFinishReceiver();
800 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
802 // Listen for download messages
803 IntentFilter downloadIntentFilter
= new IntentFilter(
804 FileDownloader
.getDownloadAddedMessage());
805 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
806 mDownloadFinishReceiver
= new DownloadFinishReceiver();
807 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
809 Log_OC
.v(TAG
, "onResume() end");
814 protected void onPause() {
815 Log_OC
.v(TAG
, "onPause() start");
816 if (mSyncBroadcastReceiver
!= null
) {
817 unregisterReceiver(mSyncBroadcastReceiver
);
818 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
819 mSyncBroadcastReceiver
= null
;
821 if (mUploadFinishReceiver
!= null
) {
822 unregisterReceiver(mUploadFinishReceiver
);
823 mUploadFinishReceiver
= null
;
825 if (mDownloadFinishReceiver
!= null
) {
826 unregisterReceiver(mDownloadFinishReceiver
);
827 mDownloadFinishReceiver
= null
;
831 Log_OC
.v(TAG
, "onPause() end");
835 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
838 * {@link BroadcastReceiver} to enable syncing feedback in UI
841 public void onReceive(Context context
, Intent intent
) {
843 String event
= intent
.getAction();
844 Log_OC
.d(TAG
, "Received broadcast " + event
);
845 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
846 String synchFolderRemotePath
=
847 intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
848 RemoteOperationResult synchResult
=
849 (RemoteOperationResult
)intent
.getSerializableExtra(
850 FileSyncAdapter
.EXTRA_RESULT
);
851 boolean sameAccount
= (getAccount() != null
&&
852 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
856 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
857 mSyncInProgress
= true
;
860 OCFile currentFile
= (getFile() == null
) ? null
:
861 getStorageManager().getFileByPath(getFile().getRemotePath());
862 OCFile currentDir
= (getCurrentDir() == null
) ? null
:
863 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
865 if (currentDir
== null
) {
866 // current folder was removed from the server
867 Toast
.makeText( FileDisplayActivity
.this,
870 sync_current_folder_was_removed
),
871 synchFolderRemotePath
),
877 if (currentFile
== null
&& !getFile().isFolder()) {
878 // currently selected file was removed in the server, and now we
880 cleanSecondFragment();
881 currentFile
= currentDir
;
884 if (synchFolderRemotePath
!= null
&&
885 currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
886 OCFileListFragment fileListFragment
= getListOfFilesFragment();
887 if (fileListFragment
!= null
) {
888 fileListFragment
.listDirectory();
889 // TODO Enable when "On Device" is recovered ?
890 // fileListFragment.listDirectory(currentDir,
891 // MainApp.getOnlyOnDevice());
894 setFile(currentFile
);
897 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
898 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
901 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
903 /// TODO refactor and make common
904 synchResult
!= null
&& !synchResult
.isSuccess() &&
905 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
906 synchResult
.isIdPRedirection() ||
907 (synchResult
.isException() && synchResult
.getException()
908 instanceof AuthenticatorException
))) {
912 OwnCloudClient client
;
913 OwnCloudAccount ocAccount
=
914 new OwnCloudAccount(getAccount(), context
);
915 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
916 removeClientFor(ocAccount
));
918 if (client
!= null
) {
919 OwnCloudCredentials cred
= client
.getCredentials();
921 AccountManager am
= AccountManager
.get(context
);
922 if (cred
.authTokenExpires()) {
923 am
.invalidateAuthToken(
928 am
.clearPassword(getAccount());
932 requestCredentialsUpdate();
934 } catch (AccountNotFoundException e
) {
935 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
940 removeStickyBroadcast(intent
);
941 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
942 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
943 /*|| mRefreshSharesInProgress*/);
949 if (synchResult
!= null
) {
950 if (synchResult
.getCode().equals(
951 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
952 mLastSslUntrustedServerResult
= synchResult
;
955 } catch (RuntimeException e
) {
956 // avoid app crashes after changing the serial id of RemoteOperationResult
957 // in owncloud library with broadcast notifications pending to process
958 removeStickyBroadcast(intent
);
964 * Show a text message on screen view for notifying user if content is
965 * loading or folder is empty
967 private void setBackgroundText() {
968 OCFileListFragment ocFileListFragment
= getListOfFilesFragment();
969 if (ocFileListFragment
!= null
) {
970 int message
= R
.string
.file_list_loading
;
971 if (!mSyncInProgress
) {
972 // In case file list is empty
973 message
= R
.string
.file_list_empty
;
975 ocFileListFragment
.setMessageForEmptyList(getString(message
));
977 Log_OC
.e(TAG
, "OCFileListFragment is null");
982 * Once the file upload has finished -> update view
984 private class UploadFinishReceiver
extends BroadcastReceiver
{
986 * Once the file upload has finished -> update view
987 * @author David A. Velasco
988 * {@link BroadcastReceiver} to enable upload feedback in UI
991 public void onReceive(Context context
, Intent intent
) {
993 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
994 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
995 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
996 OCFile currentDir
= getCurrentDir();
997 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) &&
998 (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1000 if (sameAccount
&& isDescendant
) {
1001 refreshListOfFilesFragment();
1004 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
,
1006 boolean renamedInUpload
= getFile().getRemotePath().
1007 equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
1008 boolean sameFile
= getFile().getRemotePath().equals(uploadedRemotePath
) ||
1010 FileFragment details
= getSecondFragment();
1011 boolean detailFragmentIsShown
= (details
!= null
&&
1012 details
instanceof FileDetailFragment
);
1014 if (sameAccount
&& sameFile
&& detailFragmentIsShown
) {
1015 if (uploadWasFine
) {
1016 setFile(getStorageManager().getFileByPath(uploadedRemotePath
));
1018 if (renamedInUpload
) {
1019 String newName
= (new File(uploadedRemotePath
)).getName();
1020 Toast msg
= Toast
.makeText(
1023 getString(R
.string
.filedetails_renamed_in_upload_msg
),
1028 if (uploadWasFine
|| getFile().fileExists()) {
1029 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1031 cleanSecondFragment();
1034 // Force the preview if the file is an image
1035 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
1036 startImagePreview(getFile());
1037 } // TODO what about other kind of previews?
1040 setSupportProgressBarIndeterminate(false
);
1043 if (intent
!= null
) {
1044 removeStickyBroadcast(intent
);
1054 * Class waiting for broadcast events from the {@link FileDownloader} service.
1056 * Updates the UI when a download is started or finished, provided that it is relevant for the
1059 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1061 //int refreshCounter = 0;
1063 public void onReceive(Context context
, Intent intent
) {
1065 boolean sameAccount
= isSameAccount(context
, intent
);
1066 String downloadedRemotePath
=
1067 intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1068 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1070 if (sameAccount
&& isDescendant
) {
1071 String linkedToRemotePath
=
1072 intent
.getStringExtra(FileDownloader
.EXTRA_LINKED_TO_PATH
);
1073 if (linkedToRemotePath
== null
|| isAscendant(linkedToRemotePath
)) {
1074 //Log_OC.v(TAG, "refresh #" + ++refreshCounter);
1075 refreshListOfFilesFragment();
1077 refreshSecondFragment(
1079 downloadedRemotePath
,
1080 intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)
1084 if (mWaitingToSend
!= null
) {
1086 getStorageManager().getFileByPath(mWaitingToSend
.getRemotePath());
1087 if (mWaitingToSend
.isDown()) {
1088 sendDownloadedFile();
1093 if (intent
!= null
) {
1094 removeStickyBroadcast(intent
);
1099 private boolean isDescendant(String downloadedRemotePath
) {
1100 OCFile currentDir
= getCurrentDir();
1102 currentDir
!= null
&&
1103 downloadedRemotePath
!= null
&&
1104 downloadedRemotePath
.startsWith(currentDir
.getRemotePath())
1108 private boolean isAscendant(String linkedToRemotePath
) {
1109 OCFile currentDir
= getCurrentDir();
1111 currentDir
!= null
&&
1112 currentDir
.getRemotePath().startsWith(linkedToRemotePath
)
1116 private boolean isSameAccount(Context context
, Intent intent
) {
1117 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1118 return (accountName
!= null
&& getAccount() != null
&&
1119 accountName
.equals(getAccount().name
));
1124 public void browseToRoot() {
1125 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1126 if (listOfFiles
!= null
) { // should never be null, indeed
1127 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
1128 listOfFiles
.listDirectory(root
);
1129 // TODO Enable when "On Device" is recovered ?
1130 // listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
1131 setFile(listOfFiles
.getCurrentFile());
1132 startSyncFolderOperation(root
, false
);
1134 cleanSecondFragment();
1142 * Updates action bar and second fragment, if in dual pane mode.
1145 public void onBrowsedDownTo(OCFile directory
) {
1147 cleanSecondFragment();
1149 startSyncFolderOperation(directory
, false
);
1153 * Shows the information of the {@link OCFile} received as a
1154 * parameter in the second fragment.
1156 * @param file {@link OCFile} whose details will be shown
1159 public void showDetails(OCFile file
) {
1160 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1161 setSecondFragment(detailFragment
);
1162 updateFragmentsVisibility(true
);
1163 updateActionBarTitleAndHomeButton(file
);
1168 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
1170 // in dual pane mode, keep the focus of title an action bar in the current folder
1171 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1174 super.updateActionBarTitleAndHomeButton(chosenFile
);
1180 protected ServiceConnection
newTransferenceServiceConnection() {
1181 return new ListServiceConnection();
1184 /** Defines callbacks for service binding, passed to bindService() */
1185 private class ListServiceConnection
implements ServiceConnection
{
1188 public void onServiceConnected(ComponentName component
, IBinder service
) {
1189 if (component
.equals(new ComponentName(
1190 FileDisplayActivity
.this, FileDownloader
.class))) {
1191 Log_OC
.d(TAG
, "Download service connected");
1192 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1193 if (mWaitingToPreview
!= null
)
1194 if (getStorageManager() != null
) {
1197 getStorageManager().getFileById(mWaitingToPreview
.getFileId());
1198 if (!mWaitingToPreview
.isDown()) {
1199 requestForDownload();
1203 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1204 FileUploader
.class))) {
1205 Log_OC
.d(TAG
, "Upload service connected");
1206 mUploaderBinder
= (FileUploaderBinder
) service
;
1210 // a new chance to get the mDownloadBinder through
1211 // getFileDownloadBinder() - THIS IS A MESS
1212 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1213 if (listOfFiles
!= null
) {
1214 listOfFiles
.listDirectory();
1215 // TODO Enable when "On Device" is recovered ?
1216 // listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1218 FileFragment secondFragment
= getSecondFragment();
1219 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1220 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1221 detailFragment
.listenForTransferProgress();
1222 detailFragment
.updateFileDetails(false
, false
);
1227 public void onServiceDisconnected(ComponentName component
) {
1228 if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1229 FileDownloader
.class))) {
1230 Log_OC
.d(TAG
, "Download service disconnected");
1231 mDownloaderBinder
= null
;
1232 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1233 FileUploader
.class))) {
1234 Log_OC
.d(TAG
, "Upload service disconnected");
1235 mUploaderBinder
= null
;
1241 public void onSavedCertificate() {
1242 startSyncFolderOperation(getCurrentDir(), false
);
1247 public void onFailedSavingCertificate() {
1248 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
1249 R
.string
.ssl_validator_not_saved
, new String
[]{}, R
.string
.common_ok
, -1, -1
1251 dialog
.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED
);
1255 public void onCancelCertificate() {
1260 * Updates the view associated to the activity after the finish of some operation over files
1261 * in the current account.
1263 * @param operation Removal operation performed.
1264 * @param result Result of the removal.
1267 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1268 super.onRemoteOperationFinish(operation
, result
);
1270 if (operation
instanceof RemoveFileOperation
) {
1271 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1273 } else if (operation
instanceof RenameFileOperation
) {
1274 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1276 } else if (operation
instanceof SynchronizeFileOperation
) {
1277 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1279 } else if (operation
instanceof CreateFolderOperation
) {
1280 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1282 } else if (operation
instanceof CreateShareOperation
) {
1283 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1285 } else if (operation
instanceof UnshareLinkOperation
) {
1286 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
1288 } else if (operation
instanceof MoveFileOperation
) {
1289 onMoveFileOperationFinish((MoveFileOperation
)operation
, result
);
1295 private void onCreateShareOperationFinish(CreateShareOperation operation
,
1296 RemoteOperationResult result
) {
1297 if (result
.isSuccess()) {
1298 refreshShowDetails();
1299 refreshListOfFilesFragment();
1304 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
1305 RemoteOperationResult result
) {
1306 if (result
.isSuccess()) {
1307 refreshShowDetails();
1308 refreshListOfFilesFragment();
1310 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
1311 cleanSecondFragment();
1312 refreshListOfFilesFragment();
1316 private void refreshShowDetails() {
1317 FileFragment details
= getSecondFragment();
1318 if (details
!= null
) {
1319 OCFile file
= details
.getFile();
1321 file
= getStorageManager().getFileByPath(file
.getRemotePath());
1322 if (details
instanceof PreviewMediaFragment
) {
1323 // Refresh OCFile of the fragment
1324 ((PreviewMediaFragment
) details
).updateFile(file
);
1329 invalidateOptionsMenu();
1334 * Updates the view associated to the activity after the finish of an operation trying to
1337 * @param operation Removal operation performed.
1338 * @param result Result of the removal.
1340 private void onRemoveFileOperationFinish(RemoveFileOperation operation
,
1341 RemoteOperationResult result
) {
1342 dismissLoadingDialog();
1344 Toast msg
= Toast
.makeText(this,
1345 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1349 if (result
.isSuccess()) {
1350 OCFile removedFile
= operation
.getFile();
1351 FileFragment second
= getSecondFragment();
1352 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1353 if (second
instanceof PreviewMediaFragment
) {
1354 ((PreviewMediaFragment
)second
).stopPreview(true
);
1356 setFile(getStorageManager().getFileById(removedFile
.getParentId()));
1357 cleanSecondFragment();
1359 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1360 refreshListOfFilesFragment();
1362 invalidateOptionsMenu();
1364 if (result
.isSslRecoverableException()) {
1365 mLastSslUntrustedServerResult
= result
;
1366 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1373 * Updates the view associated to the activity after the finish of an operation trying to move a
1376 * @param operation Move operation performed.
1377 * @param result Result of the move operation.
1379 private void onMoveFileOperationFinish(MoveFileOperation operation
,
1380 RemoteOperationResult result
) {
1381 if (result
.isSuccess()) {
1382 dismissLoadingDialog();
1383 refreshListOfFilesFragment();
1385 dismissLoadingDialog();
1387 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1388 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1392 } catch (NotFoundException e
) {
1393 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1400 * Updates the view associated to the activity after the finish of an operation trying to rename
1403 * @param operation Renaming operation performed.
1404 * @param result Result of the renaming.
1406 private void onRenameFileOperationFinish(RenameFileOperation operation
,
1407 RemoteOperationResult result
) {
1408 dismissLoadingDialog();
1409 OCFile renamedFile
= operation
.getFile();
1410 if (result
.isSuccess()) {
1411 FileFragment details
= getSecondFragment();
1412 if (details
!= null
) {
1413 if (details
instanceof FileDetailFragment
&&
1414 renamedFile
.equals(details
.getFile()) ) {
1415 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1416 showDetails(renamedFile
);
1418 } else if (details
instanceof PreviewMediaFragment
&&
1419 renamedFile
.equals(details
.getFile())) {
1420 ((PreviewMediaFragment
) details
).updateFile(renamedFile
);
1421 if (PreviewMediaFragment
.canBePreviewed(renamedFile
)) {
1422 int position
= ((PreviewMediaFragment
)details
).getPosition();
1423 startMediaPreview(renamedFile
, position
, true
);
1425 getFileOperationsHelper().openFile(renamedFile
);
1430 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())){
1431 refreshListOfFilesFragment();
1435 Toast msg
= Toast
.makeText(this,
1436 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1440 if (result
.isSslRecoverableException()) {
1441 mLastSslUntrustedServerResult
= result
;
1442 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1447 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
1448 RemoteOperationResult result
) {
1449 dismissLoadingDialog();
1450 OCFile syncedFile
= operation
.getLocalFile();
1451 if (!result
.isSuccess()) {
1452 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1453 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1454 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1455 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
1461 if (operation
.transferWasRequested()) {
1462 onTransferStateChanged(syncedFile
, true
, true
);
1465 Toast msg
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
1466 operation
, getResources()), Toast
.LENGTH_LONG
);
1469 invalidateOptionsMenu();
1474 * Updates the view associated to the activity after the finish of an operation trying create a
1477 * @param operation Creation operation performed.
1478 * @param result Result of the creation.
1480 private void onCreateFolderOperationFinish(CreateFolderOperation operation
,
1481 RemoteOperationResult result
) {
1482 if (result
.isSuccess()) {
1483 dismissLoadingDialog();
1484 refreshListOfFilesFragment();
1486 dismissLoadingDialog();
1488 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1489 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1493 } catch (NotFoundException e
) {
1494 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1504 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1505 refreshListOfFilesFragment();
1506 FileFragment details
= getSecondFragment();
1507 if (details
!= null
&& details
instanceof FileDetailFragment
&&
1508 file
.equals(details
.getFile()) ) {
1509 if (downloading
|| uploading
) {
1510 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1512 if (!file
.fileExists()) {
1513 cleanSecondFragment();
1515 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1523 private void requestForDownload() {
1524 Account account
= getAccount();
1525 //if (!mWaitingToPreview.isDownloading()) {
1526 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1527 Intent i
= new Intent(this, FileDownloader
.class);
1528 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1529 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1535 private OCFile
getCurrentDir() {
1536 OCFile file
= getFile();
1538 if (file
.isFolder()) {
1540 } else if (getStorageManager() != null
) {
1541 String parentPath
= file
.getRemotePath().substring(0,
1542 file
.getRemotePath().lastIndexOf(file
.getFileName()));
1543 return getStorageManager().getFileByPath(parentPath
);
1549 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
1550 long currentSyncTime
= System
.currentTimeMillis();
1552 mSyncInProgress
= true
;
1554 // perform folder synchronization
1555 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
1558 getFileOperationsHelper().isSharedSupported(),
1560 getStorageManager(),
1562 getApplicationContext()
1564 synchFolderOp
.execute(getAccount(), MainApp
.getAppContext(), this, null
, null
);
1566 setSupportProgressBarIndeterminateVisibility(true
);
1568 setBackgroundText();
1572 * Show untrusted cert dialog
1574 public void showUntrustedCertDialog(RemoteOperationResult result
) {
1575 // Show a dialog with the certificate info
1576 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.newInstanceForFullSslError(
1577 (CertificateCombinedException
) result
.getException());
1578 FragmentManager fm
= getSupportFragmentManager();
1579 FragmentTransaction ft
= fm
.beginTransaction();
1580 dialog
.show(ft
, DIALOG_UNTRUSTED_CERT
);
1583 private void requestForDownload(OCFile file
) {
1584 Account account
= getAccount();
1585 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1586 Intent i
= new Intent(this, FileDownloader
.class);
1587 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1588 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
1593 private void sendDownloadedFile(){
1594 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend
);
1595 mWaitingToSend
= null
;
1600 * Requests the download of the received {@link OCFile} , updates the UI
1601 * to monitor the download progress and prepares the activity to send the file
1602 * when the download finishes.
1604 * @param file {@link OCFile} to download and preview.
1606 public void startDownloadForSending(OCFile file
) {
1607 mWaitingToSend
= file
;
1608 requestForDownload(mWaitingToSend
);
1609 boolean hasSecondFragment
= (getSecondFragment()!= null
);
1610 updateFragmentsVisibility(hasSecondFragment
);
1614 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1616 * @param file Image {@link OCFile} to show.
1618 public void startImagePreview(OCFile file
) {
1619 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1620 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1621 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1622 startActivity(showDetailsIntent
);
1627 * Stars the preview of an already down media {@link OCFile}.
1629 * @param file Media {@link OCFile} to preview.
1630 * @param startPlaybackPosition Media position where the playback will be started,
1632 * @param autoplay When 'true', the playback will start without user
1635 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1636 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
,
1638 setSecondFragment(mediaFragment
);
1639 updateFragmentsVisibility(true
);
1640 updateActionBarTitleAndHomeButton(file
);
1645 * Requests the download of the received {@link OCFile} , updates the UI
1646 * to monitor the download progress and prepares the activity to preview
1647 * or open the file when the download finishes.
1649 * @param file {@link OCFile} to download and preview.
1651 public void startDownloadForPreview(OCFile file
) {
1652 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1653 setSecondFragment(detailFragment
);
1654 mWaitingToPreview
= file
;
1655 requestForDownload();
1656 updateFragmentsVisibility(true
);
1657 updateActionBarTitleAndHomeButton(file
);
1662 public void cancelTransference(OCFile file
) {
1663 getFileOperationsHelper().cancelTransference(file
);
1664 if (mWaitingToPreview
!= null
&&
1665 mWaitingToPreview
.getRemotePath().equals(file
.getRemotePath())) {
1666 mWaitingToPreview
= null
;
1668 if (mWaitingToSend
!= null
&&
1669 mWaitingToSend
.getRemotePath().equals(file
.getRemotePath())) {
1670 mWaitingToSend
= null
;
1672 onTransferStateChanged(file
, false
, false
);
1676 public void onRefresh(boolean ignoreETag
) {
1677 refreshList(ignoreETag
);
1681 public void onRefresh() {
1685 private void refreshList(boolean ignoreETag
) {
1686 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1687 if (listOfFiles
!= null
) {
1688 OCFile folder
= listOfFiles
.getCurrentFile();
1689 if (folder
!= null
) {
1690 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1691 listDirectory(mFile);*/
1692 startSyncFolderOperation(folder
, ignoreETag
);
1697 private void sortByDate(boolean ascending
){
1698 getListOfFilesFragment().sortByDate(ascending
);
1701 private void sortBySize(boolean ascending
){
1702 getListOfFilesFragment().sortBySize(ascending
);
1705 private void sortByName(boolean ascending
){
1706 getListOfFilesFragment().sortByName(ascending
);
1709 public void allFilesOption() {