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
.Toast
;
52 import com
.owncloud
.android
.BuildConfig
;
53 import com
.owncloud
.android
.MainApp
;
54 import com
.owncloud
.android
.R
;
55 import com
.owncloud
.android
.authentication
.AccountUtils
;
56 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
57 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
58 import com
.owncloud
.android
.datamodel
.OCFile
;
59 import com
.owncloud
.android
.files
.FileOperationsHelper
;
60 import com
.owncloud
.android
.files
.services
.FileDownloader
;
61 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
62 import com
.owncloud
.android
.files
.services
.FileUploader
;
63 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
64 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
65 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
66 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
67 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
68 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
69 import com
.owncloud
.android
.operations
.CreateShareOperation
;
70 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
71 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
72 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
73 import com
.owncloud
.android
.services
.OperationsService
;
74 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
75 import com
.owncloud
.android
.ui
.NavigationDrawerItem
;
76 import com
.owncloud
.android
.ui
.adapter
.NavigationDrawerListAdapter
;
77 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
78 import com
.owncloud
.android
.ui
.dialog
.SharePasswordDialogFragment
;
79 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
81 import java
.util
.ArrayList
;
85 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud
88 public class FileActivity
extends AppCompatActivity
89 implements OnRemoteOperationListener
, ComponentsGetter
{
91 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
92 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
93 public static final String EXTRA_WAITING_TO_PREVIEW
=
94 "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
95 public static final String EXTRA_FROM_NOTIFICATION
=
96 "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
98 public static final String TAG
= FileActivity
.class.getSimpleName();
100 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
101 private static final String KEY_WAITING_FOR_OP_ID
= "WAITING_FOR_OP_ID";
102 private static final String DIALOG_SHARE_PASSWORD
= "DIALOG_SHARE_PASSWORD";
103 private static final String KEY_TRY_SHARE_AGAIN
= "TRY_SHARE_AGAIN";
104 private static final String KEY_ACTION_BAR_TITLE
= "ACTION_BAR_TITLE";
106 protected static final long DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
= 200;
109 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.*/
110 private Account mAccount
;
112 /** Main {@link OCFile} handled by the activity.*/
113 private OCFile mFile
;
115 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud
117 private boolean mRedirectingToSetupAccount
= false
;
119 /** Flag to signal when the value of mAccount was set */
120 protected boolean mAccountWasSet
;
122 /** Flag to signal when the value of mAccount was restored from a saved state */
123 protected boolean mAccountWasRestored
;
125 /** Flag to signal if the activity is launched by a notification */
126 private boolean mFromNotification
;
128 /** Messages handler associated to the main thread and the life cycle of the activity */
129 private Handler mHandler
;
131 /** Access point to the cached database for the current ownCloud {@link Account} */
132 private FileDataStorageManager mStorageManager
= null
;
134 private FileOperationsHelper mFileOperationsHelper
;
136 private ServiceConnection mOperationsServiceConnection
= null
;
138 private OperationsServiceBinder mOperationsServiceBinder
= null
;
140 protected FileDownloaderBinder mDownloaderBinder
= null
;
141 protected FileUploaderBinder mUploaderBinder
= null
;
142 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
144 private boolean mTryShareAgain
= false
;
147 protected DrawerLayout mDrawerLayout
;
148 protected ActionBarDrawerToggle mDrawerToggle
;
149 protected ListView mDrawerList
;
152 protected String
[] mDrawerTitles
;
153 protected String
[] mDrawerContentDescriptions
;
155 protected ArrayList
<NavigationDrawerItem
> mDrawerItems
;
157 protected NavigationDrawerListAdapter mNavigationDrawerAdapter
= null
;
160 // TODO re-enable when "Accounts" is available in Navigation Drawer
161 // protected boolean mShowAccounts = false;
164 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
165 * the {@link FileActivity}.
167 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
168 * is requested to create a new one.
171 protected void onCreate(Bundle savedInstanceState
) {
172 super.onCreate(savedInstanceState
);
173 mHandler
= new Handler();
174 mFileOperationsHelper
= new FileOperationsHelper(this);
175 Account account
= null
;
176 if(savedInstanceState
!= null
) {
177 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
178 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
179 mFileOperationsHelper
.setOpIdWaitingFor(
180 savedInstanceState
.getLong(KEY_WAITING_FOR_OP_ID
, Long
.MAX_VALUE
)
182 mTryShareAgain
= savedInstanceState
.getBoolean(KEY_TRY_SHARE_AGAIN
);
183 getSupportActionBar().setTitle(savedInstanceState
.getString(KEY_ACTION_BAR_TITLE
));
185 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
186 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
187 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
,
191 AccountUtils
.updateAccountVersion(this); // best place, before any access to AccountManager
194 setAccount(account
, savedInstanceState
!= null
);
196 mOperationsServiceConnection
= new OperationsServiceConnection();
197 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
,
198 Context
.BIND_AUTO_CREATE
);
200 mDownloadServiceConnection
= newTransferenceServiceConnection();
201 if (mDownloadServiceConnection
!= null
) {
202 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
203 Context
.BIND_AUTO_CREATE
);
205 mUploadServiceConnection
= newTransferenceServiceConnection();
206 if (mUploadServiceConnection
!= null
) {
207 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
208 Context
.BIND_AUTO_CREATE
);
214 protected void onNewIntent (Intent intent
) {
215 Log_OC
.v(TAG
, "onNewIntent() start");
216 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
217 if (current
!= null
&& mAccount
!= null
&& !mAccount
.name
.equals(current
.name
)) {
220 Log_OC
.v(TAG
, "onNewIntent() stop");
224 * Since ownCloud {@link Account}s can be managed from the system setting menu,
225 * the existence of the {@link Account} associated to the instance must be checked
226 * every time it is restarted.
229 protected void onRestart() {
230 Log_OC
.v(TAG
, "onRestart() start");
232 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.exists(mAccount
, this));
234 swapToDefaultAccount();
236 Log_OC
.v(TAG
, "onRestart() end");
241 protected void onStart() {
244 if (mAccountWasSet
) {
245 onAccountSet(mAccountWasRestored
);
250 protected void onResume() {
253 if (mOperationsServiceBinder
!= null
) {
254 doOnResumeAndBound();
259 protected void onPause() {
260 if (mOperationsServiceBinder
!= null
) {
261 mOperationsServiceBinder
.removeOperationListener(this);
269 protected void onDestroy() {
270 if (mOperationsServiceConnection
!= null
) {
271 unbindService(mOperationsServiceConnection
);
272 mOperationsServiceBinder
= null
;
274 if (mDownloadServiceConnection
!= null
) {
275 unbindService(mDownloadServiceConnection
);
276 mDownloadServiceConnection
= null
;
278 if (mUploadServiceConnection
!= null
) {
279 unbindService(mUploadServiceConnection
);
280 mUploadServiceConnection
= null
;
287 protected void onPostCreate(Bundle savedInstanceState
) {
288 super.onPostCreate(savedInstanceState
);
289 // Sync the toggle state after onRestoreInstanceState has occurred.
290 if (mDrawerToggle
!= null
) {
291 mDrawerToggle
.syncState();
292 if (isDrawerOpen()) {
293 getSupportActionBar().setTitle(R
.string
.app_name
);
294 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
300 public void onConfigurationChanged(Configuration newConfig
) {
301 super.onConfigurationChanged(newConfig
);
302 if (mDrawerToggle
!= null
) {
303 mDrawerToggle
.onConfigurationChanged(newConfig
);
308 public void onBackPressed() {
309 if (isDrawerOpen()) {
313 super.onBackPressed();
316 public boolean isDrawerOpen() {
317 return mDrawerLayout
.isDrawerOpen(GravityCompat
.START
);
320 public void closeNavDrawer() {
321 mDrawerLayout
.closeDrawer(GravityCompat
.START
);
324 protected void initDrawer(){
325 // constant settings for action bar when navigation drawer is inited
326 getSupportActionBar().setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
329 mDrawerLayout
= (DrawerLayout
) findViewById(R
.id
.drawer_layout
);
330 // Notification Drawer
331 RelativeLayout navigationDrawerLayout
= (RelativeLayout
) findViewById(R
.id
.left_drawer
);
332 mDrawerList
= (ListView
) navigationDrawerLayout
.findViewById(R
.id
.drawer_list
);
334 // TODO re-enable when "Accounts" is available in Navigation Drawer
335 // // load Account in the Drawer Title
337 // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
338 // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
341 // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
342 // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
344 // if (account != null) {
345 // int lastAtPos = account.name.lastIndexOf("@");
346 // username.setText(account.name.substring(0, lastAtPos));
349 // load slide menu items
350 mDrawerTitles
= getResources().getStringArray(R
.array
.drawer_items
);
352 // nav drawer content description from resources
353 mDrawerContentDescriptions
= getResources().
354 getStringArray(R
.array
.drawer_content_descriptions
);
357 mDrawerItems
= new ArrayList
<NavigationDrawerItem
>();
358 // adding nav drawer items to array
359 // TODO re-enable when "Accounts" is available in Navigation Drawer
361 // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
362 // mDrawerContentDescriptions[0]));
364 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[0], mDrawerContentDescriptions
[0],
365 R
.drawable
.ic_folder_open
));
367 // TODO Enable when "On Device" is recovered
369 //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
370 // mDrawerContentDescriptions[2]));
373 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[1], mDrawerContentDescriptions
[1],
374 R
.drawable
.ic_settings
));
376 if (BuildConfig
.DEBUG
) {
377 mDrawerItems
.add(new NavigationDrawerItem(mDrawerTitles
[2],
378 mDrawerContentDescriptions
[2],R
.drawable
.ic_log
));
381 // setting the nav drawer list adapter
382 mNavigationDrawerAdapter
= new NavigationDrawerListAdapter(getApplicationContext(), this,
384 mDrawerList
.setAdapter(mNavigationDrawerAdapter
);
387 mDrawerToggle
= new ActionBarDrawerToggle(this, mDrawerLayout
,R
.string
.drawer_open
,R
.string
.drawer_close
) {
389 //new ActionBarDrawerToggle(
392 // R.drawable.ic_drawer,
393 // R.string.app_name,
394 // R.string.drawer_close) {
396 /** Called when a drawer has settled in a completely closed state. */
397 public void onDrawerClosed(View view
) {
398 super.onDrawerClosed(view
);
399 updateActionBarTitleAndHomeButton(null
);
400 invalidateOptionsMenu();
403 /** Called when a drawer has settled in a completely open state. */
404 public void onDrawerOpened(View drawerView
) {
405 super.onDrawerOpened(drawerView
);
406 getSupportActionBar().setTitle(R
.string
.app_name
);
407 mDrawerToggle
.setDrawerIndicatorEnabled(true
);
408 invalidateOptionsMenu();
412 //mDrawerToggle.setDrawerIndicatorEnabled(true);
413 // Set the list's click listener
414 mDrawerList
.setOnItemClickListener(new DrawerItemClickListener());
416 // Set the drawer toggle as the DrawerListener
417 mDrawerLayout
.setDrawerListener(mDrawerToggle
);
422 * Updates title bar and home buttons (state and icon).
424 * Assumes that navigation drawer is NOT visible.
426 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile
) {
427 String title
= getString(R
.string
.default_display_name_for_root_folder
); // default
430 /// choose the appropiate title
431 if (chosenFile
== null
) {
432 chosenFile
= mFile
; // if no file is passed, current file decides
435 chosenFile
== null
||
436 (chosenFile
.isFolder() && chosenFile
.getParentId() == FileDataStorageManager
.ROOT_PARENT_ID
)
439 title
= chosenFile
.getFileName();
442 /// set the chosen title
443 ActionBar actionBar
= getSupportActionBar();
444 actionBar
.setTitle(title
);
445 /// also as content description
446 View actionBarTitleView
= getWindow().getDecorView().findViewById(
447 getResources().getIdentifier("action_bar_title", "id", "android")
449 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
450 actionBarTitleView
.setContentDescription(title
);
453 /// set home button properties
454 mDrawerToggle
.setDrawerIndicatorEnabled(inRoot
);
455 actionBar
.setDisplayHomeAsUpEnabled(true
);
456 actionBar
.setDisplayShowTitleEnabled(true
);
462 * Sets and validates the ownCloud {@link Account} associated to the Activity.
464 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
466 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
468 * @param account New {@link Account} to set.
469 * @param savedAccount When 'true', account was retrieved from a saved instance state.
471 protected void setAccount(Account account
, boolean savedAccount
) {
472 Account oldAccount
= mAccount
;
473 boolean validAccount
=
474 (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(),
478 mAccountWasSet
= true
;
479 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
482 swapToDefaultAccount();
488 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
490 * If no valid ownCloud {@link Account} exists, the the user is requested
491 * to create a new ownCloud {@link Account}.
493 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
495 private void swapToDefaultAccount() {
496 // default to the most recently used account
497 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
498 if (newAccount
== null
) {
499 /// no account available: force account creation
500 createFirstAccount();
501 mRedirectingToSetupAccount
= true
;
502 mAccountWasSet
= false
;
503 mAccountWasRestored
= false
;
506 mAccountWasSet
= true
;
507 mAccountWasRestored
= (newAccount
.equals(mAccount
));
508 mAccount
= newAccount
;
514 * Launches the account creation activity. To use when no ownCloud account is available
516 private void createFirstAccount() {
517 AccountManager am
= AccountManager
.get(getApplicationContext());
518 am
.addAccount(MainApp
.getAccountType(),
523 new AccountCreationCallback(),
532 protected void onSaveInstanceState(Bundle outState
) {
533 super.onSaveInstanceState(outState
);
534 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
535 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
536 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mFileOperationsHelper
.getOpIdWaitingFor());
537 outState
.putBoolean(KEY_TRY_SHARE_AGAIN
, mTryShareAgain
);
538 outState
.putString(KEY_ACTION_BAR_TITLE
, getSupportActionBar().getTitle().toString());
543 * Getter for the main {@link OCFile} handled by the activity.
545 * @return Main {@link OCFile} handled by the activity.
547 public OCFile
getFile() {
553 * Setter for the main {@link OCFile} handled by the activity.
555 * @param file Main {@link OCFile} to be handled by the activity.
557 public void setFile(OCFile file
) {
563 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
566 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
569 public Account
getAccount() {
573 protected void setAccount(Account account
) {
578 * @return Value of mFromNotification: True if the Activity is launched by a notification
580 public boolean fromNotification() {
581 return mFromNotification
;
585 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
587 protected boolean isRedirectingToSetupAccount() {
588 return mRedirectingToSetupAccount
;
591 public boolean isTryShareAgain(){
592 return mTryShareAgain
;
595 public void setTryShareAgain(boolean tryShareAgain
) {
596 mTryShareAgain
= tryShareAgain
;
599 public OperationsServiceBinder
getOperationsServiceBinder() {
600 return mOperationsServiceBinder
;
603 protected ServiceConnection
newTransferenceServiceConnection() {
608 * Helper class handling a callback from the {@link AccountManager} after the creation of
609 * a new ownCloud {@link Account} finished, successfully or not.
611 * At this moment, only called after the creation of the first account.
613 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
616 public void run(AccountManagerFuture
<Bundle
> future
) {
617 FileActivity
.this.mRedirectingToSetupAccount
= false
;
618 boolean accountWasSet
= false
;
619 if (future
!= null
) {
622 result
= future
.getResult();
623 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
624 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
625 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
626 setAccount(new Account(name
, type
), false
);
627 accountWasSet
= true
;
629 } catch (OperationCanceledException e
) {
630 Log_OC
.d(TAG
, "Account creation canceled");
632 } catch (Exception e
) {
633 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
637 Log_OC
.e(TAG
, "Account creation callback with null bundle");
639 if (!accountWasSet
) {
640 moveTaskToBack(true
);
648 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
650 * Child classes must grant that state depending on the {@link Account} is updated.
652 protected void onAccountSet(boolean stateWasRecovered
) {
653 if (getAccount() != null
) {
654 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
657 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
662 public FileDataStorageManager
getStorageManager() {
663 return mStorageManager
;
667 public OnRemoteOperationListener
getRemoteOperationListener() {
672 public Handler
getHandler() {
676 public FileOperationsHelper
getFileOperationsHelper() {
677 return mFileOperationsHelper
;
682 * @param operation Removal operation performed.
683 * @param result Result of the removal.
686 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
687 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the "
688 + "FileActivities ");
690 mFileOperationsHelper
.setOpIdWaitingFor(Long
.MAX_VALUE
);
692 if (!result
.isSuccess() && (
693 result
.getCode() == ResultCode
.UNAUTHORIZED
||
694 result
.isIdPRedirection() ||
695 (result
.isException() && result
.getException() instanceof AuthenticatorException
)
698 requestCredentialsUpdate();
700 if (result
.getCode() == ResultCode
.UNAUTHORIZED
) {
701 dismissLoadingDialog();
702 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
703 operation
, getResources()),
707 mTryShareAgain
= false
;
709 } else if (operation
instanceof CreateShareOperation
) {
710 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
712 } else if (operation
instanceof UnshareLinkOperation
) {
713 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
715 } else if (operation
instanceof SynchronizeFolderOperation
) {
716 onSynchronizeFolderOperationFinish((SynchronizeFolderOperation
)operation
, result
);
718 }else if (operation
instanceof SynchronizeFileOperation
) {
719 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
724 protected void requestCredentialsUpdate() {
725 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
726 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
727 updateAccountCredentials
.putExtra(
728 AuthenticatorActivity
.EXTRA_ACTION
,
729 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
);
730 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
731 startActivity(updateAccountCredentials
);
735 private void onCreateShareOperationFinish(CreateShareOperation operation
,
736 RemoteOperationResult result
) {
737 dismissLoadingDialog();
738 if (result
.isSuccess()) {
739 mTryShareAgain
= false
;
742 Intent sendIntent
= operation
.getSendIntent();
743 startActivity(sendIntent
);
745 // Detect Failure (403) --> needs Password
746 if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
747 if (!isTryShareAgain()) {
748 SharePasswordDialogFragment dialog
=
749 SharePasswordDialogFragment
.newInstance(new OCFile(operation
.getPath()),
750 operation
.getSendIntent());
751 dialog
.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD
);
753 Toast t
= Toast
.makeText(this,
754 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
757 mTryShareAgain
= false
;
760 Toast t
= Toast
.makeText(this,
761 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
769 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
770 RemoteOperationResult result
) {
771 dismissLoadingDialog();
773 if (result
.isSuccess()){
777 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
778 operation
, getResources()), Toast
.LENGTH_LONG
);
783 private void onSynchronizeFolderOperationFinish(
784 SynchronizeFolderOperation operation
, RemoteOperationResult result
786 if (!result
.isSuccess() && result
.getCode() != ResultCode
.CANCELLED
){
787 Toast t
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
788 operation
, getResources()), Toast
.LENGTH_LONG
);
793 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
,
794 RemoteOperationResult result
) {
795 dismissLoadingDialog();
796 OCFile syncedFile
= operation
.getLocalFile();
797 if (!result
.isSuccess()) {
798 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
799 Intent i
= new Intent(this, ConflictsResolveActivity
.class);
800 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, syncedFile
);
801 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, getAccount());
806 if (!operation
.transferWasRequested()) {
807 Toast msg
= Toast
.makeText(this, ErrorMessageAdapter
.getErrorCauseMessage(result
,
808 operation
, getResources()), Toast
.LENGTH_LONG
);
814 protected void updateFileFromDB(){
815 OCFile file
= getFile();
817 file
= getStorageManager().getFileByPath(file
.getRemotePath());
824 * Show loading dialog
826 public void showLoadingDialog() {
828 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
829 FragmentManager fm
= getSupportFragmentManager();
830 FragmentTransaction ft
= fm
.beginTransaction();
831 loading
.show(ft
, DIALOG_WAIT_TAG
);
837 * Dismiss loading dialog
839 public void dismissLoadingDialog(){
840 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
842 LoadingDialog loading
= (LoadingDialog
) frag
;
848 private void doOnResumeAndBound() {
849 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
850 long waitingForOpId
= mFileOperationsHelper
.getOpIdWaitingFor();
851 if (waitingForOpId
<= Integer
.MAX_VALUE
) {
852 boolean wait
= mOperationsServiceBinder
.dispatchResultIfFinished((int)waitingForOpId
,
855 dismissLoadingDialog();
862 * Implements callback methods for service binding. Passed as a parameter to {
864 private class OperationsServiceConnection
implements ServiceConnection
{
867 public void onServiceConnected(ComponentName component
, IBinder service
) {
868 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
869 Log_OC
.d(TAG
, "Operations service connected");
870 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
871 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
872 dismissLoadingDialog();
874 doOnResumeAndBound();
883 public void onServiceDisconnected(ComponentName component
) {
884 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
885 Log_OC
.d(TAG
, "Operations service disconnected");
886 mOperationsServiceBinder
= null
;
887 // TODO whatever could be waiting for the service is unbound
894 public FileDownloaderBinder
getFileDownloaderBinder() {
895 return mDownloaderBinder
;
900 public FileUploaderBinder
getFileUploaderBinder() {
901 return mUploaderBinder
;
905 public void restart(){
906 Intent i
= new Intent(this, FileDisplayActivity
.class);
907 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
911 // TODO re-enable when "Accounts" is available in Navigation Drawer
912 // public void closeDrawer() {
913 // mDrawerLayout.closeDrawers();
916 public void allFilesOption(){
920 private class DrawerItemClickListener
implements ListView
.OnItemClickListener
{
922 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
923 // TODO re-enable when "Accounts" is available in Navigation Drawer
924 // if (mShowAccounts && position > 0){
925 // position = position - 1;
928 // TODO re-enable when "Accounts" is available in Navigation Drawer
929 // case 0: // Accounts
930 // mShowAccounts = !mShowAccounts;
931 // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
932 // mNavigationDrawerAdapter.notifyDataSetChanged();
937 mDrawerLayout
.closeDrawers();
940 // TODO Enable when "On Device" is recovered ?
942 // MainApp.showOnlyFilesOnDevice(true);
943 // mDrawerLayout.closeDrawers();
947 Intent settingsIntent
= new Intent(getApplicationContext(),
949 startActivity(settingsIntent
);
950 mDrawerLayout
.closeDrawers();
954 Intent loggerIntent
= new Intent(getApplicationContext(),
955 LogHistoryActivity
.class);
956 startActivity(loggerIntent
);
957 mDrawerLayout
.closeDrawers();