1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.Activity
;
25 import android
.app
.AlertDialog
;
26 import android
.app
.ProgressDialog
;
27 import android
.app
.AlertDialog
.Builder
;
28 import android
.app
.Dialog
;
29 import android
.content
.BroadcastReceiver
;
30 import android
.content
.ComponentName
;
31 import android
.content
.ContentResolver
;
32 import android
.content
.Context
;
33 import android
.content
.DialogInterface
;
34 import android
.content
.DialogInterface
.OnClickListener
;
35 import android
.content
.Intent
;
36 import android
.content
.IntentFilter
;
37 import android
.content
.ServiceConnection
;
38 import android
.content
.SharedPreferences
;
39 import android
.content
.SharedPreferences
.Editor
;
40 import android
.content
.pm
.PackageInfo
;
41 import android
.content
.pm
.PackageManager
.NameNotFoundException
;
42 import android
.content
.res
.Resources
.NotFoundException
;
43 import android
.database
.Cursor
;
44 import android
.graphics
.Bitmap
;
45 import android
.graphics
.drawable
.BitmapDrawable
;
46 import android
.net
.Uri
;
47 import android
.os
.Bundle
;
48 import android
.os
.Handler
;
49 import android
.os
.IBinder
;
50 import android
.preference
.PreferenceManager
;
51 import android
.provider
.MediaStore
;
52 import android
.support
.v4
.app
.Fragment
;
53 import android
.support
.v4
.app
.FragmentTransaction
;
54 import android
.util
.Log
;
55 import android
.view
.View
;
56 import android
.view
.ViewGroup
;
57 import android
.widget
.ArrayAdapter
;
58 import android
.widget
.EditText
;
59 import android
.widget
.TextView
;
60 import android
.widget
.Toast
;
62 import com
.actionbarsherlock
.app
.ActionBar
;
63 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
64 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
65 import com
.actionbarsherlock
.view
.Menu
;
66 import com
.actionbarsherlock
.view
.MenuInflater
;
67 import com
.actionbarsherlock
.view
.MenuItem
;
68 import com
.actionbarsherlock
.view
.Window
;
69 import com
.owncloud
.android
.AccountUtils
;
70 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
71 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
72 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
73 import com
.owncloud
.android
.datamodel
.OCFile
;
74 import com
.owncloud
.android
.files
.services
.FileDownloader
;
75 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
76 import com
.owncloud
.android
.files
.services
.FileObserverService
;
77 import com
.owncloud
.android
.files
.services
.FileUploader
;
78 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
79 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
80 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
81 import com
.owncloud
.android
.operations
.RemoteOperation
;
82 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
83 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
84 import com
.owncloud
.android
.operations
.RenameFileOperation
;
85 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
86 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
87 import com
.owncloud
.android
.syncadapter
.FileSyncService
;
88 import com
.owncloud
.android
.ui
.dialog
.ChangelogDialog
;
89 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
;
90 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
.OnSslValidatorListener
;
91 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
92 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
93 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
94 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
95 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
97 import com
.owncloud
.android
.R
;
98 import eu
.alefzero
.webdav
.WebdavClient
;
101 * Displays, what files the user has available in his ownCloud.
103 * @author Bartek Przybylski
107 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
108 OCFileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
, OnSslValidatorListener
, OnRemoteOperationListener
{
110 private ArrayAdapter
<String
> mDirectories
;
111 private OCFile mCurrentDir
= null
;
112 private OCFile mCurrentFile
= null
;
114 private DataStorageManager mStorageManager
;
115 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
116 private UploadFinishReceiver mUploadFinishReceiver
;
117 private DownloadFinishReceiver mDownloadFinishReceiver
;
118 private FileDownloaderBinder mDownloaderBinder
= null
;
119 private FileUploaderBinder mUploaderBinder
= null
;
120 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
121 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
123 private OCFileListFragment mFileList
;
125 private boolean mDualPane
;
127 private static final int DIALOG_SETUP_ACCOUNT
= 0;
128 private static final int DIALOG_CREATE_DIR
= 1;
129 private static final int DIALOG_ABOUT_APP
= 2;
130 public static final int DIALOG_SHORT_WAIT
= 3;
131 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 4;
132 private static final int DIALOG_SSL_VALIDATOR
= 5;
133 private static final int DIALOG_CERT_NOT_SAVED
= 6;
134 private static final String DIALOG_CHANGELOG_TAG
= "DIALOG_CHANGELOG";
137 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
138 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
140 private static final String TAG
= "FileDisplayActivity";
142 private static int[] mMenuIdentifiersToPatch
= {R
.id
.action_about_app
};
144 private OCFile mWaitingToPreview
;
145 private Handler mHandler
;
149 public void onCreate(Bundle savedInstanceState
) {
150 Log
.d(getClass().toString(), "onCreate() start");
151 super.onCreate(savedInstanceState
);
153 /// Load of parameters from received intent
154 mCurrentDir
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
); // no check necessary, mCurrenDir == null if the parameter is not in the intent
155 Account account
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
157 AccountUtils
.setCurrentOwnCloudAccount(this, account
.name
);
159 /// Load of saved instance state: keep this always before initDataFromCurrentAccount()
160 if(savedInstanceState
!= null
) {
161 // TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER, or SOME TIMES
162 mCurrentDir
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
163 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(FileDetailActivity
.KEY_WAITING_TO_PREVIEW
);
166 mWaitingToPreview
= null
;
169 if (!AccountUtils
.accountsAreSetup(this)) {
170 /// no account available: FORCE ACCOUNT CREATION
171 mStorageManager
= null
;
172 createFirstAccount();
174 } else { /// at least an account is available
176 initDataFromCurrentAccount(); // it checks mCurrentDir and mCurrentFile with the current account
180 mUploadConnection
= new ListServiceConnection();
181 mDownloadConnection
= new ListServiceConnection();
182 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
183 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
185 // PIN CODE request ; best location is to decide, let's try this first
186 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
191 Intent observer_intent
= new Intent(this, FileObserverService
.class);
192 observer_intent
.putExtra(FileObserverService
.KEY_FILE_CMD
, FileObserverService
.CMD_INIT_OBSERVED_LIST
);
193 startService(observer_intent
);
197 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
199 // Drop-down navigation
200 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
201 OCFile currFile
= mCurrentDir
;
202 while(currFile
!= null
&& currFile
.getFileName() != OCFile
.PATH_SEPARATOR
) {
203 mDirectories
.add(currFile
.getFileName());
204 currFile
= mStorageManager
.getFileById(currFile
.getParentId());
206 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
208 // Inflate and set the layout view
209 setContentView(R
.layout
.files
);
210 mFileList
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
211 mDualPane
= (findViewById(R
.id
.file_details_container
) != null
);
213 initFileDetailsInDualPane();
217 ActionBar actionBar
= getSupportActionBar();
218 actionBar
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
219 actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0);
220 actionBar
.setDisplayShowTitleEnabled(false
);
221 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
222 actionBar
.setListNavigationCallbacks(mDirectories
, this);
223 setSupportProgressBarIndeterminateVisibility(false
); // always AFTER setContentView(...) ; to workaround bug in its implementation
226 // show changelog, if needed
229 Log
.d(getClass().toString(), "onCreate() end");
234 * Shows a dialog with the change log of the current version after each app update
236 * TODO make it permanent; by now, only to advice the workaround app for 4.1.x
238 private void showChangeLog() {
239 if (android
.os
.Build
.VERSION
.SDK_INT
== android
.os
.Build
.VERSION_CODES
.JELLY_BEAN
) {
240 final String KEY_VERSION
= "version";
241 SharedPreferences sharedPref
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
242 int currentVersionNumber
= 0;
243 int savedVersionNumber
= sharedPref
.getInt(KEY_VERSION
, 0);
245 PackageInfo pi
= getPackageManager().getPackageInfo(getPackageName(), 0);
246 currentVersionNumber
= pi
.versionCode
;
247 } catch (Exception e
) {}
249 if (currentVersionNumber
> savedVersionNumber
) {
250 ChangelogDialog
.newInstance(true
).show(getSupportFragmentManager(), DIALOG_CHANGELOG_TAG
);
251 Editor editor
= sharedPref
.edit();
252 editor
.putInt(KEY_VERSION
, currentVersionNumber
);
260 * Launches the account creation activity. To use when no ownCloud account is available
262 private void createFirstAccount() {
263 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
264 intent
.putExtra(android
.provider
.Settings
.EXTRA_AUTHORITIES
, new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
265 startActivity(intent
); // the new activity won't be created until this.onStart() and this.onResume() are finished;
270 * Load of state dependent of the existence of an ownCloud account
272 private void initDataFromCurrentAccount() {
273 /// Storage manager initialization - access to local database
274 mStorageManager
= new FileDataStorageManager(
275 AccountUtils
.getCurrentOwnCloudAccount(this),
276 getContentResolver());
278 /// Check if mCurrentDir is a directory
279 if(mCurrentDir
!= null
&& !mCurrentDir
.isDirectory()) {
280 mCurrentFile
= mCurrentDir
;
281 mCurrentDir
= mStorageManager
.getFileById(mCurrentDir
.getParentId());
284 /// Check if mCurrentDir and mCurrentFile are in the current account, and update them
285 if (mCurrentDir
!= null
) {
286 mCurrentDir
= mStorageManager
.getFileByPath(mCurrentDir
.getRemotePath()); // mCurrentDir == null if it is not in the current account
288 if (mCurrentFile
!= null
) {
289 if (mCurrentFile
.fileExists()) {
290 mCurrentFile
= mStorageManager
.getFileByPath(mCurrentFile
.getRemotePath()); // mCurrentFile == null if it is not in the current account
291 } // 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
294 /// Default to root if mCurrentDir was not found
295 if (mCurrentDir
== null
) {
296 mCurrentDir
= mStorageManager
.getFileByPath("/"); // will be NULL if the database was never synchronized
301 private void initFileDetailsInDualPane() {
302 if (mDualPane
&& getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
) == null
) {
303 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
304 if (mCurrentFile
!= null
) {
305 if (PreviewMediaFragment
.canBePreviewed(mCurrentFile
)) {
306 if (mCurrentFile
.isDown()) {
307 transaction
.replace(R
.id
.file_details_container
, new PreviewMediaFragment(mCurrentFile
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
309 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(mCurrentFile
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
310 mWaitingToPreview
= mCurrentFile
;
313 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(mCurrentFile
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
318 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
); // empty FileDetailFragment
320 transaction
.commit();
326 public void onDestroy() {
328 if (mDownloadConnection
!= null
)
329 unbindService(mDownloadConnection
);
330 if (mUploadConnection
!= null
)
331 unbindService(mUploadConnection
);
336 public boolean onCreateOptionsMenu(Menu menu
) {
337 MenuInflater inflater
= getSherlock().getMenuInflater();
338 inflater
.inflate(R
.menu
.main_menu
, menu
);
340 patchHiddenAccents(menu
);
346 * Workaround for this: <a href="http://code.google.com/p/android/issues/detail?id=3974">http://code.google.com/p/android/issues/detail?id=3974</a>
348 * @param menu Menu to patch
350 private void patchHiddenAccents(Menu menu
) {
351 for (int i
= 0; i
< mMenuIdentifiersToPatch
.length
; i
++) {
352 MenuItem aboutItem
= menu
.findItem(mMenuIdentifiersToPatch
[i
]);
353 if (aboutItem
!= null
&& aboutItem
.getIcon() instanceof BitmapDrawable
) {
354 // Clip off the bottom three (density independent) pixels of transparent padding
355 Bitmap original
= ((BitmapDrawable
) aboutItem
.getIcon()).getBitmap();
356 float scale
= getResources().getDisplayMetrics().density
;
357 int clippedHeight
= (int) (original
.getHeight() - (3 * scale
));
358 Bitmap scaled
= Bitmap
.createBitmap(original
, 0, 0, original
.getWidth(), clippedHeight
);
359 aboutItem
.setIcon(new BitmapDrawable(getResources(), scaled
));
366 public boolean onOptionsItemSelected(MenuItem item
) {
367 boolean retval
= true
;
368 switch (item
.getItemId()) {
369 case R
.id
.action_create_dir
: {
370 showDialog(DIALOG_CREATE_DIR
);
373 case R
.id
.action_sync_account
: {
374 startSynchronization();
377 case R
.id
.action_upload
: {
378 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
381 case R
.id
.action_settings
: {
382 Intent settingsIntent
= new Intent(this, Preferences
.class);
383 startActivity(settingsIntent
);
386 case R
.id
.action_about_app
: {
387 showDialog(DIALOG_ABOUT_APP
);
390 case android
.R
.id
.home
: {
391 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
397 retval
= super.onOptionsItemSelected(item
);
402 private void startSynchronization() {
403 ContentResolver
.cancelSync(null
, AccountAuthenticator
.AUTH_TOKEN_TYPE
); // cancel the current synchronizations of any ownCloud account
404 Bundle bundle
= new Bundle();
405 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
406 ContentResolver
.requestSync(
407 AccountUtils
.getCurrentOwnCloudAccount(this),
408 AccountAuthenticator
.AUTH_TOKEN_TYPE
, bundle
);
413 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
414 int i
= itemPosition
;
418 // the next operation triggers a new call to this method, but it's necessary to
419 // ensure that the name exposed in the action bar is the current directory when the
420 // user selected it in the navigation list
421 if (itemPosition
!= 0)
422 getSupportActionBar().setSelectedNavigationItem(0);
427 * Called, when the user selected something for uploading
429 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
431 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
432 requestSimpleUpload(data
, resultCode
);
434 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
435 requestMultipleUpload(data
, resultCode
);
440 private void requestMultipleUpload(Intent data
, int resultCode
) {
441 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
442 if (filePaths
!= null
) {
443 String
[] remotePaths
= new String
[filePaths
.length
];
444 String remotePathBase
= "";
445 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
446 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
448 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
449 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
450 for (int j
= 0; j
< remotePaths
.length
; j
++) {
451 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
454 Intent i
= new Intent(this, FileUploader
.class);
455 i
.putExtra(FileUploader
.KEY_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
456 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
457 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
458 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
459 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
460 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
464 Log
.d("FileDisplay", "User clicked on 'Update' with no selection");
465 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
472 private void requestSimpleUpload(Intent data
, int resultCode
) {
473 String filepath
= null
;
475 Uri selectedImageUri
= data
.getData();
477 String filemanagerstring
= selectedImageUri
.getPath();
478 String selectedImagePath
= getPath(selectedImageUri
);
480 if (selectedImagePath
!= null
)
481 filepath
= selectedImagePath
;
483 filepath
= filemanagerstring
;
485 } catch (Exception e
) {
486 Log
.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
490 if (filepath
== null
) {
491 Log
.e("FileDisplay", "Couldnt resolve path to file");
492 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
498 Intent i
= new Intent(this, FileUploader
.class);
499 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
500 AccountUtils
.getCurrentOwnCloudAccount(this));
501 String remotepath
= new String();
502 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
503 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
505 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
506 remotepath
+= OCFile
.PATH_SEPARATOR
;
507 remotepath
+= new File(filepath
).getName();
509 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
510 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
511 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
512 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
513 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
519 public void onBackPressed() {
520 if (mDirectories
.getCount() <= 1) {
525 mFileList
.onNavigateUp();
526 mCurrentDir
= mFileList
.getCurrentFile();
529 // Resets the FileDetailsFragment on Tablets so that it always displays
530 Fragment fileFragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
531 if (fileFragment
!= null
&& (fileFragment
instanceof PreviewMediaFragment
|| !((FileDetailFragment
) fileFragment
).isEmpty())) {
532 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
533 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
); // empty FileDetailFragment
534 transaction
.commit();
538 if(mCurrentDir
.getParentId() == 0){
539 ActionBar actionBar
= getSupportActionBar();
540 actionBar
.setDisplayHomeAsUpEnabled(false
);
545 protected void onSaveInstanceState(Bundle outState
) {
546 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
547 Log
.d(getClass().toString(), "onSaveInstanceState() start");
548 super.onSaveInstanceState(outState
);
549 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
551 FileFragment fragment
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
552 if (fragment
!= null
) {
553 OCFile file
= fragment
.getFile();
555 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, file
);
559 outState
.putParcelable(FileDetailActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
560 Log
.d(getClass().toString(), "onSaveInstanceState() end");
564 protected void onResume() {
565 Log
.d(getClass().toString(), "onResume() start");
568 if (AccountUtils
.accountsAreSetup(this)) {
570 if (mStorageManager
== null
) {
571 // this is necessary for handling the come back to FileDisplayActivity when the first ownCloud account is created
572 initDataFromCurrentAccount();
574 initFileDetailsInDualPane();
578 // Listen for sync messages
579 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
580 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
581 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
583 // Listen for upload messages
584 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
585 mUploadFinishReceiver
= new UploadFinishReceiver();
586 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
588 // Listen for download messages
589 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
590 //downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
591 mDownloadFinishReceiver
= new DownloadFinishReceiver();
592 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
594 // List current directory
595 mFileList
.listDirectory(mCurrentDir
); // TODO we should find the way to avoid the need of this (maybe it's not necessary yet; to check)
599 mStorageManager
= null
; // an invalid object will be there if all the ownCloud accounts are removed
600 showDialog(DIALOG_SETUP_ACCOUNT
);
603 Log
.d(getClass().toString(), "onResume() end");
608 protected void onPause() {
609 Log
.d(getClass().toString(), "onPause() start");
611 if (mSyncBroadcastReceiver
!= null
) {
612 unregisterReceiver(mSyncBroadcastReceiver
);
613 mSyncBroadcastReceiver
= null
;
615 if (mUploadFinishReceiver
!= null
) {
616 unregisterReceiver(mUploadFinishReceiver
);
617 mUploadFinishReceiver
= null
;
619 if (mDownloadFinishReceiver
!= null
) {
620 unregisterReceiver(mDownloadFinishReceiver
);
621 mDownloadFinishReceiver
= null
;
623 if (!AccountUtils
.accountsAreSetup(this)) {
624 dismissDialog(DIALOG_SETUP_ACCOUNT
);
627 Log
.d(getClass().toString(), "onPause() end");
632 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
633 if (id
== DIALOG_SSL_VALIDATOR
&& mLastSslUntrustedServerResult
!= null
) {
634 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
640 protected Dialog
onCreateDialog(int id
) {
641 Dialog dialog
= null
;
642 AlertDialog
.Builder builder
;
644 case DIALOG_SETUP_ACCOUNT
: {
645 builder
= new AlertDialog
.Builder(this);
646 builder
.setTitle(R
.string
.main_tit_accsetup
);
647 builder
.setMessage(R
.string
.main_wrn_accsetup
);
648 builder
.setCancelable(false
);
649 builder
.setPositiveButton(android
.R
.string
.ok
, new OnClickListener() {
650 public void onClick(DialogInterface dialog
, int which
) {
651 createFirstAccount();
655 String message
= String
.format(getString(R
.string
.common_exit
), getString(R
.string
.app_name
));
656 builder
.setNegativeButton(message
, new OnClickListener() {
657 public void onClick(DialogInterface dialog
, int which
) {
662 //builder.setNegativeButton(android.R.string.cancel, this);
663 dialog
= builder
.create();
666 case DIALOG_ABOUT_APP
: {
667 builder
= new AlertDialog
.Builder(this);
668 builder
.setTitle(getString(R
.string
.about_title
));
671 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
672 builder
.setMessage(String
.format(getString(R
.string
.about_message
), getString(R
.string
.app_name
), pkg
.versionName
));
673 builder
.setIcon(android
.R
.drawable
.ic_menu_info_details
);
674 dialog
= builder
.create();
675 } catch (NameNotFoundException e
) {
678 Log
.e(TAG
, "Error while showing about dialog", e
);
682 case DIALOG_CREATE_DIR
: {
683 builder
= new Builder(this);
684 final EditText dirNameInput
= new EditText(getBaseContext());
685 builder
.setView(dirNameInput
);
686 builder
.setTitle(R
.string
.uploader_info_dirname
);
687 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
688 dirNameInput
.setTextColor(typed_color
);
689 builder
.setPositiveButton(android
.R
.string
.ok
,
690 new OnClickListener() {
691 public void onClick(DialogInterface dialog
, int which
) {
692 String directoryName
= dirNameInput
.getText().toString();
693 if (directoryName
.trim().length() == 0) {
698 // Figure out the path where the dir needs to be created
700 if (mCurrentDir
== null
) {
701 // this is just a patch; we should ensure that mCurrentDir never is null
702 if (!mStorageManager
.fileExists(OCFile
.PATH_SEPARATOR
)) {
703 OCFile file
= new OCFile(OCFile
.PATH_SEPARATOR
);
704 mStorageManager
.saveFile(file
);
706 mCurrentDir
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
708 path
= FileDisplayActivity
.this.mCurrentDir
.getRemotePath();
711 path
+= directoryName
+ OCFile
.PATH_SEPARATOR
;
712 Thread thread
= new Thread(new DirectoryCreator(path
, AccountUtils
.getCurrentOwnCloudAccount(FileDisplayActivity
.this), new Handler()));
717 showDialog(DIALOG_SHORT_WAIT
);
720 builder
.setNegativeButton(R
.string
.common_cancel
,
721 new OnClickListener() {
722 public void onClick(DialogInterface dialog
, int which
) {
726 dialog
= builder
.create();
729 case DIALOG_SHORT_WAIT
: {
730 ProgressDialog working_dialog
= new ProgressDialog(this);
731 working_dialog
.setMessage(getResources().getString(
732 R
.string
.wait_a_moment
));
733 working_dialog
.setIndeterminate(true
);
734 working_dialog
.setCancelable(false
);
735 dialog
= working_dialog
;
738 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
739 final String
[] items
= { getString(R
.string
.actionbar_upload_files
),
740 getString(R
.string
.actionbar_upload_from_apps
) };
741 builder
= new AlertDialog
.Builder(this);
742 builder
.setTitle(R
.string
.actionbar_upload
);
743 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
744 public void onClick(DialogInterface dialog
, int item
) {
747 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
748 action
.putExtra(UploadFilesActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(FileDisplayActivity
.this));
749 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
751 // TODO create and handle new fragment LocalFileListFragment
753 } else if (item
== 1) {
754 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
755 action
= action
.setType("*/*")
756 .addCategory(Intent
.CATEGORY_OPENABLE
);
757 startActivityForResult(
758 Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
759 ACTION_SELECT_CONTENT_FROM_APPS
);
763 dialog
= builder
.create();
766 case DIALOG_SSL_VALIDATOR
: {
767 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
770 case DIALOG_CERT_NOT_SAVED
: {
771 builder
= new AlertDialog
.Builder(this);
772 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
773 builder
.setCancelable(false
);
774 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
776 public void onClick(DialogInterface dialog
, int which
) {
780 dialog
= builder
.create();
792 * Translates a content URI of an image to a physical path
794 * @param uri The URI to resolve
795 * @return The path to the image or null if it could not be found
797 public String
getPath(Uri uri
) {
798 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
799 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
800 if (cursor
!= null
) {
801 int column_index
= cursor
802 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
803 cursor
.moveToFirst();
804 return cursor
.getString(column_index
);
810 * Pushes a directory to the drop down list
811 * @param directory to push
812 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
814 public void pushDirname(OCFile directory
) {
815 if(!directory
.isDirectory()){
816 throw new IllegalArgumentException("Only directories may be pushed!");
818 mDirectories
.insert(directory
.getFileName(), 0);
819 mCurrentDir
= directory
;
823 * Pops a directory name from the drop down list
824 * @return True, unless the stack is empty
826 public boolean popDirname() {
827 mDirectories
.remove(mDirectories
.getItem(0));
828 return !mDirectories
.isEmpty();
831 private class DirectoryCreator
implements Runnable
{
832 private String mTargetPath
;
833 private Account mAccount
;
834 private Handler mHandler
;
836 public DirectoryCreator(String targetPath
, Account account
, Handler handler
) {
837 mTargetPath
= targetPath
;
844 WebdavClient wdc
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, getApplicationContext());
845 boolean created
= wdc
.createDirectory(mTargetPath
);
847 mHandler
.post(new Runnable() {
850 dismissDialog(DIALOG_SHORT_WAIT
);
852 // Save new directory in local database
853 OCFile newDir
= new OCFile(mTargetPath
);
854 newDir
.setMimetype("DIR");
855 newDir
.setParentId(mCurrentDir
.getFileId());
856 mStorageManager
.saveFile(newDir
);
858 // Display the new folder right away
859 mFileList
.listDirectory();
864 mHandler
.post(new Runnable() {
867 dismissDialog(DIALOG_SHORT_WAIT
);
869 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
872 } catch (NotFoundException e
) {
873 Log
.e(TAG
, "Error while trying to show fail message " , e
);
882 // Custom array adapter to override text colors
883 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
885 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
889 public View
getView(int position
, View convertView
, ViewGroup parent
) {
890 View v
= super.getView(position
, convertView
, parent
);
892 ((TextView
) v
).setTextColor(getResources().getColorStateList(
893 android
.R
.color
.white
));
897 public View
getDropDownView(int position
, View convertView
,
899 View v
= super.getDropDownView(position
, convertView
, parent
);
901 ((TextView
) v
).setTextColor(getResources().getColorStateList(
902 android
.R
.color
.white
));
909 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
912 * {@link BroadcastReceiver} to enable syncing feedback in UI
915 public void onReceive(Context context
, Intent intent
) {
916 boolean inProgress
= intent
.getBooleanExtra(
917 FileSyncService
.IN_PROGRESS
, false
);
918 String accountName
= intent
919 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
921 Log
.d("FileDisplay", "sync of account " + accountName
922 + " is in_progress: " + inProgress
);
924 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
)) {
926 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
);
928 boolean fillBlankRoot
= false
;
929 if (mCurrentDir
== null
) {
930 mCurrentDir
= mStorageManager
.getFileByPath("/");
931 fillBlankRoot
= (mCurrentDir
!= null
);
934 if ((synchFolderRemotePath
!= null
&& mCurrentDir
!= null
&& (mCurrentDir
.getRemotePath().equals(synchFolderRemotePath
)))
937 mCurrentDir
= getStorageManager().getFileByPath(synchFolderRemotePath
);
938 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager()
939 .findFragmentById(R
.id
.fileList
);
940 if (fileListFragment
!= null
) {
941 fileListFragment
.listDirectory(mCurrentDir
);
945 setSupportProgressBarIndeterminateVisibility(inProgress
);
946 removeStickyBroadcast(intent
);
950 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.getSerializableExtra(FileSyncService
.SYNC_RESULT
);
951 if (synchResult
!= null
) {
952 if (synchResult
.getCode().equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
953 mLastSslUntrustedServerResult
= synchResult
;
954 showDialog(DIALOG_SSL_VALIDATOR
);
961 private class UploadFinishReceiver
extends BroadcastReceiver
{
963 * Once the file upload has finished -> update view
964 * @author David A. Velasco
965 * {@link BroadcastReceiver} to enable upload feedback in UI
968 public void onReceive(Context context
, Intent intent
) {
969 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
970 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
971 boolean sameAccount
= accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
);
972 boolean isDescendant
= (mCurrentDir
!= null
) && (uploadedRemotePath
!= null
) && (uploadedRemotePath
.startsWith(mCurrentDir
.getRemotePath()));
973 if (sameAccount
&& isDescendant
) {
974 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
975 if (fileListFragment
!= null
) {
976 fileListFragment
.listDirectory();
985 * Once the file download has finished -> update view
987 private class DownloadFinishReceiver
extends BroadcastReceiver
{
989 public void onReceive(Context context
, Intent intent
) {
990 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
991 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
992 if (accountName
!= null
&& AccountUtils
.getCurrentOwnCloudAccount(context
) != null
) {
993 boolean sameAccount
= accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
);
994 boolean isDescendant
= (mCurrentDir
!= null
) && (downloadedRemotePath
!= null
) && (downloadedRemotePath
.startsWith(mCurrentDir
.getRemotePath()));
995 if (sameAccount
&& isDescendant
) {
996 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
997 if (fileListFragment
!= null
) {
998 fileListFragment
.listDirectory();
999 if ( mWaitingToPreview
!= null
&&
1000 mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
) &&
1001 intent
.getAction().equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
) ) {
1003 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1004 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
1005 FileDetailFragment detailFragment
= (FileDetailFragment
) fragment
;
1006 if (detailFragment
.getFile().getRemotePath().equals(downloadedRemotePath
)) {
1007 detailFragment
.listenForTransferProgress();
1008 detailFragment
.updateFileDetails(true
);
1025 public DataStorageManager
getStorageManager() {
1026 return mStorageManager
;
1034 public void onDirectoryClick(OCFile directory
) {
1035 pushDirname(directory
);
1036 ActionBar actionBar
= getSupportActionBar();
1037 actionBar
.setDisplayHomeAsUpEnabled(true
);
1040 // Resets the FileDetailsFragment on Tablets so that it always displays
1041 Fragment fileFragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1042 if (fileFragment
!= null
&& (fileFragment
instanceof PreviewMediaFragment
|| !((FileDetailFragment
) fileFragment
).isEmpty())) {
1043 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1044 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
); // empty FileDetailFragment
1045 transaction
.commit();
1055 public void onFileClick(OCFile file
) {
1057 if (file
!= null
&& PreviewMediaFragment
.canBePreviewed(file
)) {
1058 if (file
.isImage()) {
1059 // preview image - it handles the download, if needed
1060 startPreviewImage(file
);
1062 } else if (file
.isDown()) {
1064 startOtherPreview(file
);
1067 // automatic download, preview on finish
1068 startDownloadForPreview(file
);
1077 private void startPreviewImage(OCFile file
) {
1078 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1079 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1080 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1081 startActivity(showDetailsIntent
);
1084 private void startOtherPreview(OCFile file
) {
1086 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1087 transaction
.replace(R
.id
.file_details_container
, new PreviewMediaFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1088 transaction
.commit();
1091 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1092 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1093 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1094 startActivity(showDetailsIntent
);
1098 private void startDownloadForPreview(OCFile file
) {
1100 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1101 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1102 transaction
.commit();
1103 mWaitingToPreview
= file
;
1104 requestForDownload();
1107 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1108 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1109 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1110 startActivity(showDetailsIntent
);
1115 private void startDetails(OCFile file
) {
1116 if (mDualPane
&& !file
.isImage()) {
1117 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1118 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1119 transaction
.commit();
1121 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1122 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1123 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1124 startActivity(showDetailsIntent
);
1133 public OCFile
getInitialDirectory() {
1142 public void onFileStateChanged() {
1143 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
1144 if (fileListFragment
!= null
) {
1145 fileListFragment
.listDirectory();
1154 public FileDownloaderBinder
getFileDownloaderBinder() {
1155 return mDownloaderBinder
;
1163 public FileUploaderBinder
getFileUploaderBinder() {
1164 return mUploaderBinder
;
1168 /** Defines callbacks for service binding, passed to bindService() */
1169 private class ListServiceConnection
implements ServiceConnection
{
1172 public void onServiceConnected(ComponentName component
, IBinder service
) {
1173 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1174 Log
.d(TAG
, "Download service connected");
1175 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1176 if (mWaitingToPreview
!= null
) {
1177 requestForDownload();
1180 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1181 Log
.d(TAG
, "Upload service connected");
1182 mUploaderBinder
= (FileUploaderBinder
) service
;
1186 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1187 if (mFileList
!= null
)
1188 mFileList
.listDirectory();
1190 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1191 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
1192 FileDetailFragment detailFragment
= (FileDetailFragment
)fragment
;
1193 detailFragment
.listenForTransferProgress();
1194 detailFragment
.updateFileDetails(false
);
1200 public void onServiceDisconnected(ComponentName component
) {
1201 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1202 Log
.d(TAG
, "Download service disconnected");
1203 mDownloaderBinder
= null
;
1204 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1205 Log
.d(TAG
, "Upload service disconnected");
1206 mUploaderBinder
= null
;
1214 * Launch an intent to request the PIN code to the user before letting him use the app
1216 private void requestPinCode() {
1217 boolean pinStart
= false
;
1218 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
1219 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1221 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1222 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");
1229 public void onSavedCertificate() {
1230 startSynchronization();
1235 public void onFailedSavingCertificate() {
1236 showDialog(DIALOG_CERT_NOT_SAVED
);
1241 * Updates the view associated to the activity after the finish of some operation over files
1242 * in the current account.
1244 * @param operation Removal operation performed.
1245 * @param result Result of the removal.
1248 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1249 if (operation
instanceof RemoveFileOperation
) {
1250 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1252 } else if (operation
instanceof RenameFileOperation
) {
1253 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1255 } else if (operation
instanceof SynchronizeFileOperation
) {
1256 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1262 * Updates the view associated to the activity after the finish of an operation trying to remove a
1265 * @param operation Removal operation performed.
1266 * @param result Result of the removal.
1268 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
1269 dismissDialog(DIALOG_SHORT_WAIT
);
1270 if (result
.isSuccess()) {
1271 Toast msg
= Toast
.makeText(this, R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
1273 OCFile removedFile
= operation
.getFile();
1275 FileFragment details
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1276 if (details
!= null
&& removedFile
.equals(details
.getFile())) {
1277 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1278 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
1279 transaction
.commit();
1282 if (mStorageManager
.getFileById(removedFile
.getParentId()).equals(mCurrentDir
)) {
1283 mFileList
.listDirectory();
1287 Toast msg
= Toast
.makeText(this, R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
1289 if (result
.isSslRecoverableException()) {
1290 mLastSslUntrustedServerResult
= result
;
1291 showDialog(DIALOG_SSL_VALIDATOR
);
1297 * Updates the view associated to the activity after the finish of an operation trying to rename a
1300 * @param operation Renaming operation performed.
1301 * @param result Result of the renaming.
1303 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1304 dismissDialog(DIALOG_SHORT_WAIT
);
1305 OCFile renamedFile
= operation
.getFile();
1306 if (result
.isSuccess()) {
1308 FileFragment details
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1309 if (details
!= null
&& details
instanceof FileDetailFragment
&& renamedFile
.equals(details
.getFile()) ) {
1310 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, AccountUtils
.getCurrentOwnCloudAccount(this));
1313 if (mStorageManager
.getFileById(renamedFile
.getParentId()).equals(mCurrentDir
)) {
1314 mFileList
.listDirectory();
1318 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1319 Toast msg
= Toast
.makeText(this, R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1321 // TODO throw again the new rename dialog
1323 Toast msg
= Toast
.makeText(this, R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1325 if (result
.isSslRecoverableException()) {
1326 mLastSslUntrustedServerResult
= result
;
1327 showDialog(DIALOG_SSL_VALIDATOR
);
1334 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1335 dismissDialog(DIALOG_SHORT_WAIT
);
1336 OCFile syncedFile
= operation
.getLocalFile();
1337 if (!result
.isSuccess()) {
1338 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1339 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1340 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1341 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1345 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_fail_msg
, Toast
.LENGTH_LONG
);
1350 if (operation
.transferWasRequested()) {
1351 mFileList
.listDirectory();
1352 onTransferStateChanged(syncedFile
, true
, true
);
1355 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1366 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1367 /*OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
1368 if (fileListFragment != null) {
1369 fileListFragment.listDirectory();
1372 FileFragment details
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
1373 if (details
!= null
&& details
instanceof FileDetailFragment
&& file
.equals(details
.getFile()) ) {
1374 if (downloading
|| uploading
) {
1375 ((FileDetailFragment
)details
).updateFileDetails(file
, AccountUtils
.getCurrentOwnCloudAccount(this));
1377 ((FileDetailFragment
)details
).updateFileDetails(downloading
|| uploading
);
1385 public void showFragmentWithDetails(OCFile file
) {
1387 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1388 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1389 transaction
.commit();
1392 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
1393 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
1394 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
1395 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
1396 startActivity(showDetailsIntent
);
1402 public void notifySuccessfulDownload(OCFile file
, Intent intent
, boolean success
) {
1404 if (mWaitingToPreview
!= null
) {
1405 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
1406 transaction
.replace(R
.id
.file_details_container
, new PreviewMediaFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
1407 transaction
.commit();
1408 mWaitingToPreview
= null
;
1411 mDownloadFinishReceiver
.onReceive(this, intent
);
1415 private void requestForDownload() {
1416 Account account
= AccountUtils
.getCurrentOwnCloudAccount(this);
1417 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1418 Intent i
= new Intent(this, FileDownloader
.class);
1419 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1420 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);