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
.accounts
.AccountManager
;
25 import android
.app
.AlertDialog
;
26 import android
.app
.Dialog
;
27 import android
.app
.ProgressDialog
;
28 import android
.content
.BroadcastReceiver
;
29 import android
.content
.ComponentName
;
30 import android
.content
.ContentResolver
;
31 import android
.content
.Context
;
32 import android
.content
.DialogInterface
;
33 import android
.content
.Intent
;
34 import android
.content
.IntentFilter
;
35 import android
.content
.ServiceConnection
;
36 import android
.content
.SharedPreferences
;
37 import android
.content
.SyncRequest
;
38 import android
.content
.res
.Resources
.NotFoundException
;
39 import android
.database
.Cursor
;
40 import android
.net
.Uri
;
41 import android
.os
.Bundle
;
42 import android
.os
.Handler
;
43 import android
.os
.IBinder
;
44 import android
.preference
.PreferenceManager
;
45 import android
.provider
.MediaStore
;
46 import android
.support
.v4
.app
.Fragment
;
47 import android
.support
.v4
.app
.FragmentManager
;
48 import android
.support
.v4
.app
.FragmentTransaction
;
49 import android
.util
.Log
;
50 import android
.view
.View
;
51 import android
.view
.ViewGroup
;
52 import android
.widget
.ArrayAdapter
;
53 import android
.widget
.TextView
;
54 import android
.widget
.Toast
;
56 import com
.actionbarsherlock
.app
.ActionBar
;
57 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
58 import com
.actionbarsherlock
.view
.Menu
;
59 import com
.actionbarsherlock
.view
.MenuInflater
;
60 import com
.actionbarsherlock
.view
.MenuItem
;
61 import com
.actionbarsherlock
.view
.Window
;
62 import com
.owncloud
.android
.MainApp
;
63 import com
.owncloud
.android
.R
;
64 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
65 import com
.owncloud
.android
.datamodel
.OCFile
;
66 import com
.owncloud
.android
.files
.services
.FileDownloader
;
67 import com
.owncloud
.android
.files
.services
.FileObserverService
;
68 import com
.owncloud
.android
.files
.services
.FileUploader
;
69 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
70 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
71 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
73 import com
.owncloud
.android
.operations
.GetSharesOperation
;
75 import com
.owncloud
.android
.lib
.accounts
.OwnCloudAccount
;
76 import com
.owncloud
.android
.lib
.operations
.common
.OnRemoteOperationListener
;
77 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
78 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
79 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
81 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
82 import com
.owncloud
.android
.operations
.RenameFileOperation
;
83 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
84 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
85 import com
.owncloud
.android
.syncadapter
.FileSyncService
;
86 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
87 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
88 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
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
;
96 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
97 import com
.owncloud
.android
.utils
.DisplayUtils
;
98 import com
.owncloud
.android
.utils
.Log_OC
;
102 * Displays, what files the user has available in his ownCloud.
104 * @author Bartek Przybylski
105 * @author David A. Velasco
108 public class FileDisplayActivity
extends FileActivity
implements
109 OCFileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
, OnSslValidatorListener
, OnRemoteOperationListener
, EditNameDialogListener
{
111 private ArrayAdapter
<String
> mDirectories
;
113 /** Access point to the cached database for the current ownCloud {@link Account} */
114 private FileDataStorageManager mStorageManager
= null
;
116 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
117 private UploadFinishReceiver mUploadFinishReceiver
;
118 private DownloadFinishReceiver mDownloadFinishReceiver
;
119 private FileDownloaderBinder mDownloaderBinder
= null
;
120 private FileUploaderBinder mUploaderBinder
= null
;
121 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
122 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
124 private boolean mDualPane
;
125 private View mLeftFragmentContainer
;
126 private View mRightFragmentContainer
;
128 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
129 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
131 public static final int DIALOG_SHORT_WAIT
= 0;
132 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 1;
133 private static final int DIALOG_SSL_VALIDATOR
= 2;
134 private static final int DIALOG_CERT_NOT_SAVED
= 3;
136 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
138 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
140 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
141 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
143 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
145 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
146 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
148 private OCFile mWaitingToPreview
;
149 private Handler mHandler
;
151 private boolean mSyncInProgress
= 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
);
186 mWaitingToPreview
= null
;
187 mSyncInProgress
= false
;
192 // Inflate and set the layout view
193 setContentView(R
.layout
.files
);
194 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
195 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
196 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
197 if (savedInstanceState
== null
) {
198 createMinFragments();
202 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
203 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
204 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
); // always AFTER setContentView(...) ; to work around bug in its implementation
206 Log_OC
.d(TAG
, "onCreate() end");
210 protected void onStart() {
212 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
216 protected void onDestroy() {
218 if (mDownloadConnection
!= null
)
219 unbindService(mDownloadConnection
);
220 if (mUploadConnection
!= null
)
221 unbindService(mUploadConnection
);
226 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
229 protected void onAccountSet(boolean stateWasRecovered
) {
230 if (getAccount() != null
) {
231 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
233 /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
234 OCFile file
= getFile();
235 // get parent from path
236 String parentPath
= "";
238 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
239 // upload in progress - right now, files are not inserted in the local cache until the upload is successful
240 // get parent from path
241 parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
242 if (mStorageManager
.getFileByPath(parentPath
) == null
)
243 file
= null
; // not able to know the directory where the file is uploading
245 file
= mStorageManager
.getFileByPath(file
.getRemotePath()); // currentDir = null if not in the current Account
249 // fall back to root folder
250 file
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null
253 setNavigationListWithFolder(file
);
255 if (!stateWasRecovered
) {
256 Log_OC
.e(TAG
, "Initializing Fragments in onAccountChanged..");
257 initFragmentsWithFile();
258 if (file
.isFolder()) {
259 startSyncFolderOperation(file
);
263 updateFragmentsVisibility(!file
.isFolder());
264 updateNavigationElementsInActionBar(file
.isFolder() ? null
: file
);
269 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
274 private void setNavigationListWithFolder(OCFile file
) {
275 mDirectories
.clear();
276 OCFile fileIt
= file
;
278 while(fileIt
!= null
&& fileIt
.getFileName() != OCFile
.ROOT_PATH
) {
279 if (fileIt
.isFolder()) {
280 mDirectories
.add(fileIt
.getFileName());
282 //fileIt = mStorageManager.getFileById(fileIt.getParentId());
283 // get parent from path
284 parentPath
= fileIt
.getRemotePath().substring(0, fileIt
.getRemotePath().lastIndexOf(fileIt
.getFileName()));
285 fileIt
= mStorageManager
.getFileByPath(parentPath
);
287 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
291 private void createMinFragments() {
292 OCFileListFragment listOfFiles
= new OCFileListFragment();
293 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
294 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
295 transaction
.commit();
298 private void initFragmentsWithFile() {
299 if (getAccount() != null
&& getFile() != null
) {
301 OCFileListFragment listOfFiles
= getListOfFilesFragment();
302 if (listOfFiles
!= null
) {
303 listOfFiles
.listDirectory(getCurrentDir());
305 Log
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
309 OCFile file
= getFile();
310 Fragment secondFragment
= chooseInitialSecondFragment(file
);
311 if (secondFragment
!= null
) {
312 setSecondFragment(secondFragment
);
313 updateFragmentsVisibility(true
);
314 updateNavigationElementsInActionBar(file
);
317 cleanSecondFragment();
321 Log
.wtf(TAG
, "initFragments() called with invalid NULLs!");
322 if (getAccount() == null
) {
323 Log
.wtf(TAG
, "\t account is NULL");
325 if (getFile() == null
) {
326 Log
.wtf(TAG
, "\t file is NULL");
331 private Fragment
chooseInitialSecondFragment(OCFile file
) {
332 Fragment secondFragment
= null
;
333 if (file
!= null
&& !file
.isFolder()) {
334 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
335 && file
.getLastSyncDateForProperties() > 0 // temporal fix
337 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
338 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
339 secondFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
342 secondFragment
= new FileDetailFragment(file
, getAccount());
345 return secondFragment
;
350 * Replaces the second fragment managed by the activity with the received as
353 * Assumes never will be more than two fragments managed at the same time.
355 * @param fragment New second Fragment to set.
357 private void setSecondFragment(Fragment fragment
) {
358 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
359 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
360 transaction
.commit();
364 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
366 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
367 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
369 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
370 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
373 } else if (existsSecondFragment
) {
374 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
375 mLeftFragmentContainer
.setVisibility(View
.GONE
);
377 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
378 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
382 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
383 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
385 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
386 mRightFragmentContainer
.setVisibility(View
.GONE
);
392 private OCFileListFragment
getListOfFilesFragment() {
393 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_LIST_OF_FILES
);
394 if (listOfFiles
!= null
) {
395 return (OCFileListFragment
)listOfFiles
;
397 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
401 protected FileFragment
getSecondFragment() {
402 Fragment second
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
403 if (second
!= null
) {
404 return (FileFragment
)second
;
409 public void cleanSecondFragment() {
410 Fragment second
= getSecondFragment();
411 if (second
!= null
) {
412 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
416 updateFragmentsVisibility(false
);
417 updateNavigationElementsInActionBar(null
);
420 protected void refeshListOfFilesFragment() {
421 OCFileListFragment fileListFragment
= getListOfFilesFragment();
422 if (fileListFragment
!= null
) {
423 fileListFragment
.listDirectory();
427 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
428 FileFragment secondFragment
= getSecondFragment();
429 boolean waitedPreview
= (mWaitingToPreview
!= null
&& mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
430 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
431 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
432 OCFile fileInFragment
= detailsFragment
.getFile();
433 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
434 // the user browsed to other file ; forget the automatic preview
435 mWaitingToPreview
= null
;
437 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
438 // grant that the right panel updates the progress bar
439 detailsFragment
.listenForTransferProgress();
440 detailsFragment
.updateFileDetails(true
, false
);
442 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
443 // update the right panel
444 boolean detailsFragmentChanged
= false
;
447 mWaitingToPreview
= mStorageManager
.getFileById(mWaitingToPreview
.getFileId()); // update the file from database, for the local storage path
448 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
449 startMediaPreview(mWaitingToPreview
, 0, true
);
450 detailsFragmentChanged
= true
;
452 openFile(mWaitingToPreview
);
455 mWaitingToPreview
= null
;
457 if (!detailsFragmentChanged
) {
458 detailsFragment
.updateFileDetails(false
, (success
));
466 public boolean onCreateOptionsMenu(Menu menu
) {
467 MenuInflater inflater
= getSherlock().getMenuInflater();
468 inflater
.inflate(R
.menu
.main_menu
, menu
);
473 public boolean onOptionsItemSelected(MenuItem item
) {
474 boolean retval
= true
;
475 switch (item
.getItemId()) {
476 case R
.id
.action_create_dir
: {
477 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.uploader_info_dirname
), "", -1, -1, this);
478 dialog
.show(getSupportFragmentManager(), "createdirdialog");
481 case R
.id
.action_sync_account
: {
482 startSynchronization();
485 case R
.id
.action_upload
: {
486 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
489 case R
.id
.action_settings
: {
490 Intent settingsIntent
= new Intent(this, Preferences
.class);
491 startActivity(settingsIntent
);
494 case android
.R
.id
.home
: {
495 FileFragment second
= getSecondFragment();
496 OCFile currentDir
= getCurrentDir();
497 if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
498 (second
!= null
&& second
.getFile() != null
)) {
505 retval
= super.onOptionsItemSelected(item
);
510 private void startSynchronization() {
511 Log_OC
.e(TAG
, "Got to start sync");
512 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
513 Log_OC
.e(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
514 ContentResolver
.cancelSync(null
, MainApp
.getAuthority()); // cancel the current synchronizations of any ownCloud account
515 Bundle bundle
= new Bundle();
516 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
517 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
518 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority());
519 ContentResolver
.requestSync(
521 MainApp
.getAuthority(), bundle
);
523 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority() + " with new API");
524 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
525 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
526 builder
.setExpedited(true
);
527 builder
.setManual(true
);
529 SyncRequest request
= builder
.build();
530 ContentResolver
.requestSync(request
);
536 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
537 if (itemPosition
!= 0) {
538 String targetPath
= "";
539 for (int i
=itemPosition
; i
< mDirectories
.getCount() - 1; i
++) {
540 targetPath
= mDirectories
.getItem(i
) + OCFile
.PATH_SEPARATOR
+ targetPath
;
542 targetPath
= OCFile
.PATH_SEPARATOR
+ targetPath
;
543 OCFile targetFolder
= mStorageManager
.getFileByPath(targetPath
);
544 if (targetFolder
!= null
) {
545 browseTo(targetFolder
);
548 // the next operation triggers a new call to this method, but it's necessary to
549 // ensure that the name exposed in the action bar is the current directory when the
550 // user selected it in the navigation list
551 getSupportActionBar().setSelectedNavigationItem(0);
557 * Called, when the user selected something for uploading
559 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
560 super.onActivityResult(requestCode
, resultCode
, data
);
562 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
563 requestSimpleUpload(data
, resultCode
);
565 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
566 requestMultipleUpload(data
, resultCode
);
571 private void requestMultipleUpload(Intent data
, int resultCode
) {
572 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
573 if (filePaths
!= null
) {
574 String
[] remotePaths
= new String
[filePaths
.length
];
575 String remotePathBase
= "";
576 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
577 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
579 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
580 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
581 for (int j
= 0; j
< remotePaths
.length
; j
++) {
582 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
585 Intent i
= new Intent(this, FileUploader
.class);
586 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
587 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
588 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
589 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
590 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
591 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
595 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
596 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
603 private void requestSimpleUpload(Intent data
, int resultCode
) {
604 String filepath
= null
;
606 Uri selectedImageUri
= data
.getData();
608 String filemanagerstring
= selectedImageUri
.getPath();
609 String selectedImagePath
= getPath(selectedImageUri
);
611 if (selectedImagePath
!= null
)
612 filepath
= selectedImagePath
;
614 filepath
= filemanagerstring
;
616 } catch (Exception e
) {
617 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
621 if (filepath
== null
) {
622 Log_OC
.e(TAG
, "Couldnt resolve path to file");
623 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
629 Intent i
= new Intent(this, FileUploader
.class);
630 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
632 String remotepath
= new String();
633 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
634 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
636 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
637 remotepath
+= OCFile
.PATH_SEPARATOR
;
638 remotepath
+= new File(filepath
).getName();
640 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
641 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
642 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
643 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
644 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
649 public void onBackPressed() {
650 OCFileListFragment listOfFiles
= getListOfFilesFragment();
651 if (mDualPane
|| getSecondFragment() == null
) {
652 if (listOfFiles
!= null
) { // should never be null, indeed
653 if (mDirectories
.getCount() <= 1) {
657 int levelsUp
= listOfFiles
.onBrowseUp();
658 for (int i
=0; i
< levelsUp
&& mDirectories
.getCount() > 1 ; i
++) {
663 if (listOfFiles
!= null
) { // should never be null, indeed
664 setFile(listOfFiles
.getCurrentFile());
666 cleanSecondFragment();
671 protected void onSaveInstanceState(Bundle outState
) {
672 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
673 Log_OC
.e(TAG
, "onSaveInstanceState() start");
674 super.onSaveInstanceState(outState
);
675 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
676 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
678 Log_OC
.d(TAG
, "onSaveInstanceState() end");
684 protected void onResume() {
686 Log_OC
.e(TAG
, "onResume() start");
688 // Listen for sync messages
689 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.getSyncMessage());
690 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
691 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
693 // Listen for upload messages
694 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
695 mUploadFinishReceiver
= new UploadFinishReceiver();
696 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
698 // Listen for download messages
699 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.getDownloadAddedMessage());
700 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
701 mDownloadFinishReceiver
= new DownloadFinishReceiver();
702 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
704 Log_OC
.d(TAG
, "onResume() end");
709 protected void onPause() {
711 Log_OC
.e(TAG
, "onPause() start");
712 if (mSyncBroadcastReceiver
!= null
) {
713 unregisterReceiver(mSyncBroadcastReceiver
);
714 mSyncBroadcastReceiver
= null
;
716 if (mUploadFinishReceiver
!= null
) {
717 unregisterReceiver(mUploadFinishReceiver
);
718 mUploadFinishReceiver
= null
;
720 if (mDownloadFinishReceiver
!= null
) {
721 unregisterReceiver(mDownloadFinishReceiver
);
722 mDownloadFinishReceiver
= null
;
725 Log_OC
.d(TAG
, "onPause() end");
730 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
731 if (id
== DIALOG_SSL_VALIDATOR
&& mLastSslUntrustedServerResult
!= null
) {
732 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
738 protected Dialog
onCreateDialog(int id
) {
739 Dialog dialog
= null
;
740 AlertDialog
.Builder builder
;
742 case DIALOG_SHORT_WAIT
: {
743 ProgressDialog working_dialog
= new ProgressDialog(this);
744 working_dialog
.setMessage(getResources().getString(
745 R
.string
.wait_a_moment
));
746 working_dialog
.setIndeterminate(true
);
747 working_dialog
.setCancelable(false
);
748 dialog
= working_dialog
;
751 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
753 String
[] items
= null
;
755 String
[] allTheItems
= { getString(R
.string
.actionbar_upload_files
),
756 getString(R
.string
.actionbar_upload_from_apps
),
757 getString(R
.string
.actionbar_failed_instant_upload
) };
759 String
[] commonItems
= { getString(R
.string
.actionbar_upload_files
),
760 getString(R
.string
.actionbar_upload_from_apps
) };
762 if (InstantUploadActivity
.IS_ENABLED
)
767 builder
= new AlertDialog
.Builder(this);
768 builder
.setTitle(R
.string
.actionbar_upload
);
769 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
770 public void onClick(DialogInterface dialog
, int item
) {
773 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
774 action
.putExtra(UploadFilesActivity
.EXTRA_ACCOUNT
, FileDisplayActivity
.this.getAccount());
775 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
777 // TODO create and handle new fragment
778 // LocalFileListFragment
780 } else if (item
== 1) {
781 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
782 action
= action
.setType("*/*").addCategory(Intent
.CATEGORY_OPENABLE
);
783 startActivityForResult(Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
784 ACTION_SELECT_CONTENT_FROM_APPS
);
785 } else if (item
== 2 && InstantUploadActivity
.IS_ENABLED
) {
786 Intent action
= new Intent(FileDisplayActivity
.this, InstantUploadActivity
.class);
787 action
.putExtra(FileUploader
.KEY_ACCOUNT
, FileDisplayActivity
.this.getAccount());
788 startActivity(action
);
792 dialog
= builder
.create();
795 case DIALOG_SSL_VALIDATOR
: {
796 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
799 case DIALOG_CERT_NOT_SAVED
: {
800 builder
= new AlertDialog
.Builder(this);
801 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
802 builder
.setCancelable(false
);
803 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
805 public void onClick(DialogInterface dialog
, int which
) {
809 dialog
= builder
.create();
821 * Show loading dialog
823 public void showLoadingDialog() {
825 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
826 FragmentManager fm
= getSupportFragmentManager();
827 FragmentTransaction ft
= fm
.beginTransaction();
828 loading
.show(ft
, DIALOG_WAIT_TAG
);
833 * Dismiss loading dialog
835 public void dismissLoadingDialog(){
836 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
838 LoadingDialog loading
= (LoadingDialog
) frag
;
845 * Translates a content URI of an image to a physical path
847 * @param uri The URI to resolve
848 * @return The path to the image or null if it could not be found
850 public String
getPath(Uri uri
) {
851 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
852 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
853 if (cursor
!= null
) {
854 int column_index
= cursor
855 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
856 cursor
.moveToFirst();
857 return cursor
.getString(column_index
);
863 * Pushes a directory to the drop down list
864 * @param directory to push
865 * @throws IllegalArgumentException If the {@link OCFile#isFolder()} returns false.
867 public void pushDirname(OCFile directory
) {
868 if(!directory
.isFolder()){
869 throw new IllegalArgumentException("Only directories may be pushed!");
871 mDirectories
.insert(directory
.getFileName(), 0);
876 * Pops a directory name from the drop down list
877 * @return True, unless the stack is empty
879 public boolean popDirname() {
880 mDirectories
.remove(mDirectories
.getItem(0));
881 return !mDirectories
.isEmpty();
884 // Custom array adapter to override text colors
885 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
887 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
891 public View
getView(int position
, View convertView
, ViewGroup parent
) {
892 View v
= super.getView(position
, convertView
, parent
);
894 ((TextView
) v
).setTextColor(getResources().getColorStateList(
895 android
.R
.color
.white
));
899 public View
getDropDownView(int position
, View convertView
,
901 View v
= super.getDropDownView(position
, convertView
, parent
);
903 ((TextView
) v
).setTextColor(getResources().getColorStateList(
904 android
.R
.color
.white
));
911 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
914 * {@link BroadcastReceiver} to enable syncing feedback in UI
917 public void onReceive(Context context
, Intent intent
) {
918 boolean inProgress
= intent
.getBooleanExtra(FileSyncService
.IN_PROGRESS
, false
);
919 String accountName
= intent
.getStringExtra(FileSyncService
.ACCOUNT_NAME
);
920 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.getSerializableExtra(FileSyncService
.SYNC_RESULT
);
922 if (getAccount() != null
&& accountName
.equals(getAccount().name
)
923 && mStorageManager
!= null
926 /// get the shared files
927 if (isSharedSupported()) {
931 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
);
933 OCFile currentFile
= (getFile() == null
) ? null
: mStorageManager
.getFileByPath(getFile().getRemotePath());
934 OCFile currentDir
= (getCurrentDir() == null
) ? null
: mStorageManager
.getFileByPath(getCurrentDir().getRemotePath());
936 if (currentDir
== null
) {
937 // current folder was removed from the server
938 Toast
.makeText( FileDisplayActivity
.this,
939 String
.format(getString(R
.string
.sync_current_folder_was_removed
), mDirectories
.getItem(0)),
945 if (currentFile
== null
&& !getFile().isFolder()) {
946 // currently selected file was removed in the server, and now we know it
947 cleanSecondFragment();
948 currentFile
= currentDir
;
951 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
952 OCFileListFragment fileListFragment
= getListOfFilesFragment();
953 if (fileListFragment
!= null
) {
954 fileListFragment
.listDirectory(currentDir
);
957 setFile(currentFile
);
960 setSupportProgressBarIndeterminateVisibility(inProgress
);
961 removeStickyBroadcast(intent
);
962 mSyncInProgress
= inProgress
;
966 if (synchResult
!= null
) {
967 if (synchResult
.getCode().equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
968 mLastSslUntrustedServerResult
= synchResult
;
969 showDialog(DIALOG_SSL_VALIDATOR
);
976 private class UploadFinishReceiver
extends BroadcastReceiver
{
978 * Once the file upload has finished -> update view
979 * @author David A. Velasco
980 * {@link BroadcastReceiver} to enable upload feedback in UI
983 public void onReceive(Context context
, Intent intent
) {
984 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
985 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
986 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
987 OCFile currentDir
= getCurrentDir();
988 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) && (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
989 if (sameAccount
&& isDescendant
) {
990 refeshListOfFilesFragment();
998 * Class waiting for broadcast events from the {@link FielDownloader} service.
1000 * Updates the UI when a download is started or finished, provided that it is relevant for the
1003 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1005 public void onReceive(Context context
, Intent intent
) {
1006 boolean sameAccount
= isSameAccount(context
, intent
);
1007 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1008 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1010 if (sameAccount
&& isDescendant
) {
1011 refeshListOfFilesFragment();
1012 refreshSecondFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
1015 removeStickyBroadcast(intent
);
1018 private boolean isDescendant(String downloadedRemotePath
) {
1019 OCFile currentDir
= getCurrentDir();
1020 return (currentDir
!= null
&& downloadedRemotePath
!= null
&& downloadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1023 private boolean isSameAccount(Context context
, Intent intent
) {
1024 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1025 return (accountName
!= null
&& getAccount() != null
&& accountName
.equals(getAccount().name
));
1034 public FileDataStorageManager
getStorageManager() {
1035 return mStorageManager
;
1039 public void browseToRoot() {
1040 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1041 if (listOfFiles
!= null
) { // should never be null, indeed
1042 while (mDirectories
.getCount() > 1) {
1045 OCFile root
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
);
1046 listOfFiles
.listDirectory(root
);
1047 setFile(listOfFiles
.getCurrentFile());
1048 startSyncFolderOperation(root
);
1050 cleanSecondFragment();
1054 public void browseTo(OCFile folder
) {
1055 if (folder
== null
|| !folder
.isFolder()) {
1056 throw new IllegalArgumentException("Trying to browse to invalid folder " + folder
);
1058 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1059 if (listOfFiles
!= null
) {
1060 setNavigationListWithFolder(folder
);
1061 listOfFiles
.listDirectory(folder
);
1062 setFile(listOfFiles
.getCurrentFile());
1063 startSyncFolderOperation(folder
);
1065 Log_OC
.e(TAG
, "Unexpected null when accessing list fragment");
1067 cleanSecondFragment();
1074 * Updates action bar and second fragment, if in dual pane mode.
1077 public void onBrowsedDownTo(OCFile directory
) {
1078 pushDirname(directory
);
1079 cleanSecondFragment();
1082 startSyncFolderOperation(directory
);
1087 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1089 * @param file Image {@link OCFile} to show.
1092 public void startImagePreview(OCFile file
) {
1093 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1094 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1095 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1096 startActivity(showDetailsIntent
);
1100 * Stars the preview of an already down media {@link OCFile}.
1102 * @param file Media {@link OCFile} to preview.
1103 * @param startPlaybackPosition Media position where the playback will be started, in milliseconds.
1104 * @param autoplay When 'true', the playback will start without user interactions.
1107 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1108 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
1109 setSecondFragment(mediaFragment
);
1110 updateFragmentsVisibility(true
);
1111 updateNavigationElementsInActionBar(file
);
1116 * Requests the download of the received {@link OCFile} , updates the UI
1117 * to monitor the download progress and prepares the activity to preview
1118 * or open the file when the download finishes.
1120 * @param file {@link OCFile} to download and preview.
1123 public void startDownloadForPreview(OCFile file
) {
1124 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1125 setSecondFragment(detailFragment
);
1126 mWaitingToPreview
= file
;
1127 requestForDownload();
1128 updateFragmentsVisibility(true
);
1129 updateNavigationElementsInActionBar(file
);
1135 * Shows the information of the {@link OCFile} received as a
1136 * parameter in the second fragment.
1138 * @param file {@link OCFile} whose details will be shown
1141 public void showDetails(OCFile file
) {
1142 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1143 setSecondFragment(detailFragment
);
1144 updateFragmentsVisibility(true
);
1145 updateNavigationElementsInActionBar(file
);
1153 private void updateNavigationElementsInActionBar(OCFile chosenFile
) {
1154 ActionBar actionBar
= getSupportActionBar();
1155 if (chosenFile
== null
|| mDualPane
) {
1156 // only list of files - set for browsing through folders
1157 OCFile currentDir
= getCurrentDir();
1158 actionBar
.setDisplayHomeAsUpEnabled(currentDir
!= null
&& currentDir
.getParentId() != 0);
1159 actionBar
.setDisplayShowTitleEnabled(false
);
1160 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
1161 actionBar
.setListNavigationCallbacks(mDirectories
, this); // assuming mDirectories is updated
1164 actionBar
.setDisplayHomeAsUpEnabled(true
);
1165 actionBar
.setDisplayShowTitleEnabled(true
);
1166 actionBar
.setTitle(chosenFile
.getFileName());
1167 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
1176 public void onFileStateChanged() {
1177 refeshListOfFilesFragment();
1178 updateNavigationElementsInActionBar(getSecondFragment().getFile());
1186 public FileDownloaderBinder
getFileDownloaderBinder() {
1187 return mDownloaderBinder
;
1195 public FileUploaderBinder
getFileUploaderBinder() {
1196 return mUploaderBinder
;
1200 /** Defines callbacks for service binding, passed to bindService() */
1201 private class ListServiceConnection
implements ServiceConnection
{
1204 public void onServiceConnected(ComponentName component
, IBinder service
) {
1205 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1206 Log_OC
.d(TAG
, "Download service connected");
1207 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1208 if (mWaitingToPreview
!= null
) {
1209 requestForDownload();
1212 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1213 Log_OC
.d(TAG
, "Upload service connected");
1214 mUploaderBinder
= (FileUploaderBinder
) service
;
1218 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1219 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1220 if (listOfFiles
!= null
) {
1221 listOfFiles
.listDirectory();
1223 FileFragment secondFragment
= getSecondFragment();
1224 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1225 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1226 detailFragment
.listenForTransferProgress();
1227 detailFragment
.updateFileDetails(false
, false
);
1232 public void onServiceDisconnected(ComponentName component
) {
1233 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1234 Log_OC
.d(TAG
, "Download service disconnected");
1235 mDownloaderBinder
= null
;
1236 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1237 Log_OC
.d(TAG
, "Upload service disconnected");
1238 mUploaderBinder
= null
;
1246 * Launch an intent to request the PIN code to the user before letting him use the app
1248 private void requestPinCode() {
1249 boolean pinStart
= false
;
1250 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
1251 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1253 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1254 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");
1261 public void onSavedCertificate() {
1262 startSyncFolderOperation(getCurrentDir());
1267 public void onFailedSavingCertificate() {
1268 showDialog(DIALOG_CERT_NOT_SAVED
);
1273 * Updates the view associated to the activity after the finish of some operation over files
1274 * in the current account.
1276 * @param operation Removal operation performed.
1277 * @param result Result of the removal.
1280 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1281 if (operation
instanceof RemoveFileOperation
) {
1282 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1284 } else if (operation
instanceof RenameFileOperation
) {
1285 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1287 } else if (operation
instanceof SynchronizeFileOperation
) {
1288 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1290 } else if (operation
instanceof CreateFolderOperation
) {
1291 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1293 } else if (operation
instanceof GetSharesOperation
) {
1294 onGetSharesOperationFinish((GetSharesOperation
) operation
, result
);
1299 /** Updates the data about shared files
1301 * @param operation Get Shared Files
1302 * @param result Result of the operation
1304 private void onGetSharesOperationFinish(GetSharesOperation operation
, RemoteOperationResult result
) {
1306 // Refresh the filelist with the information
1307 refeshListOfFilesFragment();
1312 * Updates the view associated to the activity after the finish of an operation trying to remove a
1315 * @param operation Removal operation performed.
1316 * @param result Result of the removal.
1318 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
1319 dismissLoadingDialog();
1320 if (result
.isSuccess()) {
1321 Toast msg
= Toast
.makeText(this, R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
1323 OCFile removedFile
= operation
.getFile();
1324 getSecondFragment();
1325 FileFragment second
= getSecondFragment();
1326 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1327 cleanSecondFragment();
1329 if (mStorageManager
.getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1330 refeshListOfFilesFragment();
1334 Toast msg
= Toast
.makeText(this, R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
1336 if (result
.isSslRecoverableException()) {
1337 mLastSslUntrustedServerResult
= result
;
1338 showDialog(DIALOG_SSL_VALIDATOR
);
1344 * Updates the view associated to the activity after the finish of an operation trying create a new folder
1346 * @param operation Creation operation performed.
1347 * @param result Result of the creation.
1349 private void onCreateFolderOperationFinish(CreateFolderOperation operation
, RemoteOperationResult result
) {
1350 if (result
.isSuccess()) {
1351 dismissLoadingDialog();
1352 refeshListOfFilesFragment();
1355 dismissLoadingDialog();
1356 if (result
.getCode() == ResultCode
.INVALID_CHARACTER_IN_NAME
) {
1357 Toast
.makeText(FileDisplayActivity
.this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
).show();
1360 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
1363 } catch (NotFoundException e
) {
1364 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1372 * Updates the view associated to the activity after the finish of an operation trying to rename a
1375 * @param operation Renaming operation performed.
1376 * @param result Result of the renaming.
1378 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1379 dismissLoadingDialog();
1380 OCFile renamedFile
= operation
.getFile();
1381 if (result
.isSuccess()) {
1383 FileFragment details
= getSecondFragment();
1384 if (details
!= null
&& details
instanceof FileDetailFragment
&& renamedFile
.equals(details
.getFile()) ) {
1385 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1388 if (mStorageManager
.getFileById(renamedFile
.getParentId()).equals(getCurrentDir())) {
1389 refeshListOfFilesFragment();
1393 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1394 Toast msg
= Toast
.makeText(this, R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1396 // TODO throw again the new rename dialog
1397 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
1398 Toast msg
= Toast
.makeText(this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
1401 Toast msg
= Toast
.makeText(this, R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1403 if (result
.isSslRecoverableException()) {
1404 mLastSslUntrustedServerResult
= result
;
1405 showDialog(DIALOG_SSL_VALIDATOR
);
1412 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1413 dismissLoadingDialog();
1414 OCFile syncedFile
= operation
.getLocalFile();
1415 if (!result
.isSuccess()) {
1416 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1417 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1418 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1419 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
1425 if (operation
.transferWasRequested()) {
1426 refeshListOfFilesFragment();
1427 onTransferStateChanged(syncedFile
, true
, true
);
1430 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1441 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1443 FileFragment details
= getSecondFragment();
1444 if (details
!= null
&& details
instanceof FileDetailFragment
&& file
.equals(details
.getFile()) ) {
1445 if (downloading
|| uploading
) {
1446 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1448 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1455 public void onDismiss(EditNameDialog dialog
) {
1456 if (dialog
.getResult()) {
1457 String newDirectoryName
= dialog
.getNewFilename().trim();
1458 Log_OC
.d(TAG
, "'create directory' dialog dismissed with new name " + newDirectoryName
);
1459 if (newDirectoryName
.length() > 0) {
1460 String path
= getCurrentDir().getRemotePath();
1463 path
+= newDirectoryName
+ OCFile
.PATH_SEPARATOR
;
1464 RemoteOperation operation
= new CreateFolderOperation(path
, false
, mStorageManager
);
1465 operation
.execute( getAccount(),
1466 FileDisplayActivity
.this,
1467 FileDisplayActivity
.this,
1469 FileDisplayActivity
.this);
1471 showLoadingDialog();
1477 private void requestForDownload() {
1478 Account account
= getAccount();
1479 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1480 Intent i
= new Intent(this, FileDownloader
.class);
1481 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1482 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1488 private OCFile
getCurrentDir() {
1489 OCFile file
= getFile();
1491 if (file
.isFolder()) {
1493 } else if (mStorageManager
!= null
) {
1494 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
1495 return mStorageManager
.getFileByPath(parentPath
);
1501 public void startSyncFolderOperation(OCFile folder
) {
1502 long currentSyncTime
= System
.currentTimeMillis();
1504 mSyncInProgress
= true
;
1506 // perform folder synchronization
1507 RemoteOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
1510 getStorageManager(),
1512 getApplicationContext()
1514 synchFolderOp
.execute(getAccount(), this, null
, null
, this);
1516 setSupportProgressBarIndeterminateVisibility(true
);
1520 private void startGetShares() {
1521 // Get shared files/folders
1522 AccountManager accountMngr
= AccountManager
.get(this);
1523 String urlServer
= accountMngr
.getUserData(getAccount(), OwnCloudAccount
.Constants
.KEY_OC_BASE_URL
);
1525 RemoteOperation getShares
= new GetSharesOperation(urlServer
, mStorageManager
);
1526 getShares
.execute(getAccount(), this, this, mHandler
, this);
1530 // public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
1531 // int childCount = viewGroup.getChildCount();
1532 // for (int i = 0; i < childCount; i++) {
1533 // View view = viewGroup.getChildAt(i);
1534 // view.setEnabled(enabled);
1535 // view.setClickable(!enabled);
1536 // if (view instanceof ViewGroup) {
1537 // enableDisableViewGroup((ViewGroup) view, enabled);