Better condition
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileActivity.java
index 1b42ff0..fbb62ed 100644 (file)
@@ -1,6 +1,9 @@
-/* ownCloud Android client application
+/**
+ *   ownCloud Android client application
+ *
+ *   @author David A. Velasco
  *   Copyright (C) 2011  Bartek Przybylski
  *   Copyright (C) 2011  Bartek Przybylski
- *   Copyright (C) 2012-2014 ownCloud Inc.
+ *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
@@ -18,8 +21,6 @@
 
 package com.owncloud.android.ui.activity;
 
 
 package com.owncloud.android.ui.activity;
 
-import java.io.IOException;
-
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountManagerCallback;
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountManagerCallback;
@@ -50,38 +51,40 @@ import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
-import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.operations.CreateShareOperation;
 import com.owncloud.android.operations.CreateShareOperation;
+import com.owncloud.android.operations.SynchronizeFolderOperation;
 import com.owncloud.android.operations.UnshareLinkOperation;
 import com.owncloud.android.operations.UnshareLinkOperation;
-
 import com.owncloud.android.services.OperationsService;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.ui.dialog.LoadingDialog;
 import com.owncloud.android.services.OperationsService;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.ui.dialog.LoadingDialog;
+import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.ErrorMessageAdapter;
-import com.owncloud.android.utils.Log_OC;
 
 
 /**
  * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
 
 
 /**
  * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
- * 
- * @author David A. Velasco
  */
  */
