0385833bc89cc88f52074f7ffc719ac02e5eff66
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileActivity.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2011 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 package com.owncloud.android.ui.activity;
23
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.ActionBarDrawerToggle;
39 import android.support.v4.app.Fragment;
40 import android.support.v4.app.FragmentManager;
41 import android.support.v4.app.FragmentTransaction;
42 import android.support.v4.widget.DrawerLayout;
43 import android.support.v7.app.ActionBar;
44 import android.support.v7.app.ActionBarActivity;
45 import android.view.View;
46 import android.widget.AdapterView;
47 import android.widget.ImageView;
48 import android.widget.LinearLayout;
49 import android.widget.ListView;
50 import android.widget.TextView;
51 import android.widget.Toast;
52
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.FileUploader;
63 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
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.lib.resources.files.FileUtils;
71 import com.owncloud.android.operations.CreateShareOperation;
72 import com.owncloud.android.operations.SynchronizeFolderOperation;
73 import com.owncloud.android.operations.UnshareLinkOperation;
74 import com.owncloud.android.services.OperationsService;
75 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
76 import com.owncloud.android.ui.NavigationDrawerItem;
77 import com.owncloud.android.ui.adapter.NavigationDrawerListAdapter;
78 import com.owncloud.android.ui.dialog.LoadingDialog;
79 import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
80 import com.owncloud.android.utils.DisplayUtils;
81 import com.owncloud.android.utils.ErrorMessageAdapter;
82
83 import java.util.ArrayList;
84
85
86 /**
87 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud
88 * {@link Account}s .
89 */
90 public class FileActivity extends ActionBarActivity
91 implements OnRemoteOperationListener, ComponentsGetter {
92
93 public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
94 public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
95 public static final String EXTRA_WAITING_TO_PREVIEW =
96 "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
97 public static final String EXTRA_FROM_NOTIFICATION =
98 "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
99
100 public static final String TAG = FileActivity.class.getSimpleName();
101
102 private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
103 private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID";
104 private static final String DIALOG_SHARE_PASSWORD = "DIALOG_SHARE_PASSWORD";
105 private static final String KEY_TRY_SHARE_AGAIN = "TRY_SHARE_AGAIN";
106
107 protected static final long DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS = 200;
108
109
110 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.*/
111 private Account mAccount;
112
113 /** Main {@link OCFile} handled by the activity.*/
114 private OCFile mFile;
115
116 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud
117 * {@link Account} */
118 private boolean mRedirectingToSetupAccount = false;
119
120 /** Flag to signal when the value of mAccount was set */
121 protected boolean mAccountWasSet;
122
123 /** Flag to signal when the value of mAccount was restored from a saved state */
124 protected boolean mAccountWasRestored;
125
126 /** Flag to signal if the activity is launched by a notification */
127 private boolean mFromNotification;
128
129 /** Messages handler associated to the main thread and the life cycle of the activity */
130 private Handler mHandler;
131
132 /** Access point to the cached database for the current ownCloud {@link Account} */
133 private FileDataStorageManager mStorageManager = null;
134
135 private FileOperationsHelper mFileOperationsHelper;
136
137 private ServiceConnection mOperationsServiceConnection = null;
138
139 private OperationsServiceBinder mOperationsServiceBinder = null;
140
141 protected FileDownloaderBinder mDownloaderBinder = null;
142 protected FileUploaderBinder mUploaderBinder = null;
143 private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
144
145 private boolean mTryShareAgain = false;
146
147 // Navigation Drawer
148 protected DrawerLayout mDrawerLayout;
149 protected ActionBarDrawerToggle mDrawerToggle;
150 protected ListView mDrawerList;
151
152 // Slide menu items
153 protected String[] mDrawerTitles;
154 protected String[] mDrawerContentDescriptions;
155
156 protected ArrayList<NavigationDrawerItem> mDrawerItems;
157
158 protected NavigationDrawerListAdapter mNavigationDrawerAdapter = null;
159
160 protected boolean mShowAccounts = false;
161
162 /**
163 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
164 * the {@link FileActivity}.
165 *
166 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
167 * is requested to create a new one.
168 */
169 @Override
170 protected void onCreate(Bundle savedInstanceState) {
171 super.onCreate(savedInstanceState);
172 mHandler = new Handler();
173 mFileOperationsHelper = new FileOperationsHelper(this);
174 Account account = null;
175 if(savedInstanceState != null) {
176 mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
177 mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
178 mFileOperationsHelper.setOpIdWaitingFor(
179 savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID, Long.MAX_VALUE)
180 );
181 mTryShareAgain = savedInstanceState.getBoolean(KEY_TRY_SHARE_AGAIN);
182 } else {
183 account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
184 mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
185 mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION,
186 false);
187 }
188
189 AccountUtils.updateAccountVersion(this); // best place, before any access to AccountManager
190 // or database
191
192 setAccount(account, savedInstanceState != null);
193
194 mOperationsServiceConnection = new OperationsServiceConnection();
195 bindService(new Intent(this, OperationsService.class), mOperationsServiceConnection,
196 Context.BIND_AUTO_CREATE);
197
198 mDownloadServiceConnection = newTransferenceServiceConnection();
199 if (mDownloadServiceConnection != null) {
200 bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
201 Context.BIND_AUTO_CREATE);
202 }
203 mUploadServiceConnection = newTransferenceServiceConnection();
204 if (mUploadServiceConnection != null) {
205 bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
206 Context.BIND_AUTO_CREATE);
207 }
208
209 }
210
211 @Override
212 protected void onNewIntent (Intent intent) {
213 Log_OC.v(TAG, "onNewIntent() start");
214 Account current = AccountUtils.getCurrentOwnCloudAccount(this);
215 if (current != null && mAccount != null && !mAccount.name.equals(current.name)) {
216 mAccount = current;
217 }
218 Log_OC.v(TAG, "onNewIntent() stop");
219 }
220
221 /**
222 * Since ownCloud {@link Account}s can be managed from the system setting menu,
223 * the existence of the {@link Account} associated to the instance must be checked
224 * every time it is restarted.
225 */
226 @Override
227 protected void onRestart() {
228 Log_OC.v(TAG, "onRestart() start");
229 super.onRestart();
230 boolean validAccount = (mAccount != null && AccountUtils.exists(mAccount, this));
231 if (!validAccount) {
232 swapToDefaultAccount();
233 }
234 Log_OC.v(TAG, "onRestart() end");
235 }
236
237
238 @Override
239 protected void onStart() {
240 super.onStart();
241
242 if (mAccountWasSet) {
243 onAccountSet(mAccountWasRestored);
244 }
245 }
246
247 @Override
248 protected void onResume() {
249 super.onResume();
250
251 if (mOperationsServiceBinder != null) {
252 doOnResumeAndBound();
253 }
254 }
255
256 @Override
257 protected void onPause() {
258 if (mOperationsServiceBinder != null) {
259 mOperationsServiceBinder.removeOperationListener(this);
260 }
261
262 super.onPause();
263 }
264
265
266 @Override
267 protected void onDestroy() {
268 if (mOperationsServiceConnection != null) {
269 unbindService(mOperationsServiceConnection);
270 mOperationsServiceBinder = null;
271 }
272 if (mDownloadServiceConnection != null) {
273 unbindService(mDownloadServiceConnection);
274 mDownloadServiceConnection = null;
275 }
276 if (mUploadServiceConnection != null) {
277 unbindService(mUploadServiceConnection);
278 mUploadServiceConnection = null;
279 }
280
281 super.onDestroy();
282 }
283
284 @Override
285 protected void onPostCreate(Bundle savedInstanceState) {
286 super.onPostCreate(savedInstanceState);
287 // Sync the toggle state after onRestoreInstanceState has occurred.
288 if (mDrawerToggle != null) {
289 mDrawerToggle.syncState();
290 }
291 }
292
293 @Override
294 public void onConfigurationChanged(Configuration newConfig) {
295 super.onConfigurationChanged(newConfig);
296 if (mDrawerToggle != null) {
297 mDrawerToggle.onConfigurationChanged(newConfig);
298 }
299 }
300
301 protected void initDrawer(){
302 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
303 // Notification Drawer
304 LinearLayout navigationDrawerLayout = (LinearLayout) findViewById(R.id.left_drawer);
305 mDrawerList = (ListView) navigationDrawerLayout.findViewById(R.id.drawer_list);
306
307 // load Account in the Drawer Title
308 // User-Icon
309 ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
310 userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
311
312 // Username
313 TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
314 Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
315
316 if (account != null) {
317 int lastAtPos = account.name.lastIndexOf("@");
318 username.setText(account.name.substring(0, lastAtPos));
319 }
320
321 // load slide menu items
322 mDrawerTitles = getResources().getStringArray(R.array.drawer_items);
323
324 // nav drawer content description from resources
325 mDrawerContentDescriptions = getResources().
326 getStringArray(R.array.drawer_content_descriptions);
327
328 // nav drawer items
329 mDrawerItems = new ArrayList<NavigationDrawerItem>();
330 // adding nav drawer items to array
331 // Accounts
332 mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0], mDrawerContentDescriptions[0]));
333 // All Files
334 mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[1], mDrawerContentDescriptions[1]));
335
336 // TODO Enable when "On Device" is recovered
337 // On Device
338 //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
339 // mDrawerContentDescriptions[2]));
340
341 // Settings
342 mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2], mDrawerContentDescriptions[2]));
343 // Logs
344 if (BuildConfig.DEBUG) {
345 mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[3],
346 mDrawerContentDescriptions[3]));
347 }
348
349 // setting the nav drawer list adapter
350 mNavigationDrawerAdapter = new NavigationDrawerListAdapter(getApplicationContext(), this,
351 mDrawerItems);
352 mDrawerList.setAdapter(mNavigationDrawerAdapter);
353
354 mDrawerToggle = new ActionBarDrawerToggle(
355 this,
356 mDrawerLayout,
357 R.drawable.ic_drawer,
358 R.string.drawer_open,
359 R.string.drawer_close) {
360
361 /** Called when a drawer has settled in a completely closed state. */
362 public void onDrawerClosed(View view) {
363 super.onDrawerClosed(view);
364 updateActionBarTitle();
365 getSupportActionBar().setDisplayShowTitleEnabled(true);
366 getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
367 invalidateOptionsMenu();
368 }
369
370 /** Called when a drawer has settled in a completely open state. */
371 public void onDrawerOpened(View drawerView) {
372 super.onDrawerOpened(drawerView);
373 getSupportActionBar().setTitle(R.string.drawer_open);
374 getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
375 invalidateOptionsMenu();
376 }
377 };
378
379 mDrawerToggle.setDrawerIndicatorEnabled(true);
380 // Set the list's click listener
381 mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
382
383 // Set the drawer toggle as the DrawerListener
384 mDrawerLayout.setDrawerListener(mDrawerToggle);
385 }
386
387 protected void updateActionBarTitle(){
388 if (mFile.getParentId() == 0) {
389 getSupportActionBar().setTitle(getString(
390 R.string.default_display_name_for_root_folder));
391 } else {
392 getSupportActionBar().setTitle(mFile.getFileName().toString());
393 }
394 }
395 /**
396 * Sets and validates the ownCloud {@link Account} associated to the Activity.
397 *
398 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
399 *
400 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
401 *
402 * @param account New {@link Account} to set.
403 * @param savedAccount When 'true', account was retrieved from a saved instance state.
404 */
405 protected void setAccount(Account account, boolean savedAccount) {
406 Account oldAccount = mAccount;
407 boolean validAccount =
408 (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(),
409 account.name));
410 if (validAccount) {
411 mAccount = account;
412 mAccountWasSet = true;
413 mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));
414
415 } else {
416 swapToDefaultAccount();
417 }
418 }
419
420
421 /**
422 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
423 *
424 * If no valid ownCloud {@link Account} exists, the the user is requested
425 * to create a new ownCloud {@link Account}.
426 *
427 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
428 */
429 private void swapToDefaultAccount() {
430 // default to the most recently used account
431 Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
432 if (newAccount == null) {
433 /// no account available: force account creation
434 createFirstAccount();
435 mRedirectingToSetupAccount = true;
436 mAccountWasSet = false;
437 mAccountWasRestored = false;
438
439 } else {
440 mAccountWasSet = true;
441 mAccountWasRestored = (newAccount.equals(mAccount));
442 mAccount = newAccount;
443 }
444 }
445
446
447 /**
448 * Launches the account creation activity. To use when no ownCloud account is available
449 */
450 private void createFirstAccount() {
451 AccountManager am = AccountManager.get(getApplicationContext());
452 am.addAccount(MainApp.getAccountType(),
453 null,
454 null,
455 null,
456 this,
457 new AccountCreationCallback(),
458 null);
459 }
460
461
462 /**
463 * {@inheritDoc}
464 */
465 @Override
466 protected void onSaveInstanceState(Bundle outState) {
467 super.onSaveInstanceState(outState);
468 outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
469 outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
470 outState.putLong(KEY_WAITING_FOR_OP_ID, mFileOperationsHelper.getOpIdWaitingFor());
471 outState.putBoolean(KEY_TRY_SHARE_AGAIN, mTryShareAgain);
472 }
473
474
475 /**
476 * Getter for the main {@link OCFile} handled by the activity.
477 *
478 * @return Main {@link OCFile} handled by the activity.
479 */
480 public OCFile getFile() {
481 return mFile;
482 }
483
484
485 /**
486 * Setter for the main {@link OCFile} handled by the activity.
487 *
488 * @param file Main {@link OCFile} to be handled by the activity.
489 */
490 public void setFile(OCFile file) {
491 mFile = file;
492 }
493
494
495 /**
496 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
497 * is located.
498 *
499 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
500 * is located.
501 */
502 public Account getAccount() {
503 return mAccount;
504 }
505
506 protected void setAccount(Account account) {
507 mAccount = account;
508 }
509
510 /**
511 * @return Value of mFromNotification: True if the Activity is launched by a notification
512 */
513 public boolean fromNotification() {
514 return mFromNotification;
515 }
516
517 /**
518 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
519 */
520 protected boolean isRedirectingToSetupAccount() {
521 return mRedirectingToSetupAccount;
522 }
523
524 public boolean isTryShareAgain(){
525 return mTryShareAgain;
526 }
527
528 public void setTryShareAgain(boolean tryShareAgain) {
529 mTryShareAgain = tryShareAgain;
530 }
531
532 public OperationsServiceBinder getOperationsServiceBinder() {
533 return mOperationsServiceBinder;
534 }
535
536 protected ServiceConnection newTransferenceServiceConnection() {
537 return null;
538 }
539
540 /**
541 * Helper class handling a callback from the {@link AccountManager} after the creation of
542 * a new ownCloud {@link Account} finished, successfully or not.
543 *
544 * At this moment, only called after the creation of the first account.
545 */
546 public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
547
548 @Override
549 public void run(AccountManagerFuture<Bundle> future) {
550 FileActivity.this.mRedirectingToSetupAccount = false;
551 boolean accountWasSet = false;
552 if (future != null) {
553 try {
554 Bundle result;
555 result = future.getResult();
556 String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
557 String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
558 if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
559 setAccount(new Account(name, type), false);
560 accountWasSet = true;
561 }
562 } catch (OperationCanceledException e) {
563 Log_OC.d(TAG, "Account creation canceled");
564
565 } catch (Exception e) {
566 Log_OC.e(TAG, "Account creation finished in exception: ", e);
567 }
568
569 } else {
570 Log_OC.e(TAG, "Account creation callback with null bundle");
571 }
572 if (!accountWasSet) {
573 moveTaskToBack(true);
574 }
575 }
576
577 }
578
579
580 /**
581 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
582 *
583 * Child classes must grant that state depending on the {@link Account} is updated.
584 */
585 protected void onAccountSet(boolean stateWasRecovered) {
586 if (getAccount() != null) {
587 mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
588
589 } else {
590 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
591 }
592 }
593
594
595 public FileDataStorageManager getStorageManager() {
596 return mStorageManager;
597 }
598
599
600 public OnRemoteOperationListener getRemoteOperationListener() {
601 return this;
602 }
603
604
605 public Handler getHandler() {
606 return mHandler;
607 }
608
609 public FileOperationsHelper getFileOperationsHelper() {
610 return mFileOperationsHelper;
611 }
612
613 /**
614 *
615 * @param operation Removal operation performed.
616 * @param result Result of the removal.
617 */
618 @Override
619 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
620 Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the "
621 + "FileActivities ");
622
623 mFileOperationsHelper.setOpIdWaitingFor(Long.MAX_VALUE);
624
625 if (!result.isSuccess() && (
626 result.getCode() == ResultCode.UNAUTHORIZED ||
627 result.isIdPRedirection() ||
628 (result.isException() && result.getException() instanceof AuthenticatorException)
629 )) {
630
631 requestCredentialsUpdate();
632
633 if (result.getCode() == ResultCode.UNAUTHORIZED) {
634 dismissLoadingDialog();
635 Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
636 operation, getResources()),
637 Toast.LENGTH_LONG);
638 t.show();
639 }
640 mTryShareAgain = false;
641
642 } else if (operation instanceof CreateShareOperation) {
643 onCreateShareOperationFinish((CreateShareOperation) operation, result);
644
645 } else if (operation instanceof UnshareLinkOperation) {
646 onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
647
648 } else if (operation instanceof SynchronizeFolderOperation) {
649 onSynchronizeFolderOperationFinish((SynchronizeFolderOperation)operation, result);
650
651 }
652 }
653
654 protected void requestCredentialsUpdate() {
655 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
656 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
657 updateAccountCredentials.putExtra(
658 AuthenticatorActivity.EXTRA_ACTION,
659 AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
660 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
661 startActivity(updateAccountCredentials);
662 }
663
664
665 private void onCreateShareOperationFinish(CreateShareOperation operation,
666 RemoteOperationResult result) {
667 dismissLoadingDialog();
668 if (result.isSuccess()) {
669 mTryShareAgain = false;
670 updateFileFromDB();
671
672 Intent sendIntent = operation.getSendIntent();
673 startActivity(sendIntent);
674 } else {
675 // Detect Failure (403) --> needs Password
676 if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
677 if (!isTryShareAgain()) {
678 SharePasswordDialogFragment dialog =
679 SharePasswordDialogFragment.newInstance(new OCFile(operation.getPath()),
680 operation.getSendIntent());
681 dialog.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD);
682 } else {
683 Toast t = Toast.makeText(this,
684 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
685 Toast.LENGTH_LONG);
686 t.show();
687 mTryShareAgain = false;
688 }
689 } else {
690 Toast t = Toast.makeText(this,
691 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
692 Toast.LENGTH_LONG);
693 t.show();
694 }
695 }
696 }
697
698
699 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation,
700 RemoteOperationResult result) {
701 dismissLoadingDialog();
702
703 if (result.isSuccess()){
704 updateFileFromDB();
705
706 } else {
707 Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
708 operation, getResources()), Toast.LENGTH_LONG);
709 t.show();
710 }
711 }
712
713 private void onSynchronizeFolderOperationFinish(
714 SynchronizeFolderOperation operation, RemoteOperationResult result
715 ) {
716 if (!result.isSuccess() && result.getCode() != ResultCode.CANCELLED){
717 Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
718 operation, getResources()), Toast.LENGTH_LONG);
719 t.show();
720 }
721 }
722
723 protected void updateFileFromDB(){
724 OCFile file = getFile();
725 if (file != null) {
726 file = getStorageManager().getFileByPath(file.getRemotePath());
727 setFile(file);
728 }
729 }
730
731 /**
732 * Show loading dialog
733 */
734 public void showLoadingDialog() {
735 // Construct dialog
736 LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
737 FragmentManager fm = getSupportFragmentManager();
738 FragmentTransaction ft = fm.beginTransaction();
739 loading.show(ft, DIALOG_WAIT_TAG);
740
741 }
742
743
744 /**
745 * Dismiss loading dialog
746 */
747 public void dismissLoadingDialog(){
748 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
749 if (frag != null) {
750 LoadingDialog loading = (LoadingDialog) frag;
751 loading.dismiss();
752 }
753 }
754
755
756 private void doOnResumeAndBound() {
757 mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler);
758 long waitingForOpId = mFileOperationsHelper.getOpIdWaitingFor();
759 if (waitingForOpId <= Integer.MAX_VALUE) {
760 boolean wait = mOperationsServiceBinder.dispatchResultIfFinished((int)waitingForOpId,
761 this);
762 if (!wait ) {
763 dismissLoadingDialog();
764 }
765 }
766 }
767
768
769 /**
770 * Implements callback methods for service binding. Passed as a parameter to {
771 */
772 private class OperationsServiceConnection implements ServiceConnection {
773
774 @Override
775 public void onServiceConnected(ComponentName component, IBinder service) {
776 if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
777 Log_OC.d(TAG, "Operations service connected");
778 mOperationsServiceBinder = (OperationsServiceBinder) service;
779 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
780 dismissLoadingDialog();
781 }*/
782 doOnResumeAndBound();
783
784 } else {
785 return;
786 }
787 }
788
789
790 @Override
791 public void onServiceDisconnected(ComponentName component) {
792 if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
793 Log_OC.d(TAG, "Operations service disconnected");
794 mOperationsServiceBinder = null;
795 // TODO whatever could be waiting for the service is unbound
796 }
797 }
798 }
799
800
801 @Override
802 public FileDownloaderBinder getFileDownloaderBinder() {
803 return mDownloaderBinder;
804 }
805
806
807 @Override
808 public FileUploaderBinder getFileUploaderBinder() {
809 return mUploaderBinder;
810 }
811
812
813 public void restart(){
814 Intent i = new Intent(this, FileDisplayActivity.class);
815 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
816 startActivity(i);
817 }
818
819 public void closeDrawer() {
820 mDrawerLayout.closeDrawers();
821 }
822
823 public void allFilesOption(){
824 restart();
825 }
826
827 private class DrawerItemClickListener implements ListView.OnItemClickListener {
828 @Override
829 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
830 if (mShowAccounts && position > 0){
831 position = position - 1;
832 }
833 switch (position){
834 case 0: // Accounts
835 mShowAccounts = !mShowAccounts;
836 mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
837 mNavigationDrawerAdapter.notifyDataSetChanged();
838 break;
839
840 case 1: // All Files
841 allFilesOption();
842 mDrawerLayout.closeDrawers();
843 break;
844
845 // TODO Enable when "On Device" is recovered ?
846 // case 2:
847 // MainApp.showOnlyFilesOnDevice(true);
848 // mDrawerLayout.closeDrawers();
849 // break;
850
851 case 2: // Settings
852 Intent settingsIntent = new Intent(getApplicationContext(),
853 Preferences.class);
854 startActivity(settingsIntent);
855 break;
856
857 case 3: // Logs
858 Intent loggerIntent = new Intent(getApplicationContext(),
859 LogHistoryActivity.class);
860 startActivity(loggerIntent);
861 break;
862 }
863 }
864 }
865 }