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