-public class FileActivity extends SherlockFragmentActivity 
-implements OnRemoteOperationListener, ComponentsGetter {
+public class FileActivity extends SherlockFragmentActivity
+        implements OnRemoteOperationListener, ComponentsGetter {
 
     public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
     public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
     public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
 
     public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
     public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
     public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
-    public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
+    public static final String EXTRA_FROM_NOTIFICATION = "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
     
     public static final String TAG = FileActivity.class.getSimpleName();
     
     private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
     
     public static final String TAG = FileActivity.class.getSimpleName();
     
     private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
-    private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID";;
+    private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID";
+    private static final String DIALOG_SHARE_PASSWORD = "DIALOG_SHARE_PASSWORD";
+    private static final String KEY_TRY_SHARE_AGAIN = "TRY_SHARE_AGAIN";
+    
+    protected static final long DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS = 200;
     
     
     /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
     
     
     /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
@@ -94,10 +97,10 @@ implements OnRemoteOperationListener, ComponentsGetter {
     private boolean mRedirectingToSetupAccount = false;
     
     /** Flag to signal when the value of mAccount was set */ 
     private boolean mRedirectingToSetupAccount = false;
     
     /** Flag to signal when the value of mAccount was set */ 
-    private boolean mAccountWasSet;
+    protected boolean mAccountWasSet;
     
     /** Flag to signal when the value of mAccount was restored from a saved state */ 
     
     /** Flag to signal when the value of mAccount was restored from a saved state */ 
-    private boolean mAccountWasRestored;
+    protected boolean mAccountWasRestored;
     
     /** Flag to signal if the activity is launched by a notification */
     private boolean mFromNotification;
     
     /** Flag to signal if the activity is launched by a notification */
     private boolean mFromNotification;
@@ -117,6 +120,8 @@ implements OnRemoteOperationListener, ComponentsGetter {
     protected FileDownloaderBinder mDownloaderBinder = null;
     protected FileUploaderBinder mUploaderBinder = null;
     private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
     protected FileDownloaderBinder mDownloaderBinder = null;
     protected FileUploaderBinder mUploaderBinder = null;
     private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
+
+    private boolean mTryShareAgain = false;
     
     
     /**
     
     
     /**
@@ -131,20 +136,22 @@ implements OnRemoteOperationListener, ComponentsGetter {
         super.onCreate(savedInstanceState);
         mHandler = new Handler();
         mFileOperationsHelper = new FileOperationsHelper(this);
         super.onCreate(savedInstanceState);
         mHandler = new Handler();
         mFileOperationsHelper = new FileOperationsHelper(this);
-        Account account;
+        Account account = null;
         if(savedInstanceState != null) {
         if(savedInstanceState != null) {
-            account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
             mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
             mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
             mFileOperationsHelper.setOpIdWaitingFor(
                     savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID, Long.MAX_VALUE)
                     );
             mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
             mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
             mFileOperationsHelper.setOpIdWaitingFor(
                     savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID, Long.MAX_VALUE)
                     );
+            mTryShareAgain = savedInstanceState.getBoolean(KEY_TRY_SHARE_AGAIN);
         } else {
             account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
             mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
             mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
         }
 
         } else {
             account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
             mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
             mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
         }
 
+        AccountUtils.updateAccountVersion(this); // best place, before any access to AccountManager or database
+
         setAccount(account, savedInstanceState != null);
         
         mOperationsServiceConnection = new OperationsServiceConnection();
         setAccount(account, savedInstanceState != null);
         
         mOperationsServiceConnection = new OperationsServiceConnection();
@@ -158,10 +165,19 @@ implements OnRemoteOperationListener, ComponentsGetter {
         if (mUploadServiceConnection != null) {
             bindService(new Intent(this, FileUploader.class), mUploadServiceConnection, Context.BIND_AUTO_CREATE);
         }
         if (mUploadServiceConnection != null) {
             bindService(new Intent(this, FileUploader.class), mUploadServiceConnection, Context.BIND_AUTO_CREATE);
         }
-        
+
+    }
+
+    @Override
+    protected void onNewIntent (Intent intent) {
+        Log_OC.v(TAG, "onNewIntent() start");
+        Account current = AccountUtils.getCurrentOwnCloudAccount(this);
+        if (current != null && mAccount != null && !mAccount.name.equals(current.name)) {
+            mAccount = current;
+        }
+        Log_OC.v(TAG, "onNewIntent() stop");
     }
 
     }
 
-    
     /**
      *  Since ownCloud {@link Account}s can be managed from the system setting menu, 
      *  the existence of the {@link Account} associated to the instance must be checked 
     /**
      *  Since ownCloud {@link Account}s can be managed from the system setting menu, 
      *  the existence of the {@link Account} associated to the instance must be checked 
@@ -169,11 +185,13 @@ implements OnRemoteOperationListener, ComponentsGetter {
      */
     @Override
     protected void onRestart() {
      */
     @Override
     protected void onRestart() {
+        Log_OC.v(TAG, "onRestart() start");
         super.onRestart();
         super.onRestart();
-        boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name));
+        boolean validAccount = (mAccount != null && AccountUtils.exists(mAccount, this));
         if (!validAccount) {
             swapToDefaultAccount();
         }
         if (!validAccount) {
             swapToDefaultAccount();
         }
+        Log_OC.v(TAG, "onRestart() end");
     }
 
     
     }
 
     
@@ -193,30 +211,10 @@ implements OnRemoteOperationListener, ComponentsGetter {
         if (mOperationsServiceBinder != null) {
             doOnResumeAndBound();
         }
         if (mOperationsServiceBinder != null) {
             doOnResumeAndBound();
         }
-
     }
     
     @Override
     protected void onPause()  {
     }
     
     @Override
     protected void onPause()  {
-        // Save cookies here
-        Log_OC.wtf(TAG, "Saving Cookies" );
-        if (mAccount != null) {
-            try {
-                ((MainApp)getApplicationContext()).getOwnCloudClientManager().
-                    saveClient(mAccount, this);
-                
-                // TODO get rid of the exceptions
-            } catch (AccountNotFoundException e) {
-                e.printStackTrace();
-            } catch (AuthenticatorException e) {
-                e.printStackTrace();
-            } catch (OperationCanceledException e) {
-                e.printStackTrace();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-        
         if (mOperationsServiceBinder != null) {
             mOperationsServiceBinder.removeOperationListener(this);
         }
         if (mOperationsServiceBinder != null) {
             mOperationsServiceBinder.removeOperationListener(this);
         }
@@ -239,6 +237,7 @@ implements OnRemoteOperationListener, ComponentsGetter {
             unbindService(mUploadServiceConnection);
             mUploadServiceConnection = null;
         }
             unbindService(mUploadServiceConnection);
             mUploadServiceConnection = null;
         }
+
         super.onDestroy();
     }
     
         super.onDestroy();
     }
     
@@ -253,9 +252,10 @@ implements OnRemoteOperationListener, ComponentsGetter {
      *  @param account          New {@link Account} to set.
      *  @param savedAccount     When 'true', account was retrieved from a saved instance state.
      */
      *  @param account          New {@link Account} to set.
      *  @param savedAccount     When 'true', account was retrieved from a saved instance state.
      */
-    private void setAccount(Account account, boolean savedAccount) {
+    protected void setAccount(Account account, boolean savedAccount) {
         Account oldAccount = mAccount;
         Account oldAccount = mAccount;
-        boolean validAccount = (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
+        boolean validAccount =
+                (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
         if (validAccount) {
             mAccount = account;
             mAccountWasSet = true;
         if (validAccount) {
             mAccount = account;
             mAccountWasSet = true;
@@ -274,8 +274,6 @@ implements OnRemoteOperationListener, ComponentsGetter {
      *  to create a new ownCloud {@link Account}.
      *  
      *  POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
      *  to create a new ownCloud {@link Account}.
      *  
      *  POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
-     *   
-     *  @return     'True' if the checked {@link Account} was valid.
      */
     private void swapToDefaultAccount() {
         // default to the most recently used account
      */
     private void swapToDefaultAccount() {
         // default to the most recently used account
@@ -317,9 +315,9 @@ implements OnRemoteOperationListener, ComponentsGetter {
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
-        outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
         outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
         outState.putLong(KEY_WAITING_FOR_OP_ID, mFileOperationsHelper.getOpIdWaitingFor());
         outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
         outState.putLong(KEY_WAITING_FOR_OP_ID, mFileOperationsHelper.getOpIdWaitingFor());
+        outState.putBoolean(KEY_TRY_SHARE_AGAIN, mTryShareAgain);
     }
     
     
     }
     
     
@@ -352,6 +350,10 @@ implements OnRemoteOperationListener, ComponentsGetter {
         return mAccount;
     }
 
         return mAccount;
     }
 
+    protected void setAccount(Account account) {
+        mAccount = account;
+    }
+
     /**
      * @return Value of mFromNotification: True if the Activity is launched by a notification
      */
     /**
      * @return Value of mFromNotification: True if the Activity is launched by a notification
      */
@@ -365,7 +367,14 @@ implements OnRemoteOperationListener, ComponentsGetter {
     protected boolean isRedirectingToSetupAccount() {
         return mRedirectingToSetupAccount;
     }
     protected boolean isRedirectingToSetupAccount() {
         return mRedirectingToSetupAccount;
     }
-    
+
+    public boolean isTryShareAgain(){
+        return mTryShareAgain;
+    }
+
+    public void setTryShareAgain(boolean tryShareAgain) {
+       mTryShareAgain = tryShareAgain;
+    }
     
     public OperationsServiceBinder getOperationsServiceBinder() {
         return mOperationsServiceBinder;
     
     public OperationsServiceBinder getOperationsServiceBinder() {
         return mOperationsServiceBinder;
@@ -374,15 +383,12 @@ implements OnRemoteOperationListener, ComponentsGetter {
     protected ServiceConnection newTransferenceServiceConnection() {
         return null;
     }
     protected ServiceConnection newTransferenceServiceConnection() {
         return null;
     }
-    
 
     /**
      * Helper class handling a callback from the {@link AccountManager} after the creation of
      * a new ownCloud {@link Account} finished, successfully or not.
      * 
      * At this moment, only called after the creation of the first account.
 
     /**
      * Helper class handling a callback from the {@link AccountManager} after the creation of
      * a new ownCloud {@link Account} finished, successfully or not.
      * 
      * At this moment, only called after the creation of the first account.
-     * 
-     * @author David A. Velasco
      */
     public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
 
      */
     public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
 
@@ -472,10 +478,12 @@ implements OnRemoteOperationListener, ComponentsGetter {
             
             if (result.getCode() == ResultCode.UNAUTHORIZED) {
                 dismissLoadingDialog();
             
             if (result.getCode() == ResultCode.UNAUTHORIZED) {
                 dismissLoadingDialog();
-                Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()), 
+                Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
+                                operation, getResources()),
                         Toast.LENGTH_LONG);
                 t.show();
             }
                         Toast.LENGTH_LONG);
                 t.show();
             }
+            mTryShareAgain = false;
 
         } else if (operation instanceof CreateShareOperation) {
             onCreateShareOperationFinish((CreateShareOperation) operation, result);
 
         } else if (operation instanceof CreateShareOperation) {
             onCreateShareOperationFinish((CreateShareOperation) operation, result);
@@ -483,7 +491,10 @@ implements OnRemoteOperationListener, ComponentsGetter {
         } else if (operation instanceof UnshareLinkOperation) {
             onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
         
         } else if (operation instanceof UnshareLinkOperation) {
             onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
         
-        } 
+        } else if (operation instanceof SynchronizeFolderOperation) {
+            onSynchronizeFolderOperationFinish((SynchronizeFolderOperation)operation, result);
+
+        }
     }
 
     protected void requestCredentialsUpdate() {
     }
 
     protected void requestCredentialsUpdate() {
@@ -497,18 +508,36 @@ implements OnRemoteOperationListener, ComponentsGetter {
     }
     
 
     }
     
 
-    private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) {
+    private void onCreateShareOperationFinish(CreateShareOperation operation,
+                                              RemoteOperationResult result) {
         dismissLoadingDialog();
         if (result.isSuccess()) {
         dismissLoadingDialog();
         if (result.isSuccess()) {
+            mTryShareAgain = false;
             updateFileFromDB();
             
             Intent sendIntent = operation.getSendIntent();
             startActivity(sendIntent);
             updateFileFromDB();
             
             Intent sendIntent = operation.getSendIntent();
             startActivity(sendIntent);
-            
-        } else { 
-            Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()), 
-                    Toast.LENGTH_LONG);
-            t.show();
+        } else {
+            // Detect Failure (403) --> needs Password
+            if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
+                if (!isTryShareAgain()) {
+                    SharePasswordDialogFragment dialog =
+                            SharePasswordDialogFragment.newInstance(new OCFile(operation.getPath()),
+                                    operation.getSendIntent());
+                    dialog.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD);
+                } else {
+                    Toast t = Toast.makeText(this,
+                        ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
+                        Toast.LENGTH_LONG);
+                    t.show();
+                    mTryShareAgain = false;
+                }
+            } else {
+                Toast t = Toast.makeText(this,
+                        ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
+                        Toast.LENGTH_LONG);
+                t.show();
+            }
         } 
     }
     
         } 
     }
     
@@ -525,13 +554,23 @@ implements OnRemoteOperationListener, ComponentsGetter {
             t.show();
         } 
     }
             t.show();
         } 
     }
+
+    private void onSynchronizeFolderOperationFinish(
+            SynchronizeFolderOperation operation, RemoteOperationResult result
+    ) {
+        if (!result.isSuccess() && result.getCode() != ResultCode.CANCELLED){
+            Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
+                    Toast.LENGTH_LONG);
+            t.show();
+        }
+    }
     
     
-    
-    private void updateFileFromDB(){
-      OCFile file = getStorageManager().getFileByPath(getFile().getRemotePath());
-      if (file != null) {
-          setFile(file);
-      }
+    protected void updateFileFromDB(){
+        OCFile file = getFile();
+        if (file != null) {
+            file = getStorageManager().getFileByPath(file.getRemotePath());
+            setFile(file);
+        }
     }
     
     /**
     }
     
     /**
@@ -612,7 +651,7 @@ implements OnRemoteOperationListener, ComponentsGetter {
     @Override
     public FileUploaderBinder getFileUploaderBinder() {
         return mUploaderBinder;
     @Override
     public FileUploaderBinder getFileUploaderBinder() {
         return mUploaderBinder;
-    };    
+    }
     
     
 }
     
     
 }