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
.IBinder
;
42 import android
.preference
.PreferenceManager
;
43 import android
.provider
.MediaStore
;
44 import android
.support
.v4
.app
.Fragment
;
45 import android
.support
.v4
.app
.FragmentTransaction
;
46 import android
.support
.v4
.content
.LocalBroadcastManager
;
47 import android
.util
.Log
;
48 import android
.view
.View
;
49 import android
.view
.ViewGroup
;
50 import android
.widget
.ArrayAdapter
;
51 import android
.widget
.TextView
;
52 import android
.widget
.Toast
;
54 import com
.actionbarsherlock
.app
.ActionBar
;
55 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
56 import com
.actionbarsherlock
.view
.Menu
;
57 import com
.actionbarsherlock
.view
.MenuInflater
;
58 import com
.actionbarsherlock
.view
.MenuItem
;
59 import com
.actionbarsherlock
.view
.Window
;
60 import com
.owncloud
.android
.MainApp
;
61 import com
.owncloud
.android
.R
;
62 import com
.owncloud
.android
.datamodel
.OCFile
;
63 import com
.owncloud
.android
.files
.services
.FileDownloader
;
64 import com
.owncloud
.android
.files
.services
.FileObserverService
;
65 import com
.owncloud
.android
.files
.services
.FileUploader
;
66 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
67 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
68 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
70 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
71 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
72 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
73 import com
.owncloud
.android
.operations
.CreateShareOperation
;
74 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
75 import com
.owncloud
.android
.operations
.RenameFileOperation
;
76 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
77 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
78 import com
.owncloud
.android
.services
.OperationsService
;
79 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
80 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
81 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
82 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
;
83 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
.OnSslValidatorListener
;
84 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
85 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
86 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
87 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
88 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
89 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
90 import com
.owncloud
.android
.utils
.DisplayUtils
;
91 import com
.owncloud
.android
.utils
.Log_OC
;
95 * Displays, what files the user has available in his ownCloud.
97 * @author Bartek Przybylski
98 * @author David A. Velasco
101 public class FileDisplayActivity
extends FileActivity
implements
102 OCFileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
, OnSslValidatorListener
, EditNameDialogListener
{
104 private ArrayAdapter
<String
> mDirectories
;
106 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
107 private UploadFinishReceiver mUploadFinishReceiver
;
108 private DownloadFinishReceiver mDownloadFinishReceiver
;
109 private OperationsServiceReceiver mOperationsServiceReceiver
;
110 private FileDownloaderBinder mDownloaderBinder
= null
;
111 private FileUploaderBinder mUploaderBinder
= null
;
112 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
113 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
115 private boolean mDualPane
;
116 private View mLeftFragmentContainer
;
117 private View mRightFragmentContainer
;
119 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
120 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
121 private static final String KEY_REFRESH_SHARES_IN_PROGRESS
= "SHARES_IN_PROGRESS";
123 public static final int DIALOG_SHORT_WAIT
= 0;
124 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 1;
125 private static final int DIALOG_SSL_VALIDATOR
= 2;
126 private static final int DIALOG_CERT_NOT_SAVED
= 3;
128 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
130 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
131 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
133 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
135 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
136 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
138 private OCFile mWaitingToPreview
;
140 private boolean mSyncInProgress
= false
;
141 private boolean mRefreshSharesInProgress
= false
;
144 protected void onCreate(Bundle savedInstanceState
) {
145 Log_OC
.d(TAG
, "onCreate() start");
146 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
148 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account is valid
150 /// bindings to transference services
151 mUploadConnection
= new ListServiceConnection();
152 mDownloadConnection
= new ListServiceConnection();
153 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
154 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
156 // PIN CODE request ; best location is to decide, let's try this first
157 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
159 } else if (getIntent().getAction() == null
&& savedInstanceState
== null
) {
164 Intent observer_intent
= new Intent(this, FileObserverService
.class);
165 observer_intent
.putExtra(FileObserverService
.KEY_FILE_CMD
, FileObserverService
.CMD_INIT_OBSERVED_LIST
);
166 startService(observer_intent
);
168 /// Load of saved instance state
169 if(savedInstanceState
!= null
) {
170 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
171 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
172 mRefreshSharesInProgress
= savedInstanceState
.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS
);
175 mWaitingToPreview
= null
;
176 mSyncInProgress
= false
;
177 mRefreshSharesInProgress
= false
;
182 // Inflate and set the layout view
183 setContentView(R
.layout
.files
);
184 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
185 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
186 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
187 if (savedInstanceState
== null
) {
188 createMinFragments();
192 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
193 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
194 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
|| mRefreshSharesInProgress
); // always AFTER setContentView(...) ; to work around bug in its implementation
196 Log_OC
.d(TAG
, "onCreate() end");
200 protected void onStart() {
202 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
206 protected void onDestroy() {
208 if (mDownloadConnection
!= null
)
209 unbindService(mDownloadConnection
);
210 if (mUploadConnection
!= null
)
211 unbindService(mUploadConnection
);
216 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
219 protected void onAccountSet(boolean stateWasRecovered
) {
220 super.onAccountSet(stateWasRecovered
);
221 if (getAccount() != null
) {
222 /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
223 OCFile file
= getFile();
224 // get parent from path
225 String parentPath
= "";
227 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
228 // upload in progress - right now, files are not inserted in the local cache until the upload is successful
229 // get parent from path
230 parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
231 if (getStorageManager().getFileByPath(parentPath
) == null
)
232 file
= null
; // not able to know the directory where the file is uploading
234 file
= getStorageManager().getFileByPath(file
.getRemotePath()); // currentDir = null if not in the current Account
238 // fall back to root folder
239 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
242 setNavigationListWithFolder(file
);
244 if (!stateWasRecovered
) {
245 Log_OC
.e(TAG
, "Initializing Fragments in onAccountChanged..");
246 initFragmentsWithFile();
247 if (file
.isFolder()) {
248 startSyncFolderOperation(file
);
252 updateFragmentsVisibility(!file
.isFolder());
253 updateNavigationElementsInActionBar(file
.isFolder() ? null
: file
);
259 private void setNavigationListWithFolder(OCFile file
) {
260 mDirectories
.clear();
261 OCFile fileIt
= file
;
263 while(fileIt
!= null
&& fileIt
.getFileName() != OCFile
.ROOT_PATH
) {
264 if (fileIt
.isFolder()) {
265 mDirectories
.add(fileIt
.getFileName());
267 // get parent from path
268 parentPath
= fileIt
.getRemotePath().substring(0, fileIt
.getRemotePath().lastIndexOf(fileIt
.getFileName()));
269 fileIt
= getStorageManager().getFileByPath(parentPath
);
271 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
275 private void createMinFragments() {
276 OCFileListFragment listOfFiles
= new OCFileListFragment();
277 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
278 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
279 transaction
.commit();
282 private void initFragmentsWithFile() {
283 if (getAccount() != null
&& getFile() != null
) {
285 OCFileListFragment listOfFiles
= getListOfFilesFragment();
286 if (listOfFiles
!= null
) {
287 listOfFiles
.listDirectory(getCurrentDir());
289 Log
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
293 OCFile file
= getFile();
294 Fragment secondFragment
= chooseInitialSecondFragment(file
);
295 if (secondFragment
!= null
) {
296 setSecondFragment(secondFragment
);
297 updateFragmentsVisibility(true
);
298 updateNavigationElementsInActionBar(file
);
301 cleanSecondFragment();
305 Log
.wtf(TAG
, "initFragments() called with invalid NULLs!");
306 if (getAccount() == null
) {
307 Log
.wtf(TAG
, "\t account is NULL");
309 if (getFile() == null
) {
310 Log
.wtf(TAG
, "\t file is NULL");
315 private Fragment
chooseInitialSecondFragment(OCFile file
) {
316 Fragment secondFragment
= null
;
317 if (file
!= null
&& !file
.isFolder()) {
318 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
319 && file
.getLastSyncDateForProperties() > 0 // temporal fix
321 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
322 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
323 secondFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
326 secondFragment
= new FileDetailFragment(file
, getAccount());
329 return secondFragment
;
334 * Replaces the second fragment managed by the activity with the received as
337 * Assumes never will be more than two fragments managed at the same time.
339 * @param fragment New second Fragment to set.
341 private void setSecondFragment(Fragment fragment
) {
342 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
343 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
344 transaction
.commit();
348 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
350 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
351 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
353 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
354 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
357 } else if (existsSecondFragment
) {
358 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
359 mLeftFragmentContainer
.setVisibility(View
.GONE
);
361 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
362 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
366 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
367 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
369 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
370 mRightFragmentContainer
.setVisibility(View
.GONE
);
376 private OCFileListFragment
getListOfFilesFragment() {
377 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_LIST_OF_FILES
);
378 if (listOfFiles
!= null
) {
379 return (OCFileListFragment
)listOfFiles
;
381 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
385 protected FileFragment
getSecondFragment() {
386 Fragment second
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
387 if (second
!= null
) {
388 return (FileFragment
)second
;
393 public void cleanSecondFragment() {
394 Fragment second
= getSecondFragment();
395 if (second
!= null
) {
396 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
400 updateFragmentsVisibility(false
);
401 updateNavigationElementsInActionBar(null
);
404 protected void refeshListOfFilesFragment() {
405 OCFileListFragment fileListFragment
= getListOfFilesFragment();
406 if (fileListFragment
!= null
) {
407 fileListFragment
.listDirectory();
411 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
412 FileFragment secondFragment
= getSecondFragment();
413 boolean waitedPreview
= (mWaitingToPreview
!= null
&& mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
414 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
415 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
416 OCFile fileInFragment
= detailsFragment
.getFile();
417 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
418 // the user browsed to other file ; forget the automatic preview
419 mWaitingToPreview
= null
;
421 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
422 // grant that the right panel updates the progress bar
423 detailsFragment
.listenForTransferProgress();
424 detailsFragment
.updateFileDetails(true
, false
);
426 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
427 // update the right panel
428 boolean detailsFragmentChanged
= false
;
431 mWaitingToPreview
= getStorageManager().getFileById(mWaitingToPreview
.getFileId()); // update the file from database, for the local storage path
432 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
433 startMediaPreview(mWaitingToPreview
, 0, true
);
434 detailsFragmentChanged
= true
;
436 getFileOperationsHelper().openFile(mWaitingToPreview
, this);
439 mWaitingToPreview
= null
;
441 if (!detailsFragmentChanged
) {
442 detailsFragment
.updateFileDetails(false
, (success
));
449 public boolean onCreateOptionsMenu(Menu menu
) {
450 MenuInflater inflater
= getSherlock().getMenuInflater();
451 inflater
.inflate(R
.menu
.main_menu
, menu
);
456 public boolean onOptionsItemSelected(MenuItem item
) {
457 boolean retval
= true
;
458 switch (item
.getItemId()) {
459 case R
.id
.action_create_dir
: {
460 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.uploader_info_dirname
), "", -1, -1, this);
461 dialog
.show(getSupportFragmentManager(), "createdirdialog");
464 case R
.id
.action_sync_account
: {
465 startSynchronization();
468 case R
.id
.action_upload
: {
469 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
472 case R
.id
.action_settings
: {
473 Intent settingsIntent
= new Intent(this, Preferences
.class);
474 startActivity(settingsIntent
);
477 case android
.R
.id
.home
: {
478 FileFragment second
= getSecondFragment();
479 OCFile currentDir
= getCurrentDir();
480 if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
481 (second
!= null
&& second
.getFile() != null
)) {
488 retval
= super.onOptionsItemSelected(item
);
493 private void startSynchronization() {
494 Log_OC
.e(TAG
, "Got to start sync");
495 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
496 Log_OC
.e(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
497 ContentResolver
.cancelSync(null
, MainApp
.getAuthority()); // cancel the current synchronizations of any ownCloud account
498 Bundle bundle
= new Bundle();
499 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
500 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
501 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority());
502 ContentResolver
.requestSync(
504 MainApp
.getAuthority(), bundle
);
506 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority() + " with new API");
507 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
508 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
509 builder
.setExpedited(true
);
510 builder
.setManual(true
);
512 SyncRequest request
= builder
.build();
513 ContentResolver
.requestSync(request
);
519 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
520 if (itemPosition
!= 0) {
521 String targetPath
= "";
522 for (int i
=itemPosition
; i
< mDirectories
.getCount() - 1; i
++) {
523 targetPath
= mDirectories
.getItem(i
) + OCFile
.PATH_SEPARATOR
+ targetPath
;
525 targetPath
= OCFile
.PATH_SEPARATOR
+ targetPath
;
526 OCFile targetFolder
= getStorageManager().getFileByPath(targetPath
);
527 if (targetFolder
!= null
) {
528 browseTo(targetFolder
);
531 // the next operation triggers a new call to this method, but it's necessary to
532 // ensure that the name exposed in the action bar is the current directory when the
533 // user selected it in the navigation list
534 getSupportActionBar().setSelectedNavigationItem(0);
540 * Called, when the user selected something for uploading
542 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
543 super.onActivityResult(requestCode
, resultCode
, data
);
545 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
546 requestSimpleUpload(data
, resultCode
);
548 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
549 requestMultipleUpload(data
, resultCode
);
554 private void requestMultipleUpload(Intent data
, int resultCode
) {
555 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
556 if (filePaths
!= null
) {
557 String
[] remotePaths
= new String
[filePaths
.length
];
558 String remotePathBase
= "";
559 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
560 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
562 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
563 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
564 for (int j
= 0; j
< remotePaths
.length
; j
++) {
565 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
568 Intent i
= new Intent(this, FileUploader
.class);
569 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
570 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
571 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
572 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
573 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
574 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
578 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
579 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
586 private void requestSimpleUpload(Intent data
, int resultCode
) {
587 String filepath
= null
;
589 Uri selectedImageUri
= data
.getData();
591 String filemanagerstring
= selectedImageUri
.getPath();
592 String selectedImagePath
= getPath(selectedImageUri
);
594 if (selectedImagePath
!= null
)
595 filepath
= selectedImagePath
;
597 filepath
= filemanagerstring
;
599 } catch (Exception e
) {
600 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
604 if (filepath
== null
) {
605 Log_OC
.e(TAG
, "Couldnt resolve path to file");
606 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
612 Intent i
= new Intent(this, FileUploader
.class);
613 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
615 String remotepath
= new String();
616 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
617 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
619 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
620 remotepath
+= OCFile
.PATH_SEPARATOR
;
621 remotepath
+= new File(filepath
).getName();
623 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
624 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
625 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
626 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
627 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
632 public void onBackPressed() {
633 OCFileListFragment listOfFiles
= getListOfFilesFragment();
634 if (mDualPane
|| getSecondFragment() == null
) {
635 if (listOfFiles
!= null
) { // should never be null, indeed
636 if (mDirectories
.getCount() <= 1) {
640 int levelsUp
= listOfFiles
.onBrowseUp();
641 for (int i
=0; i
< levelsUp
&& mDirectories
.getCount() > 1 ; i
++) {
646 if (listOfFiles
!= null
) { // should never be null, indeed
647 setFile(listOfFiles
.getCurrentFile());
649 cleanSecondFragment();
654 protected void onSaveInstanceState(Bundle outState
) {
655 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
656 Log_OC
.e(TAG
, "onSaveInstanceState() start");
657 super.onSaveInstanceState(outState
);
658 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
659 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
660 outState
.putBoolean(FileDisplayActivity
.KEY_REFRESH_SHARES_IN_PROGRESS
, mRefreshSharesInProgress
);
662 Log_OC
.d(TAG
, "onSaveInstanceState() end");
668 protected void onResume() {
670 Log_OC
.e(TAG
, "onResume() start");
672 // Listen for sync messages
673 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
674 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
675 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FOLDER_SIZE_SYNCED
);
676 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FOLDER_CONTENTS_SYNCED
);
677 syncIntentFilter
.addAction(SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
);
678 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
679 //registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
680 LocalBroadcastManager
.getInstance(this).registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
682 // Listen for upload messages
683 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
684 mUploadFinishReceiver
= new UploadFinishReceiver();
685 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
687 // Listen for download messages
688 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.getDownloadAddedMessage());
689 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
690 mDownloadFinishReceiver
= new DownloadFinishReceiver();
691 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
693 // Listen for messages from the OperationsService
694 IntentFilter operationsIntentFilter
= new IntentFilter(OperationsService
.ACTION_OPERATION_ADDED
);
695 operationsIntentFilter
.addAction(OperationsService
.ACTION_OPERATION_FINISHED
);
696 mOperationsServiceReceiver
= new OperationsServiceReceiver();
697 LocalBroadcastManager
.getInstance(this).registerReceiver(mOperationsServiceReceiver
, operationsIntentFilter
);
699 Log_OC
.d(TAG
, "onResume() end");
704 protected void onPause() {
706 Log_OC
.e(TAG
, "onPause() start");
707 if (mSyncBroadcastReceiver
!= null
) {
708 //unregisterReceiver(mSyncBroadcastReceiver);
709 LocalBroadcastManager
.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver
);
710 mSyncBroadcastReceiver
= null
;
712 if (mUploadFinishReceiver
!= null
) {
713 unregisterReceiver(mUploadFinishReceiver
);
714 mUploadFinishReceiver
= null
;
716 if (mDownloadFinishReceiver
!= null
) {
717 unregisterReceiver(mDownloadFinishReceiver
);
718 mDownloadFinishReceiver
= null
;
720 if (mOperationsServiceReceiver
!= null
) {
721 LocalBroadcastManager
.getInstance(this).unregisterReceiver(mOperationsServiceReceiver
);
722 mOperationsServiceReceiver
= null
;
724 Log_OC
.d(TAG
, "onPause() end");
729 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
730 if (id
== DIALOG_SSL_VALIDATOR
&& mLastSslUntrustedServerResult
!= null
) {
731 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
737 protected Dialog
onCreateDialog(int id
) {
738 Dialog dialog
= null
;
739 AlertDialog
.Builder builder
;
741 case DIALOG_SHORT_WAIT
: {
742 ProgressDialog working_dialog
= new ProgressDialog(this);
743 working_dialog
.setMessage(getResources().getString(
744 R
.string
.wait_a_moment
));
745 working_dialog
.setIndeterminate(true
);
746 working_dialog
.setCancelable(false
);
747 dialog
= working_dialog
;
750 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
752 String
[] items
= null
;
754 String
[] allTheItems
= { getString(R
.string
.actionbar_upload_files
),
755 getString(R
.string
.actionbar_upload_from_apps
),
756 getString(R
.string
.actionbar_failed_instant_upload
) };
758 String
[] commonItems
= { getString(R
.string
.actionbar_upload_files
),
759 getString(R
.string
.actionbar_upload_from_apps
) };
761 if (InstantUploadActivity
.IS_ENABLED
)
766 builder
= new AlertDialog
.Builder(this);
767 builder
.setTitle(R
.string
.actionbar_upload
);
768 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
769 public void onClick(DialogInterface dialog
, int item
) {
772 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
773 action
.putExtra(UploadFilesActivity
.EXTRA_ACCOUNT
, FileDisplayActivity
.this.getAccount());
774 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
776 // TODO create and handle new fragment
777 // LocalFileListFragment
779 } else if (item
== 1) {
780 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
781 action
= action
.setType("*/*").addCategory(Intent
.CATEGORY_OPENABLE
);
782 startActivityForResult(Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
783 ACTION_SELECT_CONTENT_FROM_APPS
);
784 } else if (item
== 2 && InstantUploadActivity
.IS_ENABLED
) {
785 Intent action
= new Intent(FileDisplayActivity
.this, InstantUploadActivity
.class);
786 action
.putExtra(FileUploader
.KEY_ACCOUNT
, FileDisplayActivity
.this.getAccount());
787 startActivity(action
);
791 dialog
= builder
.create();
794 case DIALOG_SSL_VALIDATOR
: {
795 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
798 case DIALOG_CERT_NOT_SAVED
: {
799 builder
= new AlertDialog
.Builder(this);
800 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
801 builder
.setCancelable(false
);
802 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
804 public void onClick(DialogInterface dialog
, int which
) {
808 dialog
= builder
.create();
820 * Translates a content URI of an image to a physical path
822 * @param uri The URI to resolve
823 * @return The path to the image or null if it could not be found
825 public String
getPath(Uri uri
) {
826 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
827 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
828 if (cursor
!= null
) {
829 int column_index
= cursor
830 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
831 cursor
.moveToFirst();
832 return cursor
.getString(column_index
);
838 * Pushes a directory to the drop down list
839 * @param directory to push
840 * @throws IllegalArgumentException If the {@link OCFile#isFolder()} returns false.
842 public void pushDirname(OCFile directory
) {
843 if(!directory
.isFolder()){
844 throw new IllegalArgumentException("Only directories may be pushed!");
846 mDirectories
.insert(directory
.getFileName(), 0);
851 * Pops a directory name from the drop down list
852 * @return True, unless the stack is empty
854 public boolean popDirname() {
855 mDirectories
.remove(mDirectories
.getItem(0));
856 return !mDirectories
.isEmpty();
859 // Custom array adapter to override text colors
860 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
862 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
866 public View
getView(int position
, View convertView
, ViewGroup parent
) {
867 View v
= super.getView(position
, convertView
, parent
);
869 ((TextView
) v
).setTextColor(getResources().getColorStateList(
870 android
.R
.color
.white
));
874 public View
getDropDownView(int position
, View convertView
,
876 View v
= super.getDropDownView(position
, convertView
, parent
);
878 ((TextView
) v
).setTextColor(getResources().getColorStateList(
879 android
.R
.color
.white
));
886 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
889 * {@link BroadcastReceiver} to enable syncing feedback in UI
892 public void onReceive(Context context
, Intent intent
) {
893 String event
= intent
.getAction();
894 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
895 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
896 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
897 boolean sameAccount
= (getAccount() != null
&& accountName
.equals(getAccount().name
) && getStorageManager() != null
);
901 if (!FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
902 OCFile currentFile
= (getFile() == null
) ? null
: getStorageManager().getFileByPath(getFile().getRemotePath());
903 OCFile currentDir
= (getCurrentDir() == null
) ? null
: getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
905 if (currentDir
== null
) {
906 // current folder was removed from the server
907 Toast
.makeText( FileDisplayActivity
.this,
908 String
.format(getString(R
.string
.sync_current_folder_was_removed
), mDirectories
.getItem(0)),
914 if (currentFile
== null
&& !getFile().isFolder()) {
915 // currently selected file was removed in the server, and now we know it
916 cleanSecondFragment();
917 currentFile
= currentDir
;
920 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
921 OCFileListFragment fileListFragment
= getListOfFilesFragment();
922 if (fileListFragment
!= null
) {
923 fileListFragment
.listDirectory(currentDir
);
926 setFile(currentFile
);
929 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
930 !SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
.equals(event
) &&
931 (synchResult
== null
|| synchResult
.isSuccess())) ;
933 if (synchResult
!= null
&&
934 synchResult
.isSuccess() &&
935 (SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
.equals(event
) ||
936 FileSyncAdapter
.EVENT_FOLDER_CONTENTS_SYNCED
.equals(event
)
938 !mRefreshSharesInProgress
&&
939 getFileOperationsHelper().isSharedSupported(FileDisplayActivity
.this)
945 //removeStickyBroadcast(intent);
946 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
|| mRefreshSharesInProgress
);
949 if (synchResult
!= null
) {
950 if (synchResult
.getCode().equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
951 mLastSslUntrustedServerResult
= synchResult
;
952 showDialog(DIALOG_SSL_VALIDATOR
);
959 private class UploadFinishReceiver
extends BroadcastReceiver
{
961 * Once the file upload has finished -> update view
962 * @author David A. Velasco
963 * {@link BroadcastReceiver} to enable upload feedback in UI
966 public void onReceive(Context context
, Intent intent
) {
967 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
968 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
969 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
970 OCFile currentDir
= getCurrentDir();
971 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) && (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
972 if (sameAccount
&& isDescendant
) {
973 refeshListOfFilesFragment();
981 * Class waiting for broadcast events from the {@link FielDownloader} service.
983 * Updates the UI when a download is started or finished, provided that it is relevant for the
986 private class DownloadFinishReceiver
extends BroadcastReceiver
{
988 public void onReceive(Context context
, Intent intent
) {
989 boolean sameAccount
= isSameAccount(context
, intent
);
990 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
991 boolean isDescendant
= isDescendant(downloadedRemotePath
);
993 if (sameAccount
&& isDescendant
) {
994 refeshListOfFilesFragment();
995 refreshSecondFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
998 removeStickyBroadcast(intent
);
1001 private boolean isDescendant(String downloadedRemotePath
) {
1002 OCFile currentDir
= getCurrentDir();
1003 return (currentDir
!= null
&& downloadedRemotePath
!= null
&& downloadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1006 private boolean isSameAccount(Context context
, Intent intent
) {
1007 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1008 return (accountName
!= null
&& getAccount() != null
&& accountName
.equals(getAccount().name
));
1014 * Class waiting for broadcast events from the {@link OperationsService}.
1016 * Updates the list of files when a get for shares is finished; at this moment the refresh of shares is the only
1017 * operation performed in {@link OperationsService}.
1019 * In the future will handle the progress or finalization of all the operations performed in {@link OperationsService},
1020 * probably all the operations associated to the app model.
1022 private class OperationsServiceReceiver
extends BroadcastReceiver
{
1025 public void onReceive(Context context
, Intent intent
) {
1026 if (OperationsService
.ACTION_OPERATION_ADDED
.equals(intent
.getAction())) {
1028 } else if (OperationsService
.ACTION_OPERATION_FINISHED
.equals(intent
.getAction())) {
1029 mRefreshSharesInProgress
= false
;
1031 Account account
= intent
.getParcelableExtra(OperationsService
.EXTRA_ACCOUNT
);
1032 RemoteOperationResult getSharesResult
= (RemoteOperationResult
)intent
.getSerializableExtra(OperationsService
.EXTRA_RESULT
);
1033 if (getAccount() != null
&& account
.name
.equals(getAccount().name
)
1034 && getStorageManager() != null
1036 refeshListOfFilesFragment();
1038 if ((getSharesResult
!= null
) &&
1039 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
.equals(getSharesResult
.getCode())) {
1040 mLastSslUntrustedServerResult
= getSharesResult
;
1041 showDialog(DIALOG_SSL_VALIDATOR
);
1044 setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress
|| mSyncInProgress
);
1051 public void browseToRoot() {
1052 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1053 if (listOfFiles
!= null
) { // should never be null, indeed
1054 while (mDirectories
.getCount() > 1) {
1057 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
1058 listOfFiles
.listDirectory(root
);
1059 setFile(listOfFiles
.getCurrentFile());
1060 startSyncFolderOperation(root
);
1062 cleanSecondFragment();
1066 public void browseTo(OCFile folder
) {
1067 if (folder
== null
|| !folder
.isFolder()) {
1068 throw new IllegalArgumentException("Trying to browse to invalid folder " + folder
);
1070 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1071 if (listOfFiles
!= null
) {
1072 setNavigationListWithFolder(folder
);
1073 listOfFiles
.listDirectory(folder
);
1074 setFile(listOfFiles
.getCurrentFile());
1075 startSyncFolderOperation(folder
);
1077 Log_OC
.e(TAG
, "Unexpected null when accessing list fragment");
1079 cleanSecondFragment();
1086 * Updates action bar and second fragment, if in dual pane mode.
1089 public void onBrowsedDownTo(OCFile directory
) {
1090 pushDirname(directory
);
1091 cleanSecondFragment();
1094 startSyncFolderOperation(directory
);
1099 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1101 * @param file Image {@link OCFile} to show.
1104 public void startImagePreview(OCFile file
) {
1105 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1106 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1107 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1108 startActivity(showDetailsIntent
);
1112 * Stars the preview of an already down media {@link OCFile}.
1114 * @param file Media {@link OCFile} to preview.
1115 * @param startPlaybackPosition Media position where the playback will be started, in milliseconds.
1116 * @param autoplay When 'true', the playback will start without user interactions.
1119 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1120 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
1121 setSecondFragment(mediaFragment
);
1122 updateFragmentsVisibility(true
);
1123 updateNavigationElementsInActionBar(file
);
1128 * Requests the download of the received {@link OCFile} , updates the UI
1129 * to monitor the download progress and prepares the activity to preview
1130 * or open the file when the download finishes.
1132 * @param file {@link OCFile} to download and preview.
1135 public void startDownloadForPreview(OCFile file
) {
1136 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1137 setSecondFragment(detailFragment
);
1138 mWaitingToPreview
= file
;
1139 requestForDownload();
1140 updateFragmentsVisibility(true
);
1141 updateNavigationElementsInActionBar(file
);
1147 * Shows the information of the {@link OCFile} received as a
1148 * parameter in the second fragment.
1150 * @param file {@link OCFile} whose details will be shown
1153 public void showDetails(OCFile file
) {
1154 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1155 setSecondFragment(detailFragment
);
1156 updateFragmentsVisibility(true
);
1157 updateNavigationElementsInActionBar(file
);
1165 private void updateNavigationElementsInActionBar(OCFile chosenFile
) {
1166 ActionBar actionBar
= getSupportActionBar();
1167 if (chosenFile
== null
|| mDualPane
) {
1168 // only list of files - set for browsing through folders
1169 OCFile currentDir
= getCurrentDir();
1170 actionBar
.setDisplayHomeAsUpEnabled(currentDir
!= null
&& currentDir
.getParentId() != 0);
1171 actionBar
.setDisplayShowTitleEnabled(false
);
1172 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
1173 actionBar
.setListNavigationCallbacks(mDirectories
, this); // assuming mDirectories is updated
1176 actionBar
.setDisplayHomeAsUpEnabled(true
);
1177 actionBar
.setDisplayShowTitleEnabled(true
);
1178 actionBar
.setTitle(chosenFile
.getFileName());
1179 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
1188 public void onFileStateChanged() {
1189 refeshListOfFilesFragment();
1190 updateNavigationElementsInActionBar(getSecondFragment().getFile());
1198 public FileDownloaderBinder
getFileDownloaderBinder() {
1199 return mDownloaderBinder
;
1207 public FileUploaderBinder
getFileUploaderBinder() {
1208 return mUploaderBinder
;
1212 /** Defines callbacks for service binding, passed to bindService() */
1213 private class ListServiceConnection
implements ServiceConnection
{
1216 public void onServiceConnected(ComponentName component
, IBinder service
) {
1217 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1218 Log_OC
.d(TAG
, "Download service connected");
1219 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1220 if (mWaitingToPreview
!= null
) {
1221 requestForDownload();
1224 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1225 Log_OC
.d(TAG
, "Upload service connected");
1226 mUploaderBinder
= (FileUploaderBinder
) service
;
1230 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1231 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1232 if (listOfFiles
!= null
) {
1233 listOfFiles
.listDirectory();
1235 FileFragment secondFragment
= getSecondFragment();
1236 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1237 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1238 detailFragment
.listenForTransferProgress();
1239 detailFragment
.updateFileDetails(false
, false
);
1244 public void onServiceDisconnected(ComponentName component
) {
1245 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1246 Log_OC
.d(TAG
, "Download service disconnected");
1247 mDownloaderBinder
= null
;
1248 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1249 Log_OC
.d(TAG
, "Upload service disconnected");
1250 mUploaderBinder
= null
;
1258 * Launch an intent to request the PIN code to the user before letting him use the app
1260 private void requestPinCode() {
1261 boolean pinStart
= false
;
1262 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
1263 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1265 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1266 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");
1273 public void onSavedCertificate() {
1274 startSyncFolderOperation(getCurrentDir());
1279 public void onFailedSavingCertificate() {
1280 showDialog(DIALOG_CERT_NOT_SAVED
);
1285 * Updates the view associated to the activity after the finish of some operation over files
1286 * in the current account.
1288 * @param operation Removal operation performed.
1289 * @param result Result of the removal.
1292 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1293 super.onRemoteOperationFinish(operation
, result
);
1295 if (operation
instanceof RemoveFileOperation
) {
1296 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1298 } else if (operation
instanceof RenameFileOperation
) {
1299 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1301 } else if (operation
instanceof SynchronizeFileOperation
) {
1302 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1304 } else if (operation
instanceof CreateFolderOperation
) {
1305 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1307 } else if (operation
instanceof CreateShareOperation
) {
1308 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1314 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
1315 if (result
.isSuccess()) {
1316 refeshListOfFilesFragment();
1322 * Updates the view associated to the activity after the finish of an operation trying to remove a
1325 * @param operation Removal operation performed.
1326 * @param result Result of the removal.
1328 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
1329 dismissLoadingDialog();
1330 if (result
.isSuccess()) {
1331 Toast msg
= Toast
.makeText(this, R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
1333 OCFile removedFile
= operation
.getFile();
1334 getSecondFragment();
1335 FileFragment second
= getSecondFragment();
1336 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1337 cleanSecondFragment();
1339 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1340 refeshListOfFilesFragment();
1344 Toast msg
= Toast
.makeText(this, R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
1346 if (result
.isSslRecoverableException()) {
1347 mLastSslUntrustedServerResult
= result
;
1348 showDialog(DIALOG_SSL_VALIDATOR
);
1354 * Updates the view associated to the activity after the finish of an operation trying create a new folder
1356 * @param operation Creation operation performed.
1357 * @param result Result of the creation.
1359 private void onCreateFolderOperationFinish(CreateFolderOperation operation
, RemoteOperationResult result
) {
1360 if (result
.isSuccess()) {
1361 dismissLoadingDialog();
1362 refeshListOfFilesFragment();
1365 dismissLoadingDialog();
1366 if (result
.getCode() == ResultCode
.INVALID_CHARACTER_IN_NAME
) {
1367 Toast
.makeText(FileDisplayActivity
.this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
).show();
1370 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
1373 } catch (NotFoundException e
) {
1374 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1382 * Updates the view associated to the activity after the finish of an operation trying to rename a
1385 * @param operation Renaming operation performed.
1386 * @param result Result of the renaming.
1388 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1389 dismissLoadingDialog();
1390 OCFile renamedFile
= operation
.getFile();
1391 if (result
.isSuccess()) {
1393 FileFragment details
= getSecondFragment();
1394 if (details
!= null
&& details
instanceof FileDetailFragment
&& renamedFile
.equals(details
.getFile()) ) {
1395 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1398 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())) {
1399 refeshListOfFilesFragment();
1403 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1404 Toast msg
= Toast
.makeText(this, R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1406 // TODO throw again the new rename dialog
1407 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
1408 Toast msg
= Toast
.makeText(this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
1411 Toast msg
= Toast
.makeText(this, R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1413 if (result
.isSslRecoverableException()) {
1414 mLastSslUntrustedServerResult
= result
;
1415 showDialog(DIALOG_SSL_VALIDATOR
);
1422 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1423 dismissLoadingDialog();
1424 OCFile syncedFile
= operation
.getLocalFile();
1425 if (!result
.isSuccess()) {
1426 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1427 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1428 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1429 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
1435 if (operation
.transferWasRequested()) {
1436 refeshListOfFilesFragment();
1437 onTransferStateChanged(syncedFile
, true
, true
);
1440 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1451 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1453 FileFragment details
= getSecondFragment();
1454 if (details
!= null
&& details
instanceof FileDetailFragment
&& file
.equals(details
.getFile()) ) {
1455 if (downloading
|| uploading
) {
1456 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1458 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1465 public void onDismiss(EditNameDialog dialog
) {
1466 if (dialog
.getResult()) {
1467 String newDirectoryName
= dialog
.getNewFilename().trim();
1468 Log_OC
.d(TAG
, "'create directory' dialog dismissed with new name " + newDirectoryName
);
1469 if (newDirectoryName
.length() > 0) {
1470 String path
= getCurrentDir().getRemotePath();
1473 path
+= newDirectoryName
+ OCFile
.PATH_SEPARATOR
;
1474 RemoteOperation operation
= new CreateFolderOperation(path
, false
, getStorageManager());
1475 operation
.execute( getAccount(),
1476 FileDisplayActivity
.this,
1477 FileDisplayActivity
.this,
1479 FileDisplayActivity
.this);
1481 showLoadingDialog();
1487 private void requestForDownload() {
1488 Account account
= getAccount();
1489 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1490 Intent i
= new Intent(this, FileDownloader
.class);
1491 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1492 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1498 private OCFile
getCurrentDir() {
1499 OCFile file
= getFile();
1501 if (file
.isFolder()) {
1503 } else if (getStorageManager() != null
) {
1504 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
1505 return getStorageManager().getFileByPath(parentPath
);
1511 public void startSyncFolderOperation(OCFile folder
) {
1512 long currentSyncTime
= System
.currentTimeMillis();
1514 mSyncInProgress
= true
;
1516 // perform folder synchronization
1517 RemoteOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
1520 getStorageManager(),
1522 getApplicationContext()
1524 synchFolderOp
.execute(getAccount(), this, null
, null
, this);
1526 setSupportProgressBarIndeterminateVisibility(true
);
1530 private void startGetShares() {
1531 // Get shared files/folders
1532 Intent intent
= new Intent(this, OperationsService
.class);
1533 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, getAccount());
1534 startService(intent
);
1536 mRefreshSharesInProgress
= true
;