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