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
.support
.v4
.content
.LocalBroadcastManager
;
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
.lib
.operations
.common
.OnRemoteOperationListener
;
74 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
75 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
76 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
78 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
79 import com
.owncloud
.android
.operations
.RenameFileOperation
;
80 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
81 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
82 import com
.owncloud
.android
.services
.OperationsService
;
83 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
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 OperationsServiceReceiver mOperationsServiceReceiver
;
118 private FileDownloaderBinder mDownloaderBinder
= null
;
119 private FileUploaderBinder mUploaderBinder
= null
;
120 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
121 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
123 private boolean mDualPane
;
124 private View mLeftFragmentContainer
;
125 private View mRightFragmentContainer
;
127 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
128 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
129 private static final String KEY_REFRESH_SHARES_IN_PROGRESS
= "SHARES_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
;
152 private boolean mRefreshSharesInProgress
= false
;
155 protected void onCreate(Bundle savedInstanceState
) {
156 Log_OC
.d(TAG
, "onCreate() start");
157 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
159 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account is valid
161 mHandler
= new Handler();
163 /// bindings to transference services
164 mUploadConnection
= new ListServiceConnection();
165 mDownloadConnection
= new ListServiceConnection();
166 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
167 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
169 // PIN CODE request ; best location is to decide, let's try this first
170 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
172 } else if (getIntent().getAction() == null
&& savedInstanceState
== null
) {
177 Intent observer_intent
= new Intent(this, FileObserverService
.class);
178 observer_intent
.putExtra(FileObserverService
.KEY_FILE_CMD
, FileObserverService
.CMD_INIT_OBSERVED_LIST
);
179 startService(observer_intent
);
181 /// Load of saved instance state
182 if(savedInstanceState
!= null
) {
183 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
184 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
185 mRefreshSharesInProgress
= savedInstanceState
.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS
);
188 mWaitingToPreview
= null
;
189 mSyncInProgress
= false
;
190 mRefreshSharesInProgress
= false
;
195 // Inflate and set the layout view
196 setContentView(R
.layout
.files
);
197 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
198 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
199 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
200 if (savedInstanceState
== null
) {
201 createMinFragments();
205 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
206 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
207 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
|| mRefreshSharesInProgress
); // always AFTER setContentView(...) ; to work around bug in its implementation
209 Log_OC
.d(TAG
, "onCreate() end");
213 protected void onStart() {
215 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
219 protected void onDestroy() {
221 if (mDownloadConnection
!= null
)
222 unbindService(mDownloadConnection
);
223 if (mUploadConnection
!= null
)
224 unbindService(mUploadConnection
);
229 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
232 protected void onAccountSet(boolean stateWasRecovered
) {
233 if (getAccount() != null
) {
234 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
236 /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
237 OCFile file
= getFile();
238 // get parent from path
239 String parentPath
= "";
241 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
242 // upload in progress - right now, files are not inserted in the local cache until the upload is successful
243 // get parent from path
244 parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
245 if (mStorageManager
.getFileByPath(parentPath
) == null
)
246 file
= null
; // not able to know the directory where the file is uploading
248 file
= mStorageManager
.getFileByPath(file
.getRemotePath()); // currentDir = null if not in the current Account
252 // fall back to root folder
253 file
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null
256 setNavigationListWithFolder(file
);
258 if (!stateWasRecovered
) {
259 Log_OC
.e(TAG
, "Initializing Fragments in onAccountChanged..");
260 initFragmentsWithFile();
261 if (file
.isFolder()) {
262 startSyncFolderOperation(file
);
266 updateFragmentsVisibility(!file
.isFolder());
267 updateNavigationElementsInActionBar(file
.isFolder() ? null
: file
);
272 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
277 private void setNavigationListWithFolder(OCFile file
) {
278 mDirectories
.clear();
279 OCFile fileIt
= file
;
281 while(fileIt
!= null
&& fileIt
.getFileName() != OCFile
.ROOT_PATH
) {
282 if (fileIt
.isFolder()) {
283 mDirectories
.add(fileIt
.getFileName());
285 //fileIt = mStorageManager.getFileById(fileIt.getParentId());
286 // get parent from path
287 parentPath
= fileIt
.getRemotePath().substring(0, fileIt
.getRemotePath().lastIndexOf(fileIt
.getFileName()));
288 fileIt
= mStorageManager
.getFileByPath(parentPath
);
290 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
294 private void createMinFragments() {
295 OCFileListFragment listOfFiles
= new OCFileListFragment();
296 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
297 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
298 transaction
.commit();
301 private void initFragmentsWithFile() {
302 if (getAccount() != null
&& getFile() != null
) {
304 OCFileListFragment listOfFiles
= getListOfFilesFragment();
305 if (listOfFiles
!= null
) {
306 listOfFiles
.listDirectory(getCurrentDir());
308 Log
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
312 OCFile file
= getFile();
313 Fragment secondFragment
= chooseInitialSecondFragment(file
);
314 if (secondFragment
!= null
) {
315 setSecondFragment(secondFragment
);
316 updateFragmentsVisibility(true
);
317 updateNavigationElementsInActionBar(file
);
320 cleanSecondFragment();
324 Log
.wtf(TAG
, "initFragments() called with invalid NULLs!");
325 if (getAccount() == null
) {
326 Log
.wtf(TAG
, "\t account is NULL");
328 if (getFile() == null
) {
329 Log
.wtf(TAG
, "\t file is NULL");
334 private Fragment
chooseInitialSecondFragment(OCFile file
) {
335 Fragment secondFragment
= null
;
336 if (file
!= null
&& !file
.isFolder()) {
337 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
338 && file
.getLastSyncDateForProperties() > 0 // temporal fix
340 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
341 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
342 secondFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
345 secondFragment
= new FileDetailFragment(file
, getAccount());
348 return secondFragment
;
353 * Replaces the second fragment managed by the activity with the received as
356 * Assumes never will be more than two fragments managed at the same time.
358 * @param fragment New second Fragment to set.
360 private void setSecondFragment(Fragment fragment
) {
361 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
362 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
363 transaction
.commit();
367 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
369 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
370 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
372 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
373 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
376 } else if (existsSecondFragment
) {
377 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
378 mLeftFragmentContainer
.setVisibility(View
.GONE
);
380 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
381 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
385 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
386 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
388 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
389 mRightFragmentContainer
.setVisibility(View
.GONE
);
395 private OCFileListFragment
getListOfFilesFragment() {
396 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_LIST_OF_FILES
);
397 if (listOfFiles
!= null
) {
398 return (OCFileListFragment
)listOfFiles
;
400 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
404 protected FileFragment
getSecondFragment() {
405 Fragment second
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
406 if (second
!= null
) {
407 return (FileFragment
)second
;
412 public void cleanSecondFragment() {
413 Fragment second
= getSecondFragment();
414 if (second
!= null
) {
415 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
419 updateFragmentsVisibility(false
);
420 updateNavigationElementsInActionBar(null
);
423 protected void refeshListOfFilesFragment() {
424 OCFileListFragment fileListFragment
= getListOfFilesFragment();
425 if (fileListFragment
!= null
) {
426 fileListFragment
.listDirectory();
430 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
431 FileFragment secondFragment
= getSecondFragment();
432 boolean waitedPreview
= (mWaitingToPreview
!= null
&& mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
433 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
434 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
435 OCFile fileInFragment
= detailsFragment
.getFile();
436 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
437 // the user browsed to other file ; forget the automatic preview
438 mWaitingToPreview
= null
;
440 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
441 // grant that the right panel updates the progress bar
442 detailsFragment
.listenForTransferProgress();
443 detailsFragment
.updateFileDetails(true
, false
);
445 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
446 // update the right panel
447 boolean detailsFragmentChanged
= false
;
450 mWaitingToPreview
= mStorageManager
.getFileById(mWaitingToPreview
.getFileId()); // update the file from database, for the local storage path
451 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
452 startMediaPreview(mWaitingToPreview
, 0, true
);
453 detailsFragmentChanged
= true
;
455 openFile(mWaitingToPreview
);
458 mWaitingToPreview
= null
;
460 if (!detailsFragmentChanged
) {
461 detailsFragment
.updateFileDetails(false
, (success
));
469 public boolean onCreateOptionsMenu(Menu menu
) {
470 MenuInflater inflater
= getSherlock().getMenuInflater();
471 inflater
.inflate(R
.menu
.main_menu
, menu
);
476 public boolean onOptionsItemSelected(MenuItem item
) {
477 boolean retval
= true
;
478 switch (item
.getItemId()) {
479 case R
.id
.action_create_dir
: {
480 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.uploader_info_dirname
), "", -1, -1, this);
481 dialog
.show(getSupportFragmentManager(), "createdirdialog");
484 case R
.id
.action_sync_account
: {
485 startSynchronization();
488 case R
.id
.action_upload
: {
489 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
492 case R
.id
.action_settings
: {
493 Intent settingsIntent
= new Intent(this, Preferences
.class);
494 startActivity(settingsIntent
);
497 case android
.R
.id
.home
: {
498 FileFragment second
= getSecondFragment();
499 OCFile currentDir
= getCurrentDir();
500 if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
501 (second
!= null
&& second
.getFile() != null
)) {
508 retval
= super.onOptionsItemSelected(item
);
513 private void startSynchronization() {
514 Log_OC
.e(TAG
, "Got to start sync");
515 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
516 Log_OC
.e(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
517 ContentResolver
.cancelSync(null
, MainApp
.getAuthority()); // cancel the current synchronizations of any ownCloud account
518 Bundle bundle
= new Bundle();
519 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
520 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
521 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority());
522 ContentResolver
.requestSync(
524 MainApp
.getAuthority(), bundle
);
526 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority() + " with new API");
527 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
528 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
529 builder
.setExpedited(true
);
530 builder
.setManual(true
);
532 SyncRequest request
= builder
.build();
533 ContentResolver
.requestSync(request
);
539 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
540 if (itemPosition
!= 0) {
541 String targetPath
= "";
542 for (int i
=itemPosition
; i
< mDirectories
.getCount() - 1; i
++) {
543 targetPath
= mDirectories
.getItem(i
) + OCFile
.PATH_SEPARATOR
+ targetPath
;
545 targetPath
= OCFile
.PATH_SEPARATOR
+ targetPath
;
546 OCFile targetFolder
= mStorageManager
.getFileByPath(targetPath
);
547 if (targetFolder
!= null
) {
548 browseTo(targetFolder
);
551 // the next operation triggers a new call to this method, but it's necessary to
552 // ensure that the name exposed in the action bar is the current directory when the
553 // user selected it in the navigation list
554 getSupportActionBar().setSelectedNavigationItem(0);
560 * Called, when the user selected something for uploading
562 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
563 super.onActivityResult(requestCode
, resultCode
, data
);
565 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
566 requestSimpleUpload(data
, resultCode
);
568 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
569 requestMultipleUpload(data
, resultCode
);
574 private void requestMultipleUpload(Intent data
, int resultCode
) {
575 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
576 if (filePaths
!= null
) {
577 String
[] remotePaths
= new String
[filePaths
.length
];
578 String remotePathBase
= "";
579 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
580 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
582 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
583 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
584 for (int j
= 0; j
< remotePaths
.length
; j
++) {
585 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
588 Intent i
= new Intent(this, FileUploader
.class);
589 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
590 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
591 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
592 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
593 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
594 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
598 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
599 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
606 private void requestSimpleUpload(Intent data
, int resultCode
) {
607 String filepath
= null
;
609 Uri selectedImageUri
= data
.getData();
611 String filemanagerstring
= selectedImageUri
.getPath();
612 String selectedImagePath
= getPath(selectedImageUri
);
614 if (selectedImagePath
!= null
)
615 filepath
= selectedImagePath
;
617 filepath
= filemanagerstring
;
619 } catch (Exception e
) {
620 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
624 if (filepath
== null
) {
625 Log_OC
.e(TAG
, "Couldnt resolve path to file");
626 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
632 Intent i
= new Intent(this, FileUploader
.class);
633 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
635 String remotepath
= new String();
636 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
637 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
639 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
640 remotepath
+= OCFile
.PATH_SEPARATOR
;
641 remotepath
+= new File(filepath
).getName();
643 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
644 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
645 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
646 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
647 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
652 public void onBackPressed() {
653 OCFileListFragment listOfFiles
= getListOfFilesFragment();
654 if (mDualPane
|| getSecondFragment() == null
) {
655 if (listOfFiles
!= null
) { // should never be null, indeed
656 if (mDirectories
.getCount() <= 1) {
660 int levelsUp
= listOfFiles
.onBrowseUp();
661 for (int i
=0; i
< levelsUp
&& mDirectories
.getCount() > 1 ; i
++) {
666 if (listOfFiles
!= null
) { // should never be null, indeed
667 setFile(listOfFiles
.getCurrentFile());
669 cleanSecondFragment();
674 protected void onSaveInstanceState(Bundle outState
) {
675 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
676 Log_OC
.e(TAG
, "onSaveInstanceState() start");
677 super.onSaveInstanceState(outState
);
678 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
679 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
680 outState
.putBoolean(FileDisplayActivity
.KEY_REFRESH_SHARES_IN_PROGRESS
, mRefreshSharesInProgress
);
682 Log_OC
.d(TAG
, "onSaveInstanceState() end");
688 protected void onResume() {
690 Log_OC
.e(TAG
, "onResume() start");
692 // Listen for sync messages
693 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
694 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
695 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FOLDER_SIZE_SYNCED
);
696 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FOLDER_CONTENTS_SYNCED
);
697 syncIntentFilter
.addAction(SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
);
698 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
699 //registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
700 LocalBroadcastManager
.getInstance(this).registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
702 // Listen for upload messages
703 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
704 mUploadFinishReceiver
= new UploadFinishReceiver();
705 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
707 // Listen for download messages
708 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.getDownloadAddedMessage());
709 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
710 mDownloadFinishReceiver
= new DownloadFinishReceiver();
711 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
713 // Listen for messages from the OperationsService
714 IntentFilter operationsIntentFilter
= new IntentFilter(OperationsService
.ACTION_OPERATION_ADDED
);
715 operationsIntentFilter
.addAction(OperationsService
.ACTION_OPERATION_FINISHED
);
716 mOperationsServiceReceiver
= new OperationsServiceReceiver();
717 LocalBroadcastManager
.getInstance(this).registerReceiver(mOperationsServiceReceiver
, operationsIntentFilter
);
719 Log_OC
.d(TAG
, "onResume() end");
724 protected void onPause() {
726 Log_OC
.e(TAG
, "onPause() start");
727 if (mSyncBroadcastReceiver
!= null
) {
728 //unregisterReceiver(mSyncBroadcastReceiver);
729 LocalBroadcastManager
.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver
);
730 mSyncBroadcastReceiver
= null
;
732 if (mUploadFinishReceiver
!= null
) {
733 unregisterReceiver(mUploadFinishReceiver
);
734 mUploadFinishReceiver
= null
;
736 if (mDownloadFinishReceiver
!= null
) {
737 unregisterReceiver(mDownloadFinishReceiver
);
738 mDownloadFinishReceiver
= null
;
740 if (mOperationsServiceReceiver
!= null
) {
741 LocalBroadcastManager
.getInstance(this).unregisterReceiver(mOperationsServiceReceiver
);
742 mOperationsServiceReceiver
= null
;
744 Log_OC
.d(TAG
, "onPause() end");
749 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
750 if (id
== DIALOG_SSL_VALIDATOR
&& mLastSslUntrustedServerResult
!= null
) {
751 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
757 protected Dialog
onCreateDialog(int id
) {
758 Dialog dialog
= null
;
759 AlertDialog
.Builder builder
;
761 case DIALOG_SHORT_WAIT
: {
762 ProgressDialog working_dialog
= new ProgressDialog(this);
763 working_dialog
.setMessage(getResources().getString(
764 R
.string
.wait_a_moment
));
765 working_dialog
.setIndeterminate(true
);
766 working_dialog
.setCancelable(false
);
767 dialog
= working_dialog
;
770 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
772 String
[] items
= null
;
774 String
[] allTheItems
= { getString(R
.string
.actionbar_upload_files
),
775 getString(R
.string
.actionbar_upload_from_apps
),
776 getString(R
.string
.actionbar_failed_instant_upload
) };
778 String
[] commonItems
= { getString(R
.string
.actionbar_upload_files
),
779 getString(R
.string
.actionbar_upload_from_apps
) };
781 if (InstantUploadActivity
.IS_ENABLED
)
786 builder
= new AlertDialog
.Builder(this);
787 builder
.setTitle(R
.string
.actionbar_upload
);
788 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
789 public void onClick(DialogInterface dialog
, int item
) {
792 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
793 action
.putExtra(UploadFilesActivity
.EXTRA_ACCOUNT
, FileDisplayActivity
.this.getAccount());
794 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
796 // TODO create and handle new fragment
797 // LocalFileListFragment
799 } else if (item
== 1) {
800 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
801 action
= action
.setType("*/*").addCategory(Intent
.CATEGORY_OPENABLE
);
802 startActivityForResult(Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
803 ACTION_SELECT_CONTENT_FROM_APPS
);
804 } else if (item
== 2 && InstantUploadActivity
.IS_ENABLED
) {
805 Intent action
= new Intent(FileDisplayActivity
.this, InstantUploadActivity
.class);
806 action
.putExtra(FileUploader
.KEY_ACCOUNT
, FileDisplayActivity
.this.getAccount());
807 startActivity(action
);
811 dialog
= builder
.create();
814 case DIALOG_SSL_VALIDATOR
: {
815 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
818 case DIALOG_CERT_NOT_SAVED
: {
819 builder
= new AlertDialog
.Builder(this);
820 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
821 builder
.setCancelable(false
);
822 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
824 public void onClick(DialogInterface dialog
, int which
) {
828 dialog
= builder
.create();
840 * Show loading dialog
842 public void showLoadingDialog() {
844 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
845 FragmentManager fm
= getSupportFragmentManager();
846 FragmentTransaction ft
= fm
.beginTransaction();
847 loading
.show(ft
, DIALOG_WAIT_TAG
);
852 * Dismiss loading dialog
854 public void dismissLoadingDialog(){
855 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
857 LoadingDialog loading
= (LoadingDialog
) frag
;
864 * Translates a content URI of an image to a physical path
866 * @param uri The URI to resolve
867 * @return The path to the image or null if it could not be found
869 public String
getPath(Uri uri
) {
870 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
871 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
872 if (cursor
!= null
) {
873 int column_index
= cursor
874 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
875 cursor
.moveToFirst();
876 return cursor
.getString(column_index
);
882 * Pushes a directory to the drop down list
883 * @param directory to push
884 * @throws IllegalArgumentException If the {@link OCFile#isFolder()} returns false.
886 public void pushDirname(OCFile directory
) {
887 if(!directory
.isFolder()){
888 throw new IllegalArgumentException("Only directories may be pushed!");
890 mDirectories
.insert(directory
.getFileName(), 0);
895 * Pops a directory name from the drop down list
896 * @return True, unless the stack is empty
898 public boolean popDirname() {
899 mDirectories
.remove(mDirectories
.getItem(0));
900 return !mDirectories
.isEmpty();
903 // Custom array adapter to override text colors
904 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
906 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
910 public View
getView(int position
, View convertView
, ViewGroup parent
) {
911 View v
= super.getView(position
, convertView
, parent
);
913 ((TextView
) v
).setTextColor(getResources().getColorStateList(
914 android
.R
.color
.white
));
918 public View
getDropDownView(int position
, View convertView
,
920 View v
= super.getDropDownView(position
, convertView
, parent
);
922 ((TextView
) v
).setTextColor(getResources().getColorStateList(
923 android
.R
.color
.white
));
930 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
933 * {@link BroadcastReceiver} to enable syncing feedback in UI
936 public void onReceive(Context context
, Intent intent
) {
937 String event
= intent
.getAction();
938 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
939 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
940 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
941 boolean sameAccount
= (getAccount() != null
&& accountName
.equals(getAccount().name
) && mStorageManager
!= null
);
945 if (!FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
947 OCFile currentFile
= (getFile() == null
) ? null
: mStorageManager
.getFileByPath(getFile().getRemotePath());
948 OCFile currentDir
= (getCurrentDir() == null
) ? null
: mStorageManager
.getFileByPath(getCurrentDir().getRemotePath());
950 if (currentDir
== null
) {
951 // current folder was removed from the server
952 Toast
.makeText( FileDisplayActivity
.this,
953 String
.format(getString(R
.string
.sync_current_folder_was_removed
), mDirectories
.getItem(0)),
959 if (currentFile
== null
&& !getFile().isFolder()) {
960 // currently selected file was removed in the server, and now we know it
961 cleanSecondFragment();
962 currentFile
= currentDir
;
965 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
966 OCFileListFragment fileListFragment
= getListOfFilesFragment();
967 if (fileListFragment
!= null
) {
968 fileListFragment
.listDirectory(currentDir
);
971 setFile(currentFile
);
974 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
975 !SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
.equals(event
) &&
976 (synchResult
== null
|| synchResult
.isSuccess())) ;
978 if (synchResult
!= null
&&
979 synchResult
.isSuccess() &&
980 (SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
.equals(event
) ||
981 FileSyncAdapter
.EVENT_FOLDER_CONTENTS_SYNCED
.equals(event
)
988 //removeStickyBroadcast(intent);
989 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
|| mRefreshSharesInProgress
);
992 if (synchResult
!= null
) {
993 if (synchResult
.getCode().equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
994 mLastSslUntrustedServerResult
= synchResult
;
995 showDialog(DIALOG_SSL_VALIDATOR
);
1002 private class UploadFinishReceiver
extends BroadcastReceiver
{
1004 * Once the file upload has finished -> update view
1005 * @author David A. Velasco
1006 * {@link BroadcastReceiver} to enable upload feedback in UI
1009 public void onReceive(Context context
, Intent intent
) {
1010 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1011 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
1012 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
1013 OCFile currentDir
= getCurrentDir();
1014 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) && (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1015 if (sameAccount
&& isDescendant
) {
1016 refeshListOfFilesFragment();
1024 * Class waiting for broadcast events from the {@link FielDownloader} service.
1026 * Updates the UI when a download is started or finished, provided that it is relevant for the
1029 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1031 public void onReceive(Context context
, Intent intent
) {
1032 boolean sameAccount
= isSameAccount(context
, intent
);
1033 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1034 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1036 if (sameAccount
&& isDescendant
) {
1037 refeshListOfFilesFragment();
1038 refreshSecondFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
1041 removeStickyBroadcast(intent
);
1044 private boolean isDescendant(String downloadedRemotePath
) {
1045 OCFile currentDir
= getCurrentDir();
1046 return (currentDir
!= null
&& downloadedRemotePath
!= null
&& downloadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1049 private boolean isSameAccount(Context context
, Intent intent
) {
1050 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1051 return (accountName
!= null
&& getAccount() != null
&& accountName
.equals(getAccount().name
));
1057 * Class waiting for broadcast events from the {@link OperationsService}.
1059 * Updates the list of files when a get for shares is finished; at this moment the refresh of shares is the only
1060 * operation performed in {@link OperationsService}.
1062 * In the future will handle the progress or finalization of all the operations performed in {@link OperationsService},
1063 * probably all the operations associated to the app model.
1065 private class OperationsServiceReceiver
extends BroadcastReceiver
{
1068 public void onReceive(Context context
, Intent intent
) {
1069 if (OperationsService
.ACTION_OPERATION_ADDED
.equals(intent
.getAction())) {
1071 } else if (OperationsService
.ACTION_OPERATION_FINISHED
.equals(intent
.getAction())) {
1072 mRefreshSharesInProgress
= false
;
1074 Account account
= intent
.getParcelableExtra(OperationsService
.EXTRA_ACCOUNT
);
1075 RemoteOperationResult getSharesResult
= (RemoteOperationResult
)intent
.getSerializableExtra(OperationsService
.EXTRA_RESULT
);
1076 if (getAccount() != null
&& account
.name
.equals(getAccount().name
)
1077 && mStorageManager
!= null
1079 refeshListOfFilesFragment();
1081 if ((getSharesResult
!= null
) &&
1082 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
.equals(getSharesResult
.getCode())) {
1083 mLastSslUntrustedServerResult
= getSharesResult
;
1084 showDialog(DIALOG_SSL_VALIDATOR
);
1087 setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress
|| mSyncInProgress
);
1099 public FileDataStorageManager
getStorageManager() {
1100 return mStorageManager
;
1104 public void browseToRoot() {
1105 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1106 if (listOfFiles
!= null
) { // should never be null, indeed
1107 while (mDirectories
.getCount() > 1) {
1110 OCFile root
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
);
1111 listOfFiles
.listDirectory(root
);
1112 setFile(listOfFiles
.getCurrentFile());
1113 startSyncFolderOperation(root
);
1115 cleanSecondFragment();
1119 public void browseTo(OCFile folder
) {
1120 if (folder
== null
|| !folder
.isFolder()) {
1121 throw new IllegalArgumentException("Trying to browse to invalid folder " + folder
);
1123 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1124 if (listOfFiles
!= null
) {
1125 setNavigationListWithFolder(folder
);
1126 listOfFiles
.listDirectory(folder
);
1127 setFile(listOfFiles
.getCurrentFile());
1128 startSyncFolderOperation(folder
);
1130 Log_OC
.e(TAG
, "Unexpected null when accessing list fragment");
1132 cleanSecondFragment();
1139 * Updates action bar and second fragment, if in dual pane mode.
1142 public void onBrowsedDownTo(OCFile directory
) {
1143 pushDirname(directory
);
1144 cleanSecondFragment();
1147 startSyncFolderOperation(directory
);
1152 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1154 * @param file Image {@link OCFile} to show.
1157 public void startImagePreview(OCFile file
) {
1158 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1159 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1160 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1161 startActivity(showDetailsIntent
);
1165 * Stars the preview of an already down media {@link OCFile}.
1167 * @param file Media {@link OCFile} to preview.
1168 * @param startPlaybackPosition Media position where the playback will be started, in milliseconds.
1169 * @param autoplay When 'true', the playback will start without user interactions.
1172 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1173 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
1174 setSecondFragment(mediaFragment
);
1175 updateFragmentsVisibility(true
);
1176 updateNavigationElementsInActionBar(file
);
1181 * Requests the download of the received {@link OCFile} , updates the UI
1182 * to monitor the download progress and prepares the activity to preview
1183 * or open the file when the download finishes.
1185 * @param file {@link OCFile} to download and preview.
1188 public void startDownloadForPreview(OCFile file
) {
1189 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1190 setSecondFragment(detailFragment
);
1191 mWaitingToPreview
= file
;
1192 requestForDownload();
1193 updateFragmentsVisibility(true
);
1194 updateNavigationElementsInActionBar(file
);
1200 * Shows the information of the {@link OCFile} received as a
1201 * parameter in the second fragment.
1203 * @param file {@link OCFile} whose details will be shown
1206 public void showDetails(OCFile file
) {
1207 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1208 setSecondFragment(detailFragment
);
1209 updateFragmentsVisibility(true
);
1210 updateNavigationElementsInActionBar(file
);
1218 private void updateNavigationElementsInActionBar(OCFile chosenFile
) {
1219 ActionBar actionBar
= getSupportActionBar();
1220 if (chosenFile
== null
|| mDualPane
) {
1221 // only list of files - set for browsing through folders
1222 OCFile currentDir
= getCurrentDir();
1223 actionBar
.setDisplayHomeAsUpEnabled(currentDir
!= null
&& currentDir
.getParentId() != 0);
1224 actionBar
.setDisplayShowTitleEnabled(false
);
1225 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
1226 actionBar
.setListNavigationCallbacks(mDirectories
, this); // assuming mDirectories is updated
1229 actionBar
.setDisplayHomeAsUpEnabled(true
);
1230 actionBar
.setDisplayShowTitleEnabled(true
);
1231 actionBar
.setTitle(chosenFile
.getFileName());
1232 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
1241 public void onFileStateChanged() {
1242 refeshListOfFilesFragment();
1243 updateNavigationElementsInActionBar(getSecondFragment().getFile());
1251 public FileDownloaderBinder
getFileDownloaderBinder() {
1252 return mDownloaderBinder
;
1260 public FileUploaderBinder
getFileUploaderBinder() {
1261 return mUploaderBinder
;
1265 /** Defines callbacks for service binding, passed to bindService() */
1266 private class ListServiceConnection
implements ServiceConnection
{
1269 public void onServiceConnected(ComponentName component
, IBinder service
) {
1270 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1271 Log_OC
.d(TAG
, "Download service connected");
1272 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1273 if (mWaitingToPreview
!= null
) {
1274 requestForDownload();
1277 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1278 Log_OC
.d(TAG
, "Upload service connected");
1279 mUploaderBinder
= (FileUploaderBinder
) service
;
1283 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1284 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1285 if (listOfFiles
!= null
) {
1286 listOfFiles
.listDirectory();
1288 FileFragment secondFragment
= getSecondFragment();
1289 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1290 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1291 detailFragment
.listenForTransferProgress();
1292 detailFragment
.updateFileDetails(false
, false
);
1297 public void onServiceDisconnected(ComponentName component
) {
1298 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1299 Log_OC
.d(TAG
, "Download service disconnected");
1300 mDownloaderBinder
= null
;
1301 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1302 Log_OC
.d(TAG
, "Upload service disconnected");
1303 mUploaderBinder
= null
;
1311 * Launch an intent to request the PIN code to the user before letting him use the app
1313 private void requestPinCode() {
1314 boolean pinStart
= false
;
1315 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
1316 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1318 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1319 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");
1326 public void onSavedCertificate() {
1327 startSyncFolderOperation(getCurrentDir());
1332 public void onFailedSavingCertificate() {
1333 showDialog(DIALOG_CERT_NOT_SAVED
);
1338 * Updates the view associated to the activity after the finish of some operation over files
1339 * in the current account.
1341 * @param operation Removal operation performed.
1342 * @param result Result of the removal.
1345 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1346 if (operation
instanceof RemoveFileOperation
) {
1347 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1349 } else if (operation
instanceof RenameFileOperation
) {
1350 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1352 } else if (operation
instanceof SynchronizeFileOperation
) {
1353 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1355 } else if (operation
instanceof CreateFolderOperation
) {
1356 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1362 * Updates the view associated to the activity after the finish of an operation trying to remove a
1365 * @param operation Removal operation performed.
1366 * @param result Result of the removal.
1368 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
1369 dismissLoadingDialog();
1370 if (result
.isSuccess()) {
1371 Toast msg
= Toast
.makeText(this, R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
1373 OCFile removedFile
= operation
.getFile();
1374 getSecondFragment();
1375 FileFragment second
= getSecondFragment();
1376 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1377 cleanSecondFragment();
1379 if (mStorageManager
.getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1380 refeshListOfFilesFragment();
1384 Toast msg
= Toast
.makeText(this, R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
1386 if (result
.isSslRecoverableException()) {
1387 mLastSslUntrustedServerResult
= result
;
1388 showDialog(DIALOG_SSL_VALIDATOR
);
1394 * Updates the view associated to the activity after the finish of an operation trying create a new folder
1396 * @param operation Creation operation performed.
1397 * @param result Result of the creation.
1399 private void onCreateFolderOperationFinish(CreateFolderOperation operation
, RemoteOperationResult result
) {
1400 if (result
.isSuccess()) {
1401 dismissLoadingDialog();
1402 refeshListOfFilesFragment();
1405 dismissLoadingDialog();
1406 if (result
.getCode() == ResultCode
.INVALID_CHARACTER_IN_NAME
) {
1407 Toast
.makeText(FileDisplayActivity
.this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
).show();
1410 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
1413 } catch (NotFoundException e
) {
1414 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1422 * Updates the view associated to the activity after the finish of an operation trying to rename a
1425 * @param operation Renaming operation performed.
1426 * @param result Result of the renaming.
1428 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1429 dismissLoadingDialog();
1430 OCFile renamedFile
= operation
.getFile();
1431 if (result
.isSuccess()) {
1433 FileFragment details
= getSecondFragment();
1434 if (details
!= null
&& details
instanceof FileDetailFragment
&& renamedFile
.equals(details
.getFile()) ) {
1435 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1438 if (mStorageManager
.getFileById(renamedFile
.getParentId()).equals(getCurrentDir())) {
1439 refeshListOfFilesFragment();
1443 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1444 Toast msg
= Toast
.makeText(this, R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1446 // TODO throw again the new rename dialog
1447 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
1448 Toast msg
= Toast
.makeText(this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
1451 Toast msg
= Toast
.makeText(this, R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1453 if (result
.isSslRecoverableException()) {
1454 mLastSslUntrustedServerResult
= result
;
1455 showDialog(DIALOG_SSL_VALIDATOR
);
1462 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1463 dismissLoadingDialog();
1464 OCFile syncedFile
= operation
.getLocalFile();
1465 if (!result
.isSuccess()) {
1466 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1467 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1468 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1469 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
1475 if (operation
.transferWasRequested()) {
1476 refeshListOfFilesFragment();
1477 onTransferStateChanged(syncedFile
, true
, true
);
1480 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1491 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1493 FileFragment details
= getSecondFragment();
1494 if (details
!= null
&& details
instanceof FileDetailFragment
&& file
.equals(details
.getFile()) ) {
1495 if (downloading
|| uploading
) {
1496 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1498 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1505 public void onDismiss(EditNameDialog dialog
) {
1506 if (dialog
.getResult()) {
1507 String newDirectoryName
= dialog
.getNewFilename().trim();
1508 Log_OC
.d(TAG
, "'create directory' dialog dismissed with new name " + newDirectoryName
);
1509 if (newDirectoryName
.length() > 0) {
1510 String path
= getCurrentDir().getRemotePath();
1513 path
+= newDirectoryName
+ OCFile
.PATH_SEPARATOR
;
1514 RemoteOperation operation
= new CreateFolderOperation(path
, false
, mStorageManager
);
1515 operation
.execute( getAccount(),
1516 FileDisplayActivity
.this,
1517 FileDisplayActivity
.this,
1519 FileDisplayActivity
.this);
1521 showLoadingDialog();
1527 private void requestForDownload() {
1528 Account account
= getAccount();
1529 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1530 Intent i
= new Intent(this, FileDownloader
.class);
1531 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1532 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1538 private OCFile
getCurrentDir() {
1539 OCFile file
= getFile();
1541 if (file
.isFolder()) {
1543 } else if (mStorageManager
!= null
) {
1544 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
1545 return mStorageManager
.getFileByPath(parentPath
);
1551 public void startSyncFolderOperation(OCFile folder
) {
1552 long currentSyncTime
= System
.currentTimeMillis();
1554 mSyncInProgress
= true
;
1556 // perform folder synchronization
1557 RemoteOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
1560 getStorageManager(),
1562 getApplicationContext()
1564 synchFolderOp
.execute(getAccount(), this, null
, null
, this);
1566 setSupportProgressBarIndeterminateVisibility(true
);
1570 private void startGetShares() {
1571 // Get shared files/folders
1572 Intent intent
= new Intent(this, OperationsService
.class);
1573 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, getAccount());
1574 startService(intent
);
1576 mRefreshSharesInProgress
= true
;