added sorting icon from material, spring cleaning in lower resolution icons and obsol...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 package com.owncloud.android.ui.activity;
24
25 import android.accounts.Account;
26 import android.accounts.AccountManager;
27 import android.accounts.AuthenticatorException;
28 import android.annotation.TargetApi;
29 import android.support.v7.app.AlertDialog;
30 import android.content.BroadcastReceiver;
31 import android.content.ComponentName;
32 import android.content.ContentResolver;
33 import android.content.Context;
34 import android.content.DialogInterface;
35 import android.content.Intent;
36 import android.content.IntentFilter;
37 import android.content.ServiceConnection;
38 import android.content.SharedPreferences;
39 import android.content.SyncRequest;
40 import android.content.res.Resources.NotFoundException;
41 import android.database.Cursor;
42 import android.net.Uri;
43 import android.os.Build;
44 import android.os.Bundle;
45 import android.os.IBinder;
46 import android.preference.PreferenceManager;
47 import android.provider.OpenableColumns;
48 import android.support.v4.app.Fragment;
49 import android.support.v4.app.FragmentManager;
50 import android.support.v4.app.FragmentTransaction;
51 import android.support.v4.content.ContextCompat;
52 import android.support.v4.view.GravityCompat;
53 import android.view.Menu;
54 import android.view.MenuInflater;
55 import android.view.MenuItem;
56 import android.view.View;
57 import android.widget.ProgressBar;
58 import android.widget.RelativeLayout;
59 import android.widget.TextView;
60 import android.widget.Toast;
61
62 import com.owncloud.android.MainApp;
63 import com.owncloud.android.R;
64 import com.owncloud.android.datamodel.FileDataStorageManager;
65 import com.owncloud.android.datamodel.OCFile;
66 import com.owncloud.android.files.services.FileDownloader;
67 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
68 import com.owncloud.android.files.services.FileUploader;
69 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
70 import com.owncloud.android.lib.common.OwnCloudAccount;
71 import com.owncloud.android.lib.common.OwnCloudClient;
72 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
73 import com.owncloud.android.lib.common.OwnCloudCredentials;
74 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
75 import com.owncloud.android.lib.common.network.CertificateCombinedException;
76 import com.owncloud.android.lib.common.operations.RemoteOperation;
77 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
78 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
79 import com.owncloud.android.lib.common.utils.Log_OC;
80 import com.owncloud.android.operations.CopyFileOperation;
81 import com.owncloud.android.operations.CreateFolderOperation;
82 import com.owncloud.android.operations.CreateShareOperation;
83 import com.owncloud.android.operations.MoveFileOperation;
84 import com.owncloud.android.operations.RefreshFolderOperation;
85 import com.owncloud.android.operations.RemoveFileOperation;
86 import com.owncloud.android.operations.RenameFileOperation;
87 import com.owncloud.android.operations.SynchronizeFileOperation;
88 import com.owncloud.android.operations.UnshareLinkOperation;
89 import com.owncloud.android.services.observer.FileObserverService;
90 import com.owncloud.android.syncadapter.FileSyncAdapter;
91 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
92 import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
93 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
94 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
95 import com.owncloud.android.ui.dialog.UploadSourceDialogFragment;
96 import com.owncloud.android.ui.fragment.FileDetailFragment;
97 import com.owncloud.android.ui.fragment.FileFragment;
98 import com.owncloud.android.ui.fragment.OCFileListFragment;
99 import com.owncloud.android.ui.preview.PreviewImageActivity;
100 import com.owncloud.android.ui.preview.PreviewImageFragment;
101 import com.owncloud.android.ui.preview.PreviewMediaFragment;
102 import com.owncloud.android.ui.preview.PreviewTextFragment;
103 import com.owncloud.android.ui.preview.PreviewVideoActivity;
104 import com.owncloud.android.utils.DisplayUtils;
105 import com.owncloud.android.utils.ErrorMessageAdapter;
106 import com.owncloud.android.utils.FileStorageUtils;
107 import com.owncloud.android.utils.UriUtils;
108
109 import java.io.File;
110
111 /**
112 * Displays, what files the user has available in his ownCloud.
113 */
114
115 public class FileDisplayActivity extends HookActivity
116 implements FileFragment.ContainerActivity,
117 OnSslUntrustedCertListener, OnEnforceableRefreshListener {
118
119
120
121 private SyncBroadcastReceiver mSyncBroadcastReceiver;
122 private UploadFinishReceiver mUploadFinishReceiver;
123 private DownloadFinishReceiver mDownloadFinishReceiver;
124 private RemoteOperationResult mLastSslUntrustedServerResult = null;
125
126 private boolean mDualPane;
127 private View mLeftFragmentContainer;
128 private View mRightFragmentContainer;
129 private ProgressBar mProgressBar;
130
131 private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
132 private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
133 private static final String KEY_WAITING_TO_SEND = "WAITING_TO_SEND";
134
135 public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS";
136
137 public static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;
138 public static final int ACTION_SELECT_MULTIPLE_FILES = 2;
139 public static final int ACTION_MOVE_FILES = 3;
140 public static final int ACTION_COPY_FILES = 4;
141
142 private static final String TAG = FileDisplayActivity.class.getSimpleName();
143
144 private static final String TAG_LIST_OF_FILES = "LIST_OF_FILES";
145 private static final String TAG_SECOND_FRAGMENT = "SECOND_FRAGMENT";
146
147 private OCFile mWaitingToPreview;
148
149 private boolean mSyncInProgress = false;
150
151 private static String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT";
152 public static String DIALOG_CREATE_FOLDER = "DIALOG_CREATE_FOLDER";
153 private static String DIALOG_UPLOAD_SOURCE = "DIALOG_UPLOAD_SOURCE";
154 private static String DIALOG_CERT_NOT_SAVED = "DIALOG_CERT_NOT_SAVED";
155
156 private OCFile mWaitingToSend;
157
158
159 @Override
160 protected void onCreate(Bundle savedInstanceState) {
161 Log_OC.v(TAG, "onCreate() start");
162
163 super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account
164 // is valid
165
166 /// grant that FileObserverService is watching favorite files
167 if (savedInstanceState == null) {
168 Intent initObserversIntent = FileObserverService.makeInitIntent(this);
169 startService(initObserversIntent);
170 }
171
172 /// Load of saved instance state
173 if(savedInstanceState != null) {
174 mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(
175 FileDisplayActivity.KEY_WAITING_TO_PREVIEW);
176 mSyncInProgress = savedInstanceState.getBoolean(KEY_SYNC_IN_PROGRESS);
177 mWaitingToSend = (OCFile) savedInstanceState.getParcelable(
178 FileDisplayActivity.KEY_WAITING_TO_SEND);
179 } else {
180 mWaitingToPreview = null;
181 mSyncInProgress = false;
182 mWaitingToSend = null;
183 }
184
185 /// USER INTERFACE
186
187 // Inflate and set the layout view
188 setContentView(R.layout.files);
189
190 // Navigation Drawer
191 initDrawer();
192
193 mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
194 mProgressBar.setIndeterminateDrawable(
195 ContextCompat.getDrawable(this,
196 R.drawable.actionbar_progress_indeterminate_horizontal));
197
198 mDualPane = getResources().getBoolean(R.bool.large_land_layout);
199 mLeftFragmentContainer = findViewById(R.id.left_fragment_container);
200 mRightFragmentContainer = findViewById(R.id.right_fragment_container);
201 if (savedInstanceState == null) {
202 createMinFragments();
203 }
204
205 // Action bar setup
206 getSupportActionBar().setHomeButtonEnabled(true); // mandatory since Android ICS,
207 // according to the official
208 // documentation
209
210 // enable ActionBar app icon to behave as action to toggle nav drawer
211 //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
212 getSupportActionBar().setHomeButtonEnabled(true);
213
214 mProgressBar.setIndeterminate(mSyncInProgress);
215 // always AFTER setContentView(...) ; to work around bug in its implementation
216
217 setBackgroundText();
218
219 Log_OC.v(TAG, "onCreate() end");
220 }
221
222 @Override
223 protected void onStart() {
224 Log_OC.v(TAG, "onStart() start");
225 super.onStart();
226 Log_OC.v(TAG, "onStart() end");
227 }
228
229 @Override
230 protected void onDestroy() {
231 Log_OC.v(TAG, "onDestroy() start");
232 super.onDestroy();
233 Log_OC.v(TAG, "onDestroy() end");
234 }
235
236 /**
237 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
238 */
239 @Override
240 protected void onAccountSet(boolean stateWasRecovered) {
241 super.onAccountSet(stateWasRecovered);
242 if (getAccount() != null) {
243 /// Check whether the 'main' OCFile handled by the Activity is contained in the
244 // current Account
245 OCFile file = getFile();
246 // get parent from path
247 String parentPath = "";
248 if (file != null) {
249 if (file.isDown() && file.getLastSyncDateForProperties() == 0) {
250 // upload in progress - right now, files are not inserted in the local
251 // cache until the upload is successful get parent from path
252 parentPath = file.getRemotePath().substring(0,
253 file.getRemotePath().lastIndexOf(file.getFileName()));
254 if (getStorageManager().getFileByPath(parentPath) == null)
255 file = null; // not able to know the directory where the file is uploading
256 } else {
257 file = getStorageManager().getFileByPath(file.getRemotePath());
258 // currentDir = null if not in the current Account
259 }
260 }
261 if (file == null) {
262 // fall back to root folder
263 file = getStorageManager().getFileByPath(OCFile.ROOT_PATH); // never returns null
264 }
265 setFile(file);
266
267 if (mAccountWasSet) {
268 setUsernameInDrawer((RelativeLayout) findViewById(R.id.left_drawer), getAccount());
269 }
270
271 if (!stateWasRecovered) {
272 Log_OC.d(TAG, "Initializing Fragments in onAccountChanged..");
273 initFragmentsWithFile();
274 if (file.isFolder()) {
275 startSyncFolderOperation(file, false);
276 }
277
278 } else {
279 updateFragmentsVisibility(!file.isFolder());
280 updateActionBarTitleAndHomeButton(file.isFolder() ? null : file);
281 }
282 }
283 }
284
285 private void createMinFragments() {
286 OCFileListFragment listOfFiles = new OCFileListFragment();
287 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
288 transaction.add(R.id.left_fragment_container, listOfFiles, TAG_LIST_OF_FILES);
289 transaction.commit();
290 }
291
292 private void initFragmentsWithFile() {
293 if (getAccount() != null && getFile() != null) {
294 /// First fragment
295 OCFileListFragment listOfFiles = getListOfFilesFragment();
296 if (listOfFiles != null) {
297 listOfFiles.listDirectory(getCurrentDir());
298 // TODO Enable when "On Device" is recovered
299 // listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
300
301 } else {
302 Log_OC.e(TAG, "Still have a chance to lose the initializacion of list fragment >(");
303 }
304
305 /// Second fragment
306 OCFile file = getFile();
307 Fragment secondFragment = chooseInitialSecondFragment(file);
308 if (secondFragment != null) {
309 setSecondFragment(secondFragment);
310 updateFragmentsVisibility(true);
311 updateActionBarTitleAndHomeButton(file);
312
313 } else {
314 cleanSecondFragment();
315 if (file.isDown() && PreviewTextFragment.canBePreviewed(file))
316 startTextPreview(file);
317 }
318
319 } else {
320 Log_OC.wtf(TAG, "initFragments() called with invalid NULLs!");
321 if (getAccount() == null) {
322 Log_OC.wtf(TAG, "\t account is NULL");
323 }
324 if (getFile() == null) {
325 Log_OC.wtf(TAG, "\t file is NULL");
326 }
327 }
328 }
329
330 private Fragment chooseInitialSecondFragment(OCFile file) {
331 Fragment secondFragment = null;
332 if (file != null && !file.isFolder()) {
333 if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)
334 && file.getLastSyncDateForProperties() > 0 // temporal fix
335 ) {
336 int startPlaybackPosition =
337 getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
338 boolean autoplay =
339 getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
340 secondFragment = new PreviewMediaFragment(file, getAccount(),
341 startPlaybackPosition, autoplay);
342
343 } else if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) {
344 secondFragment = null;
345 } else {
346 secondFragment = FileDetailFragment.newInstance(file, getAccount());
347 }
348 }
349 return secondFragment;
350 }
351
352
353 /**
354 * Replaces the second fragment managed by the activity with the received as
355 * a parameter.
356 * <p/>
357 * Assumes never will be more than two fragments managed at the same time.
358 *
359 * @param fragment New second Fragment to set.
360 */
361 private void setSecondFragment(Fragment fragment) {
362 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
363 transaction.replace(R.id.right_fragment_container, fragment, TAG_SECOND_FRAGMENT);
364 transaction.commit();
365 }
366
367
368 private void updateFragmentsVisibility(boolean existsSecondFragment) {
369 if (mDualPane) {
370 if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
371 mLeftFragmentContainer.setVisibility(View.VISIBLE);
372 }
373 if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
374 mRightFragmentContainer.setVisibility(View.VISIBLE);
375 }
376
377 } else if (existsSecondFragment) {
378 if (mLeftFragmentContainer.getVisibility() != View.GONE) {
379 mLeftFragmentContainer.setVisibility(View.GONE);
380 }
381 if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
382 mRightFragmentContainer.setVisibility(View.VISIBLE);
383 }
384
385 } else {
386 if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
387 mLeftFragmentContainer.setVisibility(View.VISIBLE);
388 }
389 if (mRightFragmentContainer.getVisibility() != View.GONE) {
390 mRightFragmentContainer.setVisibility(View.GONE);
391 }
392 }
393 }
394
395
396 private OCFileListFragment getListOfFilesFragment() {
397 Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(
398 FileDisplayActivity.TAG_LIST_OF_FILES);
399 if (listOfFiles != null) {
400 return (OCFileListFragment) listOfFiles;
401 }
402 Log_OC.wtf(TAG, "Access to unexisting list of files fragment!!");
403 return null;
404 }
405
406 public FileFragment getSecondFragment() {
407 Fragment second = getSupportFragmentManager().findFragmentByTag(
408 FileDisplayActivity.TAG_SECOND_FRAGMENT);
409 if (second != null) {
410 return (FileFragment) second;
411 }
412 return null;
413 }
414
415 protected void cleanSecondFragment() {
416 Fragment second = getSecondFragment();
417 if (second != null) {
418 FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
419 tr.remove(second);
420 tr.commit();
421 }
422 updateFragmentsVisibility(false);
423 updateActionBarTitleAndHomeButton(null);
424 }
425
426 protected void refreshListOfFilesFragment() {
427 OCFileListFragment fileListFragment = getListOfFilesFragment();
428 if (fileListFragment != null) {
429 fileListFragment.listDirectory();
430 // TODO Enable when "On Device" is recovered ?
431 // fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
432 }
433 }
434
435 protected void refreshSecondFragment(String downloadEvent, String downloadedRemotePath,
436 boolean success) {
437 FileFragment secondFragment = getSecondFragment();
438 boolean waitedPreview = (mWaitingToPreview != null &&
439 mWaitingToPreview.getRemotePath().equals(downloadedRemotePath));
440 if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
441 FileDetailFragment detailsFragment = (FileDetailFragment) secondFragment;
442 OCFile fileInFragment = detailsFragment.getFile();
443 if (fileInFragment != null &&
444 !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
445 // the user browsed to other file ; forget the automatic preview
446 mWaitingToPreview = null;
447
448 } else if (downloadEvent.equals(FileDownloader.getDownloadAddedMessage())) {
449 // grant that the right panel updates the progress bar
450 detailsFragment.listenForTransferProgress();
451 detailsFragment.updateFileDetails(true, false);
452
453 } else if (downloadEvent.equals(FileDownloader.getDownloadFinishMessage())) {
454 // update the right panel
455 boolean detailsFragmentChanged = false;
456 if (waitedPreview) {
457 if (success) {
458 mWaitingToPreview = getStorageManager().getFileById(
459 mWaitingToPreview.getFileId()); // update the file from database,
460 // for the local storage path
461 if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) {
462 startMediaPreview(mWaitingToPreview, 0, true);
463 detailsFragmentChanged = true;
464 } else if (PreviewTextFragment.canBePreviewed(mWaitingToPreview)) {
465 startTextPreview(mWaitingToPreview);
466 detailsFragmentChanged = true;
467 } else {
468 getFileOperationsHelper().openFile(mWaitingToPreview);
469 }
470 }
471 mWaitingToPreview = null;
472 }
473 if (!detailsFragmentChanged) {
474 detailsFragment.updateFileDetails(false, (success));
475 }
476 }
477 }
478 }
479
480 @Override
481 public boolean onPrepareOptionsMenu(Menu menu) {
482 boolean drawerOpen = mDrawerLayout.isDrawerOpen(GravityCompat.START);
483 menu.findItem(R.id.action_sort).setVisible(!drawerOpen);
484 menu.findItem(R.id.action_sync_account).setVisible(!drawerOpen);
485
486 return super.onPrepareOptionsMenu(menu);
487 }
488
489 @Override
490 public boolean onCreateOptionsMenu(Menu menu) {
491 MenuInflater inflater = getMenuInflater();
492 inflater.inflate(R.menu.main_menu, menu);
493 menu.findItem(R.id.action_create_dir).setVisible(false);
494 return true;
495 }
496
497
498 @Override
499 public boolean onOptionsItemSelected(MenuItem item) {
500 boolean retval = true;
501 switch (item.getItemId()) {
502 case R.id.action_sync_account: {
503 startSynchronization();
504 break;
505 }
506 case android.R.id.home: {
507 FileFragment second = getSecondFragment();
508 OCFile currentDir = getCurrentDir();
509 if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
510 mDrawerLayout.closeDrawer(GravityCompat.START);
511 } else if((currentDir != null && currentDir.getParentId() != 0) ||
512 (second != null && second.getFile() != null)) {
513 onBackPressed();
514
515 } else {
516 mDrawerLayout.openDrawer(GravityCompat.START);
517 }
518 break;
519 }
520 case R.id.action_sort: {
521 SharedPreferences appPreferences = PreferenceManager
522 .getDefaultSharedPreferences(this);
523
524 // Read sorting order, default to sort by name ascending
525 Integer sortOrder = appPreferences
526 .getInt("sortOrder", FileStorageUtils.SORT_NAME);
527
528 AlertDialog.Builder builder = new AlertDialog.Builder(this);
529 builder.setTitle(R.string.actionbar_sort_title)
530 .setSingleChoiceItems(R.array.actionbar_sortby, sortOrder ,
531 new DialogInterface.OnClickListener() {
532 public void onClick(DialogInterface dialog, int which) {
533 switch (which){
534 case 0:
535 sortByName(true);
536 break;
537 case 1:
538 sortByDate(false);
539 break;
540 }
541
542 dialog.dismiss();
543 }
544 });
545 builder.create().show();
546 break;
547 }
548 default:
549 retval = super.onOptionsItemSelected(item);
550 }
551 return retval;
552 }
553
554 public void createFolder() {
555 CreateFolderDialogFragment dialog =
556 CreateFolderDialogFragment.newInstance(getCurrentDir());
557 dialog.show(getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
558 }
559
560 public void uploadLocalFilesSelected() {
561 Intent action = new Intent(this, UploadFilesActivity.class);
562 action.putExtra(
563 UploadFilesActivity.EXTRA_ACCOUNT,
564 getAccount()
565 );
566 startActivityForResult(action, ACTION_SELECT_MULTIPLE_FILES);
567 }
568
569 public void uploadFromOtherAppsSelected() {
570 Intent action = new Intent(Intent.ACTION_GET_CONTENT);
571 action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
572 //Intent.EXTRA_ALLOW_MULTIPLE is only supported on api level 18+, Jelly Bean
573 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
574 action.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
575 }
576 startActivityForResult(
577 Intent.createChooser(action, getString(R.string.upload_chooser_title)),
578 ACTION_SELECT_CONTENT_FROM_APPS
579 );
580 }
581
582 private void startSynchronization() {
583 Log_OC.d(TAG, "Got to start sync");
584 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
585 Log_OC.d(TAG, "Canceling all syncs for " + MainApp.getAuthority());
586 ContentResolver.cancelSync(null, MainApp.getAuthority());
587 // cancel the current synchronizations of any ownCloud account
588 Bundle bundle = new Bundle();
589 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
590 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
591 Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
592 MainApp.getAuthority());
593 ContentResolver.requestSync(
594 getAccount(),
595 MainApp.getAuthority(), bundle);
596 } else {
597 Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
598 MainApp.getAuthority() + " with new API");
599 SyncRequest.Builder builder = new SyncRequest.Builder();
600 builder.setSyncAdapter(getAccount(), MainApp.getAuthority());
601 builder.setExpedited(true);
602 builder.setManual(true);
603 builder.syncOnce();
604
605 // Fix bug in Android Lollipop when you click on refresh the whole account
606 Bundle extras = new Bundle();
607 builder.setExtras(extras);
608
609 SyncRequest request = builder.build();
610 ContentResolver.requestSync(request);
611 }
612 }
613
614 /**
615 * Called, when the user selected something for uploading
616 *
617 */
618 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
619 @Override
620 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
621
622 if (requestCode == ACTION_SELECT_CONTENT_FROM_APPS && (resultCode == RESULT_OK ||
623 resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
624 //getClipData is only supported on api level 16+, Jelly Bean
625 if (data.getData() == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
626 for( int i = 0; i < data.getClipData().getItemCount(); i++){
627 Intent intent = new Intent();
628 intent.setData(data.getClipData().getItemAt(i).getUri());
629 requestSimpleUpload(intent, resultCode);
630 }
631 }else {
632 requestSimpleUpload(data, resultCode);
633 }
634 } else if (requestCode == ACTION_SELECT_MULTIPLE_FILES && (resultCode == RESULT_OK ||
635 resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
636 requestMultipleUpload(data, resultCode);
637
638 } else if (requestCode == ACTION_MOVE_FILES && resultCode == RESULT_OK){
639 final Intent fData = data;
640 final int fResultCode = resultCode;
641 getHandler().postDelayed(
642 new Runnable() {
643 @Override
644 public void run() {
645 requestMoveOperation(fData, fResultCode);
646 }
647 },
648 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
649 );
650
651 } else if (requestCode == ACTION_COPY_FILES && resultCode == RESULT_OK) {
652
653 final Intent fData = data;
654 final int fResultCode = resultCode;
655 getHandler().postDelayed(
656 new Runnable() {
657 @Override
658 public void run() {
659 requestCopyOperation(fData, fResultCode);
660 }
661 },
662 DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS
663 );
664
665 } else {
666 super.onActivityResult(requestCode, resultCode, data);
667 }
668
669 }
670
671 private void requestMultipleUpload(Intent data, int resultCode) {
672 String[] filePaths = data.getStringArrayExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES);
673 if (filePaths != null) {
674 String[] remotePaths = new String[filePaths.length];
675 String remotePathBase = getCurrentDir().getRemotePath();
676 for (int j = 0; j< remotePaths.length; j++) {
677 remotePaths[j] = remotePathBase + (new File(filePaths[j])).getName();
678 }
679
680 Intent i = new Intent(this, FileUploader.class);
681 i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
682 i.putExtra(FileUploader.KEY_LOCAL_FILE, filePaths);
683 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
684 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
685 if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
686 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
687 startService(i);
688
689 } else {
690 Log_OC.d(TAG, "User clicked on 'Update' with no selection");
691 Toast t = Toast.makeText(this, getString(R.string.filedisplay_no_file_selected),
692 Toast.LENGTH_LONG);
693 t.show();
694 return;
695 }
696 }
697
698
699 private void requestSimpleUpload(Intent data, int resultCode) {
700 String filePath = null;
701 String mimeType = null;
702
703 Uri selectedImageUri = data.getData();
704
705 try {
706 mimeType = getContentResolver().getType(selectedImageUri);
707
708 String fileManagerString = selectedImageUri.getPath();
709 String selectedImagePath = UriUtils.getLocalPath(selectedImageUri, this);
710
711 if (selectedImagePath != null)
712 filePath = selectedImagePath;
713 else
714 filePath = fileManagerString;
715
716 } catch (Exception e) {
717 Log_OC.e(TAG, "Unexpected exception when trying to read the result of " +
718 "Intent.ACTION_GET_CONTENT", e);
719
720 } finally {
721 if (filePath == null) {
722 Log_OC.e(TAG, "Couldn't resolve path to file");
723 Toast t = Toast.makeText(
724 this, getString(R.string.filedisplay_unexpected_bad_get_content),
725 Toast.LENGTH_LONG
726 );
727 t.show();
728 return;
729 }
730 }
731
732 Intent i = new Intent(this, FileUploader.class);
733 i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
734 OCFile currentDir = getCurrentDir();
735 String remotePath = (currentDir != null) ? currentDir.getRemotePath() : OCFile.ROOT_PATH;
736
737 if (filePath.startsWith(UriUtils.URI_CONTENT_SCHEME)) {
738 Cursor cursor = getContentResolver().query(Uri.parse(filePath), null, null, null, null);
739 try {
740 if (cursor != null && cursor.moveToFirst()) {
741 String displayName = cursor.getString(cursor.getColumnIndex(
742 OpenableColumns.DISPLAY_NAME));
743 Log_OC.v(TAG, "Display Name: " + displayName );
744
745 displayName.replace(File.separatorChar, '_');
746 displayName.replace(File.pathSeparatorChar, '_');
747 remotePath += displayName + DisplayUtils.getComposedFileExtension(filePath);
748
749 }
750 // and what happens in case of error?; wrong target name for the upload
751 } finally {
752 cursor.close();
753 }
754
755 } else {
756 remotePath += new File(filePath).getName();
757 }
758
759 i.putExtra(FileUploader.KEY_LOCAL_FILE, filePath);
760 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePath);
761 i.putExtra(FileUploader.KEY_MIME_TYPE, mimeType);
762 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
763 if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
764 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
765 startService(i);
766 }
767
768 /**
769 * Request the operation for moving the file/folder from one path to another
770 *
771 * @param data Intent received
772 * @param resultCode Result code received
773 */
774 private void requestMoveOperation(Intent data, int resultCode) {
775 OCFile folderToMoveAt = (OCFile) data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
776 OCFile targetFile = (OCFile) data.getParcelableExtra(FolderPickerActivity.EXTRA_FILE);
777 getFileOperationsHelper().moveFile(folderToMoveAt, targetFile);
778 }
779
780 /**
781 * Request the operation for copying the file/folder from one path to another
782 *
783 * @param data Intent received
784 * @param resultCode Result code received
785 */
786 private void requestCopyOperation(Intent data, int resultCode) {
787 OCFile folderToMoveAt = data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
788 OCFile targetFile = data.getParcelableExtra(FolderPickerActivity.EXTRA_FILE);
789 getFileOperationsHelper().copyFile(folderToMoveAt, targetFile);
790 }
791
792 @Override
793 public void onBackPressed() {
794 if (!isDrawerOpen()){
795 OCFileListFragment listOfFiles = getListOfFilesFragment();
796 if (mDualPane || getSecondFragment() == null) {
797 OCFile currentDir = getCurrentDir();
798 if (currentDir == null || currentDir.getParentId() == FileDataStorageManager.ROOT_PARENT_ID) {
799 finish();
800 return;
801 }
802 if (listOfFiles != null) { // should never be null, indeed
803 listOfFiles.onBrowseUp();
804 }
805 }
806 if (listOfFiles != null) { // should never be null, indeed
807 setFile(listOfFiles.getCurrentFile());
808 }
809 cleanSecondFragment();
810 } else {
811 super.onBackPressed();
812 }
813 }
814
815 @Override
816 protected void onSaveInstanceState(Bundle outState) {
817 // responsibility of restore is preferred in onCreate() before than in
818 // onRestoreInstanceState when there are Fragments involved
819 Log_OC.v(TAG, "onSaveInstanceState() start");
820 super.onSaveInstanceState(outState);
821 outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
822 outState.putBoolean(FileDisplayActivity.KEY_SYNC_IN_PROGRESS, mSyncInProgress);
823 //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
824 // mRefreshSharesInProgress);
825 outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_SEND, mWaitingToSend);
826
827 Log_OC.v(TAG, "onSaveInstanceState() end");
828 }
829
830
831 @Override
832 protected void onResume() {
833 Log_OC.v(TAG, "onResume() start");
834 super.onResume();
835 // refresh Navigation Drawer account list
836 mNavigationDrawerAdapter.updateAccountList();
837
838 // refresh list of files
839 refreshListOfFilesFragment();
840
841 // Listen for sync messages
842 IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START);
843 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END);
844 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED);
845 syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED);
846 syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED);
847 mSyncBroadcastReceiver = new SyncBroadcastReceiver();
848 registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
849 //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
850 // syncIntentFilter);
851
852 // Listen for upload messages
853 IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.getUploadFinishMessage());
854 mUploadFinishReceiver = new UploadFinishReceiver();
855 registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
856
857 // Listen for download messages
858 IntentFilter downloadIntentFilter = new IntentFilter(
859 FileDownloader.getDownloadAddedMessage());
860 downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
861 mDownloadFinishReceiver = new DownloadFinishReceiver();
862 registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
863
864 Log_OC.v(TAG, "onResume() end");
865
866 }
867
868
869 @Override
870 protected void onPause() {
871 Log_OC.v(TAG, "onPause() start");
872 if (mSyncBroadcastReceiver != null) {
873 unregisterReceiver(mSyncBroadcastReceiver);
874 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
875 mSyncBroadcastReceiver = null;
876 }
877 if (mUploadFinishReceiver != null) {
878 unregisterReceiver(mUploadFinishReceiver);
879 mUploadFinishReceiver = null;
880 }
881 if (mDownloadFinishReceiver != null) {
882 unregisterReceiver(mDownloadFinishReceiver);
883 mDownloadFinishReceiver = null;
884 }
885
886 super.onPause();
887 Log_OC.v(TAG, "onPause() end");
888 }
889
890
891 private class SyncBroadcastReceiver extends BroadcastReceiver {
892
893 /**
894 * {@link BroadcastReceiver} to enable syncing feedback in UI
895 */
896 @Override
897 public void onReceive(Context context, Intent intent) {
898 try {
899 String event = intent.getAction();
900 Log_OC.d(TAG, "Received broadcast " + event);
901 String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
902 String synchFolderRemotePath =
903 intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
904 RemoteOperationResult synchResult =
905 (RemoteOperationResult)intent.getSerializableExtra(
906 FileSyncAdapter.EXTRA_RESULT);
907 boolean sameAccount = (getAccount() != null &&
908 accountName.equals(getAccount().name) && getStorageManager() != null);
909
910 if (sameAccount) {
911
912 if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
913 mSyncInProgress = true;
914
915 } else {
916 OCFile currentFile = (getFile() == null) ? null :
917 getStorageManager().getFileByPath(getFile().getRemotePath());
918 OCFile currentDir = (getCurrentDir() == null) ? null :
919 getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
920
921 if (currentDir == null) {
922 // current folder was removed from the server
923 Toast.makeText( FileDisplayActivity.this,
924 String.format(
925 getString(R.string.
926 sync_current_folder_was_removed),
927 synchFolderRemotePath),
928 Toast.LENGTH_LONG)
929 .show();
930
931 browseToRoot();
932
933 } else {
934 if (currentFile == null && !getFile().isFolder()) {
935 // currently selected file was removed in the server, and now we
936 // know it
937 cleanSecondFragment();
938 currentFile = currentDir;
939 }
940
941 if (synchFolderRemotePath != null &&
942 currentDir.getRemotePath().equals(synchFolderRemotePath)) {
943 OCFileListFragment fileListFragment = getListOfFilesFragment();
944 if (fileListFragment != null) {
945 fileListFragment.listDirectory();
946 // TODO Enable when "On Device" is recovered ?
947 // fileListFragment.listDirectory(currentDir,
948 // MainApp.getOnlyOnDevice());
949 }
950 }
951 setFile(currentFile);
952 }
953
954 mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) &&
955 !RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED
956 .equals(event));
957
958 if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.
959 equals(event) &&/// TODO refactor and make common
960
961 synchResult != null && !synchResult.isSuccess() &&
962 (synchResult.getCode() == ResultCode.UNAUTHORIZED ||
963 synchResult.isIdPRedirection() ||
964 (synchResult.isException() && synchResult.getException()
965 instanceof AuthenticatorException))) {
966
967
968 try {
969 OwnCloudClient client;
970 OwnCloudAccount ocAccount =
971 new OwnCloudAccount(getAccount(), context);
972 client = (OwnCloudClientManagerFactory.getDefaultSingleton().
973 removeClientFor(ocAccount));
974 if (client != null) {
975 OwnCloudCredentials cred = client.getCredentials();
976 if (cred != null) {
977 AccountManager am = AccountManager.get(context);
978 if (cred.authTokenExpires()) {
979 am.invalidateAuthToken(
980 getAccount().type,
981 cred.getAuthToken()
982 );
983 } else {
984 am.clearPassword(getAccount());
985 }
986 }
987 }
988 requestCredentialsUpdate();
989
990 } catch (AccountNotFoundException e) {
991 Log_OC.e(TAG, "Account " + getAccount() + " was removed!", e);
992 }
993
994 }
995 }
996 removeStickyBroadcast(intent);
997 Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
998 mProgressBar.setIndeterminate(mSyncInProgress);
999 //mProgressBar.setVisibility((mSyncInProgress) ? View.VISIBLE : View.INVISIBLE);
1000 //setSupportProgressBarIndeterminateVisibility(mSyncInProgress
1001 /*|| mRefreshSharesInProgress*/ //);
1002
1003 setBackgroundText();
1004
1005 }
1006
1007 if (synchResult != null) {
1008 if (synchResult.getCode().equals(
1009 RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
1010 mLastSslUntrustedServerResult = synchResult;
1011 }
1012 }
1013 } catch (RuntimeException e) {
1014 // avoid app crashes after changing the serial id of RemoteOperationResult
1015 // in owncloud library with broadcast notifications pending to process
1016 removeStickyBroadcast(intent);
1017 }
1018 }
1019 }
1020
1021 /**
1022 * Show a text message on screen view for notifying user if content is
1023 * loading or folder is empty
1024 */
1025 private void setBackgroundText() {
1026 OCFileListFragment ocFileListFragment = getListOfFilesFragment();
1027 if (ocFileListFragment != null) {
1028 int message = R.string.file_list_loading;
1029 if (!mSyncInProgress) {
1030 // In case file list is empty
1031 message = R.string.file_list_empty;
1032 }
1033 ocFileListFragment.setMessageForEmptyList(getString(message));
1034 } else {
1035 Log_OC.e(TAG, "OCFileListFragment is null");
1036 }
1037 }
1038
1039 /**
1040 * Once the file upload has finished -> update view
1041 */
1042 private class UploadFinishReceiver extends BroadcastReceiver {
1043 /**
1044 * Once the file upload has finished -> update view
1045 *
1046 * @author David A. Velasco
1047 * {@link BroadcastReceiver} to enable upload feedback in UI
1048 */
1049 @Override
1050 public void onReceive(Context context, Intent intent) {
1051 try {
1052 String uploadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
1053 String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
1054 boolean sameAccount = getAccount() != null && accountName.equals(getAccount().name);
1055 OCFile currentDir = getCurrentDir();
1056 boolean isDescendant = (currentDir != null) && (uploadedRemotePath != null) &&
1057 (uploadedRemotePath.startsWith(currentDir.getRemotePath()));
1058
1059 if (sameAccount && isDescendant) {
1060 refreshListOfFilesFragment();
1061 }
1062
1063 boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT,
1064 false);
1065 boolean renamedInUpload = getFile().getRemotePath().
1066 equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH));
1067 boolean sameFile = getFile().getRemotePath().equals(uploadedRemotePath) ||
1068 renamedInUpload;
1069 FileFragment details = getSecondFragment();
1070 boolean detailFragmentIsShown = (details != null &&
1071 details instanceof FileDetailFragment);
1072
1073 if (sameAccount && sameFile && detailFragmentIsShown) {
1074 if (uploadWasFine) {
1075 setFile(getStorageManager().getFileByPath(uploadedRemotePath));
1076 }
1077 if (renamedInUpload) {
1078 String newName = (new File(uploadedRemotePath)).getName();
1079 Toast msg = Toast.makeText(
1080 context,
1081 String.format(
1082 getString(R.string.filedetails_renamed_in_upload_msg),
1083 newName),
1084 Toast.LENGTH_LONG);
1085 msg.show();
1086 }
1087 if (uploadWasFine || getFile().fileExists()) {
1088 ((FileDetailFragment) details).updateFileDetails(false, true);
1089 } else {
1090 cleanSecondFragment();
1091 }
1092
1093 // Force the preview if the file is an image or text file
1094 if (uploadWasFine) {
1095 OCFile ocFile = getFile();
1096 if (PreviewImageFragment.canBePreviewed(ocFile))
1097 startImagePreview(getFile());
1098 else if (PreviewTextFragment.canBePreviewed(ocFile))
1099 startTextPreview(ocFile);
1100 // TODO what about other kind of previews?
1101 }
1102 }
1103
1104 mProgressBar.setIndeterminate(false);
1105 } finally {
1106 if (intent != null) {
1107 removeStickyBroadcast(intent);
1108 }
1109 }
1110
1111 }
1112
1113 }
1114
1115
1116 /**
1117 * Class waiting for broadcast events from the {@link FileDownloader} service.
1118 *
1119 * Updates the UI when a download is started or finished, provided that it is relevant for the
1120 * current folder.
1121 */
1122 private class DownloadFinishReceiver extends BroadcastReceiver {
1123
1124 //int refreshCounter = 0;
1125 @Override
1126 public void onReceive(Context context, Intent intent) {
1127 try {
1128 boolean sameAccount = isSameAccount(context, intent);
1129 String downloadedRemotePath =
1130 intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
1131 boolean isDescendant = isDescendant(downloadedRemotePath);
1132
1133 if (sameAccount && isDescendant) {
1134 String linkedToRemotePath =
1135 intent.getStringExtra(FileDownloader.EXTRA_LINKED_TO_PATH);
1136 if (linkedToRemotePath == null || isAscendant(linkedToRemotePath)) {
1137 //Log_OC.v(TAG, "refresh #" + ++refreshCounter);
1138 refreshListOfFilesFragment();
1139 }
1140 refreshSecondFragment(
1141 intent.getAction(),
1142 downloadedRemotePath,
1143 intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)
1144 );
1145 }
1146
1147 if (mWaitingToSend != null) {
1148 mWaitingToSend =
1149 getStorageManager().getFileByPath(mWaitingToSend.getRemotePath());
1150 if (mWaitingToSend.isDown()) {
1151 sendDownloadedFile();
1152 }
1153 }
1154
1155 } finally {
1156 if (intent != null) {
1157 removeStickyBroadcast(intent);
1158 }
1159 }
1160 }
1161
1162 private boolean isDescendant(String downloadedRemotePath) {
1163 OCFile currentDir = getCurrentDir();
1164 return (
1165 currentDir != null &&
1166 downloadedRemotePath != null &&
1167 downloadedRemotePath.startsWith(currentDir.getRemotePath())
1168 );
1169 }
1170
1171 private boolean isAscendant(String linkedToRemotePath) {
1172 OCFile currentDir = getCurrentDir();
1173 return (
1174 currentDir != null &&
1175 currentDir.getRemotePath().startsWith(linkedToRemotePath)
1176 );
1177 }
1178
1179 private boolean isSameAccount(Context context, Intent intent) {
1180 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
1181 return (accountName != null && getAccount() != null &&
1182 accountName.equals(getAccount().name));
1183 }
1184 }
1185
1186
1187 public void browseToRoot() {
1188 OCFileListFragment listOfFiles = getListOfFilesFragment();
1189 if (listOfFiles != null) { // should never be null, indeed
1190 OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
1191 listOfFiles.listDirectory(root);
1192 // TODO Enable when "On Device" is recovered ?
1193 // listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
1194 setFile(listOfFiles.getCurrentFile());
1195 startSyncFolderOperation(root, false);
1196 }
1197 cleanSecondFragment();
1198 }
1199
1200
1201 /**
1202 * {@inheritDoc}
1203 * <p/>
1204 * Updates action bar and second fragment, if in dual pane mode.
1205 */
1206 @Override
1207 public void onBrowsedDownTo(OCFile directory) {
1208 setFile(directory);
1209 cleanSecondFragment();
1210 // Sync Folder
1211 startSyncFolderOperation(directory, false);
1212 }
1213
1214 /**
1215 * Shows the information of the {@link OCFile} received as a
1216 * parameter in the second fragment.
1217 *
1218 * @param file {@link OCFile} whose details will be shown
1219 */
1220 @Override
1221 public void showDetails(OCFile file) {
1222 Fragment detailFragment = FileDetailFragment.newInstance(file, getAccount());
1223 setSecondFragment(detailFragment);
1224 updateFragmentsVisibility(true);
1225 updateActionBarTitleAndHomeButton(file);
1226 setFile(file);
1227 }
1228
1229 @Override
1230 protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
1231 if (mDualPane) {
1232 // in dual pane mode, keep the focus of title an action bar in the current folder
1233 super.updateActionBarTitleAndHomeButton(getCurrentDir());
1234
1235 } else {
1236 super.updateActionBarTitleAndHomeButton(chosenFile);
1237 }
1238
1239 }
1240
1241 @Override
1242 protected ServiceConnection newTransferenceServiceConnection() {
1243 return new ListServiceConnection();
1244 }
1245
1246 /**
1247 * Defines callbacks for service binding, passed to bindService()
1248 */
1249 private class ListServiceConnection implements ServiceConnection {
1250
1251 @Override
1252 public void onServiceConnected(ComponentName component, IBinder service) {
1253 if (component.equals(new ComponentName(
1254 FileDisplayActivity.this, FileDownloader.class))) {
1255 Log_OC.d(TAG, "Download service connected");
1256 mDownloaderBinder = (FileDownloaderBinder) service;
1257 if (mWaitingToPreview != null)
1258 if (getStorageManager() != null) {
1259 // update the file
1260 mWaitingToPreview =
1261 getStorageManager().getFileById(mWaitingToPreview.getFileId());
1262 if (!mWaitingToPreview.isDown()) {
1263 requestForDownload();
1264 }
1265 }
1266
1267 } else if (component.equals(new ComponentName(FileDisplayActivity.this,
1268 FileUploader.class))) {
1269 Log_OC.d(TAG, "Upload service connected");
1270 mUploaderBinder = (FileUploaderBinder) service;
1271 } else {
1272 return;
1273 }
1274 // a new chance to get the mDownloadBinder through
1275 // getFileDownloadBinder() - THIS IS A MESS
1276 OCFileListFragment listOfFiles = getListOfFilesFragment();
1277 if (listOfFiles != null) {
1278 listOfFiles.listDirectory();
1279 // TODO Enable when "On Device" is recovered ?
1280 // listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
1281 }
1282 FileFragment secondFragment = getSecondFragment();
1283 if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
1284 FileDetailFragment detailFragment = (FileDetailFragment) secondFragment;
1285 detailFragment.listenForTransferProgress();
1286 detailFragment.updateFileDetails(false, false);
1287 }
1288 }
1289
1290 @Override
1291 public void onServiceDisconnected(ComponentName component) {
1292 if (component.equals(new ComponentName(FileDisplayActivity.this,
1293 FileDownloader.class))) {
1294 Log_OC.d(TAG, "Download service disconnected");
1295 mDownloaderBinder = null;
1296 } else if (component.equals(new ComponentName(FileDisplayActivity.this,
1297 FileUploader.class))) {
1298 Log_OC.d(TAG, "Upload service disconnected");
1299 mUploaderBinder = null;
1300 }
1301 }
1302 }
1303
1304 @Override
1305 public void onSavedCertificate() {
1306 startSyncFolderOperation(getCurrentDir(), false);
1307 }
1308
1309
1310 @Override
1311 public void onFailedSavingCertificate() {
1312 ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
1313 R.string.ssl_validator_not_saved, new String[]{}, R.string.common_ok, -1, -1
1314 );
1315 dialog.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED);
1316 }
1317
1318 @Override
1319 public void onCancelCertificate() {
1320 // nothing to do
1321 }
1322
1323 /**
1324 * Updates the view associated to the activity after the finish of some operation over files
1325 * in the current account.
1326 *
1327 * @param operation Removal operation performed.
1328 * @param result Result of the removal.
1329 */
1330 @Override
1331 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
1332 super.onRemoteOperationFinish(operation, result);
1333
1334 if (operation instanceof RemoveFileOperation) {
1335 onRemoveFileOperationFinish((RemoveFileOperation) operation, result);
1336
1337 } else if (operation instanceof RenameFileOperation) {
1338 onRenameFileOperationFinish((RenameFileOperation) operation, result);
1339
1340 } else if (operation instanceof SynchronizeFileOperation) {
1341 onSynchronizeFileOperationFinish((SynchronizeFileOperation) operation, result);
1342
1343 } else if (operation instanceof CreateFolderOperation) {
1344 onCreateFolderOperationFinish((CreateFolderOperation) operation, result);
1345
1346 } else if (operation instanceof CreateShareOperation) {
1347 onCreateShareOperationFinish((CreateShareOperation) operation, result);
1348
1349 } else if (operation instanceof UnshareLinkOperation) {
1350 onUnshareLinkOperationFinish((UnshareLinkOperation) operation, result);
1351
1352 } else if (operation instanceof MoveFileOperation) {
1353 onMoveFileOperationFinish((MoveFileOperation) operation, result);
1354
1355 } else if (operation instanceof CopyFileOperation) {
1356 onCopyFileOperationFinish((CopyFileOperation) operation, result);
1357 }
1358
1359 }
1360 private void onCreateShareOperationFinish(CreateShareOperation operation,
1361 RemoteOperationResult result) {
1362 if (result.isSuccess()) {
1363 refreshShowDetails();
1364 refreshListOfFilesFragment();
1365 }
1366 }
1367
1368 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation,
1369 RemoteOperationResult result) {
1370 if (result.isSuccess()) {
1371 refreshShowDetails();
1372 refreshListOfFilesFragment();
1373
1374 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
1375 cleanSecondFragment();
1376 refreshListOfFilesFragment();
1377 }
1378 }
1379
1380 private void refreshShowDetails() {
1381 FileFragment details = getSecondFragment();
1382 if (details != null) {
1383 OCFile file = details.getFile();
1384 if (file != null) {
1385 file = getStorageManager().getFileByPath(file.getRemotePath());
1386 if (details instanceof PreviewMediaFragment) {
1387 // Refresh OCFile of the fragment
1388 ((PreviewMediaFragment) details).updateFile(file);
1389 } else if (details instanceof PreviewTextFragment) {
1390 // Refresh OCFile of the fragment
1391 ((PreviewTextFragment) details).updateFile(file);
1392 } else {
1393 showDetails(file);
1394 }
1395 }
1396 invalidateOptionsMenu();
1397 }
1398 }
1399
1400 /**
1401 * Updates the view associated to the activity after the finish of an operation trying to
1402 * remove a file.
1403 *
1404 * @param operation Removal operation performed.
1405 * @param result Result of the removal.
1406 */
1407 private void onRemoveFileOperationFinish(RemoveFileOperation operation,
1408 RemoteOperationResult result) {
1409 dismissLoadingDialog();
1410
1411 Toast msg = Toast.makeText(this,
1412 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1413 Toast.LENGTH_LONG);
1414 msg.show();
1415
1416 if (result.isSuccess()) {
1417 OCFile removedFile = operation.getFile();
1418 FileFragment second = getSecondFragment();
1419 if (second != null && removedFile.equals(second.getFile())) {
1420 if (second instanceof PreviewMediaFragment) {
1421 ((PreviewMediaFragment) second).stopPreview(true);
1422 }
1423 setFile(getStorageManager().getFileById(removedFile.getParentId()));
1424 cleanSecondFragment();
1425 }
1426 if (getStorageManager().getFileById(removedFile.getParentId()).equals(getCurrentDir())){
1427 refreshListOfFilesFragment();
1428 }
1429 invalidateOptionsMenu();
1430 } else {
1431 if (result.isSslRecoverableException()) {
1432 mLastSslUntrustedServerResult = result;
1433 showUntrustedCertDialog(mLastSslUntrustedServerResult);
1434 }
1435 }
1436 }
1437
1438
1439 /**
1440 * Updates the view associated to the activity after the finish of an operation trying to move a
1441 * file.
1442 *
1443 * @param operation Move operation performed.
1444 * @param result Result of the move operation.
1445 */
1446 private void onMoveFileOperationFinish(MoveFileOperation operation,
1447 RemoteOperationResult result) {
1448 if (result.isSuccess()) {
1449 dismissLoadingDialog();
1450 refreshListOfFilesFragment();
1451 } else {
1452 dismissLoadingDialog();
1453 try {
1454 Toast msg = Toast.makeText(FileDisplayActivity.this,
1455 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1456 Toast.LENGTH_LONG);
1457 msg.show();
1458
1459 } catch (NotFoundException e) {
1460 Log_OC.e(TAG, "Error while trying to show fail message ", e);
1461 }
1462 }
1463 }
1464
1465 /**
1466 * Updates the view associated to the activity after the finish of an operation trying to copy a
1467 * file.
1468 *
1469 * @param operation Copy operation performed.
1470 * @param result Result of the copy operation.
1471 */
1472 private void onCopyFileOperationFinish(CopyFileOperation operation, RemoteOperationResult result) {
1473 if (result.isSuccess()) {
1474 dismissLoadingDialog();
1475 refreshListOfFilesFragment();
1476 } else {
1477 dismissLoadingDialog();
1478 try {
1479 Toast msg = Toast.makeText(FileDisplayActivity.this,
1480 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1481 Toast.LENGTH_LONG);
1482 msg.show();
1483
1484 } catch (NotFoundException e) {
1485 Log_OC.e(TAG, "Error while trying to show fail message ", e);
1486 }
1487 }
1488 }
1489
1490 /**
1491 * Updates the view associated to the activity after the finish of an operation trying to rename
1492 * a file.
1493 *
1494 * @param operation Renaming operation performed.
1495 * @param result Result of the renaming.
1496 */
1497 private void onRenameFileOperationFinish(RenameFileOperation operation,
1498 RemoteOperationResult result) {
1499 dismissLoadingDialog();
1500 OCFile renamedFile = operation.getFile();
1501 if (result.isSuccess()) {
1502 FileFragment details = getSecondFragment();
1503 if (details != null) {
1504 if (details instanceof FileDetailFragment &&
1505 renamedFile.equals(details.getFile()) ) {
1506 ((FileDetailFragment) details).updateFileDetails(renamedFile, getAccount());
1507 showDetails(renamedFile);
1508
1509 } else if (details instanceof PreviewMediaFragment &&
1510 renamedFile.equals(details.getFile())) {
1511 ((PreviewMediaFragment) details).updateFile(renamedFile);
1512 if (PreviewMediaFragment.canBePreviewed(renamedFile)) {
1513 int position = ((PreviewMediaFragment) details).getPosition();
1514 startMediaPreview(renamedFile, position, true);
1515 } else {
1516 getFileOperationsHelper().openFile(renamedFile);
1517 }
1518 } else if (details instanceof PreviewTextFragment &&
1519 renamedFile.equals(details.getFile())) {
1520 ((PreviewTextFragment) details).updateFile(renamedFile);
1521 if (PreviewTextFragment.canBePreviewed(renamedFile)) {
1522 startTextPreview(renamedFile);
1523 } else {
1524 getFileOperationsHelper().openFile(renamedFile);
1525 }
1526 }
1527 }
1528
1529 if (getStorageManager().getFileById(renamedFile.getParentId()).equals(getCurrentDir())){
1530 refreshListOfFilesFragment();
1531 }
1532
1533 } else {
1534 Toast msg = Toast.makeText(this,
1535 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1536 Toast.LENGTH_LONG);
1537 msg.show();
1538
1539 if (result.isSslRecoverableException()) {
1540 mLastSslUntrustedServerResult = result;
1541 showUntrustedCertDialog(mLastSslUntrustedServerResult);
1542 }
1543 }
1544 }
1545
1546 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation,
1547 RemoteOperationResult result) {
1548 if (result.isSuccess()) {
1549 if (operation.transferWasRequested()) {
1550 OCFile syncedFile = operation.getLocalFile();
1551 onTransferStateChanged(syncedFile, true, true);
1552 invalidateOptionsMenu();
1553 }
1554 }
1555 }
1556
1557 /**
1558 * Updates the view associated to the activity after the finish of an operation trying create a
1559 * new folder
1560 *
1561 * @param operation Creation operation performed.
1562 * @param result Result of the creation.
1563 */
1564 private void onCreateFolderOperationFinish(CreateFolderOperation operation,
1565 RemoteOperationResult result) {
1566 if (result.isSuccess()) {
1567 dismissLoadingDialog();
1568 refreshListOfFilesFragment();
1569 } else {
1570 dismissLoadingDialog();
1571 try {
1572 Toast msg = Toast.makeText(FileDisplayActivity.this,
1573 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
1574 Toast.LENGTH_LONG);
1575 msg.show();
1576
1577 } catch (NotFoundException e) {
1578 Log_OC.e(TAG, "Error while trying to show fail message ", e);
1579 }
1580 }
1581 }
1582
1583
1584 /**
1585 * {@inheritDoc}
1586 */
1587 @Override
1588 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
1589 refreshListOfFilesFragment();
1590 FileFragment details = getSecondFragment();
1591 if (details != null && details instanceof FileDetailFragment &&
1592 file.equals(details.getFile()) ) {
1593 if (downloading || uploading) {
1594 ((FileDetailFragment) details).updateFileDetails(file, getAccount());
1595 } else {
1596 if (!file.fileExists()) {
1597 cleanSecondFragment();
1598 } else {
1599 ((FileDetailFragment) details).updateFileDetails(false, true);
1600 }
1601 }
1602 }
1603
1604 }
1605
1606
1607 private void requestForDownload() {
1608 Account account = getAccount();
1609 //if (!mWaitingToPreview.isDownloading()) {
1610 if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
1611 Intent i = new Intent(this, FileDownloader.class);
1612 i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
1613 i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
1614 startService(i);
1615 }
1616 }
1617
1618
1619 private OCFile getCurrentDir() {
1620 OCFile file = getFile();
1621 if (file != null) {
1622 if (file.isFolder()) {
1623 return file;
1624 } else if (getStorageManager() != null) {
1625 String parentPath = file.getRemotePath().substring(0,
1626 file.getRemotePath().lastIndexOf(file.getFileName()));
1627 return getStorageManager().getFileByPath(parentPath);
1628 }
1629 }
1630 return null;
1631 }
1632
1633 public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) {
1634 long currentSyncTime = System.currentTimeMillis();
1635
1636 mSyncInProgress = true;
1637
1638 // perform folder synchronization
1639 RemoteOperation synchFolderOp = new RefreshFolderOperation( folder,
1640 currentSyncTime,
1641 false,
1642 getFileOperationsHelper().isSharedSupported(),
1643 ignoreETag,
1644 getStorageManager(),
1645 getAccount(),
1646 getApplicationContext()
1647 );
1648 synchFolderOp.execute(getAccount(), MainApp.getAppContext(), this, null, null);
1649
1650 mProgressBar.setIndeterminate(true);
1651
1652 setBackgroundText();
1653 }
1654
1655 /**
1656 * Show untrusted cert dialog
1657 */
1658 public void showUntrustedCertDialog(RemoteOperationResult result) {
1659 // Show a dialog with the certificate info
1660 SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstanceForFullSslError(
1661 (CertificateCombinedException) result.getException());
1662 FragmentManager fm = getSupportFragmentManager();
1663 FragmentTransaction ft = fm.beginTransaction();
1664 dialog.show(ft, DIALOG_UNTRUSTED_CERT);
1665 }
1666
1667 private void requestForDownload(OCFile file) {
1668 Account account = getAccount();
1669 if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
1670 Intent i = new Intent(this, FileDownloader.class);
1671 i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
1672 i.putExtra(FileDownloader.EXTRA_FILE, file);
1673 startService(i);
1674 }
1675 }
1676
1677 private void sendDownloadedFile() {
1678 getFileOperationsHelper().sendDownloadedFile(mWaitingToSend);
1679 mWaitingToSend = null;
1680 }
1681
1682
1683 /**
1684 * Requests the download of the received {@link OCFile} , updates the UI
1685 * to monitor the download progress and prepares the activity to send the file
1686 * when the download finishes.
1687 *
1688 * @param file {@link OCFile} to download and preview.
1689 */
1690 public void startDownloadForSending(OCFile file) {
1691 mWaitingToSend = file;
1692 requestForDownload(mWaitingToSend);
1693 boolean hasSecondFragment = (getSecondFragment() != null);
1694 updateFragmentsVisibility(hasSecondFragment);
1695 }
1696
1697 /**
1698 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1699 *
1700 * @param file Image {@link OCFile} to show.
1701 */
1702 public void startImagePreview(OCFile file) {
1703 Intent showDetailsIntent = new Intent(this, PreviewImageActivity.class);
1704 showDetailsIntent.putExtra(EXTRA_FILE, file);
1705 showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount());
1706 startActivity(showDetailsIntent);
1707 }
1708
1709 /**
1710 * Stars the preview of an already down media {@link OCFile}.
1711 *
1712 * @param file Media {@link OCFile} to preview.
1713 * @param startPlaybackPosition Media position where the playback will be started,
1714 * in milliseconds.
1715 * @param autoplay When 'true', the playback will start without user
1716 * interactions.
1717 */
1718 public void startMediaPreview(OCFile file, int startPlaybackPosition, boolean autoplay) {
1719 Fragment mediaFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition,
1720 autoplay);
1721 setSecondFragment(mediaFragment);
1722 updateFragmentsVisibility(true);
1723 updateActionBarTitleAndHomeButton(file);
1724 setFile(file);
1725 }
1726
1727 /**
1728 * Stars the preview of a text file {@link OCFile}.
1729 *
1730 * @param file Text {@link OCFile} to preview.
1731 */
1732 public void startTextPreview(OCFile file) {
1733 Bundle args = new Bundle();
1734 args.putParcelable(EXTRA_FILE, file);
1735 args.putParcelable(EXTRA_ACCOUNT, getAccount());
1736 Fragment textPreviewFragment = Fragment.instantiate(getApplicationContext(),
1737 PreviewTextFragment.class.getName(), args);
1738 setSecondFragment(textPreviewFragment);
1739 updateFragmentsVisibility(true);
1740 //updateNavigationElementsInActionBar(file);
1741 setFile(file);
1742 }
1743
1744 /**
1745 * Requests the download of the received {@link OCFile} , updates the UI
1746 * to monitor the download progress and prepares the activity to preview
1747 * or open the file when the download finishes.
1748 *
1749 * @param file {@link OCFile} to download and preview.
1750 */
1751 public void startDownloadForPreview(OCFile file) {
1752 Fragment detailFragment = FileDetailFragment.newInstance(file, getAccount());
1753 setSecondFragment(detailFragment);
1754 mWaitingToPreview = file;
1755 requestForDownload();
1756 updateFragmentsVisibility(true);
1757 updateActionBarTitleAndHomeButton(file);
1758 setFile(file);
1759 }
1760
1761
1762 public void cancelTransference(OCFile file) {
1763 getFileOperationsHelper().cancelTransference(file);
1764 if (mWaitingToPreview != null &&
1765 mWaitingToPreview.getRemotePath().equals(file.getRemotePath())) {
1766 mWaitingToPreview = null;
1767 }
1768 if (mWaitingToSend != null &&
1769 mWaitingToSend.getRemotePath().equals(file.getRemotePath())) {
1770 mWaitingToSend = null;
1771 }
1772 onTransferStateChanged(file, false, false);
1773 }
1774
1775 @Override
1776 public void onRefresh(boolean ignoreETag) {
1777 refreshList(ignoreETag);
1778 }
1779
1780 @Override
1781 public void onRefresh() {
1782 refreshList(true);
1783 }
1784
1785 private void refreshList(boolean ignoreETag) {
1786 OCFileListFragment listOfFiles = getListOfFilesFragment();
1787 if (listOfFiles != null) {
1788 OCFile folder = listOfFiles.getCurrentFile();
1789 if (folder != null) {
1790 /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
1791 listDirectory(mFile);*/
1792 startSyncFolderOperation(folder, ignoreETag);
1793 }
1794 }
1795 }
1796
1797 private void sortByDate(boolean ascending) {
1798 getListOfFilesFragment().sortByDate(ascending);
1799 }
1800
1801 private void sortBySize(boolean ascending) {
1802 getListOfFilesFragment().sortBySize(ascending);
1803 }
1804
1805 private void sortByName(boolean ascending) {
1806 getListOfFilesFragment().sortByName(ascending);
1807 }
1808
1809 public void allFilesOption() {
1810 browseToRoot();
1811 }
1812 }