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
.operations
.UnshareLinkOperation
;
79 import com
.owncloud
.android
.services
.OperationsService
;
80 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
81 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
82 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
83 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
;
84 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
.OnSslValidatorListener
;
85 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
86 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
87 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
88 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
89 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
90 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
91 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
92 import com
.owncloud
.android
.utils
.DisplayUtils
;
93 import com
.owncloud
.android
.utils
.Log_OC
;
97 * Displays, what files the user has available in his ownCloud.
99 * @author Bartek Przybylski
100 * @author David A. Velasco
103 public class FileDisplayActivity
extends HookActivity
implements
104 OCFileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
, OnSslValidatorListener
, EditNameDialogListener
{
106 private ArrayAdapter
<String
> mDirectories
;
108 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
109 private UploadFinishReceiver mUploadFinishReceiver
;
110 private DownloadFinishReceiver mDownloadFinishReceiver
;
111 private OperationsServiceReceiver mOperationsServiceReceiver
;
112 private FileDownloaderBinder mDownloaderBinder
= null
;
113 private FileUploaderBinder mUploaderBinder
= null
;
114 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
115 private RemoteOperationResult mLastSslUntrustedServerResult
= null
;
117 private boolean mDualPane
;
118 private View mLeftFragmentContainer
;
119 private View mRightFragmentContainer
;
121 private static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
122 private static final String KEY_SYNC_IN_PROGRESS
= "SYNC_IN_PROGRESS";
123 private static final String KEY_REFRESH_SHARES_IN_PROGRESS
= "SHARES_IN_PROGRESS";
125 public static final int DIALOG_SHORT_WAIT
= 0;
126 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 1;
127 private static final int DIALOG_SSL_VALIDATOR
= 2;
128 private static final int DIALOG_CERT_NOT_SAVED
= 3;
130 public static final String ACTION_DETAILS
= "com.owncloud.android.ui.activity.action.DETAILS";
132 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
133 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
135 private static final String TAG
= FileDisplayActivity
.class.getSimpleName();
137 private static final String TAG_LIST_OF_FILES
= "LIST_OF_FILES";
138 private static final String TAG_SECOND_FRAGMENT
= "SECOND_FRAGMENT";
140 private OCFile mWaitingToPreview
;
142 private boolean mSyncInProgress
= false
;
143 private boolean mRefreshSharesInProgress
= false
;
146 protected void onCreate(Bundle savedInstanceState
) {
147 Log_OC
.d(TAG
, "onCreate() start");
148 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
150 super.onCreate(savedInstanceState
); // this calls onAccountChanged() when ownCloud Account is valid
152 /// bindings to transference services
153 mUploadConnection
= new ListServiceConnection();
154 mDownloadConnection
= new ListServiceConnection();
155 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
156 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
158 // PIN CODE request ; best location is to decide, let's try this first
159 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
161 } else if (getIntent().getAction() == null
&& savedInstanceState
== null
) {
166 Intent observer_intent
= new Intent(this, FileObserverService
.class);
167 observer_intent
.putExtra(FileObserverService
.KEY_FILE_CMD
, FileObserverService
.CMD_INIT_OBSERVED_LIST
);
168 startService(observer_intent
);
170 /// Load of saved instance state
171 if(savedInstanceState
!= null
) {
172 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
);
173 mSyncInProgress
= savedInstanceState
.getBoolean(KEY_SYNC_IN_PROGRESS
);
174 mRefreshSharesInProgress
= savedInstanceState
.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS
);
177 mWaitingToPreview
= null
;
178 mSyncInProgress
= false
;
179 mRefreshSharesInProgress
= false
;
184 // Inflate and set the layout view
185 setContentView(R
.layout
.files
);
186 mDualPane
= getResources().getBoolean(R
.bool
.large_land_layout
);
187 mLeftFragmentContainer
= findViewById(R
.id
.left_fragment_container
);
188 mRightFragmentContainer
= findViewById(R
.id
.right_fragment_container
);
189 if (savedInstanceState
== null
) {
190 createMinFragments();
194 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
195 getSupportActionBar().setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
196 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
|| mRefreshSharesInProgress
); // always AFTER setContentView(...) ; to work around bug in its implementation
198 Log_OC
.d(TAG
, "onCreate() end");
202 protected void onStart() {
204 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
208 protected void onDestroy() {
210 if (mDownloadConnection
!= null
)
211 unbindService(mDownloadConnection
);
212 if (mUploadConnection
!= null
)
213 unbindService(mUploadConnection
);
218 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
221 protected void onAccountSet(boolean stateWasRecovered
) {
222 super.onAccountSet(stateWasRecovered
);
223 if (getAccount() != null
) {
224 /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
225 OCFile file
= getFile();
226 // get parent from path
227 String parentPath
= "";
229 if (file
.isDown() && file
.getLastSyncDateForProperties() == 0) {
230 // upload in progress - right now, files are not inserted in the local cache until the upload is successful
231 // get parent from path
232 parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
233 if (getStorageManager().getFileByPath(parentPath
) == null
)
234 file
= null
; // not able to know the directory where the file is uploading
236 file
= getStorageManager().getFileByPath(file
.getRemotePath()); // currentDir = null if not in the current Account
240 // fall back to root folder
241 file
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
); // never returns null
244 setNavigationListWithFolder(file
);
246 if (!stateWasRecovered
) {
247 Log_OC
.e(TAG
, "Initializing Fragments in onAccountChanged..");
248 initFragmentsWithFile();
249 if (file
.isFolder()) {
250 startSyncFolderOperation(file
);
254 updateFragmentsVisibility(!file
.isFolder());
255 updateNavigationElementsInActionBar(file
.isFolder() ? null
: file
);
261 private void setNavigationListWithFolder(OCFile file
) {
262 mDirectories
.clear();
263 OCFile fileIt
= file
;
265 while(fileIt
!= null
&& fileIt
.getFileName() != OCFile
.ROOT_PATH
) {
266 if (fileIt
.isFolder()) {
267 mDirectories
.add(fileIt
.getFileName());
269 // get parent from path
270 parentPath
= fileIt
.getRemotePath().substring(0, fileIt
.getRemotePath().lastIndexOf(fileIt
.getFileName()));
271 fileIt
= getStorageManager().getFileByPath(parentPath
);
273 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
277 private void createMinFragments() {
278 OCFileListFragment listOfFiles
= new OCFileListFragment();
279 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
280 transaction
.add(R
.id
.left_fragment_container
, listOfFiles
, TAG_LIST_OF_FILES
);
281 transaction
.commit();
284 private void initFragmentsWithFile() {
285 if (getAccount() != null
&& getFile() != null
) {
287 OCFileListFragment listOfFiles
= getListOfFilesFragment();
288 if (listOfFiles
!= null
) {
289 listOfFiles
.listDirectory(getCurrentDir());
291 Log
.e(TAG
, "Still have a chance to lose the initializacion of list fragment >(");
295 OCFile file
= getFile();
296 Fragment secondFragment
= chooseInitialSecondFragment(file
);
297 if (secondFragment
!= null
) {
298 setSecondFragment(secondFragment
);
299 updateFragmentsVisibility(true
);
300 updateNavigationElementsInActionBar(file
);
303 cleanSecondFragment();
307 Log
.wtf(TAG
, "initFragments() called with invalid NULLs!");
308 if (getAccount() == null
) {
309 Log
.wtf(TAG
, "\t account is NULL");
311 if (getFile() == null
) {
312 Log
.wtf(TAG
, "\t file is NULL");
317 private Fragment
chooseInitialSecondFragment(OCFile file
) {
318 Fragment secondFragment
= null
;
319 if (file
!= null
&& !file
.isFolder()) {
320 if (file
.isDown() && PreviewMediaFragment
.canBePreviewed(file
)
321 && file
.getLastSyncDateForProperties() > 0 // temporal fix
323 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
324 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
325 secondFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
328 secondFragment
= new FileDetailFragment(file
, getAccount());
331 return secondFragment
;
336 * Replaces the second fragment managed by the activity with the received as
339 * Assumes never will be more than two fragments managed at the same time.
341 * @param fragment New second Fragment to set.
343 private void setSecondFragment(Fragment fragment
) {
344 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
345 transaction
.replace(R
.id
.right_fragment_container
, fragment
, TAG_SECOND_FRAGMENT
);
346 transaction
.commit();
350 private void updateFragmentsVisibility(boolean existsSecondFragment
) {
352 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
353 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
355 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
356 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
359 } else if (existsSecondFragment
) {
360 if (mLeftFragmentContainer
.getVisibility() != View
.GONE
) {
361 mLeftFragmentContainer
.setVisibility(View
.GONE
);
363 if (mRightFragmentContainer
.getVisibility() != View
.VISIBLE
) {
364 mRightFragmentContainer
.setVisibility(View
.VISIBLE
);
368 if (mLeftFragmentContainer
.getVisibility() != View
.VISIBLE
) {
369 mLeftFragmentContainer
.setVisibility(View
.VISIBLE
);
371 if (mRightFragmentContainer
.getVisibility() != View
.GONE
) {
372 mRightFragmentContainer
.setVisibility(View
.GONE
);
378 private OCFileListFragment
getListOfFilesFragment() {
379 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_LIST_OF_FILES
);
380 if (listOfFiles
!= null
) {
381 return (OCFileListFragment
)listOfFiles
;
383 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
387 protected FileFragment
getSecondFragment() {
388 Fragment second
= getSupportFragmentManager().findFragmentByTag(FileDisplayActivity
.TAG_SECOND_FRAGMENT
);
389 if (second
!= null
) {
390 return (FileFragment
)second
;
395 public void cleanSecondFragment() {
396 Fragment second
= getSecondFragment();
397 if (second
!= null
) {
398 FragmentTransaction tr
= getSupportFragmentManager().beginTransaction();
402 updateFragmentsVisibility(false
);
403 updateNavigationElementsInActionBar(null
);
406 protected void refeshListOfFilesFragment() {
407 OCFileListFragment fileListFragment
= getListOfFilesFragment();
408 if (fileListFragment
!= null
) {
409 fileListFragment
.listDirectory();
413 protected void refreshSecondFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
414 FileFragment secondFragment
= getSecondFragment();
415 boolean waitedPreview
= (mWaitingToPreview
!= null
&& mWaitingToPreview
.getRemotePath().equals(downloadedRemotePath
));
416 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
417 FileDetailFragment detailsFragment
= (FileDetailFragment
) secondFragment
;
418 OCFile fileInFragment
= detailsFragment
.getFile();
419 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
420 // the user browsed to other file ; forget the automatic preview
421 mWaitingToPreview
= null
;
423 } else if (downloadEvent
.equals(FileDownloader
.getDownloadAddedMessage())) {
424 // grant that the right panel updates the progress bar
425 detailsFragment
.listenForTransferProgress();
426 detailsFragment
.updateFileDetails(true
, false
);
428 } else if (downloadEvent
.equals(FileDownloader
.getDownloadFinishMessage())) {
429 // update the right panel
430 boolean detailsFragmentChanged
= false
;
433 mWaitingToPreview
= getStorageManager().getFileById(mWaitingToPreview
.getFileId()); // update the file from database, for the local storage path
434 if (PreviewMediaFragment
.canBePreviewed(mWaitingToPreview
)) {
435 startMediaPreview(mWaitingToPreview
, 0, true
);
436 detailsFragmentChanged
= true
;
438 getFileOperationsHelper().openFile(mWaitingToPreview
, this);
441 mWaitingToPreview
= null
;
443 if (!detailsFragmentChanged
) {
444 detailsFragment
.updateFileDetails(false
, (success
));
451 public boolean onCreateOptionsMenu(Menu menu
) {
452 MenuInflater inflater
= getSherlock().getMenuInflater();
453 inflater
.inflate(R
.menu
.main_menu
, menu
);
458 public boolean onOptionsItemSelected(MenuItem item
) {
459 boolean retval
= true
;
460 switch (item
.getItemId()) {
461 case R
.id
.action_create_dir
: {
462 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.uploader_info_dirname
), "", -1, -1, this);
463 dialog
.show(getSupportFragmentManager(), "createdirdialog");
466 case R
.id
.action_sync_account
: {
467 startSynchronization();
470 case R
.id
.action_upload
: {
471 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
474 case R
.id
.action_settings
: {
475 Intent settingsIntent
= new Intent(this, Preferences
.class);
476 startActivity(settingsIntent
);
479 case android
.R
.id
.home
: {
480 FileFragment second
= getSecondFragment();
481 OCFile currentDir
= getCurrentDir();
482 if((currentDir
!= null
&& currentDir
.getParentId() != 0) ||
483 (second
!= null
&& second
.getFile() != null
)) {
490 retval
= super.onOptionsItemSelected(item
);
495 private void startSynchronization() {
496 Log_OC
.e(TAG
, "Got to start sync");
497 if (android
.os
.Build
.VERSION
.SDK_INT
< android
.os
.Build
.VERSION_CODES
.KITKAT
) {
498 Log_OC
.e(TAG
, "Canceling all syncs for " + MainApp
.getAuthority());
499 ContentResolver
.cancelSync(null
, MainApp
.getAuthority()); // cancel the current synchronizations of any ownCloud account
500 Bundle bundle
= new Bundle();
501 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
502 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_EXPEDITED
, true
);
503 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority());
504 ContentResolver
.requestSync(
506 MainApp
.getAuthority(), bundle
);
508 Log_OC
.e(TAG
, "Requesting sync for " + getAccount().name
+ " at " + MainApp
.getAuthority() + " with new API");
509 SyncRequest
.Builder builder
= new SyncRequest
.Builder();
510 builder
.setSyncAdapter(getAccount(), MainApp
.getAuthority());
511 builder
.setExpedited(true
);
512 builder
.setManual(true
);
514 SyncRequest request
= builder
.build();
515 ContentResolver
.requestSync(request
);
521 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
522 if (itemPosition
!= 0) {
523 String targetPath
= "";
524 for (int i
=itemPosition
; i
< mDirectories
.getCount() - 1; i
++) {
525 targetPath
= mDirectories
.getItem(i
) + OCFile
.PATH_SEPARATOR
+ targetPath
;
527 targetPath
= OCFile
.PATH_SEPARATOR
+ targetPath
;
528 OCFile targetFolder
= getStorageManager().getFileByPath(targetPath
);
529 if (targetFolder
!= null
) {
530 browseTo(targetFolder
);
533 // the next operation triggers a new call to this method, but it's necessary to
534 // ensure that the name exposed in the action bar is the current directory when the
535 // user selected it in the navigation list
536 if (getSupportActionBar().getNavigationMode() == ActionBar
.NAVIGATION_MODE_LIST
&& itemPosition
!= 0)
537 getSupportActionBar().setSelectedNavigationItem(0);
543 * Called, when the user selected something for uploading
545 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
546 super.onActivityResult(requestCode
, resultCode
, data
);
548 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
549 requestSimpleUpload(data
, resultCode
);
551 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& (resultCode
== RESULT_OK
|| resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)) {
552 requestMultipleUpload(data
, resultCode
);
557 private void requestMultipleUpload(Intent data
, int resultCode
) {
558 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
559 if (filePaths
!= null
) {
560 String
[] remotePaths
= new String
[filePaths
.length
];
561 String remotePathBase
= "";
562 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
563 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
565 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
566 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
567 for (int j
= 0; j
< remotePaths
.length
; j
++) {
568 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
571 Intent i
= new Intent(this, FileUploader
.class);
572 i
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
573 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
574 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
575 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
576 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
577 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
581 Log_OC
.d(TAG
, "User clicked on 'Update' with no selection");
582 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
589 private void requestSimpleUpload(Intent data
, int resultCode
) {
590 String filepath
= null
;
592 Uri selectedImageUri
= data
.getData();
594 String filemanagerstring
= selectedImageUri
.getPath();
595 String selectedImagePath
= getPath(selectedImageUri
);
597 if (selectedImagePath
!= null
)
598 filepath
= selectedImagePath
;
600 filepath
= filemanagerstring
;
602 } catch (Exception e
) {
603 Log_OC
.e(TAG
, "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
607 if (filepath
== null
) {
608 Log_OC
.e(TAG
, "Couldnt resolve path to file");
609 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
615 Intent i
= new Intent(this, FileUploader
.class);
616 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
618 String remotepath
= new String();
619 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
620 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
622 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
623 remotepath
+= OCFile
.PATH_SEPARATOR
;
624 remotepath
+= new File(filepath
).getName();
626 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
627 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
628 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
629 if (resultCode
== UploadFilesActivity
.RESULT_OK_AND_MOVE
)
630 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
635 public void onBackPressed() {
636 OCFileListFragment listOfFiles
= getListOfFilesFragment();
637 if (mDualPane
|| getSecondFragment() == null
) {
638 if (listOfFiles
!= null
) { // should never be null, indeed
639 if (mDirectories
.getCount() <= 1) {
643 int levelsUp
= listOfFiles
.onBrowseUp();
644 for (int i
=0; i
< levelsUp
&& mDirectories
.getCount() > 1 ; i
++) {
649 if (listOfFiles
!= null
) { // should never be null, indeed
650 setFile(listOfFiles
.getCurrentFile());
652 cleanSecondFragment();
657 protected void onSaveInstanceState(Bundle outState
) {
658 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
659 Log_OC
.e(TAG
, "onSaveInstanceState() start");
660 super.onSaveInstanceState(outState
);
661 outState
.putParcelable(FileDisplayActivity
.KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
662 outState
.putBoolean(FileDisplayActivity
.KEY_SYNC_IN_PROGRESS
, mSyncInProgress
);
663 outState
.putBoolean(FileDisplayActivity
.KEY_REFRESH_SHARES_IN_PROGRESS
, mRefreshSharesInProgress
);
665 Log_OC
.d(TAG
, "onSaveInstanceState() end");
671 protected void onResume() {
673 Log_OC
.e(TAG
, "onResume() start");
675 // Listen for sync messages
676 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
677 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
678 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FOLDER_SIZE_SYNCED
);
679 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FOLDER_CONTENTS_SYNCED
);
680 syncIntentFilter
.addAction(SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
);
681 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
682 //registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
683 LocalBroadcastManager
.getInstance(this).registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
685 // Listen for upload messages
686 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
687 mUploadFinishReceiver
= new UploadFinishReceiver();
688 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
690 // Listen for download messages
691 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.getDownloadAddedMessage());
692 downloadIntentFilter
.addAction(FileDownloader
.getDownloadFinishMessage());
693 mDownloadFinishReceiver
= new DownloadFinishReceiver();
694 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
696 // Listen for messages from the OperationsService
697 IntentFilter operationsIntentFilter
= new IntentFilter(OperationsService
.ACTION_OPERATION_ADDED
);
698 operationsIntentFilter
.addAction(OperationsService
.ACTION_OPERATION_FINISHED
);
699 mOperationsServiceReceiver
= new OperationsServiceReceiver();
700 LocalBroadcastManager
.getInstance(this).registerReceiver(mOperationsServiceReceiver
, operationsIntentFilter
);
702 Log_OC
.d(TAG
, "onResume() end");
707 protected void onPause() {
709 Log_OC
.e(TAG
, "onPause() start");
710 if (mSyncBroadcastReceiver
!= null
) {
711 //unregisterReceiver(mSyncBroadcastReceiver);
712 LocalBroadcastManager
.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver
);
713 mSyncBroadcastReceiver
= null
;
715 if (mUploadFinishReceiver
!= null
) {
716 unregisterReceiver(mUploadFinishReceiver
);
717 mUploadFinishReceiver
= null
;
719 if (mDownloadFinishReceiver
!= null
) {
720 unregisterReceiver(mDownloadFinishReceiver
);
721 mDownloadFinishReceiver
= null
;
723 if (mOperationsServiceReceiver
!= null
) {
724 LocalBroadcastManager
.getInstance(this).unregisterReceiver(mOperationsServiceReceiver
);
725 mOperationsServiceReceiver
= null
;
727 Log_OC
.d(TAG
, "onPause() end");
732 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
733 if (id
== DIALOG_SSL_VALIDATOR
&& mLastSslUntrustedServerResult
!= null
) {
734 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
740 protected Dialog
onCreateDialog(int id
) {
741 Dialog dialog
= null
;
742 AlertDialog
.Builder builder
;
744 case DIALOG_SHORT_WAIT
: {
745 ProgressDialog working_dialog
= new ProgressDialog(this);
746 working_dialog
.setMessage(getResources().getString(
747 R
.string
.wait_a_moment
));
748 working_dialog
.setIndeterminate(true
);
749 working_dialog
.setCancelable(false
);
750 dialog
= working_dialog
;
753 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
755 String
[] items
= null
;
757 String
[] allTheItems
= { getString(R
.string
.actionbar_upload_files
),
758 getString(R
.string
.actionbar_upload_from_apps
),
759 getString(R
.string
.actionbar_failed_instant_upload
) };
761 String
[] commonItems
= { getString(R
.string
.actionbar_upload_files
),
762 getString(R
.string
.actionbar_upload_from_apps
) };
764 if (InstantUploadActivity
.IS_ENABLED
)
769 builder
= new AlertDialog
.Builder(this);
770 builder
.setTitle(R
.string
.actionbar_upload
);
771 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
772 public void onClick(DialogInterface dialog
, int item
) {
775 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
776 action
.putExtra(UploadFilesActivity
.EXTRA_ACCOUNT
, FileDisplayActivity
.this.getAccount());
777 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
779 // TODO create and handle new fragment
780 // LocalFileListFragment
782 } else if (item
== 1) {
783 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
784 action
= action
.setType("*/*").addCategory(Intent
.CATEGORY_OPENABLE
);
785 startActivityForResult(Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
786 ACTION_SELECT_CONTENT_FROM_APPS
);
787 } else if (item
== 2 && InstantUploadActivity
.IS_ENABLED
) {
788 Intent action
= new Intent(FileDisplayActivity
.this, InstantUploadActivity
.class);
789 action
.putExtra(FileUploader
.KEY_ACCOUNT
, FileDisplayActivity
.this.getAccount());
790 startActivity(action
);
794 dialog
= builder
.create();
797 case DIALOG_SSL_VALIDATOR
: {
798 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
801 case DIALOG_CERT_NOT_SAVED
: {
802 builder
= new AlertDialog
.Builder(this);
803 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
804 builder
.setCancelable(false
);
805 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
807 public void onClick(DialogInterface dialog
, int which
) {
811 dialog
= builder
.create();
823 * Translates a content URI of an image to a physical path
825 * @param uri The URI to resolve
826 * @return The path to the image or null if it could not be found
828 public String
getPath(Uri uri
) {
829 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
830 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
831 if (cursor
!= null
) {
832 int column_index
= cursor
833 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
834 cursor
.moveToFirst();
835 return cursor
.getString(column_index
);
841 * Pushes a directory to the drop down list
842 * @param directory to push
843 * @throws IllegalArgumentException If the {@link OCFile#isFolder()} returns false.
845 public void pushDirname(OCFile directory
) {
846 if(!directory
.isFolder()){
847 throw new IllegalArgumentException("Only directories may be pushed!");
849 mDirectories
.insert(directory
.getFileName(), 0);
854 * Pops a directory name from the drop down list
855 * @return True, unless the stack is empty
857 public boolean popDirname() {
858 mDirectories
.remove(mDirectories
.getItem(0));
859 return !mDirectories
.isEmpty();
862 // Custom array adapter to override text colors
863 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
865 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
869 public View
getView(int position
, View convertView
, ViewGroup parent
) {
870 View v
= super.getView(position
, convertView
, parent
);
872 ((TextView
) v
).setTextColor(getResources().getColorStateList(
873 android
.R
.color
.white
));
875 fixRoot((TextView
) v
);
879 public View
getDropDownView(int position
, View convertView
,
881 View v
= super.getDropDownView(position
, convertView
, parent
);
883 ((TextView
) v
).setTextColor(getResources().getColorStateList(
884 android
.R
.color
.white
));
886 fixRoot((TextView
) v
);
890 private void fixRoot(TextView v
) {
891 if (v
.getText().equals(OCFile
.PATH_SEPARATOR
)) {
892 v
.setText(R
.string
.default_display_name_for_root_folder
);
898 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
901 * {@link BroadcastReceiver} to enable syncing feedback in UI
904 public void onReceive(Context context
, Intent intent
) {
905 String event
= intent
.getAction();
906 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
907 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
908 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
909 boolean sameAccount
= (getAccount() != null
&& accountName
.equals(getAccount().name
) && getStorageManager() != null
);
913 if (!FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
914 OCFile currentFile
= (getFile() == null
) ? null
: getStorageManager().getFileByPath(getFile().getRemotePath());
915 OCFile currentDir
= (getCurrentDir() == null
) ? null
: getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
917 if (currentDir
== null
) {
918 // current folder was removed from the server
919 Toast
.makeText( FileDisplayActivity
.this,
920 String
.format(getString(R
.string
.sync_current_folder_was_removed
), mDirectories
.getItem(0)),
926 if (currentFile
== null
&& !getFile().isFolder()) {
927 // currently selected file was removed in the server, and now we know it
928 cleanSecondFragment();
929 currentFile
= currentDir
;
932 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
933 OCFileListFragment fileListFragment
= getListOfFilesFragment();
934 if (fileListFragment
!= null
) {
935 fileListFragment
.listDirectory(currentDir
);
938 setFile(currentFile
);
941 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
942 !SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
.equals(event
) &&
943 (synchResult
== null
|| synchResult
.isSuccess())) ;
945 if (synchResult
!= null
&&
946 synchResult
.isSuccess() &&
947 (SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SYNCED
.equals(event
) ||
948 FileSyncAdapter
.EVENT_FOLDER_CONTENTS_SYNCED
.equals(event
)
950 !mRefreshSharesInProgress
&&
951 getFileOperationsHelper().isSharedSupported(FileDisplayActivity
.this)
957 //removeStickyBroadcast(intent);
958 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
|| mRefreshSharesInProgress
);
961 if (synchResult
!= null
) {
962 if (synchResult
.getCode().equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)) {
963 mLastSslUntrustedServerResult
= synchResult
;
964 showDialog(DIALOG_SSL_VALIDATOR
);
971 private class UploadFinishReceiver
extends BroadcastReceiver
{
973 * Once the file upload has finished -> update view
974 * @author David A. Velasco
975 * {@link BroadcastReceiver} to enable upload feedback in UI
978 public void onReceive(Context context
, Intent intent
) {
979 String uploadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
980 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
981 boolean sameAccount
= getAccount() != null
&& accountName
.equals(getAccount().name
);
982 OCFile currentDir
= getCurrentDir();
983 boolean isDescendant
= (currentDir
!= null
) && (uploadedRemotePath
!= null
) && (uploadedRemotePath
.startsWith(currentDir
.getRemotePath()));
984 if (sameAccount
&& isDescendant
) {
985 refeshListOfFilesFragment();
993 * Class waiting for broadcast events from the {@link FielDownloader} service.
995 * Updates the UI when a download is started or finished, provided that it is relevant for the
998 private class DownloadFinishReceiver
extends BroadcastReceiver
{
1000 public void onReceive(Context context
, Intent intent
) {
1001 boolean sameAccount
= isSameAccount(context
, intent
);
1002 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
1003 boolean isDescendant
= isDescendant(downloadedRemotePath
);
1005 if (sameAccount
&& isDescendant
) {
1006 refeshListOfFilesFragment();
1007 refreshSecondFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
1010 removeStickyBroadcast(intent
);
1013 private boolean isDescendant(String downloadedRemotePath
) {
1014 OCFile currentDir
= getCurrentDir();
1015 return (currentDir
!= null
&& downloadedRemotePath
!= null
&& downloadedRemotePath
.startsWith(currentDir
.getRemotePath()));
1018 private boolean isSameAccount(Context context
, Intent intent
) {
1019 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
1020 return (accountName
!= null
&& getAccount() != null
&& accountName
.equals(getAccount().name
));
1026 * Class waiting for broadcast events from the {@link OperationsService}.
1028 * Updates the list of files when a get for shares is finished; at this moment the refresh of shares is the only
1029 * operation performed in {@link OperationsService}.
1031 * In the future will handle the progress or finalization of all the operations performed in {@link OperationsService},
1032 * probably all the operations associated to the app model.
1034 private class OperationsServiceReceiver
extends BroadcastReceiver
{
1037 public void onReceive(Context context
, Intent intent
) {
1038 if (OperationsService
.ACTION_OPERATION_ADDED
.equals(intent
.getAction())) {
1040 } else if (OperationsService
.ACTION_OPERATION_FINISHED
.equals(intent
.getAction())) {
1041 mRefreshSharesInProgress
= false
;
1043 Account account
= intent
.getParcelableExtra(OperationsService
.EXTRA_ACCOUNT
);
1044 RemoteOperationResult getSharesResult
= (RemoteOperationResult
)intent
.getSerializableExtra(OperationsService
.EXTRA_RESULT
);
1045 if (getAccount() != null
&& account
.name
.equals(getAccount().name
)
1046 && getStorageManager() != null
1048 refeshListOfFilesFragment();
1050 if ((getSharesResult
!= null
) &&
1051 RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
.equals(getSharesResult
.getCode())) {
1052 mLastSslUntrustedServerResult
= getSharesResult
;
1053 showDialog(DIALOG_SSL_VALIDATOR
);
1056 setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress
|| mSyncInProgress
);
1063 public void browseToRoot() {
1064 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1065 if (listOfFiles
!= null
) { // should never be null, indeed
1066 while (mDirectories
.getCount() > 1) {
1069 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
1070 listOfFiles
.listDirectory(root
);
1071 setFile(listOfFiles
.getCurrentFile());
1072 startSyncFolderOperation(root
);
1074 cleanSecondFragment();
1078 public void browseTo(OCFile folder
) {
1079 if (folder
== null
|| !folder
.isFolder()) {
1080 throw new IllegalArgumentException("Trying to browse to invalid folder " + folder
);
1082 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1083 if (listOfFiles
!= null
) {
1084 setNavigationListWithFolder(folder
);
1085 listOfFiles
.listDirectory(folder
);
1086 setFile(listOfFiles
.getCurrentFile());
1087 startSyncFolderOperation(folder
);
1089 Log_OC
.e(TAG
, "Unexpected null when accessing list fragment");
1091 cleanSecondFragment();
1098 * Updates action bar and second fragment, if in dual pane mode.
1101 public void onBrowsedDownTo(OCFile directory
) {
1102 pushDirname(directory
);
1103 cleanSecondFragment();
1106 startSyncFolderOperation(directory
);
1111 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1113 * @param file Image {@link OCFile} to show.
1116 public void startImagePreview(OCFile file
) {
1117 Intent showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
1118 showDetailsIntent
.putExtra(EXTRA_FILE
, file
);
1119 showDetailsIntent
.putExtra(EXTRA_ACCOUNT
, getAccount());
1120 startActivity(showDetailsIntent
);
1124 * Stars the preview of an already down media {@link OCFile}.
1126 * @param file Media {@link OCFile} to preview.
1127 * @param startPlaybackPosition Media position where the playback will be started, in milliseconds.
1128 * @param autoplay When 'true', the playback will start without user interactions.
1131 public void startMediaPreview(OCFile file
, int startPlaybackPosition
, boolean autoplay
) {
1132 Fragment mediaFragment
= new PreviewMediaFragment(file
, getAccount(), startPlaybackPosition
, autoplay
);
1133 setSecondFragment(mediaFragment
);
1134 updateFragmentsVisibility(true
);
1135 updateNavigationElementsInActionBar(file
);
1140 * Requests the download of the received {@link OCFile} , updates the UI
1141 * to monitor the download progress and prepares the activity to preview
1142 * or open the file when the download finishes.
1144 * @param file {@link OCFile} to download and preview.
1147 public void startDownloadForPreview(OCFile file
) {
1148 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1149 setSecondFragment(detailFragment
);
1150 mWaitingToPreview
= file
;
1151 requestForDownload();
1152 updateFragmentsVisibility(true
);
1153 updateNavigationElementsInActionBar(file
);
1159 * Shows the information of the {@link OCFile} received as a
1160 * parameter in the second fragment.
1162 * @param file {@link OCFile} whose details will be shown
1165 public void showDetails(OCFile file
) {
1166 Fragment detailFragment
= new FileDetailFragment(file
, getAccount());
1167 setSecondFragment(detailFragment
);
1168 updateFragmentsVisibility(true
);
1169 updateNavigationElementsInActionBar(file
);
1177 private void updateNavigationElementsInActionBar(OCFile chosenFile
) {
1178 ActionBar actionBar
= getSupportActionBar();
1179 if (chosenFile
== null
|| mDualPane
) {
1180 // only list of files - set for browsing through folders
1181 OCFile currentDir
= getCurrentDir();
1182 boolean noRoot
= (currentDir
!= null
&& currentDir
.getParentId() != 0);
1183 actionBar
.setDisplayHomeAsUpEnabled(noRoot
);
1184 actionBar
.setDisplayShowTitleEnabled(!noRoot
);
1186 actionBar
.setTitle(getString(R
.string
.default_display_name_for_root_folder
));
1188 actionBar
.setNavigationMode(!noRoot ? ActionBar
.NAVIGATION_MODE_STANDARD
: ActionBar
.NAVIGATION_MODE_LIST
);
1189 actionBar
.setListNavigationCallbacks(mDirectories
, this); // assuming mDirectories is updated
1192 actionBar
.setDisplayHomeAsUpEnabled(true
);
1193 actionBar
.setDisplayShowTitleEnabled(true
);
1194 actionBar
.setTitle(chosenFile
.getFileName());
1195 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
1204 public void onFileStateChanged() {
1205 refeshListOfFilesFragment();
1206 updateNavigationElementsInActionBar(getSecondFragment().getFile());
1214 public FileDownloaderBinder
getFileDownloaderBinder() {
1215 return mDownloaderBinder
;
1223 public FileUploaderBinder
getFileUploaderBinder() {
1224 return mUploaderBinder
;
1228 /** Defines callbacks for service binding, passed to bindService() */
1229 private class ListServiceConnection
implements ServiceConnection
{
1232 public void onServiceConnected(ComponentName component
, IBinder service
) {
1233 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1234 Log_OC
.d(TAG
, "Download service connected");
1235 mDownloaderBinder
= (FileDownloaderBinder
) service
;
1236 if (mWaitingToPreview
!= null
) {
1237 requestForDownload();
1240 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1241 Log_OC
.d(TAG
, "Upload service connected");
1242 mUploaderBinder
= (FileUploaderBinder
) service
;
1246 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1247 OCFileListFragment listOfFiles
= getListOfFilesFragment();
1248 if (listOfFiles
!= null
) {
1249 listOfFiles
.listDirectory();
1251 FileFragment secondFragment
= getSecondFragment();
1252 if (secondFragment
!= null
&& secondFragment
instanceof FileDetailFragment
) {
1253 FileDetailFragment detailFragment
= (FileDetailFragment
)secondFragment
;
1254 detailFragment
.listenForTransferProgress();
1255 detailFragment
.updateFileDetails(false
, false
);
1260 public void onServiceDisconnected(ComponentName component
) {
1261 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
1262 Log_OC
.d(TAG
, "Download service disconnected");
1263 mDownloaderBinder
= null
;
1264 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
1265 Log_OC
.d(TAG
, "Upload service disconnected");
1266 mUploaderBinder
= null
;
1274 * Launch an intent to request the PIN code to the user before letting him use the app
1276 private void requestPinCode() {
1277 boolean pinStart
= false
;
1278 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
1279 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1281 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1282 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");
1289 public void onSavedCertificate() {
1290 startSyncFolderOperation(getCurrentDir());
1295 public void onFailedSavingCertificate() {
1296 showDialog(DIALOG_CERT_NOT_SAVED
);
1301 * Updates the view associated to the activity after the finish of some operation over files
1302 * in the current account.
1304 * @param operation Removal operation performed.
1305 * @param result Result of the removal.
1308 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1309 super.onRemoteOperationFinish(operation
, result
);
1311 if (operation
instanceof RemoveFileOperation
) {
1312 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
1314 } else if (operation
instanceof RenameFileOperation
) {
1315 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
1317 } else if (operation
instanceof SynchronizeFileOperation
) {
1318 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
1320 } else if (operation
instanceof CreateFolderOperation
) {
1321 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
1323 } else if (operation
instanceof CreateShareOperation
) {
1324 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
1326 } else if (operation
instanceof UnshareLinkOperation
) {
1327 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
1334 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
1335 if (result
.isSuccess()) {
1336 refreshShowDetails();
1337 refeshListOfFilesFragment();
1342 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
, RemoteOperationResult result
) {
1343 if (result
.isSuccess()) {
1344 refreshShowDetails();
1345 refeshListOfFilesFragment();
1349 private void refreshShowDetails() {
1350 FileFragment details
= getSecondFragment();
1351 if (details
!= null
) {
1352 OCFile file
= details
.getFile();
1354 file
= getStorageManager().getFileByPath(file
.getRemotePath()); {
1355 if (!(details
instanceof PreviewMediaFragment
|| details
instanceof PreviewImageFragment
)) {
1357 } else if (details
instanceof PreviewMediaFragment
) {
1358 startMediaPreview(file
, 0, false
);
1361 invalidateOptionsMenu();
1367 * Updates the view associated to the activity after the finish of an operation trying to remove a
1370 * @param operation Removal operation performed.
1371 * @param result Result of the removal.
1373 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
1374 dismissLoadingDialog();
1375 if (result
.isSuccess()) {
1376 Toast msg
= Toast
.makeText(this, R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
1378 OCFile removedFile
= operation
.getFile();
1379 getSecondFragment();
1380 FileFragment second
= getSecondFragment();
1381 if (second
!= null
&& removedFile
.equals(second
.getFile())) {
1382 cleanSecondFragment();
1384 if (getStorageManager().getFileById(removedFile
.getParentId()).equals(getCurrentDir())) {
1385 refeshListOfFilesFragment();
1389 Toast msg
= Toast
.makeText(this, R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
1391 if (result
.isSslRecoverableException()) {
1392 mLastSslUntrustedServerResult
= result
;
1393 showDialog(DIALOG_SSL_VALIDATOR
);
1399 * Updates the view associated to the activity after the finish of an operation trying create a new folder
1401 * @param operation Creation operation performed.
1402 * @param result Result of the creation.
1404 private void onCreateFolderOperationFinish(CreateFolderOperation operation
, RemoteOperationResult result
) {
1405 if (result
.isSuccess()) {
1406 dismissLoadingDialog();
1407 refeshListOfFilesFragment();
1410 dismissLoadingDialog();
1411 if (result
.getCode() == ResultCode
.INVALID_CHARACTER_IN_NAME
) {
1412 Toast
.makeText(FileDisplayActivity
.this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
).show();
1415 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
1418 } catch (NotFoundException e
) {
1419 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
1427 * Updates the view associated to the activity after the finish of an operation trying to rename a
1430 * @param operation Renaming operation performed.
1431 * @param result Result of the renaming.
1433 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1434 dismissLoadingDialog();
1435 OCFile renamedFile
= operation
.getFile();
1436 if (result
.isSuccess()) {
1438 FileFragment details
= getSecondFragment();
1439 if (details
!= null
&& details
instanceof FileDetailFragment
&& renamedFile
.equals(details
.getFile()) ) {
1440 ((FileDetailFragment
) details
).updateFileDetails(renamedFile
, getAccount());
1443 if (getStorageManager().getFileById(renamedFile
.getParentId()).equals(getCurrentDir())) {
1444 refeshListOfFilesFragment();
1448 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1449 Toast msg
= Toast
.makeText(this, R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1451 // TODO throw again the new rename dialog
1452 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
1453 Toast msg
= Toast
.makeText(this, R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
1456 Toast msg
= Toast
.makeText(this, R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1458 if (result
.isSslRecoverableException()) {
1459 mLastSslUntrustedServerResult
= result
;
1460 showDialog(DIALOG_SSL_VALIDATOR
);
1467 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1468 dismissLoadingDialog();
1469 OCFile syncedFile
= operation
.getLocalFile();
1470 if (!result
.isSuccess()) {
1471 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1472 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
1473 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
1474 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
1480 if (operation
.transferWasRequested()) {
1481 refeshListOfFilesFragment();
1482 onTransferStateChanged(syncedFile
, true
, true
);
1485 Toast msg
= Toast
.makeText(this, R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1496 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
1498 FileFragment details
= getSecondFragment();
1499 if (details
!= null
&& details
instanceof FileDetailFragment
&& file
.equals(details
.getFile()) ) {
1500 if (downloading
|| uploading
) {
1501 ((FileDetailFragment
)details
).updateFileDetails(file
, getAccount());
1503 ((FileDetailFragment
)details
).updateFileDetails(false
, true
);
1510 public void onDismiss(EditNameDialog dialog
) {
1511 if (dialog
.getResult()) {
1512 String newDirectoryName
= dialog
.getNewFilename().trim();
1513 Log_OC
.d(TAG
, "'create directory' dialog dismissed with new name " + newDirectoryName
);
1514 if (newDirectoryName
.length() > 0) {
1515 String path
= getCurrentDir().getRemotePath();
1518 path
+= newDirectoryName
+ OCFile
.PATH_SEPARATOR
;
1519 RemoteOperation operation
= new CreateFolderOperation(path
, false
, getStorageManager());
1520 operation
.execute( getAccount(),
1521 FileDisplayActivity
.this,
1522 FileDisplayActivity
.this,
1524 FileDisplayActivity
.this);
1526 showLoadingDialog();
1532 private void requestForDownload() {
1533 Account account
= getAccount();
1534 if (!mDownloaderBinder
.isDownloading(account
, mWaitingToPreview
)) {
1535 Intent i
= new Intent(this, FileDownloader
.class);
1536 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
1537 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
1543 private OCFile
getCurrentDir() {
1544 OCFile file
= getFile();
1546 if (file
.isFolder()) {
1548 } else if (getStorageManager() != null
) {
1549 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
1550 return getStorageManager().getFileByPath(parentPath
);
1556 public void startSyncFolderOperation(OCFile folder
) {
1557 long currentSyncTime
= System
.currentTimeMillis();
1559 mSyncInProgress
= true
;
1561 // perform folder synchronization
1562 RemoteOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
1565 getStorageManager(),
1567 getApplicationContext()
1569 synchFolderOp
.execute(getAccount(), this, null
, null
, this);
1571 setSupportProgressBarIndeterminateVisibility(true
);
1575 private void startGetShares() {
1576 // Get shared files/folders
1577 Intent intent
= new Intent(this, OperationsService
.class);
1578 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, getAccount());
1579 startService(intent
);
1581 mRefreshSharesInProgress
= true
;