2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2011 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.ui
.activity
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
26 import android
.accounts
.AccountManagerCallback
;
27 import android
.accounts
.AccountManagerFuture
;
28 import android
.accounts
.AuthenticatorException
;
29 import android
.accounts
.OperationCanceledException
;
30 import android
.content
.ComponentName
;
31 import android
.content
.Context
;
32 import android
.content
.Intent
;
33 import android
.content
.ServiceConnection
;
34 import android
.content
.res
.Configuration
;
35 import android
.os
.Bundle
;
36 import android
.os
.Handler
;
37 import android
.os
.IBinder
;
38 import android
.support
.v4
.app
.Fragment
;
39 import android
.support
.v4
.app
.FragmentManager
;
40 import android
.support
.v4
.app
.FragmentTransaction
;
41 import android
.support
.v4
.view
.GravityCompat
;
42 import android
.support
.v4
.widget
.DrawerLayout
;
43 import android
.support
.v7
.app
.ActionBar
;
44 import android
.support
.v7
.app
.ActionBarDrawerToggle
;
45 import android
.support
.v7
.app
.AppCompatActivity
;
46 import android
.util
.Log
;
47 import android
.view
.View
;
48 import android
.widget
.AdapterView
;
49 import android
.widget
.ListView
;
50 import android
.widget
.RelativeLayout
;
51 import android
.widget
.TextView
;
52 import android
.widget
.Toast
;
54 import com
.owncloud
.android
.BuildConfig
;
55 import com
.owncloud
.android
.MainApp
;
56 import com
.owncloud
.android
.R
;
57 import com
.owncloud
.android
.authentication
.AccountUtils
;
58 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
59 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
60 import com
.owncloud
.android
.datamodel
.OCFile
;
61 import com
.owncloud
.android
.files
.FileOperationsHelper
;
62 import com
.owncloud
.android
.files
.services
.FileDownloader
;
63 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
64 import com
.owncloud
.android
.files
.services
.FileUploader
;
65 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
66 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
67 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
68 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
69 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
70 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
71 import com
.owncloud
.android
.operations
.CreateShareOperation
;
72 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
73 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
74 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
75 import com
.owncloud
.android
.services
.OperationsService
;
76 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
77 import com
.owncloud
.android
.ui
.NavigationDrawerItem
;
78 import com
.owncloud
.android
.ui
.adapter
.NavigationDrawerListAdapter
;
79 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
80 import com
.owncloud
.android
.ui
.dialog
.SharePasswordDialogFragment
;
81 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
83 import java
.util
.ArrayList
;
87 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud
90 public class FileActivity
extends AppCompatActivity
91 implements OnRemoteOperationListener
, ComponentsGetter
{
93 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
94 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
95 public static final String EXTRA_WAITING_TO_PREVIEW
=
96 "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
97 public static final String EXTRA_FROM_NOTIFICATION
=
98 "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
100 public static final String TAG
= FileActivity
.class.getSimpleName();
102 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
103 private static final String KEY_WAITING_FOR_OP_ID
= "WAITING_FOR_OP_ID";
104 private static final String DIALOG_SHARE_PASSWORD
= "DIALOG_SHARE_PASSWORD";
105 private static final String KEY_TRY_SHARE_AGAIN
= "TRY_SHARE_AGAIN";
106 private static final String KEY_ACTION_BAR_TITLE
= "ACTION_BAR_TITLE";
108 protected static final long DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
= 200;
111 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.*/
112 private Account mAccount
;
114 /** Main {@link OCFile} handled by the activity.*/
115 private OCFile mFile
;
117 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud
119 private boolean mRedirectingToSetupAccount
= false
;
121 /** Flag to signal when the value of mAccount was set */
122 protected boolean mAccountWasSet
;
124 /** Flag to signal when the value of mAccount was restored from a saved state */
125 protected boolean mAccountWasRestored
;
127 /** Flag to signal if the activity is launched by a notification */
128 private boolean mFromNotification
;
130 /** Messages handler associated to the main thread and the life cycle of the activity */
131 private Handler mHandler
;
133 /** Access point to the cached database for the current ownCloud {@link Account} */
134 private FileDataStorageManager mStorageManager
= null
;
136 private FileOperationsHelper mFileOperationsHelper
;
138 private ServiceConnection mOperationsServiceConnection
= null
;
140 private OperationsServiceBinder mOperationsServiceBinder
= null
;
142 protected FileDownloaderBinder mDownloaderBinder
= null
;
143 protected FileUploaderBinder mUploaderBinder
= null
;
144 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
146 private boolean mTryShareAgain
= false
;
149 protected DrawerLayout mDrawerLayout
;
150 protected ActionBarDrawerToggle mDrawerToggle
;
151 protected ListView mDrawerList
;
154 protected String
[] mDrawerTitles
;
155 protected String
[] mDrawerContentDescriptions
;
157 protected ArrayList
<NavigationDrawerItem
> mDrawerItems
;
159 protected NavigationDrawerListAdapter mNavigationDrawerAdapter
= null
;
162 // TODO re-enable when "Accounts" is available in Navigation Drawer
163 // protected boolean mShowAccounts = false;
166 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
167 * the {@link FileActivity}.
169 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
170 * is requested to create a new one.
173 protected void onCreate(Bundle savedInstanceState
) {
174 super.onCreate(savedInstanceState
);
175 mHandler
= new Handler();
176 mFileOperationsHelper
= new FileOperationsHelper(this);
177 Account account
= null
;
178 if(savedInstanceState
!= null
) {
179 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
180 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
181 mFileOperationsHelper
.setOpIdWaitingFor(
182 savedInstanceState
.getLong(KEY_WAITING_FOR_OP_ID
, Long
.MAX_VALUE
)
184 mTryShareAgain
= savedInstanceState
.getBoolean(KEY_TRY_SHARE_AGAIN
);
185 getSupportActionBar().setTitle(savedInstanceState
.getString(KEY_ACTION_BAR_TITLE
));
187 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
188 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
189 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
,
193 AccountUtils
.updateAccountVersion(this); // best place, before any access to AccountManager
196 setAccount(account
, savedInstanceState
!= null
);
198 mOperationsServiceConnection
= new OperationsServiceConnection();
199 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
,
200 Context
.BIND_AUTO_CREATE
);
202 mDownloadServiceConnection
= newTransferenceServiceConnection();
203 if (mDownloadServiceConnection
!= null
) {
204 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
205 Context
.BIND_AUTO_CREATE
);
207 mUploadServiceConnection
= newTransferenceServiceConnection();
208 if (mUploadServiceConnection
!= null
) {
209 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
210 Context
.BIND_AUTO_CREATE
);
216 protected void onNewIntent (Intent intent
) {
217 Log_OC
.v(TAG
, "onNewIntent() start");
218 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
219 if (current
!= null
&& mAccount
!= null
&& !mAccount
.name
.equals(current
.name
)) {
222 Log_OC
.v(TAG
, "onNewIntent() stop");
226 * Since ownCloud {@link Account}s can be managed from the system setting menu,
227 * the existence of the {@link Account} associated to the instance must be checked
228 * every time it is restarted.
231 protected void onRestart() {
232 Log_OC
.v(TAG
, "onRestart() start");
234 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.exists(mAccount
, this));
236 swapToDefaultAccount();
238 Log_OC
.v(TAG
, "onRestart() end");
243 protected void onStart() {
246 if (mAccountWasSet
) {
247 onAccountSet(mAccountWasRestored
);
252 protected void onResume() {
255 if (mOperationsServiceBinder
!= null
) {
256 doOnResumeAndBound();
261 protected void onPause() {
262 if (mOperationsServiceBinder
!= null
) {
263 mOperationsServiceBinder
.removeOperationListener(this);
271 protected void onDestroy() {
272 if (mOperationsServiceConnection
!= null
) {
273 unbindService(mOperationsServiceConnection
);
274 mOperationsServiceBinder
= null
;
276 if (mDownloadServiceConnection
!= null
) {
277 unbindService(mDownloadServiceConnection
);
278 mDownloadServiceConnection
= null
;
280 if (mUploadServiceConnection
!= null
) {
281 unbindService(mUploadServiceConnection
);
282 mUploadServiceConnection
= null
;
289 protected void onPostCreate(Bundle savedInstanceState
) {
290 super.onPostCreate(savedInstanceState
);
291 // Sync the toggle state after onRestoreInstanceState has occurred.
292 if (mDrawerToggle
!= null
) {
293 mDrawerToggle
.syncState();
294 if (isDrawerOpen()) {
295 getSupportActionBar().setTitle(R
.string
.app_name
);
296 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
302 public void onConfigurationChanged(Configuration newConfig
) {
303 super.onConfigurationChanged(newConfig
);
304 if (mDrawerToggle
!= null
) {
305 mDrawerToggle
.onConfigurationChanged(newConfig
);
310 public void onBackPressed() {
311 if (isDrawerOpen()) {
315 super.onBackPressed();
319 * checks if the drawer exists and is opened.
321 * @return <code>true</code> if the drawer is open, else <code>false</code>
323 public boolean isDrawerOpen() {
324 if(mDrawerLayout
!= null
) {
325 return mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
332 * closes the navigation drawer.
334 public void closeNavDrawer() {
335 if(mDrawerLayout
!= null
) {
336 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
340 protected void initDrawer(){
341 // constant settings for action bar when navigation drawer is inited
342 getSupportActionBar().setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
345 mDrawerLayout
= (DrawerLayout
) findViewById(R
.id
.drawer_layout
);
346 // Notification Drawer
347 RelativeLayout navigationDrawerLayout
= (RelativeLayout
) findViewById(R
.id
.left_drawer
);
348 mDrawerList
= (ListView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_list
);
350 // TODO re-enable when "Accounts" is available in Navigation Drawer
351 // // load Account in the Drawer Title
353 // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
354 // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
357 // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
358 // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
360 // if (account != null) {
361 // int lastAtPos = account.name.lastIndexOf("@");
362 // username.setText(account.name.substring(0, lastAtPos));
365 // Display username in drawer
366 setUsernameInDrawer(navigationDrawerLayout
, AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext()));
368 // load slide menu items
369 mDrawerTitles
= getResources().getStringArray(R
.array
.drawer_items
);
371 // nav drawer content description from resources
372 mDrawerContentDescriptions
= getResources().
373 getStringArray(R
.array
.drawer_content_descriptions
);
376 mDrawerItems
= new ArrayList
<NavigationDrawerItem
>();
377 // adding nav drawer items to array
378 // TODO re-enable when "Accounts" is available in Navigation Drawer
380 // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
381 // mDrawerContentDescriptions[0]));
383 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[0], mDrawerContentDescriptions
[0],
384 R
.drawable
.ic_folder_open
));
386 // TODO Enable when "On Device" is recovered
388 //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
389 // mDrawerContentDescriptions[2]));
392 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[1], mDrawerContentDescriptions
[1],
393 R
.drawable
.ic_settings
));
395 if (BuildConfig
.DEBUG
) {
396 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[2],
397 mDrawerContentDescriptions
[2],R
.drawable
.ic_log
));
400 // setting the nav drawer list adapter
401 mNavigationDrawerAdapter
= new NavigationDrawerListAdapter(getApplicationContext(), this,
403 mDrawerList
.setAdapter(mNavigationDrawerAdapter
);
406 mDrawerToggle
= new ActionBarDrawerToggle(this, mDrawerLayout
,R
.string
.drawer_open
,R
.string
.drawer_close
) {
408 /** Called when a drawer has settled in a completely closed state. */
409 public void onDrawerClosed(View view
) {
410 super.onDrawerClosed(view
);
411 updateActionBarTitleAndHomeButton(null
);
412 invalidateOptionsMenu();
415 /** Called when a drawer has settled in a completely open state. */
416 public void onDrawerOpened(View drawerView
) {
417 super.onDrawerOpened(drawerView
);
418 getSupportActionBar().setTitle(R
.string
.app_name
);
419 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
420 invalidateOptionsMenu();
424 // Set the list's click listener
425 mDrawerList
.setOnItemClickListener(new DrawerItemClickListener());
427 // Set the drawer toggle as the DrawerListener
428 mDrawerLayout
.setDrawerListener(mDrawerToggle
);
429 mDrawerToggle
.setDrawerIndicatorEnabled(false
);
433 * sets the given account name in the drawer in case the drawer is available. The account name
434 * is shortened beginning from the @-sign in the username.
436 * @param navigationDrawerLayout the drawer layout to be used
437 * @param account the account to be set in the drawer
439 protected void setUsernameInDrawer(RelativeLayout navigationDrawerLayout
, Account account
) {
440 if (navigationDrawerLayout
!= null
&& getAccount() != null
) {
441 TextView username
= (TextView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_username
);
442 int lastAtPos
= account
.name
.lastIndexOf("@");
443 username
.setText(account
.name
.substring(0, lastAtPos
));
448 * Updates title bar and home buttons (state and icon).
450 * Assumes that navigation drawer is NOT visible.
452 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
453 String title
= getString(R
.string
.default_display_name_for_root_folder
); // default
456 /// choose the appropiate title
457 if (chosenFile
== null
) {
458 chosenFile
= mFile
; // if no file is passed, current file decides
461 chosenFile
== null
||
462 (chosenFile
.isFolder() && chosenFile
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
)
465 title
= chosenFile
.getFileName();
468 /// set the chosen title
469 ActionBar actionBar
= getSupportActionBar();
470 actionBar
.setTitle(title
);
471 /// also as content description
472 View actionBarTitleView
= getWindow().getDecorView().findViewById(
473 getResources().getIdentifier("action_bar_title", "id", "android")
475 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
476 actionBarTitleView
.setContentDescription(title
);
479 /// set home button properties
480 mDrawerToggle
.setDrawerIndicatorEnabled(inRoot
);
481 actionBar
.setDisplayHomeAsUpEnabled(true
);
482 actionBar
.setDisplayShowTitleEnabled(true
);
488 * Sets and validates the ownCloud {@link Account} associated to the Activity.
490 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
492 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
494 * @param account New {@link Account} to set.
495 * @param savedAccount When 'true', account was retrieved from a saved instance state.
497 protected void setAccount(Account account
, boolean savedAccount
) {
498 Account oldAccount
= mAccount
;
499 boolean validAccount
=
500 (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(),
504 mAccountWasSet
= true
;
505 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
508 swapToDefaultAccount();
514 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
516 * If no valid ownCloud {@link Account} exists, the the user is requested
517 * to create a new ownCloud {@link Account}.
519 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
521 private void swapToDefaultAccount() {
522 // default to the most recently used account
523 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
524 if (newAccount
== null
) {
525 /// no account available: force account creation
526 createFirstAccount();
527 mRedirectingToSetupAccount
= true
;
528 mAccountWasSet
= false
;
529 mAccountWasRestored
= false
;
532 mAccountWasSet
= true
;
533 mAccountWasRestored
= (newAccount
.equals(mAccount
));
534 mAccount
= newAccount
;
540 * Launches the account creation activity. To use when no ownCloud account is available
542 private void createFirstAccount() {
543 AccountManager am
= AccountManager
.get(getApplicationContext());
544 am
.addAccount(MainApp
.getAccountType(),
549 new AccountCreationCallback(),
558 protected void onSaveInstanceState(Bundle outState
) {
559 super.onSaveInstanceState(outState
);
560 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
561 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
562 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mFileOperationsHelper
.getOpIdWaitingFor());
563 outState
.putBoolean(KEY_TRY_SHARE_AGAIN
, mTryShareAgain
);
564 if(getSupportActionBar().getTitle() != null
) {
565 // Null check in case the actionbar is used in ActionBar.NAVIGATION_MODE_LIST
566 // since it doesn't have a title then
567 outState
.putString(KEY_ACTION_BAR_TITLE
, getSupportActionBar().getTitle().toString());
573 * Getter for the main {@link OCFile} handled by the activity.
575 * @return Main {@link OCFile} handled by the activity.
577 public OCFile
getFile() {
583 * Setter for the main {@link OCFile} handled by the activity.
585 * @param file Main {@link OCFile} to be handled by the activity.
587 public void setFile(OCFile file
) {
593 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
596 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
599 public Account
getAccount() {
603 protected void setAccount(Account account
) {
608 * @return Value of mFromNotification: True if the Activity is launched by a notification
610 public boolean fromNotification() {
611 return mFromNotification
;
615 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
617 protected boolean isRedirectingToSetupAccount() {
618 return mRedirectingToSetupAccount
;
621 public boolean isTryShareAgain(){
622 return mTryShareAgain
;
625 public void setTryShareAgain(boolean tryShareAgain
) {
626 mTryShareAgain
= tryShareAgain
;
629 public OperationsServiceBinder
getOperationsServiceBinder() {
630 return mOperationsServiceBinder
;
633 protected ServiceConnection
newTransferenceServiceConnection() {
638 * Helper class handling a callback from the {@link AccountManager} after the creation of
639 * a new ownCloud {@link Account} finished, successfully or not.
641 * At this moment, only called after the creation of the first account.
643 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
646 public void run(AccountManagerFuture
<Bundle
> future
) {
647 FileActivity
.this.mRedirectingToSetupAccount
= false
;
648 boolean accountWasSet
= false
;
649 if (future
!= null
) {
652 result
= future
.getResult();
653 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
654 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
655 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
656 setAccount(new Account(name
, type
), false
);
657 accountWasSet
= true
;
659 } catch (OperationCanceledException e
) {
660 Log_OC
.d(TAG
, "Account creation canceled");
662 } catch (Exception e
) {
663 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
667 Log_OC
.e(TAG
, "Account creation callback with null bundle");
669 if (!accountWasSet
) {
670 moveTaskToBack(true
);
678 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
680 * Child classes must grant that state depending on the {@link Account} is updated.
682 protected void onAccountSet(boolean stateWasRecovered
) {
683 if (getAccount() != null
) {
684 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
687 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
692 public FileDataStorageManager
getStorageManager() {
693 return mStorageManager
;
697 public OnRemoteOperationListener
getRemoteOperationListener() {
702 public Handler
getHandler() {
706 public FileOperationsHelper
getFileOperationsHelper() {
707 return mFileOperationsHelper
;
712 * @param operation Removal operation performed.
713 * @param result Result of the removal.
716 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
717 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the "
718 + "FileActivities ");
720 mFileOperationsHelper
.setOpIdWaitingFor(Long
.MAX_VALUE
);
722 if (!result
.isSuccess() && (
723 result
.getCode() == ResultCode
.UNAUTHORIZED
||
724 result
.isIdPRedirection() ||
725 (result
.isException() && result
.getException() instanceof AuthenticatorException
)
728 requestCredentialsUpdate();
730 if (result
.getCode() == ResultCode
.UNAUTHORIZED
) {
731 dismissLoadingDialog();
732 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
733 operation
, getResources()),
737 mTryShareAgain
= false
;
739 } else if (operation
instanceof CreateShareOperation
) {
740 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
742 } else if (operation
instanceof UnshareLinkOperation
) {
743 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
745 } else if (operation
instanceof SynchronizeFolderOperation
) {
746 onSynchronizeFolderOperationFinish((SynchronizeFolderOperation
)operation
, result
);
748 }else if (operation
instanceof SynchronizeFileOperation
) {
749 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
754 protected void requestCredentialsUpdate() {
755 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
756 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
757 updateAccountCredentials
.putExtra(
758 AuthenticatorActivity
.EXTRA_ACTION
,
759 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
);
760 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
761 startActivity(updateAccountCredentials
);
766 private void onCreateShareOperationFinish(CreateShareOperation operation
,
767 RemoteOperationResult result
) {
768 dismissLoadingDialog();
769 if (result
.isSuccess()) {
770 mTryShareAgain
= false
;
773 Intent sendIntent
= operation
.getSendIntent();
774 startActivity(sendIntent
);
776 // Detect Failure (403) --> needs Password
777 if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
778 if (!isTryShareAgain()) {
779 SharePasswordDialogFragment dialog
=
780 SharePasswordDialogFragment
.newInstance(new OCFile(operation
.getPath()),
781 operation
.getSendIntent());
782 dialog
.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD
);
784 Toast t
= Toast
.makeText(this,
785 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
788 mTryShareAgain
= false
;
791 Toast t
= Toast
.makeText(this,
792 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
800 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
801 RemoteOperationResult result
) {
802 dismissLoadingDialog();
804 if (result
.isSuccess()){
808 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
809 operation
, getResources()), Toast
.LENGTH_LONG
);
814 private void onSynchronizeFolderOperationFinish(
815 SynchronizeFolderOperation operation
, RemoteOperationResult result
817 if (!result
.isSuccess() && result
.getCode() != ResultCode
.CANCELLED
){
818 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
819 operation
, getResources()), Toast
.LENGTH_LONG
);
824 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
825 RemoteOperationResult result
) {
826 dismissLoadingDialog();
827 OCFile syncedFile
= operation
.getLocalFile();
828 if (!result
.isSuccess()) {
829 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
830 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
831 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
832 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
837 if (!operation
.transferWasRequested()) {
838 Toast msg
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
839 operation
, getResources()), Toast
.LENGTH_LONG
);
842 invalidateOptionsMenu();
846 protected void updateFileFromDB(){
847 OCFile file
= getFile();
849 file
= getStorageManager().getFileByPath(file
.getRemotePath());
856 * Show loading dialog
858 public void showLoadingDialog() {
860 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
861 FragmentManager fm
= getSupportFragmentManager();
862 FragmentTransaction ft
= fm
.beginTransaction();
863 loading
.show(ft
, DIALOG_WAIT_TAG
);
869 * Dismiss loading dialog
871 public void dismissLoadingDialog() {
872 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
874 LoadingDialog loading
= (LoadingDialog
) frag
;
880 private void doOnResumeAndBound() {
881 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
882 long waitingForOpId
= mFileOperationsHelper
.getOpIdWaitingFor();
883 if (waitingForOpId
<= Integer
.MAX_VALUE
) {
884 boolean wait
= mOperationsServiceBinder
.dispatchResultIfFinished((int)waitingForOpId
,
887 dismissLoadingDialog();
894 * Implements callback methods for service binding. Passed as a parameter to {
896 private class OperationsServiceConnection
implements ServiceConnection
{
899 public void onServiceConnected(ComponentName component
, IBinder service
) {
900 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
901 Log_OC
.d(TAG
, "Operations service connected");
902 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
903 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
904 dismissLoadingDialog();
906 doOnResumeAndBound();
915 public void onServiceDisconnected(ComponentName component
) {
916 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
917 Log_OC
.d(TAG
, "Operations service disconnected");
918 mOperationsServiceBinder
= null
;
919 // TODO whatever could be waiting for the service is unbound
926 public FileDownloaderBinder
getFileDownloaderBinder() {
927 return mDownloaderBinder
;
932 public FileUploaderBinder
getFileUploaderBinder() {
933 return mUploaderBinder
;
937 public void restart(){
938 Intent i
= new Intent(this, FileDisplayActivity
.class);
939 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
943 // TODO re-enable when "Accounts" is available in Navigation Drawer
944 // public void closeDrawer() {
945 // mDrawerLayout.closeDrawers();
948 public void allFilesOption(){
952 private class DrawerItemClickListener
implements ListView
.OnItemClickListener
{
954 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
955 // TODO re-enable when "Accounts" is available in Navigation Drawer
956 // if (mShowAccounts && position > 0){
957 // position = position - 1;
960 // TODO re-enable when "Accounts" is available in Navigation Drawer
961 // case 0: // Accounts
962 // mShowAccounts = !mShowAccounts;
963 // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
964 // mNavigationDrawerAdapter.notifyDataSetChanged();
969 mDrawerLayout
.closeDrawers();
972 // TODO Enable when "On Device" is recovered ?
974 // MainApp.showOnlyFilesOnDevice(true);
975 // mDrawerLayout.closeDrawers();
979 Intent settingsIntent
= new Intent(getApplicationContext(),
981 startActivity(settingsIntent
);
982 mDrawerLayout
.closeDrawers();
986 Intent loggerIntent
= new Intent(getApplicationContext(),
987 LogHistoryActivity
.class);
988 startActivity(loggerIntent
);
989 mDrawerLayout
.closeDrawers();