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
.GetSharesForFileOperation
;
73 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
74 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
75 import com
.owncloud
.android
.operations
.UnshareOperation
;
76 import com
.owncloud
.android
.services
.OperationsService
;
77 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
78 import com
.owncloud
.android
.ui
.NavigationDrawerItem
;
79 import com
.owncloud
.android
.ui
.adapter
.NavigationDrawerListAdapter
;
80 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
81 import com
.owncloud
.android
.ui
.dialog
.SharePasswordDialogFragment
;
82 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
84 import java
.util
.ArrayList
;
88 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud
91 public class FileActivity
extends AppCompatActivity
92 implements OnRemoteOperationListener
, ComponentsGetter
{
94 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
95 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
96 public static final String EXTRA_WAITING_TO_PREVIEW
=
97 "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
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 /** Main {@link OCFile} handled by the activity.*/
117 private OCFile mFile
;
119 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud
121 private boolean mRedirectingToSetupAccount
= false
;
123 /** Flag to signal when the value of mAccount was set */
124 protected boolean mAccountWasSet
;
126 /** Flag to signal when the value of mAccount was restored from a saved state */
127 protected boolean mAccountWasRestored
;
129 /** Flag to signal if the activity is launched by a notification */
130 private boolean mFromNotification
;
132 /** Messages handler associated to the main thread and the life cycle of the activity */
133 private Handler mHandler
;
135 /** Access point to the cached database for the current ownCloud {@link Account} */
136 private FileDataStorageManager mStorageManager
= null
;
138 private FileOperationsHelper mFileOperationsHelper
;
140 private ServiceConnection mOperationsServiceConnection
= null
;
142 private OperationsServiceBinder mOperationsServiceBinder
= null
;
144 protected FileDownloaderBinder mDownloaderBinder
= null
;
145 protected FileUploaderBinder mUploaderBinder
= null
;
146 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
148 private boolean mTryShareAgain
= false
;
151 protected DrawerLayout mDrawerLayout
;
152 protected ActionBarDrawerToggle mDrawerToggle
;
153 protected ListView mDrawerList
;
156 protected String
[] mDrawerTitles
;
157 protected String
[] mDrawerContentDescriptions
;
159 protected ArrayList
<NavigationDrawerItem
> mDrawerItems
;
161 protected NavigationDrawerListAdapter mNavigationDrawerAdapter
= null
;
164 // TODO re-enable when "Accounts" is available in Navigation Drawer
165 // protected boolean mShowAccounts = false;
168 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
169 * the {@link FileActivity}.
171 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
172 * is requested to create a new one.
175 protected void onCreate(Bundle savedInstanceState
) {
176 super.onCreate(savedInstanceState
);
177 mHandler
= new Handler();
178 mFileOperationsHelper
= new FileOperationsHelper(this);
179 Account account
= null
;
180 if(savedInstanceState
!= null
) {
181 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
182 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
183 mFileOperationsHelper
.setOpIdWaitingFor(
184 savedInstanceState
.getLong(KEY_WAITING_FOR_OP_ID
, Long
.MAX_VALUE
)
186 mTryShareAgain
= savedInstanceState
.getBoolean(KEY_TRY_SHARE_AGAIN
);
187 if (getSupportActionBar() != null
) {
188 getSupportActionBar().setTitle(savedInstanceState
.getString(KEY_ACTION_BAR_TITLE
));
191 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
192 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
193 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
,
197 AccountUtils
.updateAccountVersion(this); // best place, before any access to AccountManager
200 setAccount(account
, savedInstanceState
!= null
);
202 mOperationsServiceConnection
= new OperationsServiceConnection();
203 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
,
204 Context
.BIND_AUTO_CREATE
);
206 mDownloadServiceConnection
= newTransferenceServiceConnection();
207 if (mDownloadServiceConnection
!= null
) {
208 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
209 Context
.BIND_AUTO_CREATE
);
211 mUploadServiceConnection
= newTransferenceServiceConnection();
212 if (mUploadServiceConnection
!= null
) {
213 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
214 Context
.BIND_AUTO_CREATE
);
220 protected void onNewIntent (Intent intent
) {
221 Log_OC
.v(TAG
, "onNewIntent() start");
222 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
223 if (current
!= null
&& mAccount
!= null
&& !mAccount
.name
.equals(current
.name
)) {
226 Log_OC
.v(TAG
, "onNewIntent() stop");
230 * Since ownCloud {@link Account}s can be managed from the system setting menu,
231 * the existence of the {@link Account} associated to the instance must be checked
232 * every time it is restarted.
235 protected void onRestart() {
236 Log_OC
.v(TAG
, "onRestart() start");
238 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.exists(mAccount
, this));
240 swapToDefaultAccount();
242 Log_OC
.v(TAG
, "onRestart() end");
247 protected void onStart() {
250 if (mAccountWasSet
) {
251 onAccountSet(mAccountWasRestored
);
256 protected void onResume() {
259 if (mOperationsServiceBinder
!= null
) {
260 doOnResumeAndBound();
265 protected void onPause() {
266 if (mOperationsServiceBinder
!= null
) {
267 mOperationsServiceBinder
.removeOperationListener(this);
275 protected void onDestroy() {
276 if (mOperationsServiceConnection
!= null
) {
277 unbindService(mOperationsServiceConnection
);
278 mOperationsServiceBinder
= null
;
280 if (mDownloadServiceConnection
!= null
) {
281 unbindService(mDownloadServiceConnection
);
282 mDownloadServiceConnection
= null
;
284 if (mUploadServiceConnection
!= null
) {
285 unbindService(mUploadServiceConnection
);
286 mUploadServiceConnection
= null
;
293 protected void onPostCreate(Bundle savedInstanceState
) {
294 super.onPostCreate(savedInstanceState
);
295 // Sync the toggle state after onRestoreInstanceState has occurred.
296 if (mDrawerToggle
!= null
) {
297 mDrawerToggle
.syncState();
298 if (isDrawerOpen()) {
299 getSupportActionBar().setTitle(R
.string
.app_name
);
300 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
306 public void onConfigurationChanged(Configuration newConfig
) {
307 super.onConfigurationChanged(newConfig
);
308 if (mDrawerToggle
!= null
) {
309 mDrawerToggle
.onConfigurationChanged(newConfig
);
314 public void onBackPressed() {
315 if (isDrawerOpen()) {
319 super.onBackPressed();
323 * checks if the drawer exists and is opened.
325 * @return <code>true</code> if the drawer is open, else <code>false</code>
327 public boolean isDrawerOpen() {
328 if(mDrawerLayout
!= null
) {
329 return mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
336 * closes the navigation drawer.
338 public void closeNavDrawer() {
339 if(mDrawerLayout
!= null
) {
340 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
344 protected void initDrawer(){
345 // constant settings for action bar when navigation drawer is inited
346 getSupportActionBar().setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
349 mDrawerLayout
= (DrawerLayout
) findViewById(R
.id
.drawer_layout
);
350 // Notification Drawer
351 RelativeLayout navigationDrawerLayout
= (RelativeLayout
) findViewById(R
.id
.left_drawer
);
352 mDrawerList
= (ListView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_list
);
354 // TODO re-enable when "Accounts" is available in Navigation Drawer
355 // // load Account in the Drawer Title
357 // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
358 // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
361 // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
362 // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
364 // if (account != null) {
365 // int lastAtPos = account.name.lastIndexOf("@");
366 // username.setText(account.name.substring(0, lastAtPos));
369 // Display username in drawer
370 setUsernameInDrawer(navigationDrawerLayout
, AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext()));
372 // load slide menu items
373 mDrawerTitles
= getResources().getStringArray(R
.array
.drawer_items
);
375 // nav drawer content description from resources
376 mDrawerContentDescriptions
= getResources().
377 getStringArray(R
.array
.drawer_content_descriptions
);
380 mDrawerItems
= new ArrayList
<NavigationDrawerItem
>();
381 // adding nav drawer items to array
382 // TODO re-enable when "Accounts" is available in Navigation Drawer
384 // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
385 // mDrawerContentDescriptions[0]));
387 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[0], mDrawerContentDescriptions
[0],
388 R
.drawable
.ic_folder_open
));
390 // TODO Enable when "On Device" is recovered
392 //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
393 // mDrawerContentDescriptions[2]));
396 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[1], mDrawerContentDescriptions
[1],
397 R
.drawable
.ic_settings
));
399 if (BuildConfig
.DEBUG
) {
400 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[2],
401 mDrawerContentDescriptions
[2],R
.drawable
.ic_log
));
404 // setting the nav drawer list adapter
405 mNavigationDrawerAdapter
= new NavigationDrawerListAdapter(getApplicationContext(), this,
407 mDrawerList
.setAdapter(mNavigationDrawerAdapter
);
410 mDrawerToggle
= new ActionBarDrawerToggle(this, mDrawerLayout
,R
.string
.drawer_open
,R
.string
.drawer_close
) {
412 /** Called when a drawer has settled in a completely closed state. */
413 public void onDrawerClosed(View view
) {
414 super.onDrawerClosed(view
);
415 updateActionBarTitleAndHomeButton(null
);
416 invalidateOptionsMenu();
419 /** Called when a drawer has settled in a completely open state. */
420 public void onDrawerOpened(View drawerView
) {
421 super.onDrawerOpened(drawerView
);
422 getSupportActionBar().setTitle(R
.string
.app_name
);
423 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
424 invalidateOptionsMenu();
428 // Set the list's click listener
429 mDrawerList
.setOnItemClickListener(new DrawerItemClickListener());
431 // Set the drawer toggle as the DrawerListener
432 mDrawerLayout
.setDrawerListener(mDrawerToggle
);
433 mDrawerToggle
.setDrawerIndicatorEnabled(false
);
437 * sets the given account name in the drawer in case the drawer is available. The account name
438 * is shortened beginning from the @-sign in the username.
440 * @param navigationDrawerLayout the drawer layout to be used
441 * @param account the account to be set in the drawer
443 protected void setUsernameInDrawer(RelativeLayout navigationDrawerLayout
, Account account
) {
444 if (navigationDrawerLayout
!= null
&& getAccount() != null
) {
445 TextView username
= (TextView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_username
);
446 int lastAtPos
= account
.name
.lastIndexOf("@");
447 username
.setText(account
.name
.substring(0, lastAtPos
));
452 * Updates title bar and home buttons (state and icon).
454 * Assumes that navigation drawer is NOT visible.
456 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
457 String title
= getString(R
.string
.default_display_name_for_root_folder
); // default
460 /// choose the appropiate title
461 if (chosenFile
== null
) {
462 chosenFile
= mFile
; // if no file is passed, current file decides
465 chosenFile
== null
||
466 (chosenFile
.isFolder() && chosenFile
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
)
469 title
= chosenFile
.getFileName();
472 /// set the chosen title
473 ActionBar actionBar
= getSupportActionBar();
474 actionBar
.setTitle(title
);
475 /// also as content description
476 View actionBarTitleView
= getWindow().getDecorView().findViewById(
477 getResources().getIdentifier("action_bar_title", "id", "android")
479 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
480 actionBarTitleView
.setContentDescription(title
);
483 /// set home button properties
484 mDrawerToggle
.setDrawerIndicatorEnabled(inRoot
);
485 actionBar
.setDisplayHomeAsUpEnabled(true
);
486 actionBar
.setDisplayShowTitleEnabled(true
);
492 * Sets and validates the ownCloud {@link Account} associated to the Activity.
494 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
496 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
498 * @param account New {@link Account} to set.
499 * @param savedAccount When 'true', account was retrieved from a saved instance state.
501 protected void setAccount(Account account
, boolean savedAccount
) {
502 Account oldAccount
= mAccount
;
503 boolean validAccount
=
504 (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(),
508 mAccountWasSet
= true
;
509 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
512 swapToDefaultAccount();
518 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
520 * If no valid ownCloud {@link Account} exists, the the user is requested
521 * to create a new ownCloud {@link Account}.
523 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
525 private void swapToDefaultAccount() {
526 // default to the most recently used account
527 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
528 if (newAccount
== null
) {
529 /// no account available: force account creation
530 createFirstAccount();
531 mRedirectingToSetupAccount
= true
;
532 mAccountWasSet
= false
;
533 mAccountWasRestored
= false
;
536 mAccountWasSet
= true
;
537 mAccountWasRestored
= (newAccount
.equals(mAccount
));
538 mAccount
= newAccount
;
544 * Launches the account creation activity. To use when no ownCloud account is available
546 private void createFirstAccount() {
547 AccountManager am
= AccountManager
.get(getApplicationContext());
548 am
.addAccount(MainApp
.getAccountType(),
553 new AccountCreationCallback(),
562 protected void onSaveInstanceState(Bundle outState
) {
563 super.onSaveInstanceState(outState
);
564 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
565 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
566 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mFileOperationsHelper
.getOpIdWaitingFor());
567 outState
.putBoolean(KEY_TRY_SHARE_AGAIN
, mTryShareAgain
);
568 if(getSupportActionBar() != null
&& getSupportActionBar().getTitle() != null
) {
569 // Null check in case the actionbar is used in ActionBar.NAVIGATION_MODE_LIST
570 // since it doesn't have a title then
571 outState
.putString(KEY_ACTION_BAR_TITLE
, getSupportActionBar().getTitle().toString());
577 * Getter for the main {@link OCFile} handled by the activity.
579 * @return Main {@link OCFile} handled by the activity.
581 public OCFile
getFile() {
587 * Setter for the main {@link OCFile} handled by the activity.
589 * @param file Main {@link OCFile} to be handled by the activity.
591 public void setFile(OCFile file
) {
597 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
600 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
603 public Account
getAccount() {
607 protected void setAccount(Account account
) {
612 * @return Value of mFromNotification: True if the Activity is launched by a notification
614 public boolean fromNotification() {
615 return mFromNotification
;
619 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
621 protected boolean isRedirectingToSetupAccount() {
622 return mRedirectingToSetupAccount
;
625 public boolean isTryShareAgain(){
626 return mTryShareAgain
;
629 public void setTryShareAgain(boolean tryShareAgain
) {
630 mTryShareAgain
= tryShareAgain
;
633 public OperationsServiceBinder
getOperationsServiceBinder() {
634 return mOperationsServiceBinder
;
637 protected ServiceConnection
newTransferenceServiceConnection() {
642 * Helper class handling a callback from the {@link AccountManager} after the creation of
643 * a new ownCloud {@link Account} finished, successfully or not.
645 * At this moment, only called after the creation of the first account.
647 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
650 public void run(AccountManagerFuture
<Bundle
> future
) {
651 FileActivity
.this.mRedirectingToSetupAccount
= false
;
652 boolean accountWasSet
= false
;
653 if (future
!= null
) {
656 result
= future
.getResult();
657 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
658 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
659 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
660 setAccount(new Account(name
, type
), false
);
661 accountWasSet
= true
;
663 } catch (OperationCanceledException e
) {
664 Log_OC
.d(TAG
, "Account creation canceled");
666 } catch (Exception e
) {
667 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
671 Log_OC
.e(TAG
, "Account creation callback with null bundle");
673 if (!accountWasSet
) {
674 moveTaskToBack(true
);
682 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
684 * Child classes must grant that state depending on the {@link Account} is updated.
686 protected void onAccountSet(boolean stateWasRecovered
) {
687 if (getAccount() != null
) {
688 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
691 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
696 public FileDataStorageManager
getStorageManager() {
697 return mStorageManager
;
701 public OnRemoteOperationListener
getRemoteOperationListener() {
706 public Handler
getHandler() {
710 public FileOperationsHelper
getFileOperationsHelper() {
711 return mFileOperationsHelper
;
716 * @param operation Removal operation performed.
717 * @param result Result of the removal.
720 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
721 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the "
722 + "FileActivities ");
724 mFileOperationsHelper
.setOpIdWaitingFor(Long
.MAX_VALUE
);
726 dismissLoadingDialog();
728 if (!result
.isSuccess() && (
729 result
.getCode() == ResultCode
.UNAUTHORIZED
||
730 result
.isIdPRedirection() ||
731 (result
.isException() && result
.getException() instanceof AuthenticatorException
)
734 requestCredentialsUpdate();
736 if (result
.getCode() == ResultCode
.UNAUTHORIZED
) {
737 dismissLoadingDialog();
738 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
739 operation
, getResources()),
743 mTryShareAgain
= false
;
745 } else if (operation
== null
||
746 operation
instanceof CreateShareWithShareeOperation
||
747 operation
instanceof UnshareOperation
||
748 operation
instanceof SynchronizeFolderOperation
750 if (result
.isSuccess()) {
753 } else if (result
.getCode() != ResultCode
.CANCELLED
) {
754 Toast t
= Toast
.makeText(this,
755 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
760 } else if (operation
instanceof CreateShareViaLinkOperation
) {
761 onCreateShareViaLinkOperationFinish((CreateShareViaLinkOperation
) operation
, result
);
763 } else if (operation
instanceof SynchronizeFileOperation
) {
764 onSynchronizeFileOperationFinish((SynchronizeFileOperation
) operation
, result
);
766 } else if (operation
instanceof GetSharesForFileOperation
) {
767 if (result
.isSuccess()) {
770 } else if (result
.getCode() != ResultCode
.SHARE_NOT_FOUND
) {
771 Toast t
= Toast
.makeText(this,
772 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
779 protected void requestCredentialsUpdate() {
780 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
781 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
782 updateAccountCredentials
.putExtra(
783 AuthenticatorActivity
.EXTRA_ACTION
,
784 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
);
785 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
786 startActivity(updateAccountCredentials
);
791 private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation operation
,
792 RemoteOperationResult result
) {
793 if (result
.isSuccess()) {
794 mTryShareAgain
= false
;
797 Intent sendIntent
= operation
.getSendIntentWithSubject(this);
798 startActivity(sendIntent
);
800 // Detect Failure (403) --> needs Password
801 if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
802 if (!isTryShareAgain()) {
803 SharePasswordDialogFragment dialog
=
804 SharePasswordDialogFragment
.newInstance(new OCFile(operation
.getPath()),
805 operation
.getSendIntent());
806 dialog
.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD
);
808 Toast t
= Toast
.makeText(this,
809 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
812 mTryShareAgain
= false
;
815 Toast t
= Toast
.makeText(this,
816 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
823 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
824 RemoteOperationResult result
) {
825 OCFile syncedFile
= operation
.getLocalFile();
826 if (!result
.isSuccess()) {
827 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
828 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
829 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
830 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
835 if (!operation
.transferWasRequested()) {
836 Toast msg
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
837 operation
, getResources()), Toast
.LENGTH_LONG
);
840 invalidateOptionsMenu();
844 protected void updateFileFromDB(){
845 OCFile file
= getFile();
847 file
= getStorageManager().getFileByPath(file
.getRemotePath());
854 * Show loading dialog
856 public void showLoadingDialog(String message
) {
858 LoadingDialog loading
= new LoadingDialog(message
);
859 FragmentManager fm
= getSupportFragmentManager();
860 FragmentTransaction ft
= fm
.beginTransaction();
861 loading
.show(ft
, DIALOG_WAIT_TAG
);
867 * Dismiss loading dialog
869 public void dismissLoadingDialog() {
870 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
872 LoadingDialog loading
= (LoadingDialog
) frag
;
878 private void doOnResumeAndBound() {
879 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
880 long waitingForOpId
= mFileOperationsHelper
.getOpIdWaitingFor();
881 if (waitingForOpId
<= Integer
.MAX_VALUE
) {
882 boolean wait
= mOperationsServiceBinder
.dispatchResultIfFinished((int)waitingForOpId
,
885 dismissLoadingDialog();
892 * Implements callback methods for service binding. Passed as a parameter to {
894 private class OperationsServiceConnection
implements ServiceConnection
{
897 public void onServiceConnected(ComponentName component
, IBinder service
) {
898 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
899 Log_OC
.d(TAG
, "Operations service connected");
900 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
901 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
902 dismissLoadingDialog();
904 doOnResumeAndBound();
913 public void onServiceDisconnected(ComponentName component
) {
914 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
915 Log_OC
.d(TAG
, "Operations service disconnected");
916 mOperationsServiceBinder
= null
;
917 // TODO whatever could be waiting for the service is unbound
924 public FileDownloaderBinder
getFileDownloaderBinder() {
925 return mDownloaderBinder
;
930 public FileUploaderBinder
getFileUploaderBinder() {
931 return mUploaderBinder
;
935 public void restart(){
936 Intent i
= new Intent(this, FileDisplayActivity
.class);
937 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
941 // TODO re-enable when "Accounts" is available in Navigation Drawer
942 // public void closeDrawer() {
943 // mDrawerLayout.closeDrawers();
946 public void allFilesOption(){
950 private class DrawerItemClickListener
implements ListView
.OnItemClickListener
{
952 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
953 // TODO re-enable when "Accounts" is available in Navigation Drawer
954 // if (mShowAccounts && position > 0){
955 // position = position - 1;
958 // TODO re-enable when "Accounts" is available in Navigation Drawer
959 // case 0: // Accounts
960 // mShowAccounts = !mShowAccounts;
961 // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
962 // mNavigationDrawerAdapter.notifyDataSetChanged();
967 mDrawerLayout
.closeDrawers();
970 // TODO Enable when "On Device" is recovered ?
972 // MainApp.showOnlyFilesOnDevice(true);
973 // mDrawerLayout.closeDrawers();
977 Intent settingsIntent
= new Intent(getApplicationContext(),
979 startActivity(settingsIntent
);
980 mDrawerLayout
.closeDrawers();
984 Intent loggerIntent
= new Intent(getApplicationContext(),
985 LogHistoryActivity
.class);
986 startActivity(loggerIntent
);
987 mDrawerLayout
.closeDrawers();