1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.activity
;
23 import android
.accounts
.Account
;
24 import android
.app
.AlertDialog
;
25 import android
.app
.ProgressDialog
;
26 import android
.app
.Dialog
;
27 import android
.content
.BroadcastReceiver
;
28 import android
.content
.ComponentName
;
29 import android
.content
.ContentResolver
;
30 import android
.content
.Context
;
31 import android
.content
.DialogInterface
;
32 import android
.content
.DialogInterface
.OnClickListener
;
33 import android
.content
.Intent
;
34 import android
.content
.IntentFilter
;
35 import android
.content
.ServiceConnection
;
36 import android
.content
.SharedPreferences
;
37 import android
.content
.res
.Configuration
;
38 import android
.content
.res
.Resources
.NotFoundException
;
39 import android
.database
.Cursor
;
40 import android
.net
.Uri
;
41 import android
.os
.Bundle
;
42 import android
.os
.Handler
;
43 import android
.os
.IBinder
;
44 import android
.preference
.PreferenceManager
;
45 import android
.provider
.MediaStore
;
46 import android
.support
.v4
.app
.Fragment
;
47 import android
.support
.v4
.app
.FragmentTransaction
;
48 import android
.view
.View
;
49 import android
.view
.ViewGroup
;
50 import android
.widget
.ArrayAdapter
;
51 import android
.widget
.TextView
;
52 import android
.widget
.Toast
;
54 import com
.actionbarsherlock
.app
.ActionBar
;
55 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
56 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
57 import com
.actionbarsherlock
.view
.Menu
;
58 import com
.actionbarsherlock
.view
.MenuInflater
;
59 import com
.actionbarsherlock
.view
.MenuItem
;
60 import com
.actionbarsherlock
.view
.Window
;
61 import com
.owncloud
.android
.AccountUtils
;
62 import com
.owncloud
.android
.Log_OC
;
63 import com
.owncloud
.android
.R
;
64 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
65 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
66 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
67 import com
.owncloud
.android
.datamodel
.OCFile
;
68 import com
.owncloud
.android
.files
.services
.FileDownloader
;
69 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
70 import com
.owncloud
.android
.files
.services
.FileObserverService
;
71 import com
.owncloud
.android
.files
.services
.FileUploader
;
72 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
73 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
74 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
75 import com
.owncloud
.android
.operations
.RemoteOperation
;
76 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
77 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
78 import com
.owncloud
.android
.operations
.RenameFileOperation
;
79 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
80 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
81 import com
.owncloud
.android
.syncadapter
.FileSyncService
;
82 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
83 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
;
84 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
85 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
.OnSslValidatorListener
;
86 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
87 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
88 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
89 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
90 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
91 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
92 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
95 * Displays, what files the user has available in his ownCloud.
97 * @author Bartek Przybylski
98 * @author David A. Velasco
101 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
102 OCFileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
, OnSslValidatorListener
, OnRemoteOperationListener
, EditNameDialogListener
{
104 private ArrayAdapter
<String
> mDirectories
;
105 private OCFile mCurrentDir
= null
;
106 private OCFile mCurrentFile
= null
;
108 private DataStorageManager mStorageManager
;
109 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
110 private UploadFinishReceiver mUploadFinishReceiver
;
111 private DownloadFinishReceiver mDownloadFinishReceiver
;
112 private FileDownloaderBinder mDownloaderBinder
= null
;
113 private FileUploaderBinder mUploaderBinder
= null
;
114 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
115 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
117 private OCFileListFragment mFileList
;
119 private boolean mDualPane
;
120 private boolean mBackFromCreatingFirstAccount
;
122 private static final int DIALOG_SETUP_ACCOUNT
= 0;
123 public static final int DIALOG_SHORT_WAIT
= 1;
124 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 2;
125 private static final int DIALOG_SSL_VALIDATOR
= 3;
126 private static final int DIALOG_CERT_NOT_SAVED
= 4;
128 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
129 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
131 private static final String TAG
= "FileDisplayActivity";
133 private OCFile mWaitingToPreview
;
134 private Handler mHandler
;
136 private Configuration mNewConfigurationChangeToApplyOnStart
;
137 private boolean mStarted
;
140 public void onCreate(Bundle savedInstanceState
) {
141 Log_OC
.d(getClass().toString(), "onCreate() start");
142 super.onCreate(savedInstanceState
);
145 mHandler
= new Handler();
147 /// Load of parameters from received intent
148 Account account
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
149 if (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(this, account
.name
)) {
150 mCurrentDir
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
153 /// Load of saved instance state: keep this always before initDataFromCurrentAccount()
154 if(savedInstanceState
!= null
) {
155 // TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER. SOME TIMES
156 mCurrentDir
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
157 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(FileDetailActivity
.KEY_WAITING_TO_PREVIEW
);
160 mWaitingToPreview
= null
;
163 if (!AccountUtils
.accountsAreSetup(this)) {
164 /// no account available: FORCE ACCOUNT CREATION
165 mStorageManager
= null
;
166 createFirstAccount();
168 } else { /// at least an account is available
170 initDataFromCurrentAccount(); // it checks mCurrentDir and mCurrentFile with the current account
174 mUploadConnection
= new ListServiceConnection();
175 mDownloadConnection
= new ListServiceConnection();
176 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
177 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
179 // PIN CODE request ; best location is to decide, let's try this first
180 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
185 Intent observer_intent
= new Intent(this, FileObserverService
.class);
186 observer_intent
.putExtra(FileObserverService
.KEY_FILE_CMD
, FileObserverService
.CMD_INIT_OBSERVED_LIST
);
187 startService(observer_intent
);
191 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
193 // Drop-down navigation
194 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
195 OCFile currFile
= mCurrentDir
;
196 while(mStorageManager
!= null
&& currFile
!= null
&& currFile
.getFileName() != OCFile
.PATH_SEPARATOR
) {
197 mDirectories
.add(currFile
.getFileName());
198 currFile
= mStorageManager
.getFileById(currFile
.getParentId());
200 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
202 // Inflate and set the layout view
203 setContentView(R
.layout
.files
);
204 mFileList
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
205 mDualPane
= (findViewById(R
.id
.file_details_container
) != null
);
206 if (mDualPane
&& savedInstanceState
== null
) {
207 initFileDetailsInDualPane();
211 ActionBar actionBar
= getSupportActionBar();
212 actionBar
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
213 actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0);
214 actionBar
.setDisplayShowTitleEnabled(false
);
215 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
216 actionBar
.setListNavigationCallbacks(mDirectories
, this);
217 setSupportProgressBarIndeterminateVisibility(false
); // always AFTER setContentView(...) ; to workaround bug in its implementation
220 mBackFromCreatingFirstAccount
= false
;
222 Log_OC
.d(getClass().toString(), "onCreate() end");
227 public void onConfigurationChanged (Configuration newConfig
) {
228 super.onConfigurationChanged(newConfig
);
230 checkConfigurationChange(newConfig
);
232 mNewConfigurationChangeToApplyOnStart
= newConfig
;
238 * Launches the account creation activity. To use when no ownCloud account is available
240 private void createFirstAccount() {
241 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
242 intent
.putExtra(android
.provider
.Settings
.EXTRA_AUTHORITIES
, new String
[] { AccountAuthenticator
.AUTHORITY
});
243 startActivity(intent
); // the new activity won't be created until this.onStart() and this.onResume() are finished;
248 * Load of state dependent of the existence of an ownCloud account
250 private void initDataFromCurrentAccount() {
251 /// Storage manager initialization - access to local database
252 mStorageManager
= new FileDataStorageManager(
253 AccountUtils
.getCurrentOwnCloudAccount(this),
254 getContentResolver());
256 /// Check if mCurrentDir is a directory
257 if(mCurrentDir
!= null
&& !mCurrentDir
.isDirectory()) {
258 mCurrentFile
= mCurrentDir
;
259 mCurrentDir
= mStorageManager
.getFileById(mCurrentDir
.getParentId());
262 /// Check if mCurrentDir and mCurrentFile are in the current account, and update them
263 if (mCurrentDir
!= null
) {
264 mCurrentDir
= mStorageManager
.getFileByPath(mCurrentDir
.getRemotePath()); // mCurrentDir == null if it is not in the current account
266 if (mCurrentFile
!= null
) {
267 if (mCurrentFile
.fileExists()) {
268 mCurrentFile
= mStorageManager
.getFileByPath(mCurrentFile
.getRemotePath()); // mCurrentFile == null if it is not in the current account
269 } // else : keep mCurrentFile with the received value; this is currently the case of an upload in progress, when the user presses the status notification in a landscape tablet
272 /// Default to root if mCurrentDir was not found
273 if (mCurrentDir
== null
) {
274 mCurrentDir
= mStorageManager
.getFileByPath("/"); // will be NULL if the database was never synchronized
279 private void initFileDetailsInDualPane() {
280 if (mDualPane
&& getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
) == null
) {
281 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
282 if (mCurrentFile
!= null
) {
283 if (PreviewMediaFragment
.canBePreviewed(mCurrentFile
)) {
284 if (mCurrentFile
.isDown()) {
285 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
286 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
287 transaction
.replace(R
.id
.file_details_container
, new PreviewMediaFragment(mCurrentFile
, AccountUtils
.getCurrentOwnCloudAccount(this), startPlaybackPosition
, autoplay
), FileDetailFragment
.FTAG
);
289 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(mCurrentFile
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
290 mWaitingToPreview
= mCurrentFile
;
293 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(mCurrentFile
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
298 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
); // empty FileDetailFragment
300 transaction
.commit();
306 public void onStart() {
308 if (mNewConfigurationChangeToApplyOnStart
!= null
) {
309 checkConfigurationChange(mNewConfigurationChangeToApplyOnStart
);
310 mNewConfigurationChangeToApplyOnStart
= null
;
316 public void onStop() {
322 private void checkConfigurationChange(Configuration newConfig
) {
323 FileFragment fragment
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
325 && fragment
.getFile() != null
326 && (newConfig
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
327 && newConfig
.orientation
!= Configuration
.ORIENTATION_LANDSCAPE
) {
329 onFileClick(fragment
.getFile(), true
);
330 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
331 transaction
.remove((Fragment
)fragment
);
332 transaction
.commit();
336 Intent intent
= new Intent(this, FileDisplayActivity
.class);
337 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
338 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
339 startActivity(intent
);
345 public void onDestroy() {
347 if (mDownloadConnection
!= null
)
348 unbindService(mDownloadConnection
);
349 if (mUploadConnection
!= null
)
350 unbindService(mUploadConnection
);
355 public boolean onCreateOptionsMenu(Menu menu
) {
356 MenuInflater inflater
= getSherlock().getMenuInflater();
357 inflater
.inflate(R
.menu
.main_menu
, menu
);
363 public boolean onOptionsItemSelected(MenuItem item
) {
364 boolean retval
= true
;
365 switch (item
.getItemId()) {
366 case R
.id
.action_create_dir
: {
367 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.uploader_info_dirname
), "", -1, -1, this);
368 dialog
.show(getSupportFragmentManager(), "createdirdialog");
371 case R
.id
.action_sync_account
: {
372 startSynchronization();
375 case R
.id
.action_upload
: {
376 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
379 case R
.id
.action_settings
: {
380 Intent settingsIntent
= new Intent(this, Preferences
.class);
381 startActivity(settingsIntent
);
384 case android
.R
.id
.home
: {
385 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
391 retval
= super.onOptionsItemSelected(item
);
396 private void startSynchronization() {
397 ContentResolver
.cancelSync(null
, AccountAuthenticator
.AUTHORITY
); // cancel the current synchronizations of any ownCloud account
398 Bundle bundle
= new Bundle();
399 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
400 ContentResolver
.requestSync(
401 AccountUtils
.getCurrentOwnCloudAccount(this),
402 AccountAuthenticator
.AUTHORITY
, bundle
);
407 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
408 int i
= itemPosition
;
412 // the next operation triggers a new call to this method, but it's necessary to
413 // ensure that the name exposed in the action bar is the current directory when the
414 // user selected it in the navigation list
415 if (itemPosition
!= 0)
416 getSupportActionBar().setSelectedNavigationItem(0);
421 * Called, when the user selected something for uploading
423 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
424 super.onActivityResult(requestCode
, resultCode
, data
);
426 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
427 requestSimpleUpload(data
, resultCode
);
429 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
430 requestMultipleUpload(data
, resultCode
);
435 private void requestMultipleUpload(Intent data
, int resultCode
) {
436 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
437 if (filePaths
!= null
) {
438 String
[] remotePaths
= new String
[filePaths
.length
];
439 String remotePathBase
= "";
440 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
441 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
443 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
444 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
445 for (int j
= 0; j
< remotePaths
.length
; j
++) {
446 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
449 Intent i
= new Intent(this, FileUploader
.class);
450 i
.putExtra(FileUploader
.KEY_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
451 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
452 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
453 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
454 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
455 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
459 Log_OC
.d("FileDisplay", "User clicked on 'Update' with no selection");
460 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
467 private void requestSimpleUpload(Intent data
, int resultCode
) {
468 String filepath
= null
;
470 Uri selectedImageUri
= data
.getData();
472 String filemanagerstring
= selectedImageUri
.getPath();
473 String selectedImagePath
= getPath(selectedImageUri
);
475 if (selectedImagePath
!= null
)
476 filepath
= selectedImagePath
;
478 filepath
= filemanagerstring
;
480 } catch (Exception e
) {
481 Log_OC
.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
485 if (filepath
== null
) {
486 Log_OC
.e("FileDisplay", "Couldnt resolve path to file");
487 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
493 Intent i
= new Intent(this, FileUploader
.class);
494 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
495 AccountUtils
.getCurrentOwnCloudAccount(this));
496 String remotepath
= new String();
497 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
498 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
500 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
501 remotepath
+= OCFile
.PATH_SEPARATOR
;
502 remotepath
+= new File(filepath
).getName();
504 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
505 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
506 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
507 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
508 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
514 public void onBackPressed() {
515 if (mDirectories
.getCount() <= 1) {
520 mFileList
.onNavigateUp();
521 mCurrentDir
= mFileList
.getCurrentFile();
524 // Resets the FileDetailsFragment on Tablets so that it always displays
525 Fragment fileFragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
526 if (fileFragment
!= null
&& (fileFragment
instanceof PreviewMediaFragment
|| !((FileDetailFragment
) fileFragment
).isEmpty())) {
527 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
528 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
); // empty FileDetailFragment
529 transaction
.commit();
533 if(mCurrentDir
.getParentId() == 0){
534 ActionBar actionBar
= getSupportActionBar();
535 actionBar
.setDisplayHomeAsUpEnabled(false
);
540 protected void onSaveInstanceState(Bundle outState
) {
541 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
542 Log_OC
.d(getClass().toString(), "onSaveInstanceState() start");
543 super.onSaveInstanceState(outState
);
544 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
546 FileFragment fragment
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
547 if (fragment
!= null
) {
548 OCFile file
= fragment
.getFile();
550 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, file
);
554 outState
.putParcelable(FileDetailActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
555 Log_OC
.d(getClass().toString(), "onSaveInstanceState() end");
559 protected void onResume() {
560 Log_OC
.d(getClass().toString(), "onResume() start");
563 if (AccountUtils
.accountsAreSetup(this)) {
565 if (mStorageManager
== null
) {
566 // this is necessary for handling the come back to FileDisplayActivity when the first ownCloud account is created
567 initDataFromCurrentAccount();
569 initFileDetailsInDualPane();
571 mBackFromCreatingFirstAccount
= true
;
574 // Listen for sync messages
575 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
576 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
577 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
579 // Listen for upload messages
580 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
581 mUploadFinishReceiver
= new UploadFinishReceiver();
582 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
584 // Listen for download messages
585 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
586 downloadIntentFilter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
587 mDownloadFinishReceiver
= new DownloadFinishReceiver();
588 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
590 // List current directory
591 mFileList
.listDirectory(mCurrentDir
); // TODO we should find the way to avoid the need of this (maybe it's not necessary yet; to check)
595 mStorageManager
= null
; // an invalid object will be there if all the ownCloud accounts are removed
596 showDialog(DIALOG_SETUP_ACCOUNT
);
599 Log_OC
.d(getClass().toString(), "onResume() end");
604 protected void onPause() {
605 Log_OC
.d(getClass().toString(), "onPause() start");
607 if (mSyncBroadcastReceiver
!= null
) {
608 unregisterReceiver(mSyncBroadcastReceiver
);
609 mSyncBroadcastReceiver
= null
;
611 if (mUploadFinishReceiver
!= null
) {
612 unregisterReceiver(mUploadFinishReceiver
);
613 mUploadFinishReceiver
= null
;
615 if (mDownloadFinishReceiver
!= null
) {
616 unregisterReceiver(mDownloadFinishReceiver
);
617 mDownloadFinishReceiver
= null
;
619 if (!AccountUtils
.accountsAreSetup(this)) {
620 dismissDialog(DIALOG_SETUP_ACCOUNT
);
623 Log_OC
.d(getClass().toString(), "onPause() end");
628 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
629 if (id
== DIALOG_SSL_VALIDATOR
&& mLastSslUntrustedServerResult
!= null
) {
630 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
636 protected Dialog
onCreateDialog(int id
) {
637 Dialog dialog
= null
;
638 AlertDialog
.Builder builder
;
640 case DIALOG_SETUP_ACCOUNT
: {
641 builder
= new AlertDialog
.Builder(this);
642 builder
.setTitle(R
.string
.main_tit_accsetup
);
643 builder
.setMessage(R
.string
.main_wrn_accsetup
);
644 builder
.setCancelable(false
);
645 builder
.setPositiveButton(android
.R
.string
.ok
, new OnClickListener() {
646 public void onClick(DialogInterface dialog
, int which
) {
647 createFirstAccount();
651 String message
= String
.format(getString(R
.string
.common_exit
), getString(R
.string
.app_name
));
652 builder
.setNegativeButton(message
, new OnClickListener() {
653 public void onClick(DialogInterface dialog
, int which
) {
658 //builder.setNegativeButton(android.R.string.cancel, this);
659 dialog
= builder
.create();
662 case DIALOG_SHORT_WAIT
: {
663 ProgressDialog working_dialog
= new ProgressDialog(this);
664 working_dialog
.setMessage(getResources().getString(
665 R
.string
.wait_a_moment
));
666 working_dialog
.setIndeterminate(true
);
667 working_dialog
.setCancelable(false
);
668 dialog
= working_dialog
;
671 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
673 String
[] items
= null
;
675 String
[] allTheItems
= { getString(R
.string
.actionbar_upload_files
),
676 getString(R
.string
.actionbar_upload_from_apps
),
677 getString(R
.string
.actionbar_failed_instant_upload
) };
679 String
[] commonItems
= { getString(R
.string
.actionbar_upload_files
),
680 getString(R
.string
.actionbar_upload_from_apps
) };
682 if (InstantUploadActivity
.IS_ENABLED
)
687 builder
= new AlertDialog
.Builder(this);
688 builder
.setTitle(R
.string
.actionbar_upload
);
689 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
690 public void onClick(DialogInterface dialog
, int item
) {
693 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
694 action
.putExtra(UploadFilesActivity
.EXTRA_ACCOUNT
,
695 AccountUtils
.getCurrentOwnCloudAccount(FileDisplayActivity
.this));
696 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
698 // TODO create and handle new fragment
699 // LocalFileListFragment
701 } else if (item
== 1) {
702 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
703 action
= action
.setType("*/*").addCategory(Intent
.CATEGORY_OPENABLE
);
704 startActivityForResult(Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
705 ACTION_SELECT_CONTENT_FROM_APPS
);
706 } else if (item
== 2 && InstantUploadActivity
.IS_ENABLED
) {
707 Account account
= AccountUtils
.getCurrentOwnCloudAccount(FileDisplayActivity
.this);
708 Intent action
= new Intent(FileDisplayActivity
.this, InstantUploadActivity
.class);
709 action
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
710 startActivity(action
);
714 dialog
= builder
.create();
717 case DIALOG_SSL_VALIDATOR
: {
718 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
721 case DIALOG_CERT_NOT_SAVED
: {
722 builder
= new AlertDialog
.Builder(this);
723 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
724 builder
.setCancelable(false
);
725 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
727 public void onClick(DialogInterface dialog
, int which
) {
731 dialog
= builder
.create();
743 * Translates a content URI of an image to a physical path
745 * @param uri The URI to resolve
746 * @return The path to the image or null if it could not be found
748 public String
getPath(Uri uri
) {
749 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
750 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
751 if (cursor
!= null
) {
752 int column_index
= cursor
753 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
754 cursor
.moveToFirst();
755 return cursor
.getString(column_index
);
761 * Pushes a directory to the drop down list
762 * @param directory to push
763 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
765 public void pushDirname(OCFile directory
) {
766 if(!directory
.isDirectory()){
767 throw new IllegalArgumentException("Only directories may be pushed!");
769 mDirectories
.insert(directory
.getFileName(), 0);
770 mCurrentDir
= directory
;
774 * Pops a directory name from the drop down list
775 * @return True, unless the stack is empty
777 public boolean popDirname() {
778 mDirectories
.remove(mDirectories
.getItem(0));
779 return !mDirectories
.isEmpty();
782 // Custom array adapter to override text colors
783 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
785 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
789 public View
getView(int position
, View convertView
, ViewGroup parent
) {
790 View v
= super.getView(position
, convertView
, parent
);
792 ((TextView
) v
).setTextColor(getResources().getColorStateList(
793 android
.R
.color
.white
));
797 public View
getDropDownView(int position
, View convertView
,
799 View v
= super.getDropDownView(position
, convertView
, parent
);
801 ((TextView
) v
).setTextColor(getResources().getColorStateList(
802 android
.R
.color
.white
));
809 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
812 * {@link BroadcastReceiver} to enable syncing feedback in UI
815 public void onReceive(Context context
, Intent intent
) {
816 boolean inProgress
= intent
.getBooleanExtra(FileSyncService
.IN_PROGRESS
, false
);
817 String accountName
= intent
.getStringExtra(FileSyncService
.ACCOUNT_NAME
);
819 Log_OC
.d("FileDisplay", "sync of account " + accountName
+ " is in_progress: " + inProgress
);
821 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
)) {
823 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
);
825 boolean fillBlankRoot
= false
;
826 if (mCurrentDir
== null
) {
827 mCurrentDir
= mStorageManager
.getFileByPath("/");
828 fillBlankRoot
= (mCurrentDir
!= null
);
831 if ((synchFolderRemotePath
!= null
&& mCurrentDir
!= null
&& (mCurrentDir
.getRemotePath().equals(synchFolderRemotePath
)))
834 mCurrentDir
= getStorageManager().getFileByPath(synchFolderRemotePath
);
835 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager()
836 .findFragmentById(R
.id
.fileList
);
837 if (fileListFragment
!= null
) {
838 fileListFragment
.listDirectory(mCurrentDir
);
842 setSupportProgressBarIndeterminateVisibility(inProgress
);
843 if (mBackFromCreatingFirstAccount
) {
844 // awful patch to fix problem with visibility of progress circle with the first refresh of the first account
845 // TODO - kill this Activity when the first account has to be created instead of stack the account creation on it
846 getSupportActionBar().hide();
847 getSupportActionBar().show();
848 mBackFromCreatingFirstAccount
= false
;
850 removeStickyBroadcast(intent
);
854 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.getSerializableExtra(FileSyncService
.SYNC_RESULT
);
855 if (synchResult
!= null
) {
856 if (synchResult
.getCode().equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
857 mLastSslUntrustedServerResult
= synchResult
;
858 showDialog(DIALOG_SSL_VALIDATOR
);
865 private class UploadFinishReceiver
extends BroadcastReceiver
{
867 * Once the file upload has finished -> update view
868 * @author David A. Velasco
869 * {@link BroadcastReceiver} to enable upload feedback in UI
872 public void onReceive(Context context
, Intent intent
) {
873 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
874 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
875 boolean sameAccount
= accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
);
876 boolean isDescendant
= (mCurrentDir
!= null
) && (uploadedRemotePath
!= null
) && (uploadedRemotePath
.startsWith(mCurrentDir
.getRemotePath()));
877 if (sameAccount
&& isDescendant
) {
878 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
879 if (fileListFragment
!= null
) {
880 fileListFragment
.listDirectory();
889 * Class waiting for broadcast events from the {@link FielDownloader} service.
891 * Updates the UI when a download is started or finished, provided that it is relevant for the
894 private class DownloadFinishReceiver
extends BroadcastReceiver
{
896 public void onReceive(Context context
, Intent intent
) {
897 boolean sameAccount
= isSameAccount(context
, intent
);
898 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
899 boolean isDescendant
= isDescendant(downloadedRemotePath
);
901 if (sameAccount
&& isDescendant
) {
904 updateRightPanel(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
908 removeStickyBroadcast(intent
);
911 private boolean isDescendant(String downloadedRemotePath
) {
912 return (mCurrentDir
!= null
&& downloadedRemotePath
!= null
&& downloadedRemotePath
.startsWith(mCurrentDir
.getRemotePath()));
915 private boolean isSameAccount(Context context
, Intent intent
) {
916 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
917 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
922 protected void updateLeftPanel() {
923 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
924 if (fileListFragment
!= null
) {
925 fileListFragment
.listDirectory();
929 protected void updateRightPanel(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
930 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
931 boolean waitedPreview
= (mWaitingToPreview
!= null
&& mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
932 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
933 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
934 OCFile fileInFragment
= detailsFragment
.getFile();
935 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
936 // the user browsed to other file ; forget the automatic preview
937 mWaitingToPreview
= null
;
939 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
940 // grant that the right panel updates the progress bar
941 detailsFragment
.listenForTransferProgress();
942 detailsFragment
.updateFileDetails(true
, false
);
944 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
945 // update the right panel
946 if (success
&& waitedPreview
) {
947 mWaitingToPreview
= mStorageManager
.getFileById(mWaitingToPreview
.getFileId()); // update the file from database, for the local storage path
948 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
949 transaction
.replace(R
.id
.file_details_container
, new PreviewMediaFragment(mWaitingToPreview
, AccountUtils
.getCurrentOwnCloudAccount(this), 0, true
), FileDetailFragment
.FTAG
);
950 transaction
.commit();
951 mWaitingToPreview
= null
;
954 detailsFragment
.updateFileDetails(false
, (success
));
965 public DataStorageManager
getStorageManager() {
966 return mStorageManager
;
974 public void onDirectoryClick(OCFile directory
) {
975 pushDirname(directory
);
976 ActionBar actionBar
= getSupportActionBar();
977 actionBar
.setDisplayHomeAsUpEnabled(true
);
980 // Resets the FileDetailsFragment on Tablets so that it always displays
981 Fragment fileFragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
982 if (fileFragment
!= null
&& (fileFragment
instanceof PreviewMediaFragment
|| !((FileDetailFragment
) fileFragment
).isEmpty())) {
983 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
984 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
); // empty FileDetailFragment
985 transaction
.commit();
995 public void onFileClick(OCFile file
, boolean onOrientationChange
) {
996 if (file
!= null
&& PreviewImageFragment
.canBePreviewed(file
)) {
997 // preview image - it handles the download, if needed
998 startPreviewImage(file
);
1000 } else if (file
!= null
&& PreviewMediaFragment
.canBePreviewed(file
)) {
1001 if (file
.isDown()) {
1003 if (!onOrientationChange
) {
1004 startMediaPreview(file
, 0, true
, onOrientationChange
);
1006 int startPlaybackPosition
= 0;
1007 boolean autoplay
= true
;
1008 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1009 if (fragment
!= null
&& file
.isVideo()) {
1010 PreviewMediaFragment videoFragment
= (PreviewMediaFragment
)fragment
;
1011 startPlaybackPosition
= videoFragment
.getPosition();
1012 autoplay
= videoFragment
.isPlaying();
1014 startMediaPreview(file
, startPlaybackPosition
, autoplay
, onOrientationChange
);
1018 // automatic download, preview on finish
1019 startDownloadForPreview(file
, onOrientationChange
);
1024 startDetails(file
, onOrientationChange
);
1028 private void startPreviewImage(OCFile file
) {
1029 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1030 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1031 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1032 startActivity(showDetailsIntent
);
1035 private void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
, boolean onOrientationChange
) {
1036 if (mDualPane
&& !onOrientationChange
) {
1037 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1038 transaction
.replace(R
.id
.file_details_container
, new PreviewMediaFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this), startPlaybackPosition
, autoplay
), FileDetailFragment
.FTAG
);
1039 transaction
.commit();
1042 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1043 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1044 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1045 showDetailsIntent
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, startPlaybackPosition
);
1046 showDetailsIntent
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, autoplay
);
1047 startActivity(showDetailsIntent
);
1051 private void startDownloadForPreview(OCFile file
, boolean onOrientationChange
) {
1052 if (mDualPane
&& !onOrientationChange
) {
1053 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1054 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1055 transaction
.commit();
1056 mWaitingToPreview
= file
;
1057 requestForDownload();
1060 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1061 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1062 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1063 startActivity(showDetailsIntent
);
1068 private void startDetails(OCFile file
, boolean onOrientationChange
) {
1069 if (mDualPane
&& !file
.isImage() && !onOrientationChange
) {
1070 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1071 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1072 transaction
.commit();
1074 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1075 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1076 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1077 startActivity(showDetailsIntent
);
1086 public OCFile
getInitialDirectory() {
1095 public void onFileStateChanged() {
1096 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
1097 if (fileListFragment
!= null
) {
1098 fileListFragment
.listDirectory();
1107 public FileDownloaderBinder
getFileDownloaderBinder() {
1108 return mDownloaderBinder
;
1116 public FileUploaderBinder
getFileUploaderBinder() {
1117 return mUploaderBinder
;
1121 /** Defines callbacks for service binding, passed to bindService() */
1122 private class ListServiceConnection
implements ServiceConnection
{
1125 public void onServiceConnected(ComponentName component
, IBinder service
) {
1126 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1127 Log_OC
.d(TAG
, "Download service connected");
1128 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1129 if (mWaitingToPreview
!= null
) {
1130 requestForDownload();
1133 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1134 Log_OC
.d(TAG
, "Upload service connected");
1135 mUploaderBinder
= (FileUploaderBinder
) service
;
1139 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1140 if (mFileList
!= null
)
1141 mFileList
.listDirectory();
1143 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1144 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
1145 FileDetailFragment detailFragment
= (FileDetailFragment
)fragment
;
1146 detailFragment
.listenForTransferProgress();
1147 detailFragment
.updateFileDetails(false
, false
);
1153 public void onServiceDisconnected(ComponentName component
) {
1154 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1155 Log_OC
.d(TAG
, "Download service disconnected");
1156 mDownloaderBinder
= null
;
1157 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1158 Log_OC
.d(TAG
, "Upload service disconnected");
1159 mUploaderBinder
= null
;
1167 * Launch an intent to request the PIN code to the user before letting him use the app
1169 private void requestPinCode() {
1170 boolean pinStart
= false
;
1171 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
1172 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1174 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1175 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");
1182 public void onSavedCertificate() {
1183 startSynchronization();
1188 public void onFailedSavingCertificate() {
1189 showDialog(DIALOG_CERT_NOT_SAVED
);
1194 * Updates the view associated to the activity after the finish of some operation over files
1195 * in the current account.
1197 * @param operation Removal operation performed.
1198 * @param result Result of the removal.
1201 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1202 if (operation
instanceof RemoveFileOperation
) {
1203 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1205 } else if (operation
instanceof RenameFileOperation
) {
1206 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1208 } else if (operation
instanceof SynchronizeFileOperation
) {
1209 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1211 } else if (operation
instanceof CreateFolderOperation
) {
1212 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1218 * Updates the view associated to the activity after the finish of an operation trying to remove a
1221 * @param operation Removal operation performed.
1222 * @param result Result of the removal.
1224 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
1225 dismissDialog(DIALOG_SHORT_WAIT
);
1226 if (result
.isSuccess()) {
1227 Toast msg
= Toast
.makeText(this, R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
1229 OCFile removedFile
= operation
.getFile();
1231 FileFragment details
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1232 if (details
!= null
&& removedFile
.equals(details
.getFile())) {
1233 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1234 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
1235 transaction
.commit();
1238 if (mStorageManager
.getFileById(removedFile
.getParentId()).equals(mCurrentDir
)) {
1239 mFileList
.listDirectory();
1243 Toast msg
= Toast
.makeText(this, R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
1245 if (result
.isSslRecoverableException()) {
1246 mLastSslUntrustedServerResult
= result
;
1247 showDialog(DIALOG_SSL_VALIDATOR
);
1253 * Updates the view associated to the activity after the finish of an operation trying create a new folder
1255 * @param operation Creation operation performed.
1256 * @param result Result of the creation.
1258 private void onCreateFolderOperationFinish(CreateFolderOperation operation
, RemoteOperationResult result
) {
1259 if (result
.isSuccess()) {
1260 dismissDialog(DIALOG_SHORT_WAIT
);
1261 mFileList
.listDirectory();
1264 dismissDialog(DIALOG_SHORT_WAIT
);
1266 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
1269 } catch (NotFoundException e
) {
1270 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1277 * Updates the view associated to the activity after the finish of an operation trying to rename a
1280 * @param operation Renaming operation performed.
1281 * @param result Result of the renaming.
1283 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1284 dismissDialog(DIALOG_SHORT_WAIT
);
1285 OCFile renamedFile
= operation
.getFile();
1286 if (result
.isSuccess()) {
1288 FileFragment details
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1289 if (details
!= null
&& details
instanceof FileDetailFragment
&& renamedFile
.equals(details
.getFile()) ) {
1290 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, AccountUtils
.getCurrentOwnCloudAccount(this));
1293 if (mStorageManager
.getFileById(renamedFile
.getParentId()).equals(mCurrentDir
)) {
1294 mFileList
.listDirectory();
1298 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1299 Toast msg
= Toast
.makeText(this, R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1301 // TODO throw again the new rename dialog
1303 Toast msg
= Toast
.makeText(this, R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1305 if (result
.isSslRecoverableException()) {
1306 mLastSslUntrustedServerResult
= result
;
1307 showDialog(DIALOG_SSL_VALIDATOR
);
1314 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1315 dismissDialog(DIALOG_SHORT_WAIT
);
1316 OCFile syncedFile
= operation
.getLocalFile();
1317 if (!result
.isSuccess()) {
1318 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1319 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1320 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1321 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1325 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_fail_msg
, Toast
.LENGTH_LONG
);
1330 if (operation
.transferWasRequested()) {
1331 mFileList
.listDirectory();
1332 onTransferStateChanged(syncedFile
, true
, true
);
1335 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1346 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1347 /*OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
1348 if (fileListFragment != null) {
1349 fileListFragment.listDirectory();
1352 FileFragment details
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1353 if (details
!= null
&& details
instanceof FileDetailFragment
&& file
.equals(details
.getFile()) ) {
1354 if (downloading
|| uploading
) {
1355 ((FileDetailFragment
)details
).updateFileDetails(file
, AccountUtils
.getCurrentOwnCloudAccount(this));
1357 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1365 public void showFragmentWithDetails(OCFile file
) {
1367 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1368 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1369 transaction
.commit();
1372 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1373 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1374 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1375 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
1376 startActivity(showDetailsIntent
);
1380 public void onDismiss(EditNameDialog dialog
) {
1382 if (dialog
.getResult()) {
1383 String newDirectoryName
= dialog
.getNewFilename().trim();
1384 Log_OC
.d(TAG
, "'create directory' dialog dismissed with new name " + newDirectoryName
);
1385 if (newDirectoryName
.length() > 0) {
1387 if (mCurrentDir
== null
) {
1388 // this is just a patch; we should ensure that mCurrentDir never is null
1389 if (!mStorageManager
.fileExists(OCFile
.PATH_SEPARATOR
)) {
1390 OCFile file
= new OCFile(OCFile
.PATH_SEPARATOR
);
1391 mStorageManager
.saveFile(file
);
1393 mCurrentDir
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
1395 path
= FileDisplayActivity
.this.mCurrentDir
.getRemotePath();
1398 path
+= newDirectoryName
+ OCFile
.PATH_SEPARATOR
;
1399 RemoteOperation operation
= new CreateFolderOperation(path
, mCurrentDir
.getFileId(), mStorageManager
);
1400 operation
.execute( AccountUtils
.getCurrentOwnCloudAccount(FileDisplayActivity
.this),
1401 FileDisplayActivity
.this,
1402 FileDisplayActivity
.this,
1404 FileDisplayActivity
.this);
1406 showDialog(DIALOG_SHORT_WAIT
);
1412 private void requestForDownload() {
1413 Account account
= AccountUtils
.getCurrentOwnCloudAccount(this);
1414 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1415 Intent i
= new Intent(this, FileDownloader
.class);
1416 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1417 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);