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
.lib
.resources
.status
.OCCapability
;
71 import com
.owncloud
.android
.operations
.CreateShareViaLinkOperation
;
72 import com
.owncloud
.android
.operations
.CreateShareWithShareeOperation
;
73 import com
.owncloud
.android
.operations
.GetSharesForFileOperation
;
74 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
75 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
76 import com
.owncloud
.android
.operations
.UnshareOperation
;
77 import com
.owncloud
.android
.operations
.UpdateShareViaLinkOperation
;
78 import com
.owncloud
.android
.services
.OperationsService
;
79 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
80 import com
.owncloud
.android
.ui
.NavigationDrawerItem
;
81 import com
.owncloud
.android
.ui
.adapter
.NavigationDrawerListAdapter
;
82 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
83 import com
.owncloud
.android
.ui
.dialog
.SharePasswordDialogFragment
;
84 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
86 import java
.util
.ArrayList
;
90 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud
93 public class FileActivity
extends AppCompatActivity
94 implements OnRemoteOperationListener
, ComponentsGetter
{
96 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
97 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
98 public static final String EXTRA_FROM_NOTIFICATION
=
99 "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
101 public static final String TAG
= FileActivity
.class.getSimpleName();
103 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
105 private static final String KEY_WAITING_FOR_OP_ID
= "WAITING_FOR_OP_ID";
106 private static final String DIALOG_SHARE_PASSWORD
= "DIALOG_SHARE_PASSWORD";
107 private static final String KEY_TRY_SHARE_AGAIN
= "TRY_SHARE_AGAIN";
108 private static final String KEY_ACTION_BAR_TITLE
= "ACTION_BAR_TITLE";
110 protected static final long DELAY_TO_REQUEST_OPERATIONS_LATER
= 200;
113 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.*/
114 private Account mAccount
;
116 /** Capabilites of the server where {@link #mAccount} lives */
117 private OCCapability mCapabilities
;
119 /** Main {@link OCFile} handled by the activity.*/
120 private OCFile mFile
;
123 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud
125 private boolean mRedirectingToSetupAccount
= false
;
127 /** Flag to signal when the value of mAccount was set */
128 protected boolean mAccountWasSet
;
130 /** Flag to signal when the value of mAccount was restored from a saved state */
131 protected boolean mAccountWasRestored
;
133 /** Flag to signal if the activity is launched by a notification */
134 private boolean mFromNotification
;
136 /** Messages handler associated to the main thread and the life cycle of the activity */
137 private Handler mHandler
;
139 /** Access point to the cached database for the current ownCloud {@link Account} */
140 private FileDataStorageManager mStorageManager
= null
;
142 private FileOperationsHelper mFileOperationsHelper
;
144 private ServiceConnection mOperationsServiceConnection
= null
;
146 private OperationsServiceBinder mOperationsServiceBinder
= null
;
148 private boolean mResumed
= false
;
150 protected FileDownloaderBinder mDownloaderBinder
= null
;
151 protected FileUploaderBinder mUploaderBinder
= null
;
152 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
155 protected DrawerLayout mDrawerLayout
;
156 protected ActionBarDrawerToggle mDrawerToggle
;
157 protected ListView mDrawerList
;
160 protected String
[] mDrawerTitles
;
161 protected String
[] mDrawerContentDescriptions
;
163 protected ArrayList
<NavigationDrawerItem
> mDrawerItems
;
165 protected NavigationDrawerListAdapter mNavigationDrawerAdapter
= null
;
169 // TODO re-enable when "Accounts" is available in Navigation Drawer
170 // protected boolean mShowAccounts = false;
173 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
174 * the {@link FileActivity}.
176 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
177 * is requested to create a new one.
180 protected void onCreate(Bundle savedInstanceState
) {
181 super.onCreate(savedInstanceState
);
182 mHandler
= new Handler();
183 mFileOperationsHelper
= new FileOperationsHelper(this);
184 Account account
= null
;
185 if(savedInstanceState
!= null
) {
186 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
187 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
188 mFileOperationsHelper
.setOpIdWaitingFor(
189 savedInstanceState
.getLong(KEY_WAITING_FOR_OP_ID
, Long
.MAX_VALUE
)
191 if (getSupportActionBar() != null
) {
192 getSupportActionBar().setTitle(savedInstanceState
.getString(KEY_ACTION_BAR_TITLE
));
195 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
196 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
197 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
,
201 AccountUtils
.updateAccountVersion(this); // best place, before any access to AccountManager
204 setAccount(account
, savedInstanceState
!= null
);
206 mOperationsServiceConnection
= new OperationsServiceConnection();
207 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
,
208 Context
.BIND_AUTO_CREATE
);
210 mDownloadServiceConnection
= newTransferenceServiceConnection();
211 if (mDownloadServiceConnection
!= null
) {
212 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
213 Context
.BIND_AUTO_CREATE
);
215 mUploadServiceConnection
= newTransferenceServiceConnection();
216 if (mUploadServiceConnection
!= null
) {
217 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
218 Context
.BIND_AUTO_CREATE
);
224 protected void onNewIntent (Intent intent
) {
225 Log_OC
.v(TAG
, "onNewIntent() start");
226 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
227 if (current
!= null
&& mAccount
!= null
&& !mAccount
.name
.equals(current
.name
)) {
230 Log_OC
.v(TAG
, "onNewIntent() stop");
234 * Since ownCloud {@link Account}s can be managed from the system setting menu,
235 * the existence of the {@link Account} associated to the instance must be checked
236 * every time it is restarted.
239 protected void onRestart() {
240 Log_OC
.v(TAG
, "onRestart() start");
242 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.exists(mAccount
, this));
244 swapToDefaultAccount();
246 Log_OC
.v(TAG
, "onRestart() end");
251 protected void onStart() {
254 if (mAccountWasSet
) {
255 onAccountSet(mAccountWasRestored
);
260 protected void onResume() {
263 if (mOperationsServiceBinder
!= null
) {
264 doOnResumeAndBound();
269 protected void onPause() {
270 if (mOperationsServiceBinder
!= null
) {
271 mOperationsServiceBinder
.removeOperationListener(this);
279 protected void onDestroy() {
280 if (mOperationsServiceConnection
!= null
) {
281 unbindService(mOperationsServiceConnection
);
282 mOperationsServiceBinder
= null
;
284 if (mDownloadServiceConnection
!= null
) {
285 unbindService(mDownloadServiceConnection
);
286 mDownloadServiceConnection
= null
;
288 if (mUploadServiceConnection
!= null
) {
289 unbindService(mUploadServiceConnection
);
290 mUploadServiceConnection
= null
;
297 protected void onPostCreate(Bundle savedInstanceState
) {
298 super.onPostCreate(savedInstanceState
);
299 // Sync the toggle state after onRestoreInstanceState has occurred.
300 if (mDrawerToggle
!= null
) {
301 mDrawerToggle
.syncState();
302 if (isDrawerOpen()) {
303 getSupportActionBar().setTitle(R
.string
.app_name
);
304 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
310 public void onConfigurationChanged(Configuration newConfig
) {
311 super.onConfigurationChanged(newConfig
);
312 if (mDrawerToggle
!= null
) {
313 mDrawerToggle
.onConfigurationChanged(newConfig
);
318 public void onBackPressed() {
319 if (isDrawerOpen()) {
323 super.onBackPressed();
327 * checks if the drawer exists and is opened.
329 * @return <code>true</code> if the drawer is open, else <code>false</code>
331 public boolean isDrawerOpen() {
332 if(mDrawerLayout
!= null
) {
333 return mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
340 * closes the navigation drawer.
342 public void closeNavDrawer() {
343 if(mDrawerLayout
!= null
) {
344 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
348 protected void initDrawer(){
349 // constant settings for action bar when navigation drawer is inited
350 getSupportActionBar().setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
353 mDrawerLayout
= (DrawerLayout
) findViewById(R
.id
.drawer_layout
);
354 // Notification Drawer
355 RelativeLayout navigationDrawerLayout
= (RelativeLayout
) findViewById(R
.id
.left_drawer
);
356 mDrawerList
= (ListView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_list
);
358 // TODO re-enable when "Accounts" is available in Navigation Drawer
359 // // load Account in the Drawer Title
361 // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
362 // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
365 // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
366 // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
368 // if (account != null) {
369 // int lastAtPos = account.name.lastIndexOf("@");
370 // username.setText(account.name.substring(0, lastAtPos));
373 // Display username in drawer
374 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
375 if (account
!= null
) {
376 TextView username
= (TextView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_username
);
377 int lastAtPos
= account
.name
.lastIndexOf("@");
378 username
.setText(account
.name
.substring(0, lastAtPos
));
381 // load slide menu items
382 mDrawerTitles
= getResources().getStringArray(R
.array
.drawer_items
);
384 // nav drawer content description from resources
385 mDrawerContentDescriptions
= getResources().
386 getStringArray(R
.array
.drawer_content_descriptions
);
389 mDrawerItems
= new ArrayList
<NavigationDrawerItem
>();
390 // adding nav drawer items to array
391 // TODO re-enable when "Accounts" is available in Navigation Drawer
393 // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
394 // mDrawerContentDescriptions[0]));
396 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[0], mDrawerContentDescriptions
[0],
397 R
.drawable
.ic_folder_open
));
399 // TODO Enable when "On Device" is recovered
401 //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
402 // mDrawerContentDescriptions[2]));
405 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[1], mDrawerContentDescriptions
[1],
406 R
.drawable
.ic_settings
));
408 if (BuildConfig
.DEBUG
) {
409 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[2],
410 mDrawerContentDescriptions
[2],R
.drawable
.ic_log
));
413 // setting the nav drawer list adapter
414 mNavigationDrawerAdapter
= new NavigationDrawerListAdapter(getApplicationContext(), this,
416 mDrawerList
.setAdapter(mNavigationDrawerAdapter
);
419 mDrawerToggle
= new ActionBarDrawerToggle(this, mDrawerLayout
,R
.string
.drawer_open
,R
.string
.drawer_close
) {
421 /** Called when a drawer has settled in a completely closed state. */
422 public void onDrawerClosed(View view
) {
423 super.onDrawerClosed(view
);
424 updateActionBarTitleAndHomeButton(null
);
425 invalidateOptionsMenu();
428 /** Called when a drawer has settled in a completely open state. */
429 public void onDrawerOpened(View drawerView
) {
430 super.onDrawerOpened(drawerView
);
431 getSupportActionBar().setTitle(R
.string
.app_name
);
432 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
433 invalidateOptionsMenu();
437 // Set the list's click listener
438 mDrawerList
.setOnItemClickListener(new DrawerItemClickListener());
440 // Set the drawer toggle as the DrawerListener
441 mDrawerLayout
.setDrawerListener(mDrawerToggle
);
442 mDrawerToggle
.setDrawerIndicatorEnabled(false
);
446 * Updates title bar and home buttons (state and icon).
448 * Assumes that navigation drawer is NOT visible.
450 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
451 String title
= getString(R
.string
.default_display_name_for_root_folder
); // default
454 /// choose the appropiate title
455 if (chosenFile
== null
) {
456 chosenFile
= mFile
; // if no file is passed, current file decides
459 chosenFile
== null
||
460 (chosenFile
.isFolder() && chosenFile
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
)
463 title
= chosenFile
.getFileName();
466 /// set the chosen title
467 ActionBar actionBar
= getSupportActionBar();
468 actionBar
.setTitle(title
);
469 /// also as content description
470 View actionBarTitleView
= getWindow().getDecorView().findViewById(
471 getResources().getIdentifier("action_bar_title", "id", "android")
473 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
474 actionBarTitleView
.setContentDescription(title
);
477 /// set home button properties
478 mDrawerToggle
.setDrawerIndicatorEnabled(inRoot
);
479 actionBar
.setDisplayHomeAsUpEnabled(true
);
480 actionBar
.setDisplayShowTitleEnabled(true
);
486 * Sets and validates the ownCloud {@link Account} associated to the Activity.
488 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
490 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
492 * @param account New {@link Account} to set.
493 * @param savedAccount When 'true', account was retrieved from a saved instance state.
495 protected void setAccount(Account account
, boolean savedAccount
) {
496 Account oldAccount
= mAccount
;
497 boolean validAccount
=
498 (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(),
502 mAccountWasSet
= true
;
503 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
506 swapToDefaultAccount();
512 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
514 * If no valid ownCloud {@link Account} exists, the the user is requested
515 * to create a new ownCloud {@link Account}.
517 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
519 private void swapToDefaultAccount() {
520 // default to the most recently used account
521 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
522 if (newAccount
== null
) {
523 /// no account available: force account creation
524 createFirstAccount();
525 mRedirectingToSetupAccount
= true
;
526 mAccountWasSet
= false
;
527 mAccountWasRestored
= false
;
530 mAccountWasSet
= true
;
531 mAccountWasRestored
= (newAccount
.equals(mAccount
));
532 mAccount
= newAccount
;
538 * Launches the account creation activity. To use when no ownCloud account is available
540 private void createFirstAccount() {
541 AccountManager am
= AccountManager
.get(getApplicationContext());
542 am
.addAccount(MainApp
.getAccountType(),
547 new AccountCreationCallback(),
556 protected void onSaveInstanceState(Bundle outState
) {
557 super.onSaveInstanceState(outState
);
558 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
559 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
560 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mFileOperationsHelper
.getOpIdWaitingFor());
561 if(getSupportActionBar() != null
&& getSupportActionBar().getTitle() != null
) {
562 // Null check in case the actionbar is used in ActionBar.NAVIGATION_MODE_LIST
563 // since it doesn't have a title then
564 outState
.putString(KEY_ACTION_BAR_TITLE
, getSupportActionBar().getTitle().toString());
570 * Getter for the main {@link OCFile} handled by the activity.
572 * @return Main {@link OCFile} handled by the activity.
574 public OCFile
getFile() {
580 * Setter for the main {@link OCFile} handled by the activity.
582 * @param file Main {@link OCFile} to be handled by the activity.
584 public void setFile(OCFile file
) {
590 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
593 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
596 public Account
getAccount() {
600 protected void setAccount(Account account
) {
606 * Getter for the capabilities of the server where the current OC account lives.
608 * @return Capabilities of the server where the current OC account lives. Null if the account is not
611 public OCCapability
getCapabilities() {
612 return mCapabilities
;
617 * @return Value of mFromNotification: True if the Activity is launched by a notification
619 public boolean fromNotification() {
620 return mFromNotification
;
624 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
626 protected boolean isRedirectingToSetupAccount() {
627 return mRedirectingToSetupAccount
;
630 public OperationsServiceBinder
getOperationsServiceBinder() {
631 return mOperationsServiceBinder
;
634 protected ServiceConnection
newTransferenceServiceConnection() {
639 * Helper class handling a callback from the {@link AccountManager} after the creation of
640 * a new ownCloud {@link Account} finished, successfully or not.
642 * At this moment, only called after the creation of the first account.
644 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
647 public void run(AccountManagerFuture
<Bundle
> future
) {
648 FileActivity
.this.mRedirectingToSetupAccount
= false
;
649 boolean accountWasSet
= false
;
650 if (future
!= null
) {
653 result
= future
.getResult();
654 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
655 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
656 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
657 setAccount(new Account(name
, type
), false
);
658 accountWasSet
= true
;
660 } catch (OperationCanceledException e
) {
661 Log_OC
.d(TAG
, "Account creation canceled");
663 } catch (Exception e
) {
664 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
668 Log_OC
.e(TAG
, "Account creation callback with null bundle");
670 if (!accountWasSet
) {
671 moveTaskToBack(true
);
679 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
681 * Child classes must grant that state depending on the {@link Account} is updated.
683 protected void onAccountSet(boolean stateWasRecovered
) {
684 if (getAccount() != null
) {
685 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
686 mCapabilities
= mStorageManager
.getCapability(mAccount
.name
);
689 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
694 public FileDataStorageManager
getStorageManager() {
695 return mStorageManager
;
699 public OnRemoteOperationListener
getRemoteOperationListener() {
704 public Handler
getHandler() {
708 public FileOperationsHelper
getFileOperationsHelper() {
709 return mFileOperationsHelper
;
714 * @param operation Removal operation performed.
715 * @param result Result of the removal.
718 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
719 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the "
720 + "FileActivities ");
722 mFileOperationsHelper
.setOpIdWaitingFor(Long
.MAX_VALUE
);
724 dismissLoadingDialog();
726 if (!result
.isSuccess() && (
727 result
.getCode() == ResultCode
.UNAUTHORIZED
||
728 result
.isIdPRedirection() ||
729 (result
.isException() && result
.getException() instanceof AuthenticatorException
)
732 requestCredentialsUpdate();
734 if (result
.getCode() == ResultCode
.UNAUTHORIZED
) {
735 dismissLoadingDialog();
736 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
737 operation
, getResources()),
742 } else if (operation
== null
||
743 operation
instanceof CreateShareWithShareeOperation
||
744 operation
instanceof UnshareOperation
||
745 operation
instanceof SynchronizeFolderOperation
||
746 operation
instanceof UpdateShareViaLinkOperation
748 if (result
.isSuccess()) {
751 } else if (result
.getCode() != ResultCode
.CANCELLED
) {
752 Toast t
= Toast
.makeText(this,
753 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
758 } else if (operation
instanceof CreateShareViaLinkOperation
) {
759 onCreateShareViaLinkOperationFinish((CreateShareViaLinkOperation
) operation
, result
);
761 } else if (operation
instanceof SynchronizeFileOperation
) {
762 onSynchronizeFileOperationFinish((SynchronizeFileOperation
) operation
, result
);
764 } else if (operation
instanceof GetSharesForFileOperation
) {
765 if (result
.isSuccess()) {
768 } else if (result
.getCode() != ResultCode
.SHARE_NOT_FOUND
) {
769 Toast t
= Toast
.makeText(this,
770 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
777 protected void requestCredentialsUpdate() {
778 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
779 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
780 updateAccountCredentials
.putExtra(
781 AuthenticatorActivity
.EXTRA_ACTION
,
782 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
);
783 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
784 startActivity(updateAccountCredentials
);
789 private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation operation
,
790 RemoteOperationResult result
) {
791 if (result
.isSuccess()) {
794 Intent sendIntent
= operation
.getSendIntentWithSubject(this);
795 if (sendIntent
!= null
) {
796 startActivity(sendIntent
);
800 // Detect Failure (403) --> needs Password
801 if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
802 String password
= operation
.getPassword();
803 if ((password
== null
|| password
.length() == 0) &&
804 !getCapabilities().getFilesSharingPublicEnabled().isFalse())
806 // Was tried without password, but not sure that it's optional. Try with password.
807 // Try with password before giving up.
808 // See also ShareFileFragment#OnShareViaLinkListener
809 SharePasswordDialogFragment dialog
=
810 SharePasswordDialogFragment
.newInstance(new OCFile(operation
.getPath()), true
);
811 dialog
.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD
);
813 Toast t
= Toast
.makeText(this,
814 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
819 Toast t
= Toast
.makeText(this,
820 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
827 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
828 RemoteOperationResult result
) {
829 OCFile syncedFile
= operation
.getLocalFile();
830 if (!result
.isSuccess()) {
831 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
832 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
833 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
834 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
839 if (!operation
.transferWasRequested()) {
840 Toast msg
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
841 operation
, getResources()), Toast
.LENGTH_LONG
);
844 invalidateOptionsMenu();
848 protected void updateFileFromDB(){
849 OCFile file
= getFile();
851 file
= getStorageManager().getFileByPath(file
.getRemotePath());
858 * Show loading dialog
860 public void showLoadingDialog(String message
) {
862 LoadingDialog loading
= new LoadingDialog(message
);
863 FragmentManager fm
= getSupportFragmentManager();
864 FragmentTransaction ft
= fm
.beginTransaction();
865 loading
.show(ft
, DIALOG_WAIT_TAG
);
871 * Dismiss loading dialog
873 public void dismissLoadingDialog() {
874 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
876 LoadingDialog loading
= (LoadingDialog
) frag
;
882 private void doOnResumeAndBound() {
883 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
884 long waitingForOpId
= mFileOperationsHelper
.getOpIdWaitingFor();
885 if (waitingForOpId
<= Integer
.MAX_VALUE
) {
886 boolean wait
= mOperationsServiceBinder
.dispatchResultIfFinished((int)waitingForOpId
,
889 dismissLoadingDialog();
896 * Implements callback methods for service binding. Passed as a parameter to {
898 private class OperationsServiceConnection
implements ServiceConnection
{
901 public void onServiceConnected(ComponentName component
, IBinder service
) {
902 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
903 Log_OC
.d(TAG
, "Operations service connected");
904 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
905 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
906 dismissLoadingDialog();
909 doOnResumeAndBound();
919 public void onServiceDisconnected(ComponentName component
) {
920 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
921 Log_OC
.d(TAG
, "Operations service disconnected");
922 mOperationsServiceBinder
= null
;
923 // TODO whatever could be waiting for the service is unbound
930 public FileDownloaderBinder
getFileDownloaderBinder() {
931 return mDownloaderBinder
;
936 public FileUploaderBinder
getFileUploaderBinder() {
937 return mUploaderBinder
;
941 public void restart(){
942 Intent i
= new Intent(this, FileDisplayActivity
.class);
943 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
947 // TODO re-enable when "Accounts" is available in Navigation Drawer
948 // public void closeDrawer() {
949 // mDrawerLayout.closeDrawers();
952 public void allFilesOption(){
956 private class DrawerItemClickListener
implements ListView
.OnItemClickListener
{
958 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
959 // TODO re-enable when "Accounts" is available in Navigation Drawer
960 // if (mShowAccounts && position > 0){
961 // position = position - 1;
964 // TODO re-enable when "Accounts" is available in Navigation Drawer
965 // case 0: // Accounts
966 // mShowAccounts = !mShowAccounts;
967 // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
968 // mNavigationDrawerAdapter.notifyDataSetChanged();
973 mDrawerLayout
.closeDrawers();
976 // TODO Enable when "On Device" is recovered ?
978 // MainApp.showOnlyFilesOnDevice(true);
979 // mDrawerLayout.closeDrawers();
983 Intent settingsIntent
= new Intent(getApplicationContext(),
985 startActivity(settingsIntent
);
986 mDrawerLayout
.closeDrawers();
990 Intent loggerIntent
= new Intent(getApplicationContext(),
991 LogHistoryActivity
.class);
992 startActivity(loggerIntent
);
993 mDrawerLayout
.closeDrawers();