+ Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the FileActivities ");
+
+ mFileOperationsHelper.setOpIdWaitingFor(Long.MAX_VALUE);
+
+ if (!result.isSuccess() && (
+ result.getCode() == ResultCode.UNAUTHORIZED ||
+ result.isIdPRedirection() ||
+ (result.isException() && result.getException() instanceof AuthenticatorException)
+ )) {
+
+ requestCredentialsUpdate();
+
+ } else if (operation instanceof CreateShareOperation) {
+ onCreateShareOperationFinish((CreateShareOperation) operation, result);
+
+ } else if (operation instanceof UnshareLinkOperation) {
+ onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
+
+ }
+ }
+
+ private void requestCredentialsUpdate() {
+ Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+ updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
+ updateAccountCredentials.putExtra(
+ AuthenticatorActivity.EXTRA_ACTION,
+ AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
+ updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
+ startActivity(updateAccountCredentials);
+ }
+
+
+ private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) {
+ dismissLoadingDialog();
+ if (result.isSuccess()) {
+ updateFileFromDB();
+
+ Intent sendIntent = operation.getSendIntent();
+ startActivity(sendIntent);
+
+ } else {
+ Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
+ Toast.LENGTH_LONG);
+ t.show();
+ }
+ }
+
+
+ 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);
+ t.show();
+ }
+ }
+
+
+ private void updateFileFromDB(){
+ OCFile file = getStorageManager().getFileByPath(getFile().getRemotePath());
+ if (file != null) {
+ setFile(file);
+ }
+ }
+
+ /**
+ * Show loading dialog
+ */
+ public void showLoadingDialog() {
+ // Construct dialog
+ LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
+ FragmentManager fm = getSupportFragmentManager();
+ FragmentTransaction ft = fm.beginTransaction();
+ loading.show(ft, DIALOG_WAIT_TAG);
+
+ }
+
+
+ /**
+ * Dismiss loading dialog
+ */
+ public void dismissLoadingDialog(){
+ Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
+ if (frag != null) {
+ LoadingDialog loading = (LoadingDialog) frag;
+ loading.dismiss();
+ }
+ }
+
+
+ private void doOnResumeAndBound() {
+ mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler);
+ long waitingForOpId = mFileOperationsHelper.getOpIdWaitingFor();
+ if (waitingForOpId <= Integer.MAX_VALUE) {
+ boolean wait = mOperationsServiceBinder.dispatchResultIfFinished((int)waitingForOpId, this);
+ if (!wait ) {
+ dismissLoadingDialog();
+ }
+ }
+ }
+
+
+ /**
+ * Implements callback methods for service binding. Passed as a parameter to {
+ */
+ private class OperationsServiceConnection implements ServiceConnection {
+
+ @Override
+ public void onServiceConnected(ComponentName component, IBinder service) {
+ if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
+ Log_OC.d(TAG, "Operations service connected");
+ mOperationsServiceBinder = (OperationsServiceBinder) service;
+ /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
+ dismissLoadingDialog();
+ }*/
+ doOnResumeAndBound();
+
+ } else {
+ return;
+ }
+ }
+
+
+ @Override
+ public void onServiceDisconnected(ComponentName component) {
+ if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
+ Log_OC.d(TAG, "Operations service disconnected");
+ mOperationsServiceBinder = null;
+ // TODO whatever could be waiting for the service is unbound
+ }
+ }
+ }
+
+
+ @Override
+ public FileDownloaderBinder getFileDownloaderBinder() {
+ return mDownloaderBinder;