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";
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 getSupportActionBar().setTitle(savedInstanceState
.getString(KEY_ACTION_BAR_TITLE
));
188 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
189 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
190 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
,
194 AccountUtils
.updateAccountVersion(this); // best place, before any access to AccountManager
197 setAccount(account
, savedInstanceState
!= null
);
199 mOperationsServiceConnection
= new OperationsServiceConnection();
200 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
,
201 Context
.BIND_AUTO_CREATE
);
203 mDownloadServiceConnection
= newTransferenceServiceConnection();
204 if (mDownloadServiceConnection
!= null
) {
205 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
206 Context
.BIND_AUTO_CREATE
);
208 mUploadServiceConnection
= newTransferenceServiceConnection();
209 if (mUploadServiceConnection
!= null
) {
210 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
211 Context
.BIND_AUTO_CREATE
);
217 protected void onNewIntent (Intent intent
) {
218 Log_OC
.v(TAG
, "onNewIntent() start");
219 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
220 if (current
!= null
&& mAccount
!= null
&& !mAccount
.name
.equals(current
.name
)) {
223 Log_OC
.v(TAG
, "onNewIntent() stop");
227 * Since ownCloud {@link Account}s can be managed from the system setting menu,
228 * the existence of the {@link Account} associated to the instance must be checked
229 * every time it is restarted.
232 protected void onRestart() {
233 Log_OC
.v(TAG
, "onRestart() start");
235 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.exists(mAccount
, this));
237 swapToDefaultAccount();
239 Log_OC
.v(TAG
, "onRestart() end");
244 protected void onStart() {
247 if (mAccountWasSet
) {
248 onAccountSet(mAccountWasRestored
);
253 protected void onResume() {
256 if (mOperationsServiceBinder
!= null
) {
257 doOnResumeAndBound();
262 protected void onPause() {
263 if (mOperationsServiceBinder
!= null
) {
264 mOperationsServiceBinder
.removeOperationListener(this);
272 protected void onDestroy() {
273 if (mOperationsServiceConnection
!= null
) {
274 unbindService(mOperationsServiceConnection
);
275 mOperationsServiceBinder
= null
;
277 if (mDownloadServiceConnection
!= null
) {
278 unbindService(mDownloadServiceConnection
);
279 mDownloadServiceConnection
= null
;
281 if (mUploadServiceConnection
!= null
) {
282 unbindService(mUploadServiceConnection
);
283 mUploadServiceConnection
= null
;
290 protected void onPostCreate(Bundle savedInstanceState
) {
291 super.onPostCreate(savedInstanceState
);
292 // Sync the toggle state after onRestoreInstanceState has occurred.
293 if (mDrawerToggle
!= null
) {
294 mDrawerToggle
.syncState();
295 if (isDrawerOpen()) {
296 getSupportActionBar().setTitle(R
.string
.app_name
);
297 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
303 public void onConfigurationChanged(Configuration newConfig
) {
304 super.onConfigurationChanged(newConfig
);
305 if (mDrawerToggle
!= null
) {
306 mDrawerToggle
.onConfigurationChanged(newConfig
);
311 public void onBackPressed() {
312 if (isDrawerOpen()) {
316 super.onBackPressed();
320 * checks if the drawer exists and is opened.
322 * @return <code>true</code> if the drawer is open, else <code>false</code>
324 public boolean isDrawerOpen() {
325 if(mDrawerLayout
!= null
) {
326 return mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
333 * closes the navigation drawer.
335 public void closeNavDrawer() {
336 if(mDrawerLayout
!= null
) {
337 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
341 protected void initDrawer(){
342 // constant settings for action bar when navigation drawer is inited
343 getSupportActionBar().setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
346 mDrawerLayout
= (DrawerLayout
) findViewById(R
.id
.drawer_layout
);
347 // Notification Drawer
348 RelativeLayout navigationDrawerLayout
= (RelativeLayout
) findViewById(R
.id
.left_drawer
);
349 mDrawerList
= (ListView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_list
);
351 // TODO re-enable when "Accounts" is available in Navigation Drawer
352 // // load Account in the Drawer Title
354 // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
355 // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
358 // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
359 // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
361 // if (account != null) {
362 // int lastAtPos = account.name.lastIndexOf("@");
363 // username.setText(account.name.substring(0, lastAtPos));
366 // Display username in drawer
367 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
368 if (account
!= null
) {
369 TextView username
= (TextView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_username
);
370 int lastAtPos
= account
.name
.lastIndexOf("@");
371 username
.setText(account
.name
.substring(0, lastAtPos
));
374 // load slide menu items
375 mDrawerTitles
= getResources().getStringArray(R
.array
.drawer_items
);
377 // nav drawer content description from resources
378 mDrawerContentDescriptions
= getResources().
379 getStringArray(R
.array
.drawer_content_descriptions
);
382 mDrawerItems
= new ArrayList
<NavigationDrawerItem
>();
383 // adding nav drawer items to array
384 // TODO re-enable when "Accounts" is available in Navigation Drawer
386 // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
387 // mDrawerContentDescriptions[0]));
389 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[0], mDrawerContentDescriptions
[0],
390 R
.drawable
.ic_folder_open
));
392 // TODO Enable when "On Device" is recovered
394 //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
395 // mDrawerContentDescriptions[2]));
398 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[1], mDrawerContentDescriptions
[1],
399 R
.drawable
.ic_settings
));
401 if (BuildConfig
.DEBUG
) {
402 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[2],
403 mDrawerContentDescriptions
[2],R
.drawable
.ic_log
));
406 // setting the nav drawer list adapter
407 mNavigationDrawerAdapter
= new NavigationDrawerListAdapter(getApplicationContext(), this,
409 mDrawerList
.setAdapter(mNavigationDrawerAdapter
);
412 mDrawerToggle
= new ActionBarDrawerToggle(this, mDrawerLayout
,R
.string
.drawer_open
,R
.string
.drawer_close
) {
414 /** Called when a drawer has settled in a completely closed state. */
415 public void onDrawerClosed(View view
) {
416 super.onDrawerClosed(view
);
417 updateActionBarTitleAndHomeButton(null
);
418 invalidateOptionsMenu();
421 /** Called when a drawer has settled in a completely open state. */
422 public void onDrawerOpened(View drawerView
) {
423 super.onDrawerOpened(drawerView
);
424 getSupportActionBar().setTitle(R
.string
.app_name
);
425 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
426 invalidateOptionsMenu();
430 // Set the list's click listener
431 mDrawerList
.setOnItemClickListener(new DrawerItemClickListener());
433 // Set the drawer toggle as the DrawerListener
434 mDrawerLayout
.setDrawerListener(mDrawerToggle
);
435 mDrawerToggle
.setDrawerIndicatorEnabled(false
);
439 * Updates title bar and home buttons (state and icon).
441 * Assumes that navigation drawer is NOT visible.
443 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
444 String title
= getString(R
.string
.default_display_name_for_root_folder
); // default
447 /// choose the appropiate title
448 if (chosenFile
== null
) {
449 chosenFile
= mFile
; // if no file is passed, current file decides
452 chosenFile
== null
||
453 (chosenFile
.isFolder() && chosenFile
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
)
456 title
= chosenFile
.getFileName();
459 /// set the chosen title
460 ActionBar actionBar
= getSupportActionBar();
461 actionBar
.setTitle(title
);
462 /// also as content description
463 View actionBarTitleView
= getWindow().getDecorView().findViewById(
464 getResources().getIdentifier("action_bar_title", "id", "android")
466 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
467 actionBarTitleView
.setContentDescription(title
);
470 /// set home button properties
471 mDrawerToggle
.setDrawerIndicatorEnabled(inRoot
);
472 actionBar
.setDisplayHomeAsUpEnabled(true
);
473 actionBar
.setDisplayShowTitleEnabled(true
);
479 * Sets and validates the ownCloud {@link Account} associated to the Activity.
481 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
483 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
485 * @param account New {@link Account} to set.
486 * @param savedAccount When 'true', account was retrieved from a saved instance state.
488 protected void setAccount(Account account
, boolean savedAccount
) {
489 Account oldAccount
= mAccount
;
490 boolean validAccount
=
491 (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(),
495 mAccountWasSet
= true
;
496 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
499 swapToDefaultAccount();
505 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
507 * If no valid ownCloud {@link Account} exists, the the user is requested
508 * to create a new ownCloud {@link Account}.
510 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
512 private void swapToDefaultAccount() {
513 // default to the most recently used account
514 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
515 if (newAccount
== null
) {
516 /// no account available: force account creation
517 createFirstAccount();
518 mRedirectingToSetupAccount
= true
;
519 mAccountWasSet
= false
;
520 mAccountWasRestored
= false
;
523 mAccountWasSet
= true
;
524 mAccountWasRestored
= (newAccount
.equals(mAccount
));
525 mAccount
= newAccount
;
531 * Launches the account creation activity. To use when no ownCloud account is available
533 private void createFirstAccount() {
534 AccountManager am
= AccountManager
.get(getApplicationContext());
535 am
.addAccount(MainApp
.getAccountType(),
540 new AccountCreationCallback(),
549 protected void onSaveInstanceState(Bundle outState
) {
550 super.onSaveInstanceState(outState
);
551 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
552 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
553 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mFileOperationsHelper
.getOpIdWaitingFor());
554 outState
.putBoolean(KEY_TRY_SHARE_AGAIN
, mTryShareAgain
);
555 if(getSupportActionBar().getTitle() != null
) {
556 // Null check in case the actionbar is used in ActionBar.NAVIGATION_MODE_LIST
557 // since it doesn't have a title then
558 outState
.putString(KEY_ACTION_BAR_TITLE
, getSupportActionBar().getTitle().toString());
564 * Getter for the main {@link OCFile} handled by the activity.
566 * @return Main {@link OCFile} handled by the activity.
568 public OCFile
getFile() {
574 * Setter for the main {@link OCFile} handled by the activity.
576 * @param file Main {@link OCFile} to be handled by the activity.
578 public void setFile(OCFile file
) {
584 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
587 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
590 public Account
getAccount() {
594 protected void setAccount(Account account
) {
599 * @return Value of mFromNotification: True if the Activity is launched by a notification
601 public boolean fromNotification() {
602 return mFromNotification
;
606 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
608 protected boolean isRedirectingToSetupAccount() {
609 return mRedirectingToSetupAccount
;
612 public boolean isTryShareAgain(){
613 return mTryShareAgain
;
616 public void setTryShareAgain(boolean tryShareAgain
) {
617 mTryShareAgain
= tryShareAgain
;
620 public OperationsServiceBinder
getOperationsServiceBinder() {
621 return mOperationsServiceBinder
;
624 protected ServiceConnection
newTransferenceServiceConnection() {
629 * Helper class handling a callback from the {@link AccountManager} after the creation of
630 * a new ownCloud {@link Account} finished, successfully or not.
632 * At this moment, only called after the creation of the first account.
634 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
637 public void run(AccountManagerFuture
<Bundle
> future
) {
638 FileActivity
.this.mRedirectingToSetupAccount
= false
;
639 boolean accountWasSet
= false
;
640 if (future
!= null
) {
643 result
= future
.getResult();
644 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
645 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
646 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
647 setAccount(new Account(name
, type
), false
);
648 accountWasSet
= true
;
650 } catch (OperationCanceledException e
) {
651 Log_OC
.d(TAG
, "Account creation canceled");
653 } catch (Exception e
) {
654 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
658 Log_OC
.e(TAG
, "Account creation callback with null bundle");
660 if (!accountWasSet
) {
661 moveTaskToBack(true
);
669 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
671 * Child classes must grant that state depending on the {@link Account} is updated.
673 protected void onAccountSet(boolean stateWasRecovered
) {
674 if (getAccount() != null
) {
675 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
678 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
683 public FileDataStorageManager
getStorageManager() {
684 return mStorageManager
;
688 public OnRemoteOperationListener
getRemoteOperationListener() {
693 public Handler
getHandler() {
697 public FileOperationsHelper
getFileOperationsHelper() {
698 return mFileOperationsHelper
;
703 * @param operation Removal operation performed.
704 * @param result Result of the removal.
707 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
708 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the "
709 + "FileActivities ");
711 mFileOperationsHelper
.setOpIdWaitingFor(Long
.MAX_VALUE
);
713 if (!result
.isSuccess() && (
714 result
.getCode() == ResultCode
.UNAUTHORIZED
||
715 result
.isIdPRedirection() ||
716 (result
.isException() && result
.getException() instanceof AuthenticatorException
)
719 requestCredentialsUpdate();
721 if (result
.getCode() == ResultCode
.UNAUTHORIZED
) {
722 dismissLoadingDialog();
723 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
724 operation
, getResources()),
728 mTryShareAgain
= false
;
730 } else if (operation
instanceof CreateShareOperation
) {
731 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
733 } else if (operation
instanceof UnshareLinkOperation
) {
734 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
736 } else if (operation
instanceof SynchronizeFolderOperation
) {
737 onSynchronizeFolderOperationFinish((SynchronizeFolderOperation
)operation
, result
);
739 }else if (operation
instanceof SynchronizeFileOperation
) {
740 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
745 protected void requestCredentialsUpdate() {
746 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
747 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
748 updateAccountCredentials
.putExtra(
749 AuthenticatorActivity
.EXTRA_ACTION
,
750 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
);
751 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
752 startActivity(updateAccountCredentials
);
757 private void onCreateShareOperationFinish(CreateShareOperation operation
,
758 RemoteOperationResult result
) {
759 dismissLoadingDialog();
760 if (result
.isSuccess()) {
761 mTryShareAgain
= false
;
764 Intent sendIntent
= operation
.getSendIntent();
765 startActivity(sendIntent
);
767 // Detect Failure (403) --> needs Password
768 if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
769 if (!isTryShareAgain()) {
770 SharePasswordDialogFragment dialog
=
771 SharePasswordDialogFragment
.newInstance(new OCFile(operation
.getPath()),
772 operation
.getSendIntent());
773 dialog
.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD
);
775 Toast t
= Toast
.makeText(this,
776 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
779 mTryShareAgain
= false
;
782 Toast t
= Toast
.makeText(this,
783 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
791 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
792 RemoteOperationResult result
) {
793 dismissLoadingDialog();
795 if (result
.isSuccess()){
799 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
800 operation
, getResources()), Toast
.LENGTH_LONG
);
805 private void onSynchronizeFolderOperationFinish(
806 SynchronizeFolderOperation operation
, RemoteOperationResult result
808 if (!result
.isSuccess() && result
.getCode() != ResultCode
.CANCELLED
){
809 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
810 operation
, getResources()), Toast
.LENGTH_LONG
);
815 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
816 RemoteOperationResult result
) {
817 dismissLoadingDialog();
818 OCFile syncedFile
= operation
.getLocalFile();
819 if (!result
.isSuccess()) {
820 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
821 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
822 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
823 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
828 if (!operation
.transferWasRequested()) {
829 Toast msg
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
830 operation
, getResources()), Toast
.LENGTH_LONG
);
833 invalidateOptionsMenu();
837 protected void updateFileFromDB(){
838 OCFile file
= getFile();
840 file
= getStorageManager().getFileByPath(file
.getRemotePath());
847 * Show loading dialog
849 public void showLoadingDialog() {
851 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
852 FragmentManager fm
= getSupportFragmentManager();
853 FragmentTransaction ft
= fm
.beginTransaction();
854 loading
.show(ft
, DIALOG_WAIT_TAG
);
860 * Dismiss loading dialog
862 public void dismissLoadingDialog() {
863 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
865 LoadingDialog loading
= (LoadingDialog
) frag
;
871 private void doOnResumeAndBound() {
872 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
873 long waitingForOpId
= mFileOperationsHelper
.getOpIdWaitingFor();
874 if (waitingForOpId
<= Integer
.MAX_VALUE
) {
875 boolean wait
= mOperationsServiceBinder
.dispatchResultIfFinished((int)waitingForOpId
,
878 dismissLoadingDialog();
885 * Implements callback methods for service binding. Passed as a parameter to {
887 private class OperationsServiceConnection
implements ServiceConnection
{
890 public void onServiceConnected(ComponentName component
, IBinder service
) {
891 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
892 Log_OC
.d(TAG
, "Operations service connected");
893 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
894 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
895 dismissLoadingDialog();
897 doOnResumeAndBound();
906 public void onServiceDisconnected(ComponentName component
) {
907 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
908 Log_OC
.d(TAG
, "Operations service disconnected");
909 mOperationsServiceBinder
= null
;
910 // TODO whatever could be waiting for the service is unbound
917 public FileDownloaderBinder
getFileDownloaderBinder() {
918 return mDownloaderBinder
;
923 public FileUploaderBinder
getFileUploaderBinder() {
924 return mUploaderBinder
;
928 public void restart(){
929 Intent i
= new Intent(this, FileDisplayActivity
.class);
930 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
934 // TODO re-enable when "Accounts" is available in Navigation Drawer
935 // public void closeDrawer() {
936 // mDrawerLayout.closeDrawers();
939 public void allFilesOption(){
943 private class DrawerItemClickListener
implements ListView
.OnItemClickListener
{
945 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
946 // TODO re-enable when "Accounts" is available in Navigation Drawer
947 // if (mShowAccounts && position > 0){
948 // position = position - 1;
951 // TODO re-enable when "Accounts" is available in Navigation Drawer
952 // case 0: // Accounts
953 // mShowAccounts = !mShowAccounts;
954 // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
955 // mNavigationDrawerAdapter.notifyDataSetChanged();
960 mDrawerLayout
.closeDrawers();
963 // TODO Enable when "On Device" is recovered ?
965 // MainApp.showOnlyFilesOnDevice(true);
966 // mDrawerLayout.closeDrawers();
970 Intent settingsIntent
= new Intent(getApplicationContext(),
972 startActivity(settingsIntent
);
973 mDrawerLayout
.closeDrawers();
977 Intent loggerIntent
= new Intent(getApplicationContext(),
978 LogHistoryActivity
.class);
979 startActivity(loggerIntent
);
980 mDrawerLayout
.closeDrawers();