X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/83ad608faadfcfc5a0ceee9d6c460702b3ff3f11..a75ffde65f732d695f557bdfe7d954c40cfae4d4:/src/com/owncloud/android/ui/activity/FileActivity.java diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 54efc6f1..9cf695d4 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -1,6 +1,9 @@ -/* ownCloud Android client application +/** + * ownCloud Android client application + * + * @author David A. Velasco * Copyright (C) 2011 Bartek Przybylski - * Copyright (C) 2012-2015 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, @@ -23,7 +26,6 @@ import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.accounts.AuthenticatorException; -import android.accounts.OnAccountsUpdateListener; import android.accounts.OperationCanceledException; import android.content.ComponentName; import android.content.Context; @@ -35,9 +37,9 @@ import android.os.IBinder; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import android.support.v7.app.ActionBarActivity; import android.widget.Toast; -import com.actionbarsherlock.app.SherlockFragmentActivity; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; @@ -57,48 +59,52 @@ import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.operations.CreateShareOperation; import com.owncloud.android.operations.SynchronizeFolderOperation; 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.ui.dialog.SharePasswordDialogFragment; import com.owncloud.android.utils.ErrorMessageAdapter; /** - * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s . - * - * @author David A. Velasco + * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud + * {@link Account}s . */ -public class FileActivity extends SherlockFragmentActivity - implements OnRemoteOperationListener, ComponentsGetter, OnAccountsUpdateListener { +public class FileActivity extends ActionBarActivity + 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_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION"; + 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 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 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.*/ private Account mAccount; /** Main {@link OCFile} handled by the activity.*/ private OCFile mFile; - /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */ + /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud + * {@link Account} */ 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 */ - private boolean mAccountWasRestored; + protected boolean mAccountWasRestored; /** Flag to signal if the activity is launched by a notification */ private boolean mFromNotification; @@ -118,6 +124,8 @@ public class FileActivity extends SherlockFragmentActivity protected FileDownloaderBinder mDownloaderBinder = null; protected FileUploaderBinder mUploaderBinder = null; private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null; + + private boolean mTryShareAgain = false; /** @@ -132,41 +140,53 @@ public class FileActivity extends SherlockFragmentActivity super.onCreate(savedInstanceState); mHandler = new Handler(); mFileOperationsHelper = new FileOperationsHelper(this); - Account account; + Account account = 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) ); + 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); + 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(); - bindService(new Intent(this, OperationsService.class), mOperationsServiceConnection, Context.BIND_AUTO_CREATE); + bindService(new Intent(this, OperationsService.class), mOperationsServiceConnection, + Context.BIND_AUTO_CREATE); mDownloadServiceConnection = newTransferenceServiceConnection(); if (mDownloadServiceConnection != null) { - bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection, Context.BIND_AUTO_CREATE); + bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection, + Context.BIND_AUTO_CREATE); } mUploadServiceConnection = newTransferenceServiceConnection(); if (mUploadServiceConnection != null) { - bindService(new Intent(this, FileUploader.class), mUploadServiceConnection, Context.BIND_AUTO_CREATE); + bindService(new Intent(this, FileUploader.class), mUploadServiceConnection, + Context.BIND_AUTO_CREATE); } - // add AccountsUpdatedListener - AccountManager am = AccountManager.get(getApplicationContext()); - am.addOnAccountsUpdatedListener(this, null, false); - } - + @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 @@ -174,11 +194,13 @@ public class FileActivity extends SherlockFragmentActivity */ @Override protected void onRestart() { + Log_OC.v(TAG, "onRestart() start"); super.onRestart(); - boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name)); + boolean validAccount = (mAccount != null && AccountUtils.exists(mAccount, this)); if (!validAccount) { swapToDefaultAccount(); } + Log_OC.v(TAG, "onRestart() end"); } @@ -198,12 +220,10 @@ public class FileActivity extends SherlockFragmentActivity if (mOperationsServiceBinder != null) { doOnResumeAndBound(); } - } @Override protected void onPause() { - if (mOperationsServiceBinder != null) { mOperationsServiceBinder.removeOperationListener(this); } @@ -227,10 +247,6 @@ public class FileActivity extends SherlockFragmentActivity mUploadServiceConnection = null; } - // remove AccountsUpdatedListener - AccountManager am = AccountManager.get(getApplicationContext()); - am.removeOnAccountsUpdatedListener(this); - super.onDestroy(); } @@ -245,9 +261,11 @@ public class FileActivity extends SherlockFragmentActivity * @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; - boolean validAccount = (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name)); + boolean validAccount = + (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), + account.name)); if (validAccount) { mAccount = account; mAccountWasSet = true; @@ -297,7 +315,6 @@ public class FileActivity extends SherlockFragmentActivity this, new AccountCreationCallback(), null); - am.addOnAccountsUpdatedListener(this, null, true); } @@ -308,9 +325,9 @@ public class FileActivity extends SherlockFragmentActivity 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(KEY_TRY_SHARE_AGAIN, mTryShareAgain); } @@ -335,14 +352,20 @@ public class FileActivity extends SherlockFragmentActivity /** - * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located. + * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity + * is located. * - * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. + * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity + * is located. */ public Account getAccount() { return mAccount; } + protected void setAccount(Account account) { + mAccount = account; + } + /** * @return Value of mFromNotification: True if the Activity is launched by a notification */ @@ -356,7 +379,14 @@ public class FileActivity extends SherlockFragmentActivity protected boolean isRedirectingToSetupAccount() { return mRedirectingToSetupAccount; } - + + public boolean isTryShareAgain(){ + return mTryShareAgain; + } + + public void setTryShareAgain(boolean tryShareAgain) { + mTryShareAgain = tryShareAgain; + } public OperationsServiceBinder getOperationsServiceBinder() { return mOperationsServiceBinder; @@ -366,26 +396,11 @@ public class FileActivity extends SherlockFragmentActivity return null; } - @Override - public void onAccountsUpdated(Account[] accounts) { - // detect a change in the list of accounts - Log_OC.d(TAG, "onAccountsUpdated"); - if (mDownloaderBinder != null) { - mDownloaderBinder.checkAccountOfCurrentDownload(); - } - if (mUploaderBinder != null) { - mUploaderBinder.checkAccountOfCurrentUpload(); - } - } - - /** * 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 { @@ -461,7 +476,8 @@ public class FileActivity extends SherlockFragmentActivity */ @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { - Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the FileActivities "); + Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the " + + "FileActivities "); mFileOperationsHelper.setOpIdWaitingFor(Long.MAX_VALUE); @@ -475,10 +491,12 @@ public class FileActivity extends SherlockFragmentActivity 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(); } + mTryShareAgain = false; } else if (operation instanceof CreateShareOperation) { onCreateShareOperationFinish((CreateShareOperation) operation, result); @@ -503,39 +521,60 @@ public class FileActivity extends SherlockFragmentActivity } - private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) { + private void onCreateShareOperationFinish(CreateShareOperation operation, + RemoteOperationResult result) { dismissLoadingDialog(); if (result.isSuccess()) { + mTryShareAgain = false; 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(); + } } } - private void onUnshareLinkOperationFinish(UnshareLinkOperation operation, RemoteOperationResult result) { + private void onUnshareLinkOperationFinish(UnshareLinkOperation operation, + RemoteOperationResult result) { dismissLoadingDialog(); if (result.isSuccess()){ updateFileFromDB(); } else { - Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()), - Toast.LENGTH_LONG); + Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, + operation, getResources()), Toast.LENGTH_LONG); t.show(); } } - private void onSynchronizeFolderOperationFinish(SynchronizeFolderOperation operation, RemoteOperationResult result) { + 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); + Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, + operation, getResources()), Toast.LENGTH_LONG); t.show(); } } @@ -577,7 +616,8 @@ public class FileActivity extends SherlockFragmentActivity mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler); long waitingForOpId = mFileOperationsHelper.getOpIdWaitingFor(); if (waitingForOpId <= Integer.MAX_VALUE) { - boolean wait = mOperationsServiceBinder.dispatchResultIfFinished((int)waitingForOpId, this); + boolean wait = mOperationsServiceBinder.dispatchResultIfFinished((int)waitingForOpId, + this); if (!wait ) { dismissLoadingDialog(); }