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