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
.Dialog
;
26 import android
.app
.ProgressDialog
;
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
.Intent
;
33 import android
.content
.IntentFilter
;
34 import android
.content
.ServiceConnection
;
35 import android
.content
.SharedPreferences
;
36 import android
.content
.SyncRequest
;
37 import android
.content
.res
.Resources
.NotFoundException
;
38 import android
.database
.Cursor
;
39 import android
.net
.Uri
;
40 import android
.os
.Bundle
;
41 import android
.os
.Handler
;
42 import android
.os
.IBinder
;
43 import android
.preference
.PreferenceManager
;
44 import android
.provider
.MediaStore
;
45 import android
.support
.v4
.app
.Fragment
;
46 import android
.support
.v4
.app
.FragmentManager
;
47 import android
.support
.v4
.app
.FragmentTransaction
;
48 import android
.util
.Log
;
49 import android
.view
.View
;
50 import android
.view
.ViewGroup
;
51 import android
.widget
.ArrayAdapter
;
52 import android
.widget
.TextView
;
53 import android
.widget
.Toast
;
55 import com
.actionbarsherlock
.app
.ActionBar
;
56 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
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
.MainApp
;
62 import com
.owncloud
.android
.R
;
63 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
64 import com
.owncloud
.android
.datamodel
.OCFile
;
65 import com
.owncloud
.android
.files
.services
.FileDownloader
;
66 import com
.owncloud
.android
.files
.services
.FileObserverService
;
67 import com
.owncloud
.android
.files
.services
.FileUploader
;
68 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
69 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
70 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
72 import com
.owncloud
.android
.operations
.GetSharesOperation
;
74 import com
.owncloud
.android
.lib
.operations
.common
.OnRemoteOperationListener
;
75 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
76 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
77 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
79 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
80 import com
.owncloud
.android
.operations
.RenameFileOperation
;
81 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
82 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
83 import com
.owncloud
.android
.syncadapter
.FileSyncService
;
84 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
85 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
86 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
87 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
;
88 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
.OnSslValidatorListener
;
89 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
90 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
91 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
92 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
93 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
94 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
95 import com
.owncloud
.android
.utils
.DisplayUtils
;
96 import com
.owncloud
.android
.utils
.Log_OC
;
100 * Displays, what files the user has available in his ownCloud.
102 * @author Bartek Przybylski
103 * @author David A. Velasco
106 public class FileDisplayActivity
extends FileActivity
implements
107 OCFileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
, OnSslValidatorListener
, OnRemoteOperationListener
, EditNameDialogListener
{
109 private ArrayAdapter
<String
> mDirectories
;
111 /** Access point to the cached database for the current ownCloud {@link Account} */
112 private FileDataStorageManager mStorageManager
= null
;
114 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
115 private UploadFinishReceiver mUploadFinishReceiver
;
116 private DownloadFinishReceiver mDownloadFinishReceiver
;
117 private FileDownloaderBinder mDownloaderBinder
= null
;
118 private FileUploaderBinder mUploaderBinder
= null
;
119 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
120 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
122 private boolean mDualPane
;
123 private View mLeftFragmentContainer
;
124 private View mRightFragmentContainer
;
126 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
127 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
128 private static final String KEY_REFRESH_SHARES_IN_PROGRESS
= "SHARES_IN_PROGRESS";
130 public static final int DIALOG_SHORT_WAIT
= 0;
131 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 1;
132 private static final int DIALOG_SSL_VALIDATOR
= 2;
133 private static final int DIALOG_CERT_NOT_SAVED
= 3;
135 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
137 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
139 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
140 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
142 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
144 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
145 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
147 private OCFile mWaitingToPreview
;
148 private Handler mHandler
;
150 private boolean mSyncInProgress
= false
;
151 private boolean mRefreshSharesInProgress
= false
;
154 protected void onCreate(Bundle savedInstanceState
) {
155 Log_OC
.d(TAG
, "onCreate() start");
156 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
158 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account is valid
160 mHandler
= new Handler();
162 /// bindings to transference services
163 mUploadConnection
= new ListServiceConnection();
164 mDownloadConnection
= new ListServiceConnection();
165 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
166 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
168 // PIN CODE request ; best location is to decide, let's try this first
169 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
171 } else if (getIntent().getAction() == null
&& savedInstanceState
== null
) {
176 Intent observer_intent
= new Intent(this, FileObserverService
.class);
177 observer_intent
.putExtra(FileObserverService
.KEY_FILE_CMD
, FileObserverService
.CMD_INIT_OBSERVED_LIST
);
178 startService(observer_intent
);
180 /// Load of saved instance state
181 if(savedInstanceState
!= null
) {
182 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
183 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
184 mRefreshSharesInProgress
= savedInstanceState
.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS
);
187 mWaitingToPreview
= null
;
188 mSyncInProgress
= false
;
189 mRefreshSharesInProgress
= false
;
194 // Inflate and set the layout view
195 setContentView(R
.layout
.files
);
196 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
197 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
198 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
199 if (savedInstanceState
== null
) {
200 createMinFragments();
204 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
205 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
206 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
); // always AFTER setContentView(...) ; to work around bug in its implementation
208 Log_OC
.d(TAG
, "onCreate() end");
212 protected void onStart() {
214 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
218 protected void onDestroy() {
220 if (mDownloadConnection
!= null
)
221 unbindService(mDownloadConnection
);
222 if (mUploadConnection
!= null
)
223 unbindService(mUploadConnection
);
228 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
231 protected void onAccountSet(boolean stateWasRecovered
) {
232 if (getAccount() != null
) {
233 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
235 /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
236 OCFile file
= getFile();
237 // get parent from path
238 String parentPath
= "";
240 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
241 // upload in progress - right now, files are not inserted in the local cache until the upload is successful
242 // get parent from path
243 parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
244 if (mStorageManager
.getFileByPath(parentPath
) == null
)
245 file
= null
; // not able to know the directory where the file is uploading
247 file
= mStorageManager
.getFileByPath(file
.getRemotePath()); // currentDir = null if not in the current Account
251 // fall back to root folder
252 file
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null
255 setNavigationListWithFolder(file
);
257 if (!stateWasRecovered
) {
258 Log_OC
.e(TAG
, "Initializing Fragments in onAccountChanged..");
259 initFragmentsWithFile();
260 if (file
.isFolder()) {
261 startSyncFolderOperation(file
);
265 updateFragmentsVisibility(!file
.isFolder());
266 updateNavigationElementsInActionBar(file
.isFolder() ? null
: file
);
271 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
276 private void setNavigationListWithFolder(OCFile file
) {
277 mDirectories
.clear();
278 OCFile fileIt
= file
;
280 while(fileIt
!= null
&& fileIt
.getFileName() != OCFile
.ROOT_PATH
) {
281 if (fileIt
.isFolder()) {
282 mDirectories
.add(fileIt
.getFileName());
284 //fileIt = mStorageManager.getFileById(fileIt.getParentId());
285 // get parent from path
286 parentPath
= fileIt
.getRemotePath().substring(0, fileIt
.getRemotePath().lastIndexOf(fileIt
.getFileName()));
287 fileIt
= mStorageManager
.getFileByPath(parentPath
);
289 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
293 private void createMinFragments() {
294 OCFileListFragment listOfFiles
= new OCFileListFragment();
295 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
296 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
297 transaction
.commit();
300 private void initFragmentsWithFile() {
301 if (getAccount() != null
&& getFile() != null
) {
303 OCFileListFragment listOfFiles
= getListOfFilesFragment();
304 if (listOfFiles
!= null
) {
305 listOfFiles
.listDirectory(getCurrentDir());
307 Log
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
311 OCFile file
= getFile();
312 Fragment secondFragment
= chooseInitialSecondFragment(file
);
313 if (secondFragment
!= null
) {
314 setSecondFragment(secondFragment
);
315 updateFragmentsVisibility(true
);
316 updateNavigationElementsInActionBar(file
);
319 cleanSecondFragment();
323 Log
.wtf(TAG
, "initFragments() called with invalid NULLs!");
324 if (getAccount() == null
) {
325 Log
.wtf(TAG
, "\t account is NULL");
327 if (getFile() == null
) {
328 Log
.wtf(TAG
, "\t file is NULL");
333 private Fragment
chooseInitialSecondFragment(OCFile file
) {
334 Fragment secondFragment
= null
;
335 if (file
!= null
&& !file
.isFolder()) {
336 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
337 && file
.getLastSyncDateForProperties() > 0 // temporal fix
339 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
340 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
341 secondFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
344 secondFragment
= new FileDetailFragment(file
, getAccount());
347 return secondFragment
;
352 * Replaces the second fragment managed by the activity with the received as
355 * Assumes never will be more than two fragments managed at the same time.
357 * @param fragment New second Fragment to set.
359 private void setSecondFragment(Fragment fragment
) {
360 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
361 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
362 transaction
.commit();
366 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
368 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
369 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
371 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
372 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
375 } else if (existsSecondFragment
) {
376 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
377 mLeftFragmentContainer
.setVisibility(View
.GONE
);
379 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
380 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
384 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
385 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
387 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
388 mRightFragmentContainer
.setVisibility(View
.GONE
);
394 private OCFileListFragment
getListOfFilesFragment() {
395 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_LIST_OF_FILES
);
396 if (listOfFiles
!= null
) {
397 return (OCFileListFragment
)listOfFiles
;
399 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
403 protected FileFragment
getSecondFragment() {
404 Fragment second
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
405 if (second
!= null
) {
406 return (FileFragment
)second
;
411 public void cleanSecondFragment() {
412 Fragment second
= getSecondFragment();
413 if (second
!= null
) {
414 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
418 updateFragmentsVisibility(false
);
419 updateNavigationElementsInActionBar(null
);
422 protected void refeshListOfFilesFragment() {
423 OCFileListFragment fileListFragment
= getListOfFilesFragment();
424 if (fileListFragment
!= null
) {
425 fileListFragment
.listDirectory();
429 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
430 FileFragment secondFragment
= getSecondFragment();
431 boolean waitedPreview
= (mWaitingToPreview
!= null
&& mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
432 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
433 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
434 OCFile fileInFragment
= detailsFragment
.getFile();
435 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
436 // the user browsed to other file ; forget the automatic preview
437 mWaitingToPreview
= null
;
439 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
440 // grant that the right panel updates the progress bar
441 detailsFragment
.listenForTransferProgress();
442 detailsFragment
.updateFileDetails(true
, false
);
444 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
445 // update the right panel
446 boolean detailsFragmentChanged
= false
;
449 mWaitingToPreview
= mStorageManager
.getFileById(mWaitingToPreview
.getFileId()); // update the file from database, for the local storage path
450 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
451 startMediaPreview(mWaitingToPreview
, 0, true
);
452 detailsFragmentChanged
= true
;
454 openFile(mWaitingToPreview
);
457 mWaitingToPreview
= null
;
459 if (!detailsFragmentChanged
) {
460 detailsFragment
.updateFileDetails(false
, (success
));
468 public boolean onCreateOptionsMenu(Menu menu
) {
469 MenuInflater inflater
= getSherlock().getMenuInflater();
470 inflater
.inflate(R
.menu
.main_menu
, menu
);
475 public boolean onOptionsItemSelected(MenuItem item
) {
476 boolean retval
= true
;
477 switch (item
.getItemId()) {
478 case R
.id
.action_create_dir
: {
479 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.uploader_info_dirname
), "", -1, -1, this);
480 dialog
.show(getSupportFragmentManager(), "createdirdialog");
483 case R
.id
.action_sync_account
: {
484 startSynchronization();
487 case R
.id
.action_upload
: {
488 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
491 case R
.id
.action_settings
: {
492 Intent settingsIntent
= new Intent(this, Preferences
.class);
493 startActivity(settingsIntent
);
496 case android
.R
.id
.home
: {
497 FileFragment second
= getSecondFragment();
498 OCFile currentDir
= getCurrentDir();
499 if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
500 (second
!= null
&& second
.getFile() != null
)) {
507 retval
= super.onOptionsItemSelected(item
);
512 private void startSynchronization() {
513 Log_OC
.e(TAG
, "Got to start sync");
514 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
515 Log_OC
.e(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
516 ContentResolver
.cancelSync(null
, MainApp
.getAuthority()); // cancel the current synchronizations of any ownCloud account
517 Bundle bundle
= new Bundle();
518 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
519 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
520 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority());
521 ContentResolver
.requestSync(
523 MainApp
.getAuthority(), bundle
);
525 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority() + " with new API");
526 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
527 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
528 builder
.setExpedited(true
);
529 builder
.setManual(true
);
531 SyncRequest request
= builder
.build();
532 ContentResolver
.requestSync(request
);
538 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
539 if (itemPosition
!= 0) {
540 String targetPath
= "";
541 for (int i
=itemPosition
; i
< mDirectories
.getCount() - 1; i
++) {
542 targetPath
= mDirectories
.getItem(i
) + OCFile
.PATH_SEPARATOR
+ targetPath
;
544 targetPath
= OCFile
.PATH_SEPARATOR
+ targetPath
;
545 OCFile targetFolder
= mStorageManager
.getFileByPath(targetPath
);
546 if (targetFolder
!= null
) {
547 browseTo(targetFolder
);
550 // the next operation triggers a new call to this method, but it's necessary to
551 // ensure that the name exposed in the action bar is the current directory when the
552 // user selected it in the navigation list
553 getSupportActionBar().setSelectedNavigationItem(0);
559 * Called, when the user selected something for uploading
561 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
562 super.onActivityResult(requestCode
, resultCode
, data
);
564 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
565 requestSimpleUpload(data
, resultCode
);
567 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
568 requestMultipleUpload(data
, resultCode
);
573 private void requestMultipleUpload(Intent data
, int resultCode
) {
574 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
575 if (filePaths
!= null
) {
576 String
[] remotePaths
= new String
[filePaths
.length
];
577 String remotePathBase
= "";
578 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
579 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
581 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
582 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
583 for (int j
= 0; j
< remotePaths
.length
; j
++) {
584 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
587 Intent i
= new Intent(this, FileUploader
.class);
588 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
589 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
590 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
591 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
592 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
593 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
597 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
598 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
605 private void requestSimpleUpload(Intent data
, int resultCode
) {
606 String filepath
= null
;
608 Uri selectedImageUri
= data
.getData();
610 String filemanagerstring
= selectedImageUri
.getPath();
611 String selectedImagePath
= getPath(selectedImageUri
);
613 if (selectedImagePath
!= null
)
614 filepath
= selectedImagePath
;
616 filepath
= filemanagerstring
;
618 } catch (Exception e
) {
619 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
623 if (filepath
== null
) {
624 Log_OC
.e(TAG
, "Couldnt resolve path to file");
625 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
631 Intent i
= new Intent(this, FileUploader
.class);
632 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
634 String remotepath
= new String();
635 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
636 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
638 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
639 remotepath
+= OCFile
.PATH_SEPARATOR
;
640 remotepath
+= new File(filepath
).getName();
642 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
643 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
644 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
645 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
646 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
651 public void onBackPressed() {
652 OCFileListFragment listOfFiles
= getListOfFilesFragment();
653 if (mDualPane
|| getSecondFragment() == null
) {
654 if (listOfFiles
!= null
) { // should never be null, indeed
655 if (mDirectories
.getCount() <= 1) {
659 int levelsUp
= listOfFiles
.onBrowseUp();
660 for (int i
=0; i
< levelsUp
&& mDirectories
.getCount() > 1 ; i
++) {
665 if (listOfFiles
!= null
) { // should never be null, indeed
666 setFile(listOfFiles
.getCurrentFile());
668 cleanSecondFragment();
673 protected void onSaveInstanceState(Bundle outState
) {
674 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
675 Log_OC
.e(TAG
, "onSaveInstanceState() start");
676 super.onSaveInstanceState(outState
);
677 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
678 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
679 outState
.putBoolean(FileDisplayActivity
.KEY_REFRESH_SHARES_IN_PROGRESS
, mRefreshSharesInProgress
);
681 Log_OC
.d(TAG
, "onSaveInstanceState() end");
687 protected void onResume() {
689 Log_OC
.e(TAG
, "onResume() start");
691 // Listen for sync messages
692 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.getSyncMessage());
693 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
694 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
696 // Listen for upload messages
697 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
698 mUploadFinishReceiver
= new UploadFinishReceiver();
699 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
701 // Listen for download messages
702 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.getDownloadAddedMessage());
703 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
704 mDownloadFinishReceiver
= new DownloadFinishReceiver();
705 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
707 Log_OC
.d(TAG
, "onResume() end");
712 protected void onPause() {
714 Log_OC
.e(TAG
, "onPause() start");
715 if (mSyncBroadcastReceiver
!= null
) {
716 unregisterReceiver(mSyncBroadcastReceiver
);
717 mSyncBroadcastReceiver
= null
;
719 if (mUploadFinishReceiver
!= null
) {
720 unregisterReceiver(mUploadFinishReceiver
);
721 mUploadFinishReceiver
= null
;
723 if (mDownloadFinishReceiver
!= null
) {
724 unregisterReceiver(mDownloadFinishReceiver
);
725 mDownloadFinishReceiver
= null
;
728 Log_OC
.d(TAG
, "onPause() end");
733 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
734 if (id
== DIALOG_SSL_VALIDATOR
&& mLastSslUntrustedServerResult
!= null
) {
735 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
741 protected Dialog
onCreateDialog(int id
) {
742 Dialog dialog
= null
;
743 AlertDialog
.Builder builder
;
745 case DIALOG_SHORT_WAIT
: {
746 ProgressDialog working_dialog
= new ProgressDialog(this);
747 working_dialog
.setMessage(getResources().getString(
748 R
.string
.wait_a_moment
));
749 working_dialog
.setIndeterminate(true
);
750 working_dialog
.setCancelable(false
);
751 dialog
= working_dialog
;
754 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
756 String
[] items
= null
;
758 String
[] allTheItems
= { getString(R
.string
.actionbar_upload_files
),
759 getString(R
.string
.actionbar_upload_from_apps
),
760 getString(R
.string
.actionbar_failed_instant_upload
) };
762 String
[] commonItems
= { getString(R
.string
.actionbar_upload_files
),
763 getString(R
.string
.actionbar_upload_from_apps
) };
765 if (InstantUploadActivity
.IS_ENABLED
)
770 builder
= new AlertDialog
.Builder(this);
771 builder
.setTitle(R
.string
.actionbar_upload
);
772 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
773 public void onClick(DialogInterface dialog
, int item
) {
776 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
777 action
.putExtra(UploadFilesActivity
.EXTRA_ACCOUNT
, FileDisplayActivity
.this.getAccount());
778 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
780 // TODO create and handle new fragment
781 // LocalFileListFragment
783 } else if (item
== 1) {
784 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
785 action
= action
.setType("*/*").addCategory(Intent
.CATEGORY_OPENABLE
);
786 startActivityForResult(Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
787 ACTION_SELECT_CONTENT_FROM_APPS
);
788 } else if (item
== 2 && InstantUploadActivity
.IS_ENABLED
) {
789 Intent action
= new Intent(FileDisplayActivity
.this, InstantUploadActivity
.class);
790 action
.putExtra(FileUploader
.KEY_ACCOUNT
, FileDisplayActivity
.this.getAccount());
791 startActivity(action
);
795 dialog
= builder
.create();
798 case DIALOG_SSL_VALIDATOR
: {
799 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
802 case DIALOG_CERT_NOT_SAVED
: {
803 builder
= new AlertDialog
.Builder(this);
804 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
805 builder
.setCancelable(false
);
806 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
808 public void onClick(DialogInterface dialog
, int which
) {
812 dialog
= builder
.create();
824 * Show loading dialog
826 public void showLoadingDialog() {
828 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
829 FragmentManager fm
= getSupportFragmentManager();
830 FragmentTransaction ft
= fm
.beginTransaction();
831 loading
.show(ft
, DIALOG_WAIT_TAG
);
836 * Dismiss loading dialog
838 public void dismissLoadingDialog(){
839 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
841 LoadingDialog loading
= (LoadingDialog
) frag
;
848 * Translates a content URI of an image to a physical path
850 * @param uri The URI to resolve
851 * @return The path to the image or null if it could not be found
853 public String
getPath(Uri uri
) {
854 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
855 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
856 if (cursor
!= null
) {
857 int column_index
= cursor
858 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
859 cursor
.moveToFirst();
860 return cursor
.getString(column_index
);
866 * Pushes a directory to the drop down list
867 * @param directory to push
868 * @throws IllegalArgumentException If the {@link OCFile#isFolder()} returns false.
870 public void pushDirname(OCFile directory
) {
871 if(!directory
.isFolder()){
872 throw new IllegalArgumentException("Only directories may be pushed!");
874 mDirectories
.insert(directory
.getFileName(), 0);
879 * Pops a directory name from the drop down list
880 * @return True, unless the stack is empty
882 public boolean popDirname() {
883 mDirectories
.remove(mDirectories
.getItem(0));
884 return !mDirectories
.isEmpty();
887 // Custom array adapter to override text colors
888 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
890 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
894 public View
getView(int position
, View convertView
, ViewGroup parent
) {
895 View v
= super.getView(position
, convertView
, parent
);
897 ((TextView
) v
).setTextColor(getResources().getColorStateList(
898 android
.R
.color
.white
));
902 public View
getDropDownView(int position
, View convertView
,
904 View v
= super.getDropDownView(position
, convertView
, parent
);
906 ((TextView
) v
).setTextColor(getResources().getColorStateList(
907 android
.R
.color
.white
));
914 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
917 * {@link BroadcastReceiver} to enable syncing feedback in UI
920 public void onReceive(Context context
, Intent intent
) {
921 boolean inProgress
= intent
.getBooleanExtra(FileSyncService
.IN_PROGRESS
, false
);
922 String accountName
= intent
.getStringExtra(FileSyncService
.ACCOUNT_NAME
);
923 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.getSerializableExtra(FileSyncService
.SYNC_RESULT
);
925 if (getAccount() != null
&& accountName
.equals(getAccount().name
)
926 && mStorageManager
!= null
) {
928 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
);
930 OCFile currentFile
= (getFile() == null
) ? null
: mStorageManager
.getFileByPath(getFile().getRemotePath());
931 OCFile currentDir
= (getCurrentDir() == null
) ? null
: mStorageManager
.getFileByPath(getCurrentDir().getRemotePath());
933 if (currentDir
== null
) {
934 // current folder was removed from the server
935 Toast
.makeText( FileDisplayActivity
.this,
936 String
.format(getString(R
.string
.sync_current_folder_was_removed
), mDirectories
.getItem(0)),
942 if (currentFile
== null
&& !getFile().isFolder()) {
943 // currently selected file was removed in the server, and now we know it
944 cleanSecondFragment();
945 currentFile
= currentDir
;
948 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
949 OCFileListFragment fileListFragment
= getListOfFilesFragment();
950 if (fileListFragment
!= null
) {
951 fileListFragment
.listDirectory(currentDir
);
954 setFile(currentFile
);
957 if (!mRefreshSharesInProgress
) {
958 /// get the shared files
959 if (isSharedSupported()) {
962 setSupportProgressBarIndeterminateVisibility(inProgress
);
964 setSupportProgressBarIndeterminateVisibility(true
);
967 removeStickyBroadcast(intent
);
968 mSyncInProgress
= inProgress
;
972 if (synchResult
!= null
) {
973 if (synchResult
.getCode().equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
974 mLastSslUntrustedServerResult
= synchResult
;
975 showDialog(DIALOG_SSL_VALIDATOR
);
982 private class UploadFinishReceiver
extends BroadcastReceiver
{
984 * Once the file upload has finished -> update view
985 * @author David A. Velasco
986 * {@link BroadcastReceiver} to enable upload feedback in UI
989 public void onReceive(Context context
, Intent intent
) {
990 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
991 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
992 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
993 OCFile currentDir
= getCurrentDir();
994 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) && (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
995 if (sameAccount
&& isDescendant
) {
996 refeshListOfFilesFragment();
1004 * Class waiting for broadcast events from the {@link FielDownloader} service.
1006 * Updates the UI when a download is started or finished, provided that it is relevant for the
1009 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1011 public void onReceive(Context context
, Intent intent
) {
1012 boolean sameAccount
= isSameAccount(context
, intent
);
1013 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1014 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1016 if (sameAccount
&& isDescendant
) {
1017 refeshListOfFilesFragment();
1018 refreshSecondFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
1021 removeStickyBroadcast(intent
);
1024 private boolean isDescendant(String downloadedRemotePath
) {
1025 OCFile currentDir
= getCurrentDir();
1026 return (currentDir
!= null
&& downloadedRemotePath
!= null
&& downloadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1029 private boolean isSameAccount(Context context
, Intent intent
) {
1030 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1031 return (accountName
!= null
&& getAccount() != null
&& accountName
.equals(getAccount().name
));
1040 public FileDataStorageManager
getStorageManager() {
1041 return mStorageManager
;
1045 public void browseToRoot() {
1046 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1047 if (listOfFiles
!= null
) { // should never be null, indeed
1048 while (mDirectories
.getCount() > 1) {
1051 OCFile root
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
);
1052 listOfFiles
.listDirectory(root
);
1053 setFile(listOfFiles
.getCurrentFile());
1054 startSyncFolderOperation(root
);
1056 cleanSecondFragment();
1060 public void browseTo(OCFile folder
) {
1061 if (folder
== null
|| !folder
.isFolder()) {
1062 throw new IllegalArgumentException("Trying to browse to invalid folder " + folder
);
1064 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1065 if (listOfFiles
!= null
) {
1066 setNavigationListWithFolder(folder
);
1067 listOfFiles
.listDirectory(folder
);
1068 setFile(listOfFiles
.getCurrentFile());
1069 startSyncFolderOperation(folder
);
1071 Log_OC
.e(TAG
, "Unexpected null when accessing list fragment");
1073 cleanSecondFragment();
1080 * Updates action bar and second fragment, if in dual pane mode.
1083 public void onBrowsedDownTo(OCFile directory
) {
1084 pushDirname(directory
);
1085 cleanSecondFragment();
1088 startSyncFolderOperation(directory
);
1093 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1095 * @param file Image {@link OCFile} to show.
1098 public void startImagePreview(OCFile file
) {
1099 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1100 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1101 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1102 startActivity(showDetailsIntent
);
1106 * Stars the preview of an already down media {@link OCFile}.
1108 * @param file Media {@link OCFile} to preview.
1109 * @param startPlaybackPosition Media position where the playback will be started, in milliseconds.
1110 * @param autoplay When 'true', the playback will start without user interactions.
1113 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1114 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
1115 setSecondFragment(mediaFragment
);
1116 updateFragmentsVisibility(true
);
1117 updateNavigationElementsInActionBar(file
);
1122 * Requests the download of the received {@link OCFile} , updates the UI
1123 * to monitor the download progress and prepares the activity to preview
1124 * or open the file when the download finishes.
1126 * @param file {@link OCFile} to download and preview.
1129 public void startDownloadForPreview(OCFile file
) {
1130 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1131 setSecondFragment(detailFragment
);
1132 mWaitingToPreview
= file
;
1133 requestForDownload();
1134 updateFragmentsVisibility(true
);
1135 updateNavigationElementsInActionBar(file
);
1141 * Shows the information of the {@link OCFile} received as a
1142 * parameter in the second fragment.
1144 * @param file {@link OCFile} whose details will be shown
1147 public void showDetails(OCFile file
) {
1148 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1149 setSecondFragment(detailFragment
);
1150 updateFragmentsVisibility(true
);
1151 updateNavigationElementsInActionBar(file
);
1159 private void updateNavigationElementsInActionBar(OCFile chosenFile
) {
1160 ActionBar actionBar
= getSupportActionBar();
1161 if (chosenFile
== null
|| mDualPane
) {
1162 // only list of files - set for browsing through folders
1163 OCFile currentDir
= getCurrentDir();
1164 actionBar
.setDisplayHomeAsUpEnabled(currentDir
!= null
&& currentDir
.getParentId() != 0);
1165 actionBar
.setDisplayShowTitleEnabled(false
);
1166 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
1167 actionBar
.setListNavigationCallbacks(mDirectories
, this); // assuming mDirectories is updated
1170 actionBar
.setDisplayHomeAsUpEnabled(true
);
1171 actionBar
.setDisplayShowTitleEnabled(true
);
1172 actionBar
.setTitle(chosenFile
.getFileName());
1173 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
1182 public void onFileStateChanged() {
1183 refeshListOfFilesFragment();
1184 updateNavigationElementsInActionBar(getSecondFragment().getFile());
1192 public FileDownloaderBinder
getFileDownloaderBinder() {
1193 return mDownloaderBinder
;
1201 public FileUploaderBinder
getFileUploaderBinder() {
1202 return mUploaderBinder
;
1206 /** Defines callbacks for service binding, passed to bindService() */
1207 private class ListServiceConnection
implements ServiceConnection
{
1210 public void onServiceConnected(ComponentName component
, IBinder service
) {
1211 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1212 Log_OC
.d(TAG
, "Download service connected");
1213 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1214 if (mWaitingToPreview
!= null
) {
1215 requestForDownload();
1218 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1219 Log_OC
.d(TAG
, "Upload service connected");
1220 mUploaderBinder
= (FileUploaderBinder
) service
;
1224 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1225 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1226 if (listOfFiles
!= null
) {
1227 listOfFiles
.listDirectory();
1229 FileFragment secondFragment
= getSecondFragment();
1230 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1231 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1232 detailFragment
.listenForTransferProgress();
1233 detailFragment
.updateFileDetails(false
, false
);
1238 public void onServiceDisconnected(ComponentName component
) {
1239 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1240 Log_OC
.d(TAG
, "Download service disconnected");
1241 mDownloaderBinder
= null
;
1242 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1243 Log_OC
.d(TAG
, "Upload service disconnected");
1244 mUploaderBinder
= null
;
1252 * Launch an intent to request the PIN code to the user before letting him use the app
1254 private void requestPinCode() {
1255 boolean pinStart
= false
;
1256 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
1257 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1259 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1260 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");
1267 public void onSavedCertificate() {
1268 startSyncFolderOperation(getCurrentDir());
1273 public void onFailedSavingCertificate() {
1274 showDialog(DIALOG_CERT_NOT_SAVED
);
1279 * Updates the view associated to the activity after the finish of some operation over files
1280 * in the current account.
1282 * @param operation Removal operation performed.
1283 * @param result Result of the removal.
1286 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1287 if (operation
instanceof RemoveFileOperation
) {
1288 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1290 } else if (operation
instanceof RenameFileOperation
) {
1291 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1293 } else if (operation
instanceof SynchronizeFileOperation
) {
1294 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1296 } else if (operation
instanceof CreateFolderOperation
) {
1297 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1299 } else if (operation
instanceof GetSharesOperation
) {
1300 onGetSharesOperationFinish((GetSharesOperation
) operation
, result
);
1305 /** Updates the data about shared files
1307 * @param operation Get Shared Files
1308 * @param result Result of the operation
1310 private void onGetSharesOperationFinish(GetSharesOperation operation
, RemoteOperationResult result
) {
1311 // Refresh the filelist with the information
1312 refeshListOfFilesFragment();
1314 mRefreshSharesInProgress
= false
;
1316 if (!mSyncInProgress
) {
1317 setSupportProgressBarIndeterminateVisibility(false
);
1322 * Updates the view associated to the activity after the finish of an operation trying to remove a
1325 * @param operation Removal operation performed.
1326 * @param result Result of the removal.
1328 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
1329 dismissLoadingDialog();
1330 if (result
.isSuccess()) {
1331 Toast msg
= Toast
.makeText(this, R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
1333 OCFile removedFile
= operation
.getFile();
1334 getSecondFragment();
1335 FileFragment second
= getSecondFragment();
1336 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1337 cleanSecondFragment();
1339 if (mStorageManager
.getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1340 refeshListOfFilesFragment();
1344 Toast msg
= Toast
.makeText(this, R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
1346 if (result
.isSslRecoverableException()) {
1347 mLastSslUntrustedServerResult
= result
;
1348 showDialog(DIALOG_SSL_VALIDATOR
);
1354 * Updates the view associated to the activity after the finish of an operation trying create a new folder
1356 * @param operation Creation operation performed.
1357 * @param result Result of the creation.
1359 private void onCreateFolderOperationFinish(CreateFolderOperation operation
, RemoteOperationResult result
) {
1360 if (result
.isSuccess()) {
1361 dismissLoadingDialog();
1362 refeshListOfFilesFragment();
1365 dismissLoadingDialog();
1366 if (result
.getCode() == ResultCode
.INVALID_CHARACTER_IN_NAME
) {
1367 Toast
.makeText(FileDisplayActivity
.this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
).show();
1370 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
1373 } catch (NotFoundException e
) {
1374 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1382 * Updates the view associated to the activity after the finish of an operation trying to rename a
1385 * @param operation Renaming operation performed.
1386 * @param result Result of the renaming.
1388 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1389 dismissLoadingDialog();
1390 OCFile renamedFile
= operation
.getFile();
1391 if (result
.isSuccess()) {
1393 FileFragment details
= getSecondFragment();
1394 if (details
!= null
&& details
instanceof FileDetailFragment
&& renamedFile
.equals(details
.getFile()) ) {
1395 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1398 if (mStorageManager
.getFileById(renamedFile
.getParentId()).equals(getCurrentDir())) {
1399 refeshListOfFilesFragment();
1403 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1404 Toast msg
= Toast
.makeText(this, R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1406 // TODO throw again the new rename dialog
1407 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
1408 Toast msg
= Toast
.makeText(this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
1411 Toast msg
= Toast
.makeText(this, R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1413 if (result
.isSslRecoverableException()) {
1414 mLastSslUntrustedServerResult
= result
;
1415 showDialog(DIALOG_SSL_VALIDATOR
);
1422 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1423 dismissLoadingDialog();
1424 OCFile syncedFile
= operation
.getLocalFile();
1425 if (!result
.isSuccess()) {
1426 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1427 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1428 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1429 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
1435 if (operation
.transferWasRequested()) {
1436 refeshListOfFilesFragment();
1437 onTransferStateChanged(syncedFile
, true
, true
);
1440 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1451 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1453 FileFragment details
= getSecondFragment();
1454 if (details
!= null
&& details
instanceof FileDetailFragment
&& file
.equals(details
.getFile()) ) {
1455 if (downloading
|| uploading
) {
1456 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1458 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1465 public void onDismiss(EditNameDialog dialog
) {
1466 if (dialog
.getResult()) {
1467 String newDirectoryName
= dialog
.getNewFilename().trim();
1468 Log_OC
.d(TAG
, "'create directory' dialog dismissed with new name " + newDirectoryName
);
1469 if (newDirectoryName
.length() > 0) {
1470 String path
= getCurrentDir().getRemotePath();
1473 path
+= newDirectoryName
+ OCFile
.PATH_SEPARATOR
;
1474 RemoteOperation operation
= new CreateFolderOperation(path
, false
, mStorageManager
);
1475 operation
.execute( getAccount(),
1476 FileDisplayActivity
.this,
1477 FileDisplayActivity
.this,
1479 FileDisplayActivity
.this);
1481 showLoadingDialog();
1487 private void requestForDownload() {
1488 Account account
= getAccount();
1489 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1490 Intent i
= new Intent(this, FileDownloader
.class);
1491 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1492 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1498 private OCFile
getCurrentDir() {
1499 OCFile file
= getFile();
1501 if (file
.isFolder()) {
1503 } else if (mStorageManager
!= null
) {
1504 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
1505 return mStorageManager
.getFileByPath(parentPath
);
1511 public void startSyncFolderOperation(OCFile folder
) {
1512 long currentSyncTime
= System
.currentTimeMillis();
1514 mSyncInProgress
= true
;
1516 // perform folder synchronization
1517 RemoteOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
1520 getStorageManager(),
1522 getApplicationContext()
1524 synchFolderOp
.execute(getAccount(), this, null
, null
, this);
1526 setSupportProgressBarIndeterminateVisibility(true
);
1530 private void startGetShares() {
1531 // Get shared files/folders
1532 RemoteOperation getShares
= new GetSharesOperation(mStorageManager
);
1533 getShares
.execute(getAccount(), this, this, mHandler
, this);
1535 mRefreshSharesInProgress
= true
;
1536 setSupportProgressBarIndeterminateVisibility(true
);
1540 // public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
1541 // int childCount = viewGroup.getChildCount();
1542 // for (int i = 0; i < childCount; i++) {
1543 // View view = viewGroup.getChildAt(i);
1544 // view.setEnabled(enabled);
1545 // view.setClickable(!enabled);
1546 // if (view instanceof ViewGroup) {
1547 // enableDisableViewGroup((ViewGroup) view, enabled);