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
.view
.View
;
47 import android
.widget
.AdapterView
;
48 import android
.widget
.ListView
;
49 import android
.widget
.RelativeLayout
;
50 import android
.widget
.TextView
;
51 import android
.widget
.Toast
;
53 import com
.owncloud
.android
.BuildConfig
;
54 import com
.owncloud
.android
.MainApp
;
55 import com
.owncloud
.android
.R
;
56 import com
.owncloud
.android
.authentication
.AccountUtils
;
57 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
58 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
59 import com
.owncloud
.android
.datamodel
.OCFile
;
60 import com
.owncloud
.android
.files
.FileOperationsHelper
;
61 import com
.owncloud
.android
.files
.services
.FileDownloader
;
62 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
63 import com
.owncloud
.android
.files
.services
.FileUploader
;
64 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
65 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
66 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
67 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
68 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
69 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
70 import com
.owncloud
.android
.operations
.CreateShareViaLinkOperation
;
71 import com
.owncloud
.android
.operations
.CreateShareWithShareeOperation
;
72 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
73 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
74 import com
.owncloud
.android
.operations
.UnshareOperation
;
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";
104 private static final String KEY_WAITING_FOR_OP_ID
= "WAITING_FOR_OP_ID";
105 private static final String DIALOG_SHARE_PASSWORD
= "DIALOG_SHARE_PASSWORD";
106 private static final String KEY_TRY_SHARE_AGAIN
= "TRY_SHARE_AGAIN";
107 private static final String KEY_ACTION_BAR_TITLE
= "ACTION_BAR_TITLE";
109 protected static final long DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
= 200;
112 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.*/
113 private Account mAccount
;
115 /** Main {@link OCFile} handled by the activity.*/
116 private OCFile mFile
;
118 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud
120 private boolean mRedirectingToSetupAccount
= false
;
122 /** Flag to signal when the value of mAccount was set */
123 protected boolean mAccountWasSet
;
125 /** Flag to signal when the value of mAccount was restored from a saved state */
126 protected boolean mAccountWasRestored
;
128 /** Flag to signal if the activity is launched by a notification */
129 private boolean mFromNotification
;
131 /** Messages handler associated to the main thread and the life cycle of the activity */
132 private Handler mHandler
;
134 /** Access point to the cached database for the current ownCloud {@link Account} */
135 private FileDataStorageManager mStorageManager
= null
;
137 private FileOperationsHelper mFileOperationsHelper
;
139 private ServiceConnection mOperationsServiceConnection
= null
;
141 private OperationsServiceBinder mOperationsServiceBinder
= null
;
143 protected FileDownloaderBinder mDownloaderBinder
= null
;
144 protected FileUploaderBinder mUploaderBinder
= null
;
145 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
147 private boolean mTryShareAgain
= false
;
150 protected DrawerLayout mDrawerLayout
;
151 protected ActionBarDrawerToggle mDrawerToggle
;
152 protected ListView mDrawerList
;
155 protected String
[] mDrawerTitles
;
156 protected String
[] mDrawerContentDescriptions
;
158 protected ArrayList
<NavigationDrawerItem
> mDrawerItems
;
160 protected NavigationDrawerListAdapter mNavigationDrawerAdapter
= null
;
163 // TODO re-enable when "Accounts" is available in Navigation Drawer
164 // protected boolean mShowAccounts = false;
167 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
168 * the {@link FileActivity}.
170 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
171 * is requested to create a new one.
174 protected void onCreate(Bundle savedInstanceState
) {
175 super.onCreate(savedInstanceState
);
176 mHandler
= new Handler();
177 mFileOperationsHelper
= new FileOperationsHelper(this);
178 Account account
= null
;
179 if(savedInstanceState
!= null
) {
180 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
181 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
182 mFileOperationsHelper
.setOpIdWaitingFor(
183 savedInstanceState
.getLong(KEY_WAITING_FOR_OP_ID
, Long
.MAX_VALUE
)
185 mTryShareAgain
= savedInstanceState
.getBoolean(KEY_TRY_SHARE_AGAIN
);
186 if (getSupportActionBar() != null
) {
187 getSupportActionBar().setTitle(savedInstanceState
.getString(KEY_ACTION_BAR_TITLE
));
190 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
191 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
192 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
,
196 AccountUtils
.updateAccountVersion(this); // best place, before any access to AccountManager
199 setAccount(account
, savedInstanceState
!= null
);
201 mOperationsServiceConnection
= new OperationsServiceConnection();
202 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
,
203 Context
.BIND_AUTO_CREATE
);
205 mDownloadServiceConnection
= newTransferenceServiceConnection();
206 if (mDownloadServiceConnection
!= null
) {
207 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
208 Context
.BIND_AUTO_CREATE
);
210 mUploadServiceConnection
= newTransferenceServiceConnection();
211 if (mUploadServiceConnection
!= null
) {
212 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
213 Context
.BIND_AUTO_CREATE
);
219 protected void onNewIntent (Intent intent
) {
220 Log_OC
.v(TAG
, "onNewIntent() start");
221 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
222 if (current
!= null
&& mAccount
!= null
&& !mAccount
.name
.equals(current
.name
)) {
225 Log_OC
.v(TAG
, "onNewIntent() stop");
229 * Since ownCloud {@link Account}s can be managed from the system setting menu,
230 * the existence of the {@link Account} associated to the instance must be checked
231 * every time it is restarted.
234 protected void onRestart() {
235 Log_OC
.v(TAG
, "onRestart() start");
237 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.exists(mAccount
, this));
239 swapToDefaultAccount();
241 Log_OC
.v(TAG
, "onRestart() end");
246 protected void onStart() {
249 if (mAccountWasSet
) {
250 onAccountSet(mAccountWasRestored
);
255 protected void onResume() {
258 if (mOperationsServiceBinder
!= null
) {
259 doOnResumeAndBound();
264 protected void onPause() {
265 if (mOperationsServiceBinder
!= null
) {
266 mOperationsServiceBinder
.removeOperationListener(this);
274 protected void onDestroy() {
275 if (mOperationsServiceConnection
!= null
) {
276 unbindService(mOperationsServiceConnection
);
277 mOperationsServiceBinder
= null
;
279 if (mDownloadServiceConnection
!= null
) {
280 unbindService(mDownloadServiceConnection
);
281 mDownloadServiceConnection
= null
;
283 if (mUploadServiceConnection
!= null
) {
284 unbindService(mUploadServiceConnection
);
285 mUploadServiceConnection
= null
;
292 protected void onPostCreate(Bundle savedInstanceState
) {
293 super.onPostCreate(savedInstanceState
);
294 // Sync the toggle state after onRestoreInstanceState has occurred.
295 if (mDrawerToggle
!= null
) {
296 mDrawerToggle
.syncState();
297 if (isDrawerOpen()) {
298 getSupportActionBar().setTitle(R
.string
.app_name
);
299 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
305 public void onConfigurationChanged(Configuration newConfig
) {
306 super.onConfigurationChanged(newConfig
);
307 if (mDrawerToggle
!= null
) {
308 mDrawerToggle
.onConfigurationChanged(newConfig
);
313 public void onBackPressed() {
314 if (isDrawerOpen()) {
318 super.onBackPressed();
322 * checks if the drawer exists and is opened.
324 * @return <code>true</code> if the drawer is open, else <code>false</code>
326 public boolean isDrawerOpen() {
327 if(mDrawerLayout
!= null
) {
328 return mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
335 * closes the navigation drawer.
337 public void closeNavDrawer() {
338 if(mDrawerLayout
!= null
) {
339 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
343 protected void initDrawer(){
344 // constant settings for action bar when navigation drawer is inited
345 getSupportActionBar().setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
348 mDrawerLayout
= (DrawerLayout
) findViewById(R
.id
.drawer_layout
);
349 // Notification Drawer
350 RelativeLayout navigationDrawerLayout
= (RelativeLayout
) findViewById(R
.id
.left_drawer
);
351 mDrawerList
= (ListView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_list
);
353 // TODO re-enable when "Accounts" is available in Navigation Drawer
354 // // load Account in the Drawer Title
356 // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
357 // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
360 // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
361 // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
363 // if (account != null) {
364 // int lastAtPos = account.name.lastIndexOf("@");
365 // username.setText(account.name.substring(0, lastAtPos));
368 // Display username in drawer
369 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
370 if (account
!= null
) {
371 TextView username
= (TextView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_username
);
372 int lastAtPos
= account
.name
.lastIndexOf("@");
373 username
.setText(account
.name
.substring(0, lastAtPos
));
376 // load slide menu items
377 mDrawerTitles
= getResources().getStringArray(R
.array
.drawer_items
);
379 // nav drawer content description from resources
380 mDrawerContentDescriptions
= getResources().
381 getStringArray(R
.array
.drawer_content_descriptions
);
384 mDrawerItems
= new ArrayList
<NavigationDrawerItem
>();
385 // adding nav drawer items to array
386 // TODO re-enable when "Accounts" is available in Navigation Drawer
388 // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
389 // mDrawerContentDescriptions[0]));
391 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[0], mDrawerContentDescriptions
[0],
392 R
.drawable
.ic_folder_open
));
394 // TODO Enable when "On Device" is recovered
396 //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
397 // mDrawerContentDescriptions[2]));
400 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[1], mDrawerContentDescriptions
[1],
401 R
.drawable
.ic_settings
));
403 if (BuildConfig
.DEBUG
) {
404 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[2],
405 mDrawerContentDescriptions
[2],R
.drawable
.ic_log
));
408 // setting the nav drawer list adapter
409 mNavigationDrawerAdapter
= new NavigationDrawerListAdapter(getApplicationContext(), this,
411 mDrawerList
.setAdapter(mNavigationDrawerAdapter
);
414 mDrawerToggle
= new ActionBarDrawerToggle(this, mDrawerLayout
,R
.string
.drawer_open
,R
.string
.drawer_close
) {
416 /** Called when a drawer has settled in a completely closed state. */
417 public void onDrawerClosed(View view
) {
418 super.onDrawerClosed(view
);
419 updateActionBarTitleAndHomeButton(null
);
420 invalidateOptionsMenu();
423 /** Called when a drawer has settled in a completely open state. */
424 public void onDrawerOpened(View drawerView
) {
425 super.onDrawerOpened(drawerView
);
426 getSupportActionBar().setTitle(R
.string
.app_name
);
427 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
428 invalidateOptionsMenu();
432 // Set the list's click listener
433 mDrawerList
.setOnItemClickListener(new DrawerItemClickListener());
435 // Set the drawer toggle as the DrawerListener
436 mDrawerLayout
.setDrawerListener(mDrawerToggle
);
437 mDrawerToggle
.setDrawerIndicatorEnabled(false
);
441 * Updates title bar and home buttons (state and icon).
443 * Assumes that navigation drawer is NOT visible.
445 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
446 String title
= getString(R
.string
.default_display_name_for_root_folder
); // default
449 /// choose the appropiate title
450 if (chosenFile
== null
) {
451 chosenFile
= mFile
; // if no file is passed, current file decides
454 chosenFile
== null
||
455 (chosenFile
.isFolder() && chosenFile
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
)
458 title
= chosenFile
.getFileName();
461 /// set the chosen title
462 ActionBar actionBar
= getSupportActionBar();
463 actionBar
.setTitle(title
);
464 /// also as content description
465 View actionBarTitleView
= getWindow().getDecorView().findViewById(
466 getResources().getIdentifier("action_bar_title", "id", "android")
468 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
469 actionBarTitleView
.setContentDescription(title
);
472 /// set home button properties
473 mDrawerToggle
.setDrawerIndicatorEnabled(inRoot
);
474 actionBar
.setDisplayHomeAsUpEnabled(true
);
475 actionBar
.setDisplayShowTitleEnabled(true
);
481 * Sets and validates the ownCloud {@link Account} associated to the Activity.
483 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
485 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
487 * @param account New {@link Account} to set.
488 * @param savedAccount When 'true', account was retrieved from a saved instance state.
490 protected void setAccount(Account account
, boolean savedAccount
) {
491 Account oldAccount
= mAccount
;
492 boolean validAccount
=
493 (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(),
497 mAccountWasSet
= true
;
498 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
501 swapToDefaultAccount();
507 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
509 * If no valid ownCloud {@link Account} exists, the the user is requested
510 * to create a new ownCloud {@link Account}.
512 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
514 private void swapToDefaultAccount() {
515 // default to the most recently used account
516 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
517 if (newAccount
== null
) {
518 /// no account available: force account creation
519 createFirstAccount();
520 mRedirectingToSetupAccount
= true
;
521 mAccountWasSet
= false
;
522 mAccountWasRestored
= false
;
525 mAccountWasSet
= true
;
526 mAccountWasRestored
= (newAccount
.equals(mAccount
));
527 mAccount
= newAccount
;
533 * Launches the account creation activity. To use when no ownCloud account is available
535 private void createFirstAccount() {
536 AccountManager am
= AccountManager
.get(getApplicationContext());
537 am
.addAccount(MainApp
.getAccountType(),
542 new AccountCreationCallback(),
551 protected void onSaveInstanceState(Bundle outState
) {
552 super.onSaveInstanceState(outState
);
553 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
554 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
555 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mFileOperationsHelper
.getOpIdWaitingFor());
556 outState
.putBoolean(KEY_TRY_SHARE_AGAIN
, mTryShareAgain
);
557 if(getSupportActionBar() != null
&& getSupportActionBar().getTitle() != null
) {
558 // Null check in case the actionbar is used in ActionBar.NAVIGATION_MODE_LIST
559 // since it doesn't have a title then
560 outState
.putString(KEY_ACTION_BAR_TITLE
, getSupportActionBar().getTitle().toString());
566 * Getter for the main {@link OCFile} handled by the activity.
568 * @return Main {@link OCFile} handled by the activity.
570 public OCFile
getFile() {
576 * Setter for the main {@link OCFile} handled by the activity.
578 * @param file Main {@link OCFile} to be handled by the activity.
580 public void setFile(OCFile file
) {
586 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
589 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
592 public Account
getAccount() {
596 protected void setAccount(Account account
) {
601 * @return Value of mFromNotification: True if the Activity is launched by a notification
603 public boolean fromNotification() {
604 return mFromNotification
;
608 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
610 protected boolean isRedirectingToSetupAccount() {
611 return mRedirectingToSetupAccount
;
614 public boolean isTryShareAgain(){
615 return mTryShareAgain
;
618 public void setTryShareAgain(boolean tryShareAgain
) {
619 mTryShareAgain
= tryShareAgain
;
622 public OperationsServiceBinder
getOperationsServiceBinder() {
623 return mOperationsServiceBinder
;
626 protected ServiceConnection
newTransferenceServiceConnection() {
631 * Helper class handling a callback from the {@link AccountManager} after the creation of
632 * a new ownCloud {@link Account} finished, successfully or not.
634 * At this moment, only called after the creation of the first account.
636 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
639 public void run(AccountManagerFuture
<Bundle
> future
) {
640 FileActivity
.this.mRedirectingToSetupAccount
= false
;
641 boolean accountWasSet
= false
;
642 if (future
!= null
) {
645 result
= future
.getResult();
646 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
647 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
648 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
649 setAccount(new Account(name
, type
), false
);
650 accountWasSet
= true
;
652 } catch (OperationCanceledException e
) {
653 Log_OC
.d(TAG
, "Account creation canceled");
655 } catch (Exception e
) {
656 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
660 Log_OC
.e(TAG
, "Account creation callback with null bundle");
662 if (!accountWasSet
) {
663 moveTaskToBack(true
);
671 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
673 * Child classes must grant that state depending on the {@link Account} is updated.
675 protected void onAccountSet(boolean stateWasRecovered
) {
676 if (getAccount() != null
) {
677 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
680 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
685 public FileDataStorageManager
getStorageManager() {
686 return mStorageManager
;
690 public OnRemoteOperationListener
getRemoteOperationListener() {
695 public Handler
getHandler() {
699 public FileOperationsHelper
getFileOperationsHelper() {
700 return mFileOperationsHelper
;
705 * @param operation Removal operation performed.
706 * @param result Result of the removal.
709 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
710 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the "
711 + "FileActivities ");
713 mFileOperationsHelper
.setOpIdWaitingFor(Long
.MAX_VALUE
);
715 if (!result
.isSuccess() && (
716 result
.getCode() == ResultCode
.UNAUTHORIZED
||
717 result
.isIdPRedirection() ||
718 (result
.isException() && result
.getException() instanceof AuthenticatorException
)
721 requestCredentialsUpdate();
723 if (result
.getCode() == ResultCode
.UNAUTHORIZED
) {
724 dismissLoadingDialog();
725 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
726 operation
, getResources()),
730 mTryShareAgain
= false
;
732 } else if (operation
instanceof CreateShareViaLinkOperation
) {
733 onCreateShareViaLinkOperationFinish((CreateShareViaLinkOperation
) operation
, result
);
735 } else if (operation
instanceof CreateShareWithShareeOperation
) {
736 onCreateShareWithShareeOperationFinish((CreateShareWithShareeOperation
) operation
, result
);
738 } else if (operation
instanceof UnshareOperation
) {
739 onUnshareLinkOperationFinish((UnshareOperation
) operation
, result
);
741 } else if (operation
instanceof SynchronizeFolderOperation
) {
742 onSynchronizeFolderOperationFinish((SynchronizeFolderOperation
) operation
, result
);
744 }else if (operation
instanceof SynchronizeFileOperation
) {
745 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
750 protected void requestCredentialsUpdate() {
751 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
752 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
753 updateAccountCredentials
.putExtra(
754 AuthenticatorActivity
.EXTRA_ACTION
,
755 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
);
756 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
757 startActivity(updateAccountCredentials
);
762 private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation operation
,
763 RemoteOperationResult result
) {
764 dismissLoadingDialog();
765 if (result
.isSuccess()) {
766 mTryShareAgain
= false
;
769 Intent sendIntent
= operation
.getSendIntentWithSubject(this);
770 startActivity(sendIntent
);
772 // Detect Failure (403) --> needs Password
773 if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
774 if (!isTryShareAgain()) {
775 SharePasswordDialogFragment dialog
=
776 SharePasswordDialogFragment
.newInstance(new OCFile(operation
.getPath()),
777 operation
.getSendIntent());
778 dialog
.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD
);
780 Toast t
= Toast
.makeText(this,
781 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
784 mTryShareAgain
= false
;
787 Toast t
= Toast
.makeText(this,
788 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
795 private void onCreateShareWithShareeOperationFinish(CreateShareWithShareeOperation operation
,
796 RemoteOperationResult result
) {
797 dismissLoadingDialog();
798 if (result
.isSuccess()) {
802 Toast t
= Toast
.makeText(this,
803 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
809 private void onUnshareLinkOperationFinish(UnshareOperation operation
,
810 RemoteOperationResult result
) {
811 dismissLoadingDialog();
813 if (result
.isSuccess()){
817 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
818 operation
, getResources()), Toast
.LENGTH_LONG
);
823 private void onSynchronizeFolderOperationFinish(
824 SynchronizeFolderOperation operation
, RemoteOperationResult result
826 if (!result
.isSuccess() && result
.getCode() != ResultCode
.CANCELLED
){
827 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
828 operation
, getResources()), Toast
.LENGTH_LONG
);
833 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
834 RemoteOperationResult result
) {
835 dismissLoadingDialog();
836 OCFile syncedFile
= operation
.getLocalFile();
837 if (!result
.isSuccess()) {
838 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
839 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
840 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
841 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
846 if (!operation
.transferWasRequested()) {
847 Toast msg
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
848 operation
, getResources()), Toast
.LENGTH_LONG
);
851 invalidateOptionsMenu();
855 protected void updateFileFromDB(){
856 OCFile file
= getFile();
858 file
= getStorageManager().getFileByPath(file
.getRemotePath());
865 * Show loading dialog
867 public void showLoadingDialog(String message
) {
869 LoadingDialog loading
= new LoadingDialog(message
);
870 FragmentManager fm
= getSupportFragmentManager();
871 FragmentTransaction ft
= fm
.beginTransaction();
872 loading
.show(ft
, DIALOG_WAIT_TAG
);
878 * Dismiss loading dialog
880 public void dismissLoadingDialog() {
881 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
883 LoadingDialog loading
= (LoadingDialog
) frag
;
889 private void doOnResumeAndBound() {
890 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
891 long waitingForOpId
= mFileOperationsHelper
.getOpIdWaitingFor();
892 if (waitingForOpId
<= Integer
.MAX_VALUE
) {
893 boolean wait
= mOperationsServiceBinder
.dispatchResultIfFinished((int)waitingForOpId
,
896 dismissLoadingDialog();
903 * Implements callback methods for service binding. Passed as a parameter to {
905 private class OperationsServiceConnection
implements ServiceConnection
{
908 public void onServiceConnected(ComponentName component
, IBinder service
) {
909 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
910 Log_OC
.d(TAG
, "Operations service connected");
911 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
912 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
913 dismissLoadingDialog();
915 doOnResumeAndBound();
924 public void onServiceDisconnected(ComponentName component
) {
925 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
926 Log_OC
.d(TAG
, "Operations service disconnected");
927 mOperationsServiceBinder
= null
;
928 // TODO whatever could be waiting for the service is unbound
935 public FileDownloaderBinder
getFileDownloaderBinder() {
936 return mDownloaderBinder
;
941 public FileUploaderBinder
getFileUploaderBinder() {
942 return mUploaderBinder
;
946 public void restart(){
947 Intent i
= new Intent(this, FileDisplayActivity
.class);
948 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
952 // TODO re-enable when "Accounts" is available in Navigation Drawer
953 // public void closeDrawer() {
954 // mDrawerLayout.closeDrawers();
957 public void allFilesOption(){
961 private class DrawerItemClickListener
implements ListView
.OnItemClickListener
{
963 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
964 // TODO re-enable when "Accounts" is available in Navigation Drawer
965 // if (mShowAccounts && position > 0){
966 // position = position - 1;
969 // TODO re-enable when "Accounts" is available in Navigation Drawer
970 // case 0: // Accounts
971 // mShowAccounts = !mShowAccounts;
972 // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
973 // mNavigationDrawerAdapter.notifyDataSetChanged();
978 mDrawerLayout
.closeDrawers();
981 // TODO Enable when "On Device" is recovered ?
983 // MainApp.showOnlyFilesOnDevice(true);
984 // mDrawerLayout.closeDrawers();
988 Intent settingsIntent
= new Intent(getApplicationContext(),
990 startActivity(settingsIntent
);
991 mDrawerLayout
.closeDrawers();
995 Intent loggerIntent
= new Intent(getApplicationContext(),
996 LogHistoryActivity
.class);
997 startActivity(loggerIntent
);
998 mDrawerLayout
.closeDrawers();