Added operation to create shares with a sharee, separated from operation to create...
[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.view.View;
47 import android.widget.AdapterView;
48 import android.widget.ListView;
49 import android.widget.RelativeLayout;
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.FileDownloader.FileDownloaderBinder;
63 import com.owncloud.android.files.services.FileUploader;
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.operations.CreateShareViaLinkOperation;
71 import com.owncloud.android.operations.CreateShareWithShareeOperation;
72 import com.owncloud.android.operations.SynchronizeFileOperation;
73 import com.owncloud.android.operations.SynchronizeFolderOperation;
74 import com.owncloud.android.operations.UnshareOperation;
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 CreateShareViaLinkOperation) {
733 onCreateShareViaLinkOperationFinish((CreateShareViaLinkOperation) operation, result);
734
735 } else if (operation instanceof CreateShareWithShareeOperation) {
736 onCreateShareWithShareeOperationFinish((CreateShareWithShareeOperation) operation, result);
737
738 } else if (operation instanceof UnshareOperation) {
739 onUnshareLinkOperationFinish((UnshareOperation) operation, result);
740
741 } else if (operation instanceof SynchronizeFolderOperation) {
742 onSynchronizeFolderOperationFinish((SynchronizeFolderOperation) operation, result);
743
744 }else if (operation instanceof SynchronizeFileOperation) {
745 onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);
746
747 }
748 }
749
750 protected void requestCredentialsUpdate() {
751 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
752 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
753 updateAccountCredentials.putExtra(
754 AuthenticatorActivity.EXTRA_ACTION,
755 AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
756 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
757 startActivity(updateAccountCredentials);
758 }
759
760
761
762 private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation operation,
763 RemoteOperationResult result) {
764 dismissLoadingDialog();
765 if (result.isSuccess()) {
766 mTryShareAgain = false;
767 updateFileFromDB();
768
769 Intent sendIntent = operation.getSendIntentWithSubject(this);
770 startActivity(sendIntent);
771 } else {
772 // Detect Failure (403) --> needs Password
773 if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
774 if (!isTryShareAgain()) {
775 SharePasswordDialogFragment dialog =
776 SharePasswordDialogFragment.newInstance(new OCFile(operation.getPath()),
777 operation.getSendIntent());
778 dialog.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD);
779 } else {
780 Toast t = Toast.makeText(this,
781 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
782 Toast.LENGTH_LONG);
783 t.show();
784 mTryShareAgain = false;
785 }
786 } else {
787 Toast t = Toast.makeText(this,
788 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
789 Toast.LENGTH_LONG);
790 t.show();
791 }
792 }
793 }
794
795 private void onCreateShareWithShareeOperationFinish(CreateShareWithShareeOperation operation,
796 RemoteOperationResult result) {
797 dismissLoadingDialog();
798 if (result.isSuccess()) {
799 updateFileFromDB();
800
801 } else {
802 Toast t = Toast.makeText(this,
803 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
804 Toast.LENGTH_LONG);
805 t.show();
806 }
807 }
808
809 private void onUnshareLinkOperationFinish(UnshareOperation operation,
810 RemoteOperationResult result) {
811 dismissLoadingDialog();
812
813 if (result.isSuccess()){
814 updateFileFromDB();
815
816 } else {
817 Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
818 operation, getResources()), Toast.LENGTH_LONG);
819 t.show();
820 }
821 }
822
823 private void onSynchronizeFolderOperationFinish(
824 SynchronizeFolderOperation operation, RemoteOperationResult result
825 ) {
826 if (!result.isSuccess() && result.getCode() != ResultCode.CANCELLED){
827 Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
828 operation, getResources()), Toast.LENGTH_LONG);
829 t.show();
830 }
831 }
832
833 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation,
834 RemoteOperationResult result) {
835 dismissLoadingDialog();
836 OCFile syncedFile = operation.getLocalFile();
837 if (!result.isSuccess()) {
838 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
839 Intent i = new Intent(this, ConflictsResolveActivity.class);
840 i.putExtra(ConflictsResolveActivity.EXTRA_FILE, syncedFile);
841 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, getAccount());
842 startActivity(i);
843 }
844
845 } else {
846 if (!operation.transferWasRequested()) {
847 Toast msg = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
848 operation, getResources()), Toast.LENGTH_LONG);
849 msg.show();
850 }
851 invalidateOptionsMenu();
852 }
853 }
854
855 protected void updateFileFromDB(){
856 OCFile file = getFile();
857 if (file != null) {
858 file = getStorageManager().getFileByPath(file.getRemotePath());
859 setFile(file);
860 }
861 }
862
863
864 /**
865 * Show loading dialog
866 */
867 public void showLoadingDialog(String message) {
868 // Construct dialog
869 LoadingDialog loading = new LoadingDialog(message);
870 FragmentManager fm = getSupportFragmentManager();
871 FragmentTransaction ft = fm.beginTransaction();
872 loading.show(ft, DIALOG_WAIT_TAG);
873
874 }
875
876
877 /**
878 * Dismiss loading dialog
879 */
880 public void dismissLoadingDialog() {
881 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
882 if (frag != null) {
883 LoadingDialog loading = (LoadingDialog) frag;
884 loading.dismiss();
885 }
886 }
887
888
889 private void doOnResumeAndBound() {
890 mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler);
891 long waitingForOpId = mFileOperationsHelper.getOpIdWaitingFor();
892 if (waitingForOpId <= Integer.MAX_VALUE) {
893 boolean wait = mOperationsServiceBinder.dispatchResultIfFinished((int)waitingForOpId,
894 this);
895 if (!wait ) {
896 dismissLoadingDialog();
897 }
898 }
899 }
900
901
902 /**
903 * Implements callback methods for service binding. Passed as a parameter to {
904 */
905 private class OperationsServiceConnection implements ServiceConnection {
906
907 @Override
908 public void onServiceConnected(ComponentName component, IBinder service) {
909 if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
910 Log_OC.d(TAG, "Operations service connected");
911 mOperationsServiceBinder = (OperationsServiceBinder) service;
912 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
913 dismissLoadingDialog();
914 }*/
915 doOnResumeAndBound();
916
917 } else {
918 return;
919 }
920 }
921
922
923 @Override
924 public void onServiceDisconnected(ComponentName component) {
925 if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
926 Log_OC.d(TAG, "Operations service disconnected");
927 mOperationsServiceBinder = null;
928 // TODO whatever could be waiting for the service is unbound
929 }
930 }
931 }
932
933
934 @Override
935 public FileDownloaderBinder getFileDownloaderBinder() {
936 return mDownloaderBinder;
937 }
938
939
940 @Override
941 public FileUploaderBinder getFileUploaderBinder() {
942 return mUploaderBinder;
943 }
944
945
946 public void restart(){
947 Intent i = new Intent(this, FileDisplayActivity.class);
948 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
949 startActivity(i);
950 }
951
952 // TODO re-enable when "Accounts" is available in Navigation Drawer
953 // public void closeDrawer() {
954 // mDrawerLayout.closeDrawers();
955 // }
956
957 public void allFilesOption(){
958 restart();
959 }
960
961 private class DrawerItemClickListener implements ListView.OnItemClickListener {
962 @Override
963 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
964 // TODO re-enable when "Accounts" is available in Navigation Drawer
965 // if (mShowAccounts && position > 0){
966 // position = position - 1;
967 // }
968 switch (position){
969 // TODO re-enable when "Accounts" is available in Navigation Drawer
970 // case 0: // Accounts
971 // mShowAccounts = !mShowAccounts;
972 // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
973 // mNavigationDrawerAdapter.notifyDataSetChanged();
974 // break;
975
976 case 0: // All Files
977 allFilesOption();
978 mDrawerLayout.closeDrawers();
979 break;
980
981 // TODO Enable when "On Device" is recovered ?
982 // case 2:
983 // MainApp.showOnlyFilesOnDevice(true);
984 // mDrawerLayout.closeDrawers();
985 // break;
986
987 case 1: // Settings
988 Intent settingsIntent = new Intent(getApplicationContext(),
989 Preferences.class);
990 startActivity(settingsIntent);
991 mDrawerLayout.closeDrawers();
992 break;
993
994 case 2: // Logs
995 Intent loggerIntent = new Intent(getApplicationContext(),
996 LogHistoryActivity.class);
997 startActivity(loggerIntent);
998 mDrawerLayout.closeDrawers();
999 break;
1000 }
1001 }
1002 }
1003 }