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
.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
.CreateFolderOperation
;
81 import com
.owncloud
.android
.operations
.CreateShareOperation
;
82 import com
.owncloud
.android
.operations
.MoveFileOperation
;
83 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
84 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
85 import com
.owncloud
.android
.operations
.RenameFileOperation
;
86 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
87 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
88 import com
.owncloud
.android
.services
.observer
.FileObserverService
;
89 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
90 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
91 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
92 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
;
93 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
.OnSslUntrustedCertListener
;
94 import com
.owncloud
.android
.ui
.dialog
.UploadSourceDialogFragment
;
95 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
96 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
97 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
98 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
99 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
100 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
101 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
102 import com
.owncloud
.android
.utils
.DisplayUtils
;
103 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
104 import com
.owncloud
.android
.utils
.FileStorageUtils
;
105 import com
.owncloud
.android
.utils
.UriUtils
;
111 * Displays, what files the user has available in his ownCloud.
114 public class FileDisplayActivity
extends HookActivity
115 implements FileFragment
.ContainerActivity
,
116 OnSslUntrustedCertListener
, OnEnforceableRefreshListener
{
118 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
119 private UploadFinishReceiver mUploadFinishReceiver
;
120 private DownloadFinishReceiver mDownloadFinishReceiver
;
121 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
123 private boolean mDualPane
;
124 private View mLeftFragmentContainer
;
125 private View mRightFragmentContainer
;
126 private ProgressBar mProgressBar
;
128 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
129 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
130 private static final String KEY_WAITING_TO_SEND
= "WAITING_TO_SEND";
132 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
134 public static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
135 public static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
136 public static final int ACTION_MOVE_FILES
= 3;
138 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
140 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
141 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
143 private OCFile mWaitingToPreview
;
145 private boolean mSyncInProgress
= false
;
147 private static String DIALOG_UNTRUSTED_CERT
= "DIALOG_UNTRUSTED_CERT";
148 private static String DIALOG_CREATE_FOLDER
= "DIALOG_CREATE_FOLDER";
149 private static String DIALOG_UPLOAD_SOURCE
= "DIALOG_UPLOAD_SOURCE";
150 private static String DIALOG_CERT_NOT_SAVED
= "DIALOG_CERT_NOT_SAVED";
152 private OCFile mWaitingToSend
;
156 protected void onCreate(Bundle savedInstanceState
) {
157 Log_OC
.v(TAG
, "onCreate() start");
159 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account
162 /// grant that FileObserverService is watching favorite files
163 if (savedInstanceState
== null
) {
164 Intent initObserversIntent
= FileObserverService
.makeInitIntent(this);
165 startService(initObserversIntent
);
168 /// Load of saved instance state
169 if(savedInstanceState
!= null
) {
170 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(
171 FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
172 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
173 mWaitingToSend
= (OCFile
) savedInstanceState
.getParcelable(
174 FileDisplayActivity
.KEY_WAITING_TO_SEND
);
177 mWaitingToPreview
= null
;
178 mSyncInProgress
= false
;
179 mWaitingToSend
= null
;
184 // Inflate and set the layout view
185 setContentView(R
.layout
.files
);
190 mProgressBar
= (ProgressBar
) findViewById(R
.id
.progressBar
);
191 mProgressBar
.setIndeterminateDrawable(
192 ContextCompat
.getDrawable(this,
193 R
.drawable
.actionbar_progress_indeterminate_horizontal
));
195 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
196 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
197 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
198 if (savedInstanceState
== null
) {
199 createMinFragments();
203 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS,
204 // according to the official
207 // enable ActionBar app icon to behave as action to toggle nav drawer
208 //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
209 getSupportActionBar().setHomeButtonEnabled(true
);
211 mProgressBar
.setIndeterminate(mSyncInProgress
);
212 // always AFTER setContentView(...) ; to work around bug in its implementation
216 Log_OC
.v(TAG
, "onCreate() end");
220 protected void onStart() {
221 Log_OC
.v(TAG
, "onStart() start");
223 Log_OC
.v(TAG
, "onStart() end");
227 protected void onDestroy() {
228 Log_OC
.v(TAG
, "onDestroy() start");
230 Log_OC
.v(TAG
, "onDestroy() end");
234 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
237 protected void onAccountSet(boolean stateWasRecovered
) {
238 super.onAccountSet(stateWasRecovered
);
239 if (getAccount() != null
) {
240 /// Check whether the 'main' OCFile handled by the Activity is contained in the
242 OCFile file
= getFile();
243 // get parent from path
244 String parentPath
= "";
246 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
247 // upload in progress - right now, files are not inserted in the local
248 // cache until the upload is successful get parent from path
249 parentPath
= file
.getRemotePath().substring(0,
250 file
.getRemotePath().lastIndexOf(file
.getFileName()));
251 if (getStorageManager().getFileByPath(parentPath
) == null
)
252 file
= null
; // not able to know the directory where the file is uploading
254 file
= getStorageManager().getFileByPath(file
.getRemotePath());
255 // currentDir = null if not in the current Account
259 // fall back to root folder
260 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
264 if (mAccountWasSet
) {
265 setUsernameInDrawer((RelativeLayout
) findViewById(R
.id
.left_drawer
), getAccount());
268 if (!stateWasRecovered
) {
269 Log_OC
.d(TAG
, "Initializing Fragments in onAccountChanged..");
270 initFragmentsWithFile();
271 if (file
.isFolder()) {
272 startSyncFolderOperation(file
, false
);
276 updateFragmentsVisibility(!file
.isFolder());
277 updateActionBarTitleAndHomeButton(file
.isFolder() ? null
: file
);
282 private void createMinFragments() {
283 OCFileListFragment listOfFiles
= new OCFileListFragment();
284 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
285 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
286 transaction
.commit();
289 private void initFragmentsWithFile() {
290 if (getAccount() != null
&& getFile() != null
) {
292 OCFileListFragment listOfFiles
= getListOfFilesFragment();
293 if (listOfFiles
!= null
) {
294 listOfFiles
.listDirectory(getCurrentDir());
295 // TODO Enable when "On Device" is recovered
296 // listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
298 Log_OC
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
302 OCFile file
= getFile();
303 Fragment secondFragment
= chooseInitialSecondFragment(file
);
304 if (secondFragment
!= null
) {
305 setSecondFragment(secondFragment
);
306 updateFragmentsVisibility(true
);
307 updateActionBarTitleAndHomeButton(file
);
310 cleanSecondFragment();
314 Log_OC
.wtf(TAG
, "initFragments() called with invalid NULLs!");
315 if (getAccount() == null
) {
316 Log_OC
.wtf(TAG
, "\t account is NULL");
318 if (getFile() == null
) {
319 Log_OC
.wtf(TAG
, "\t file is NULL");
324 private Fragment
chooseInitialSecondFragment(OCFile file
) {
325 Fragment secondFragment
= null
;
326 if (file
!= null
&& !file
.isFolder()) {
327 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
328 && file
.getLastSyncDateForProperties() > 0 // temporal fix
330 int startPlaybackPosition
=
331 getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
333 getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
334 secondFragment
= new PreviewMediaFragment(file
, getAccount(),
335 startPlaybackPosition
, autoplay
);
338 secondFragment
= FileDetailFragment
.newInstance(file
, getAccount());
341 return secondFragment
;
346 * Replaces the second fragment managed by the activity with the received as
349 * Assumes never will be more than two fragments managed at the same time.
351 * @param fragment New second Fragment to set.
353 private void setSecondFragment(Fragment fragment
) {
354 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
355 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
356 transaction
.commit();
360 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
362 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
363 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
365 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
366 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
369 } else if (existsSecondFragment
) {
370 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
371 mLeftFragmentContainer
.setVisibility(View
.GONE
);
373 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
374 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
378 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
379 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
381 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
382 mRightFragmentContainer
.setVisibility(View
.GONE
);
388 private OCFileListFragment
getListOfFilesFragment() {
389 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(
390 FileDisplayActivity
.TAG_LIST_OF_FILES
);
391 if (listOfFiles
!= null
) {
392 return (OCFileListFragment
)listOfFiles
;
394 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
398 public FileFragment
getSecondFragment() {
399 Fragment second
= getSupportFragmentManager().findFragmentByTag(
400 FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
401 if (second
!= null
) {
402 return (FileFragment
)second
;
407 protected void cleanSecondFragment() {
408 Fragment second
= getSecondFragment();
409 if (second
!= null
) {
410 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
414 updateFragmentsVisibility(false
);
415 updateActionBarTitleAndHomeButton(null
);
418 protected void refreshListOfFilesFragment() {
419 OCFileListFragment fileListFragment
= getListOfFilesFragment();
420 if (fileListFragment
!= null
) {
421 fileListFragment
.listDirectory();
422 // TODO Enable when "On Device" is recovered ?
423 // fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
427 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
,
429 FileFragment secondFragment
= getSecondFragment();
430 boolean waitedPreview
= (mWaitingToPreview
!= null
&&
431 mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
432 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
433 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
434 OCFile fileInFragment
= detailsFragment
.getFile();
435 if (fileInFragment
!= null
&&
436 !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
437 // the user browsed to other file ; forget the automatic preview
438 mWaitingToPreview
= null
;
440 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
441 // grant that the right panel updates the progress bar
442 detailsFragment
.listenForTransferProgress();
443 detailsFragment
.updateFileDetails(true
, false
);
445 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
446 // update the right panel
447 boolean detailsFragmentChanged
= false
;
450 mWaitingToPreview
= getStorageManager().getFileById(
451 mWaitingToPreview
.getFileId()); // update the file from database,
452 // for the local storage path
453 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
454 startMediaPreview(mWaitingToPreview
, 0, true
);
455 detailsFragmentChanged
= true
;
457 getFileOperationsHelper().openFile(mWaitingToPreview
);
460 mWaitingToPreview
= null
;
462 if (!detailsFragmentChanged
) {
463 detailsFragment
.updateFileDetails(false
, (success
));
470 public boolean onPrepareOptionsMenu(Menu menu
) {
471 boolean drawerOpen
= mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
472 menu
.findItem(R
.id
.action_upload
).setVisible(!drawerOpen
);
473 menu
.findItem(R
.id
.action_create_dir
).setVisible(!drawerOpen
);
474 menu
.findItem(R
.id
.action_sort
).setVisible(!drawerOpen
);
475 menu
.findItem(R
.id
.action_sync_account
).setVisible(!drawerOpen
);
477 return super.onPrepareOptionsMenu(menu
);
481 public boolean onCreateOptionsMenu(Menu menu
) {
482 MenuInflater inflater
= getMenuInflater();
483 inflater
.inflate(R
.menu
.main_menu
, menu
);
489 public boolean onOptionsItemSelected(MenuItem item
) {
490 boolean retval
= true
;
491 switch (item
.getItemId()) {
492 case R
.id
.action_create_dir
: {
493 CreateFolderDialogFragment dialog
=
494 CreateFolderDialogFragment
.newInstance(getCurrentDir());
495 dialog
.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER
);
498 case R
.id
.action_sync_account
: {
499 startSynchronization();
502 case R
.id
.action_upload
: {
503 UploadSourceDialogFragment dialog
=
504 UploadSourceDialogFragment
.newInstance(getAccount());
505 dialog
.show(getSupportFragmentManager(), DIALOG_UPLOAD_SOURCE
);
509 case android
.R
.id
.home
: {
510 FileFragment second
= getSecondFragment();
511 OCFile currentDir
= getCurrentDir();
512 if (mDrawerLayout
.isDrawerOpen(GravityCompat
.START
)) {
513 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
514 } else if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
515 (second
!= null
&& second
.getFile() != null
)) {
519 mDrawerLayout
.openDrawer(GravityCompat
.START
);
523 case R
.id
.action_sort
: {
524 SharedPreferences appPreferences
= PreferenceManager
525 .getDefaultSharedPreferences(this);
527 // Read sorting order, default to sort by name ascending
528 Integer sortOrder
= appPreferences
529 .getInt("sortOrder", FileStorageUtils
.SORT_NAME
);
531 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
532 builder
.setTitle(R
.string
.actionbar_sort_title
)
533 .setSingleChoiceItems(R
.array
.actionbar_sortby
, sortOrder
,
534 new DialogInterface
.OnClickListener() {
535 public void onClick(DialogInterface dialog
, int which
) {
548 builder
.create().show();
552 retval
= super.onOptionsItemSelected(item
);
557 private void startSynchronization() {
558 Log_OC
.d(TAG
, "Got to start sync");
559 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
560 Log_OC
.d(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
561 ContentResolver
.cancelSync(null
, MainApp
.getAuthority());
562 // cancel the current synchronizations of any ownCloud account
563 Bundle bundle
= new Bundle();
564 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
565 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
566 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
567 MainApp
.getAuthority());
568 ContentResolver
.requestSync(
570 MainApp
.getAuthority(), bundle
);
572 Log_OC
.d(TAG
, "Requesting sync for " + getAccount().name
+ " at " +
573 MainApp
.getAuthority() + " with new API");
574 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
575 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
576 builder
.setExpedited(true
);
577 builder
.setManual(true
);
580 // Fix bug in Android Lollipop when you click on refresh the whole account
581 Bundle extras
= new Bundle();
582 builder
.setExtras(extras
);
584 SyncRequest request
= builder
.build();
585 ContentResolver
.requestSync(request
);
590 * Called, when the user selected something for uploading
593 @TargetApi(Build
.VERSION_CODES
.JELLY_BEAN
)
595 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
597 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
||
598 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
599 //getClipData is only supported on api level 16+, Jelly Bean
600 if (data
.getData() == null
&& Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
){
601 for( int i
= 0; i
< data
.getClipData().getItemCount(); i
++){
602 Intent intent
= new Intent();
603 intent
.setData(data
.getClipData().getItemAt(i
).getUri());
604 requestSimpleUpload(intent
, resultCode
);
607 requestSimpleUpload(data
, resultCode
);
609 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
||
610 resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
611 requestMultipleUpload(data
, resultCode
);
613 } else if (requestCode
== ACTION_MOVE_FILES
&& resultCode
== RESULT_OK
){
615 final Intent fData
= data
;
616 final int fResultCode
= resultCode
;
617 getHandler().postDelayed(
621 requestMoveOperation(fData
, fResultCode
);
624 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
628 super.onActivityResult(requestCode
, resultCode
, data
);
633 private void requestMultipleUpload(Intent data
, int resultCode
) {
634 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
635 if (filePaths
!= null
) {
636 String
[] remotePaths
= new String
[filePaths
.length
];
637 String remotePathBase
= getCurrentDir().getRemotePath();
638 for (int j
= 0; j
< remotePaths
.length
; j
++) {
639 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
642 Intent i
= new Intent(this, FileUploader
.class);
643 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
644 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
645 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
646 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
647 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
648 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
652 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
653 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
),
661 private void requestSimpleUpload(Intent data
, int resultCode
) {
662 String filePath
= null
;
663 String mimeType
= null
;
665 Uri selectedImageUri
= data
.getData();
668 mimeType
= getContentResolver().getType(selectedImageUri
);
670 String fileManagerString
= selectedImageUri
.getPath();
671 String selectedImagePath
= UriUtils
.getLocalPath(selectedImageUri
, this);
673 if (selectedImagePath
!= null
)
674 filePath
= selectedImagePath
;
676 filePath
= fileManagerString
;
678 } catch (Exception e
) {
679 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of " +
680 "Intent.ACTION_GET_CONTENT", e
);
683 if (filePath
== null
) {
684 Log_OC
.e(TAG
, "Couldn't resolve path to file");
685 Toast t
= Toast
.makeText(
686 this, getString(R
.string
.filedisplay_unexpected_bad_get_content
),
694 Intent i
= new Intent(this, FileUploader
.class);
695 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
696 OCFile currentDir
= getCurrentDir();
697 String remotePath
= (currentDir
!= null
) ? currentDir
.getRemotePath() : OCFile
.ROOT_PATH
;
699 if (filePath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
)) {
700 Cursor cursor
= getContentResolver().query(Uri
.parse(filePath
), null
, null
, null
, null
);
702 if (cursor
!= null
&& cursor
.moveToFirst()) {
703 String displayName
= cursor
.getString(cursor
.getColumnIndex(
704 OpenableColumns
.DISPLAY_NAME
));
705 Log_OC
.v(TAG
, "Display Name: " + displayName
);
707 displayName
.replace(File
.separatorChar
, '_');
708 displayName
.replace(File
.pathSeparatorChar
, '_');
709 remotePath
+= displayName
+ DisplayUtils
.getComposedFileExtension(filePath
);
712 // and what happens in case of error?; wrong target name for the upload
718 remotePath
+= new File(filePath
).getName();
721 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePath
);
722 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePath
);
723 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mimeType
);
724 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
725 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
726 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
731 * Request the operation for moving the file/folder from one path to another
733 * @param data Intent received
734 * @param resultCode Result code received
736 private void requestMoveOperation(Intent data
, int resultCode
) {
737 OCFile folderToMoveAt
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FOLDER
);
738 OCFile targetFile
= (OCFile
) data
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
739 getFileOperationsHelper().moveFile(folderToMoveAt
, targetFile
);
743 public void onBackPressed() {
744 if (!isDrawerOpen()){
745 OCFileListFragment listOfFiles
= getListOfFilesFragment();
746 if (mDualPane
|| getSecondFragment() == null
) {
747 OCFile currentDir
= getCurrentDir();
748 if (currentDir
== null
|| currentDir
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
) {
752 if (listOfFiles
!= null
) { // should never be null, indeed
753 listOfFiles
.onBrowseUp();
756 if (listOfFiles
!= null
) { // should never be null, indeed
757 setFile(listOfFiles
.getCurrentFile());
759 cleanSecondFragment();
761 super.onBackPressed();
766 protected void onSaveInstanceState(Bundle outState
) {
767 // responsibility of restore is preferred in onCreate() before than in
768 // onRestoreInstanceState when there are Fragments involved
769 Log_OC
.v(TAG
, "onSaveInstanceState() start");
770 super.onSaveInstanceState(outState
);
771 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
772 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
773 //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
774 // mRefreshSharesInProgress);
775 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_SEND
, mWaitingToSend
);
777 Log_OC
.v(TAG
, "onSaveInstanceState() end");
783 protected void onResume() {
784 Log_OC
.v(TAG
, "onResume() start");
787 // refresh Navigation Drawer account list
788 mNavigationDrawerAdapter
.updateAccountList();
791 // refresh list of files
792 refreshListOfFilesFragment();
794 // Listen for sync messages
795 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
796 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
797 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
798 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
799 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
800 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
801 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
802 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
803 // syncIntentFilter);
805 // Listen for upload messages
806 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
807 mUploadFinishReceiver
= new UploadFinishReceiver();
808 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
810 // Listen for download messages
811 IntentFilter downloadIntentFilter
= new IntentFilter(
812 FileDownloader
.getDownloadAddedMessage());
813 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
814 mDownloadFinishReceiver
= new DownloadFinishReceiver();
815 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
817 Log_OC
.v(TAG
, "onResume() end");
822 protected void onPause() {
823 Log_OC
.v(TAG
, "onPause() start");
824 if (mSyncBroadcastReceiver
!= null
) {
825 unregisterReceiver(mSyncBroadcastReceiver
);
826 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
827 mSyncBroadcastReceiver
= null
;
829 if (mUploadFinishReceiver
!= null
) {
830 unregisterReceiver(mUploadFinishReceiver
);
831 mUploadFinishReceiver
= null
;
833 if (mDownloadFinishReceiver
!= null
) {
834 unregisterReceiver(mDownloadFinishReceiver
);
835 mDownloadFinishReceiver
= null
;
839 Log_OC
.v(TAG
, "onPause() end");
843 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
846 * {@link BroadcastReceiver} to enable syncing feedback in UI
849 public void onReceive(Context context
, Intent intent
) {
851 String event
= intent
.getAction();
852 Log_OC
.d(TAG
, "Received broadcast " + event
);
853 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
854 String synchFolderRemotePath
=
855 intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
856 RemoteOperationResult synchResult
=
857 (RemoteOperationResult
)intent
.getSerializableExtra(
858 FileSyncAdapter
.EXTRA_RESULT
);
859 boolean sameAccount
= (getAccount() != null
&&
860 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
864 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
865 mSyncInProgress
= true
;
868 OCFile currentFile
= (getFile() == null
) ? null
:
869 getStorageManager().getFileByPath(getFile().getRemotePath());
870 OCFile currentDir
= (getCurrentDir() == null
) ? null
:
871 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
873 if (currentDir
== null
) {
874 // current folder was removed from the server
875 Toast
.makeText( FileDisplayActivity
.this,
878 sync_current_folder_was_removed
),
879 synchFolderRemotePath
),
885 if (currentFile
== null
&& !getFile().isFolder()) {
886 // currently selected file was removed in the server, and now we
888 cleanSecondFragment();
889 currentFile
= currentDir
;
892 if (synchFolderRemotePath
!= null
&&
893 currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
894 OCFileListFragment fileListFragment
= getListOfFilesFragment();
895 if (fileListFragment
!= null
) {
896 fileListFragment
.listDirectory();
897 // TODO Enable when "On Device" is recovered ?
898 // fileListFragment.listDirectory(currentDir,
899 // MainApp.getOnlyOnDevice());
902 setFile(currentFile
);
905 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
906 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
909 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
911 /// TODO refactor and make common
912 synchResult
!= null
&& !synchResult
.isSuccess() &&
913 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
914 synchResult
.isIdPRedirection() ||
915 (synchResult
.isException() && synchResult
.getException()
916 instanceof AuthenticatorException
))) {
920 OwnCloudClient client
;
921 OwnCloudAccount ocAccount
=
922 new OwnCloudAccount(getAccount(), context
);
923 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
924 removeClientFor(ocAccount
));
926 if (client
!= null
) {
927 OwnCloudCredentials cred
= client
.getCredentials();
929 AccountManager am
= AccountManager
.get(context
);
930 if (cred
.authTokenExpires()) {
931 am
.invalidateAuthToken(
936 am
.clearPassword(getAccount());
940 requestCredentialsUpdate();
942 } catch (AccountNotFoundException e
) {
943 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
948 removeStickyBroadcast(intent
);
949 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
950 mProgressBar
.setIndeterminate(mSyncInProgress
);
951 //mProgressBar.setVisibility((mSyncInProgress) ? View.VISIBLE : View.INVISIBLE);
952 //setSupportProgressBarIndeterminateVisibility(mSyncInProgress
953 /*|| mRefreshSharesInProgress*/ //);
959 if (synchResult
!= null
) {
960 if (synchResult
.getCode().equals(
961 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
962 mLastSslUntrustedServerResult
= synchResult
;
965 } catch (RuntimeException e
) {
966 // avoid app crashes after changing the serial id of RemoteOperationResult
967 // in owncloud library with broadcast notifications pending to process
968 removeStickyBroadcast(intent
);
974 * Show a text message on screen view for notifying user if content is
975 * loading or folder is empty
977 private void setBackgroundText() {
978 OCFileListFragment ocFileListFragment
= getListOfFilesFragment();
979 if (ocFileListFragment
!= null
) {
980 int message
= R
.string
.file_list_loading
;
981 if (!mSyncInProgress
) {
982 // In case file list is empty
983 message
= R
.string
.file_list_empty
;
985 ocFileListFragment
.setMessageForEmptyList(getString(message
));
987 Log_OC
.e(TAG
, "OCFileListFragment is null");
992 * Once the file upload has finished -> update view
994 private class UploadFinishReceiver
extends BroadcastReceiver
{
996 * Once the file upload has finished -> update view
997 * @author David A. Velasco
998 * {@link BroadcastReceiver} to enable upload feedback in UI
1001 public void onReceive(Context context
, Intent intent
) {
1003 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1004 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
1005 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
1006 OCFile currentDir
= getCurrentDir();
1007 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) &&
1008 (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1010 if (sameAccount
&& isDescendant
) {
1011 refreshListOfFilesFragment();
1014 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
,
1016 boolean renamedInUpload
= getFile().getRemotePath().
1017 equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
1018 boolean sameFile
= getFile().getRemotePath().equals(uploadedRemotePath
) ||
1020 FileFragment details
= getSecondFragment();
1021 boolean detailFragmentIsShown
= (details
!= null
&&
1022 details
instanceof FileDetailFragment
);
1024 if (sameAccount
&& sameFile
&& detailFragmentIsShown
) {
1025 if (uploadWasFine
) {
1026 setFile(getStorageManager().getFileByPath(uploadedRemotePath
));
1028 if (renamedInUpload
) {
1029 String newName
= (new File(uploadedRemotePath
)).getName();
1030 Toast msg
= Toast
.makeText(
1033 getString(R
.string
.filedetails_renamed_in_upload_msg
),
1038 if (uploadWasFine
|| getFile().fileExists()) {
1039 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1041 cleanSecondFragment();
1044 // Force the preview if the file is an image
1045 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
1046 startImagePreview(getFile());
1047 } // TODO what about other kind of previews?
1050 mProgressBar
.setIndeterminate(false
);
1052 if (intent
!= null
) {
1053 removeStickyBroadcast(intent
);
1063 * Class waiting for broadcast events from the {@link FileDownloader} service.
1065 * Updates the UI when a download is started or finished, provided that it is relevant for the
1068 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1070 //int refreshCounter = 0;
1072 public void onReceive(Context context
, Intent intent
) {
1074 boolean sameAccount
= isSameAccount(context
, intent
);
1075 String downloadedRemotePath
=
1076 intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1077 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1079 if (sameAccount
&& isDescendant
) {
1080 String linkedToRemotePath
=
1081 intent
.getStringExtra(FileDownloader
.EXTRA_LINKED_TO_PATH
);
1082 if (linkedToRemotePath
== null
|| isAscendant(linkedToRemotePath
)) {
1083 //Log_OC.v(TAG, "refresh #" + ++refreshCounter);
1084 refreshListOfFilesFragment();
1086 refreshSecondFragment(
1088 downloadedRemotePath
,
1089 intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)
1093 if (mWaitingToSend
!= null
) {
1095 getStorageManager().getFileByPath(mWaitingToSend
.getRemotePath());
1096 if (mWaitingToSend
.isDown()) {
1097 sendDownloadedFile();
1102 if (intent
!= null
) {
1103 removeStickyBroadcast(intent
);
1108 private boolean isDescendant(String downloadedRemotePath
) {
1109 OCFile currentDir
= getCurrentDir();
1111 currentDir
!= null
&&
1112 downloadedRemotePath
!= null
&&
1113 downloadedRemotePath
.startsWith(currentDir
.getRemotePath())
1117 private boolean isAscendant(String linkedToRemotePath
) {
1118 OCFile currentDir
= getCurrentDir();
1120 currentDir
!= null
&&
1121 currentDir
.getRemotePath().startsWith(linkedToRemotePath
)
1125 private boolean isSameAccount(Context context
, Intent intent
) {
1126 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1127 return (accountName
!= null
&& getAccount() != null
&&
1128 accountName
.equals(getAccount().name
));
1133 public void browseToRoot() {
1134 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1135 if (listOfFiles
!= null
) { // should never be null, indeed
1136 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
1137 listOfFiles
.listDirectory(root
);
1138 // TODO Enable when "On Device" is recovered ?
1139 // listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
1140 setFile(listOfFiles
.getCurrentFile());
1141 startSyncFolderOperation(root
, false
);
1143 cleanSecondFragment();
1151 * Updates action bar and second fragment, if in dual pane mode.
1154 public void onBrowsedDownTo(OCFile directory
) {
1156 cleanSecondFragment();
1158 startSyncFolderOperation(directory
, false
);
1162 * Shows the information of the {@link OCFile} received as a
1163 * parameter in the second fragment.
1165 * @param file {@link OCFile} whose details will be shown
1168 public void showDetails(OCFile file
) {
1169 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1170 setSecondFragment(detailFragment
);
1171 updateFragmentsVisibility(true
);
1172 updateActionBarTitleAndHomeButton(file
);
1177 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
1179 // in dual pane mode, keep the focus of title an action bar in the current folder
1180 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1183 super.updateActionBarTitleAndHomeButton(chosenFile
);
1189 protected ServiceConnection
newTransferenceServiceConnection() {
1190 return new ListServiceConnection();
1193 /** Defines callbacks for service binding, passed to bindService() */
1194 private class ListServiceConnection
implements ServiceConnection
{
1197 public void onServiceConnected(ComponentName component
, IBinder service
) {
1198 if (component
.equals(new ComponentName(
1199 FileDisplayActivity
.this, FileDownloader
.class))) {
1200 Log_OC
.d(TAG
, "Download service connected");
1201 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1202 if (mWaitingToPreview
!= null
)
1203 if (getStorageManager() != null
) {
1206 getStorageManager().getFileById(mWaitingToPreview
.getFileId());
1207 if (!mWaitingToPreview
.isDown()) {
1208 requestForDownload();
1212 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1213 FileUploader
.class))) {
1214 Log_OC
.d(TAG
, "Upload service connected");
1215 mUploaderBinder
= (FileUploaderBinder
) service
;
1219 // a new chance to get the mDownloadBinder through
1220 // getFileDownloadBinder() - THIS IS A MESS
1221 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1222 if (listOfFiles
!= null
) {
1223 listOfFiles
.listDirectory();
1224 // TODO Enable when "On Device" is recovered ?
1225 // listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1227 FileFragment secondFragment
= getSecondFragment();
1228 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1229 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1230 detailFragment
.listenForTransferProgress();
1231 detailFragment
.updateFileDetails(false
, false
);
1236 public void onServiceDisconnected(ComponentName component
) {
1237 if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1238 FileDownloader
.class))) {
1239 Log_OC
.d(TAG
, "Download service disconnected");
1240 mDownloaderBinder
= null
;
1241 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this,
1242 FileUploader
.class))) {
1243 Log_OC
.d(TAG
, "Upload service disconnected");
1244 mUploaderBinder
= null
;
1250 public void onSavedCertificate() {
1251 startSyncFolderOperation(getCurrentDir(), false
);
1256 public void onFailedSavingCertificate() {
1257 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
1258 R
.string
.ssl_validator_not_saved
, new String
[]{}, R
.string
.common_ok
, -1, -1
1260 dialog
.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED
);
1264 public void onCancelCertificate() {
1269 * Updates the view associated to the activity after the finish of some operation over files
1270 * in the current account.
1272 * @param operation Removal operation performed.
1273 * @param result Result of the removal.
1276 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1277 super.onRemoteOperationFinish(operation
, result
);
1279 if (operation
instanceof RemoveFileOperation
) {
1280 onRemoveFileOperationFinish((RemoveFileOperation
) operation
, result
);
1282 } else if (operation
instanceof RenameFileOperation
) {
1283 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1285 } else if (operation
instanceof SynchronizeFileOperation
) {
1286 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1288 } else if (operation
instanceof CreateFolderOperation
) {
1289 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1291 } else if (operation
instanceof CreateShareOperation
) {
1292 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1294 } else if (operation
instanceof UnshareLinkOperation
) {
1295 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
1297 } else if (operation
instanceof MoveFileOperation
) {
1298 onMoveFileOperationFinish((MoveFileOperation
)operation
, result
);
1304 private void onCreateShareOperationFinish(CreateShareOperation operation
,
1305 RemoteOperationResult result
) {
1306 if (result
.isSuccess()) {
1307 refreshShowDetails();
1308 refreshListOfFilesFragment();
1313 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
1314 RemoteOperationResult result
) {
1315 if (result
.isSuccess()) {
1316 refreshShowDetails();
1317 refreshListOfFilesFragment();
1319 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
1320 cleanSecondFragment();
1321 refreshListOfFilesFragment();
1325 private void refreshShowDetails() {
1326 FileFragment details
= getSecondFragment();
1327 if (details
!= null
) {
1328 OCFile file
= details
.getFile();
1330 file
= getStorageManager().getFileByPath(file
.getRemotePath());
1331 if (details
instanceof PreviewMediaFragment
) {
1332 // Refresh OCFile of the fragment
1333 ((PreviewMediaFragment
) details
).updateFile(file
);
1338 invalidateOptionsMenu();
1343 * Updates the view associated to the activity after the finish of an operation trying to
1346 * @param operation Removal operation performed.
1347 * @param result Result of the removal.
1349 private void onRemoveFileOperationFinish(RemoveFileOperation operation
,
1350 RemoteOperationResult result
) {
1351 dismissLoadingDialog();
1353 Toast msg
= Toast
.makeText(this,
1354 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1358 if (result
.isSuccess()) {
1359 OCFile removedFile
= operation
.getFile();
1360 FileFragment second
= getSecondFragment();
1361 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1362 if (second
instanceof PreviewMediaFragment
) {
1363 ((PreviewMediaFragment
)second
).stopPreview(true
);
1365 setFile(getStorageManager().getFileById(removedFile
.getParentId()));
1366 cleanSecondFragment();
1368 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1369 refreshListOfFilesFragment();
1371 invalidateOptionsMenu();
1373 if (result
.isSslRecoverableException()) {
1374 mLastSslUntrustedServerResult
= result
;
1375 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1382 * Updates the view associated to the activity after the finish of an operation trying to move a
1385 * @param operation Move operation performed.
1386 * @param result Result of the move operation.
1388 private void onMoveFileOperationFinish(MoveFileOperation operation
,
1389 RemoteOperationResult result
) {
1390 if (result
.isSuccess()) {
1391 dismissLoadingDialog();
1392 refreshListOfFilesFragment();
1394 dismissLoadingDialog();
1396 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1397 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1401 } catch (NotFoundException e
) {
1402 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1409 * Updates the view associated to the activity after the finish of an operation trying to rename
1412 * @param operation Renaming operation performed.
1413 * @param result Result of the renaming.
1415 private void onRenameFileOperationFinish(RenameFileOperation operation
,
1416 RemoteOperationResult result
) {
1417 dismissLoadingDialog();
1418 OCFile renamedFile
= operation
.getFile();
1419 if (result
.isSuccess()) {
1420 FileFragment details
= getSecondFragment();
1421 if (details
!= null
) {
1422 if (details
instanceof FileDetailFragment
&&
1423 renamedFile
.equals(details
.getFile()) ) {
1424 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1425 showDetails(renamedFile
);
1427 } else if (details
instanceof PreviewMediaFragment
&&
1428 renamedFile
.equals(details
.getFile())) {
1429 ((PreviewMediaFragment
) details
).updateFile(renamedFile
);
1430 if (PreviewMediaFragment
.canBePreviewed(renamedFile
)) {
1431 int position
= ((PreviewMediaFragment
)details
).getPosition();
1432 startMediaPreview(renamedFile
, position
, true
);
1434 getFileOperationsHelper().openFile(renamedFile
);
1439 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())){
1440 refreshListOfFilesFragment();
1444 Toast msg
= Toast
.makeText(this,
1445 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1449 if (result
.isSslRecoverableException()) {
1450 mLastSslUntrustedServerResult
= result
;
1451 showUntrustedCertDialog(mLastSslUntrustedServerResult
);
1456 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
1457 RemoteOperationResult result
) {
1458 if (result
.isSuccess()) {
1459 if (operation
.transferWasRequested()) {
1460 OCFile syncedFile
= operation
.getLocalFile();
1461 onTransferStateChanged(syncedFile
, true
, true
);
1462 invalidateOptionsMenu();
1468 * Updates the view associated to the activity after the finish of an operation trying create a
1471 * @param operation Creation operation performed.
1472 * @param result Result of the creation.
1474 private void onCreateFolderOperationFinish(CreateFolderOperation operation
,
1475 RemoteOperationResult result
) {
1476 if (result
.isSuccess()) {
1477 dismissLoadingDialog();
1478 refreshListOfFilesFragment();
1480 dismissLoadingDialog();
1482 Toast msg
= Toast
.makeText(FileDisplayActivity
.this,
1483 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
1487 } catch (NotFoundException e
) {
1488 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1498 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1499 refreshListOfFilesFragment();
1500 FileFragment details
= getSecondFragment();
1501 if (details
!= null
&& details
instanceof FileDetailFragment
&&
1502 file
.equals(details
.getFile()) ) {
1503 if (downloading
|| uploading
) {
1504 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1506 if (!file
.fileExists()) {
1507 cleanSecondFragment();
1509 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1517 private void requestForDownload() {
1518 Account account
= getAccount();
1519 //if (!mWaitingToPreview.isDownloading()) {
1520 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1521 Intent i
= new Intent(this, FileDownloader
.class);
1522 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1523 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1529 private OCFile
getCurrentDir() {
1530 OCFile file
= getFile();
1532 if (file
.isFolder()) {
1534 } else if (getStorageManager() != null
) {
1535 String parentPath
= file
.getRemotePath().substring(0,
1536 file
.getRemotePath().lastIndexOf(file
.getFileName()));
1537 return getStorageManager().getFileByPath(parentPath
);
1543 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
1544 long currentSyncTime
= System
.currentTimeMillis();
1546 mSyncInProgress
= true
;
1548 // perform folder synchronization
1549 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
1552 getFileOperationsHelper().isSharedSupported(),
1554 getStorageManager(),
1556 getApplicationContext()
1558 synchFolderOp
.execute(getAccount(), MainApp
.getAppContext(), this, null
, null
);
1559 mProgressBar
.setIndeterminate(true
);
1561 setBackgroundText();
1565 * Show untrusted cert dialog
1567 public void showUntrustedCertDialog(RemoteOperationResult result
) {
1568 // Show a dialog with the certificate info
1569 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.newInstanceForFullSslError(
1570 (CertificateCombinedException
) result
.getException());
1571 FragmentManager fm
= getSupportFragmentManager();
1572 FragmentTransaction ft
= fm
.beginTransaction();
1573 dialog
.show(ft
, DIALOG_UNTRUSTED_CERT
);
1576 private void requestForDownload(OCFile file
) {
1577 Account account
= getAccount();
1578 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1579 Intent i
= new Intent(this, FileDownloader
.class);
1580 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1581 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
1586 private void sendDownloadedFile(){
1587 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend
);
1588 mWaitingToSend
= null
;
1593 * Requests the download of the received {@link OCFile} , updates the UI
1594 * to monitor the download progress and prepares the activity to send the file
1595 * when the download finishes.
1597 * @param file {@link OCFile} to download and preview.
1599 public void startDownloadForSending(OCFile file
) {
1600 mWaitingToSend
= file
;
1601 requestForDownload(mWaitingToSend
);
1602 boolean hasSecondFragment
= (getSecondFragment()!= null
);
1603 updateFragmentsVisibility(hasSecondFragment
);
1607 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1609 * @param file Image {@link OCFile} to show.
1611 public void startImagePreview(OCFile file
) {
1612 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1613 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1614 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1615 startActivity(showDetailsIntent
);
1620 * Stars the preview of an already down media {@link OCFile}.
1622 * @param file Media {@link OCFile} to preview.
1623 * @param startPlaybackPosition Media position where the playback will be started,
1625 * @param autoplay When 'true', the playback will start without user
1628 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1629 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
,
1631 setSecondFragment(mediaFragment
);
1632 updateFragmentsVisibility(true
);
1633 updateActionBarTitleAndHomeButton(file
);
1638 * Requests the download of the received {@link OCFile} , updates the UI
1639 * to monitor the download progress and prepares the activity to preview
1640 * or open the file when the download finishes.
1642 * @param file {@link OCFile} to download and preview.
1644 public void startDownloadForPreview(OCFile file
) {
1645 Fragment detailFragment
= FileDetailFragment
.newInstance(file
, getAccount());
1646 setSecondFragment(detailFragment
);
1647 mWaitingToPreview
= file
;
1648 requestForDownload();
1649 updateFragmentsVisibility(true
);
1650 updateActionBarTitleAndHomeButton(file
);
1655 public void cancelTransference(OCFile file
) {
1656 getFileOperationsHelper().cancelTransference(file
);
1657 if (mWaitingToPreview
!= null
&&
1658 mWaitingToPreview
.getRemotePath().equals(file
.getRemotePath())) {
1659 mWaitingToPreview
= null
;
1661 if (mWaitingToSend
!= null
&&
1662 mWaitingToSend
.getRemotePath().equals(file
.getRemotePath())) {
1663 mWaitingToSend
= null
;
1665 onTransferStateChanged(file
, false
, false
);
1669 public void onRefresh(boolean ignoreETag
) {
1670 refreshList(ignoreETag
);
1674 public void onRefresh() {
1678 private void refreshList(boolean ignoreETag
) {
1679 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1680 if (listOfFiles
!= null
) {
1681 OCFile folder
= listOfFiles
.getCurrentFile();
1682 if (folder
!= null
) {
1683 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1684 listDirectory(mFile);*/
1685 startSyncFolderOperation(folder
, ignoreETag
);
1690 private void sortByDate(boolean ascending
){
1691 getListOfFilesFragment().sortByDate(ascending
);
1694 private void sortBySize(boolean ascending
){
1695 getListOfFilesFragment().sortBySize(ascending
);
1698 private void sortByName(boolean ascending
){
1699 getListOfFilesFragment().sortByName(ascending
);
1702 public void allFilesOption() {